haskell - Monomorphism restriction in pattern bindings -
{-# language nomonomorphismrestriction #-} module seek f :: io (a -> io string) f = homecoming $ const getline main :: io () main = g <- f :: io (a -> io string) g "string" >>= print g 5 >>= print
even nomonomorphismrestriction
flag , explicit type signature, module fails compile couldn't match expected type ‘[char]’ actual type ‘int’
, despite g
beingness polymorphic.
this not monomorphism restriction means. monomorphism restriction says if definition has no type signature , has left-hand side no parameters, specialized monomorphic type (or rather monomorphic plenty rid of class constraints). have given type signatures doesn't apply.
the problem here have given wrong type f
.
f :: io (a -> io string)
actually means
f :: forall a. io (a -> io string)
that is, first pick type a
, can bind monomorphic function of type a -> io string
a
. there no problem program, example:
main = g <- f g "string" >>= print g' <- f g' 5 >>= print
but usage illustration requires type:
f :: io (forall a. -> io string)
that is, want bind first , pick type later, i.e. utilize function @ multiple types. called "impredicative type" , unfortunately ghc has not supported them quite while, far know.
the way solve problem create newtype
wrapper explicitly quantifies inner polymorphic type:
newtype r = r { getr :: forall a. -> io string } f :: io r f = homecoming $ r (const getline) main :: io () main = g <- f getr g "string" >>= print getr g 5 >>= print
haskell
No comments:
Post a Comment