This doesn't buy you very much, unfortunately. (f $!) only forces its argument if the result is ever forced. So if the implementation of fmap doesn't force the function calls (and normally it won't, with no possible way to use the result for anything) then f <$!> x doesn't either; the arguments to those calls are not forced, and so the contents of the original structure remain unforced. For example:


Prelude> let f <$!> x = (f $!) <$> x
(<$!>) :: Functor f => (a -> b) -> f a -> f b

Prelude> let z = const True <$!> [undefined]
z :: [Bool]

Prelude> case z of (_:_) -> "matched"
"matched"
it :: String

Prelude> z
[*** Exception: Prelude.undefined


The undefined bomb only goes off when I start to force the elements of z; prior to that no strictness has been gained, and the list contains a thunk as normal. In particular, this version of <$!> wouldn't help with the lazy IO problem described in that stackoverflow question Christopher linked earlier.

-- Ben

----- Original Message -----
From:
"Will Yager" <will.yager@gmail.com>

To:
"Christopher Allen" <cma@bitemyapp.com>
Cc:
"haskell-cafe" <haskell-cafe@haskell.org>
Sent:
Mon, 10 Aug 2015 14:30:49 -0700
Subject:
Re: [Haskell-cafe] Constraints on <$> vs <$!>


What about

f <$!> x = (f $!) <$> x 

?

--Will



On Aug 10, 2015, at 14:20, Christopher Allen <cma@bitemyapp.com> wrote:

http://stackoverflow.com/questions/9423622/strict-fmap-using-only-functor-not-monad seems to cover it.

On Mon, Aug 10, 2015 at 4:17 PM, Alexey Egorov <electreg@list.ru> wrote:
Hello haskell ers,

I wonder why <$> and <$!> have different typeclass constraints?

(<$>) :: Functor f => (a -> b) -> f a -> f b
(<$!>) :: Monad m => (a -> b) -> m a -> m b
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe



--
Chris Allen
Currently working on http://haskellbook.com
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe