Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INLINE Pragma in combination with type classes

Tags:

haskell

ghc

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 fmaps which are defined in other Functor instances.

instance Functor (IResult t) where
    fmap = fmapR
    {-# INLINE fmap #-}
like image 527
Long Avatar asked Mar 28 '12 20:03

Long


1 Answers

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
like image 95
dflemstr Avatar answered Sep 24 '22 10:09

dflemstr