Proposal: Add a strict version of <$> for monads

Herbert Valerio Riedel hvr at gnu.org
Sat Nov 30 18:41:42 UTC 2013


On 2013-11-30 at 12:05:06 +0100, Roman Cheplyaka wrote:
> * Nicolas Trangez <nicolas at incubaid.com> [2013-11-30 02:11:02+0100]
>> I'm without a doubt overlooking something, but couldn't this be (otoh)
>> 
>>     fmap' :: Functor f => (a -> b) -> f a -> f b
>>     fmap' f = fmap (strictify f)
>>       where
>>         strictify s = (($!) id) . s

> No. Evaluation of your 'strictify' function will be delayed for the very
> same reason that evaluation of 'f' is delayed in the first place.
>
> It doesn't matter what 'strictify' *does* if it's not even evaluated.

jfyi, if compiled with

  ghc -O2 -dsuppress-all -dsuppress-uniques -ddump-simpl

it can be seen that fmap' is really almost the same as a plain fmap,
except for an left-over eta-expansion which ghc doesn't optimize away:

fmap' = \ @ a @ b @ f $dFunctor f1 -> fmap $dFunctor (\ x -> f1 x)



More information about the Libraries mailing list