haskell - What does exactly Identity functor do? -
it may trivial, don't understand following mean.
instance functor identity fmap = coerce
so, can try define similar:
prelude control.lens data.functor.identity> :t fmap fmap :: functor f => (a -> b) -> f -> f b prelude control.lens data.functor.identity> let z f g = coerce f g prelude control.lens data.functor.identity> :t z z :: contravariant ((->) t) => (t -> a) -> t -> b
but, mean in simpler terms?
the use of coerce
new in ghc 7.10 , done efficiency. "real" definition is
fmap :: (a -> b) -> (identity -> identity b) fmap f (identity a) = identity (f a)
the wrapping/unwrapping of identity
constructor should optimized away @ compile time, seems base
devs had reason ensure there no performance penalty using coerce
.
the reason can use coerce
identity a
isomorphic (the same as) a
, can coerce a -> b
identity -> identity b
happens definition of fmap
specialized identity
!
Comments
Post a Comment