Given the following code (copied from the attoparsec library) what does the inline pragma do? I suppose it makes sense for only fmapR
to be inlined, but not the other fmap
s which are defined in other Functor instances.
instance Functor (IResult t) where
fmap = fmapR
{-# INLINE fmap #-}
The inline pragma will copy the contents of the function (in this case fmapR
) to the location where it is called, if the compiler can prove that the functor being used is IResult
.
The function cannot be inlined in the following case, because the definition of fmap
is not known:
f :: Functor f => f Int -> f Float
f = fmap fromIntegral
Here, however, it is known, because a certain functor is being used, and the function can be inlined:
f :: IResult Int -> IResult Float
f = fmap fromIntegral
-- rewritten to: f = fmapR fromIntegral; might be further inlined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With