haskell - Understanding the type of the cast operator in Scrap Your Boilerplate -
in 2003 scrap boilerplate paper laemmel , spj there code snippet on page 3
mkt :: (typeable a, typeable b) => (b -> b) -> -> mkt f = case cast f of g -> g nothing -> id
and paper explains
that is, mkt f x applies f x if x's type same f's argument type
following in pattern of preceding examples in paper, think type of cast f
have compared type maybe (b -> b)
above evaluate just g
, seems incorrect.
what going on here signature of cast f
?
the type of cast
is
cast :: (typeable x, typeable y) => x -> maybe y
it produces nothing
if x
, y
different types , just argument
if same. note result type, y
must determined calling context when cast
used. if isn't, compilation fails unresolved overloading/ambiguous type variable error.
in particular example, types function types, (b -> b)
argument, , (a -> a)
result. so
cast f :: maybe (a -> a)
and mkt
written mkt = frommaybe id . cast
.
Comments
Post a Comment