haskell - Can fusion see through newtype wrappers? -
given:
newtype myvec = myvec { unvec :: data.vector } deriving (functor, etc)
this create (something like) this:
instance functor myvec fmap f = myvec . data.vector.fmap f . unvec
will vectors fusion rules fire , rewrite fmap f . fmap g $ myvec
fmap (f . g) myvec
?
are there pitfalls should aware of? afaik problem "pay" new types in containers solved in ghc 7.8, it?
fusion rules operate on functions, not on types. functions on myvec won't have fusion rules, unless write them reuse underlying ones.
e.g.
map :: (a -> b) -> myvec -> myvec b map f = myvec . vector.map f . unvec {-# inline map #-}
then we'd have uses:
map f . map g
which inline to:
myvec . vector.map f . unvec . myvec . vector.map g . unvec
ghc should erase newtype constructor, yielding regular stream, suitable fusion:
myvec . vector.map f . vector.map g . unvec
you can confirm running ghc , looking @ rewrite rules firing. alternatively, can add own "myvec. unvec" rewrite rule, ghc should cover that.
Comments
Post a Comment