scope - Haskell Referencing a Type Variable -
i run problem , wanted ask if there's common solution or pattern. is possible make type variable in nested context reference type outer context? example,
foo :: -> ... -> .. foo = ... bar :: -> ...
now bar
's a
different foo's a
. typically want, makes life difficult, , need make them same. i've used dirty tricks force type checker unify 2 in past, thwarted. here's latest example (a parsec function) spurred me asking question.
data project = ... deriving enum data stuff = ... pproject :: monad m => p m stuff pproject = stuff <- pstuff ... convert stuff <$> penum :: p m project penum :: (monad m, enum a) => string -> p m penum = ...
the convert
function needed type, hence had specify annotation :: p m project
. however, means have introduce m
, unfortunately not same m
in function signature. type checker reports with:
could not deduce
monad m1
arising use ofpenum
contextmonad m
is there way reference function signature's m
without ugly hack? (an ugly hack inserting dummy code doesn't executed, exists unify 2 types.)
you're looking scopedtypevariables
extension, lets reference type variables containing scopes.
{-# language scopedtypevariables #-}
for backwards compatibility, applies type signatures have explicit forall
. have write:
pproject :: forall m. monad m => p m stuff
after that, you'd able refer correct type variable m
inside scope of pproject
.
Comments
Post a Comment