haskell - Type Inference in Patterns -
i noticed ghc wanted type signature think should inferred. minimized illustration downwards this, nil meaningful (i don't recommend running on favorite types):
{-# language gadts, rankntypes, scopedtypevariables, typeoperators, nomonomorphismrestriction #-} module foo import data.typeable info bar rp rq info foo b = foo (foo b) rebar :: forall rp rq rp' rp'' . (typeable rp', typeable rp'') => proxy rp' -> proxy rp'' -> foo rp -> foo (bar rp rq) rebar p1 p2 (foo x) = -- signature y should inferred... allow y = rebar p1 p2 x -- :: foo (bar rp rq) -- case statement has nil type of y in case (eqt :: maybe (rp' :~: rp'')) of refl -> y
without type signature on definition of y
, error:
foo.hs:19:20: couldn't match type ‘rq0’ ‘rq’ ‘rq0’ untouchable within constraints (rp' ~ rp'') bound pattern constructor refl :: forall (k :: box) (a1 :: k). a1 :~: a1, in case alternative @ testsuite/foo.hs:19:12-15 ‘rq’ stiff type variable bound type signature rebar :: (typeable rp', typeable rp'') => proxy rp' -> proxy rp'' -> foo rp -> foo (bar rp rq) @ testsuite/foo.hs:12:20 expected type: foo (bar rp rq) actual type: foo (bar rp rq0) relevant bindings include y :: foo (bar rp rq0) (bound @ testsuite/foo.hs:16:7) rebar :: proxy rp' -> proxy rp'' -> foo rp -> foo (bar rp rq) (bound @ testsuite/foo.hs:14:1) in expression: y in case alternative: refl -> y failed, modules loaded: none.
having been caught dreaded monomorphism restriction on multiple occassions, turned on nomonomorphismrestriction
, doesn't alter behavior.
why type of y
not inferred output type of function?
the monomorphism restriction applies top level bindings. compiler aware of real type of y
, there no way infer monomorphic type it; cause of type error. if turn off monomorphic allow bindings, have utilize right extension:
{-# language nomonolocalbinds #-}
with it, code compiles.
for much more detail monomorphic allow bindings, see ghc wiki.
haskell ghc
No comments:
Post a Comment