asserting the type of a binding in a "do" expression

This works (with -fglasgow-exts): foo::IO Int; foo = do{ (x::Int) <- bar; return x;}; bar = undefined But this does not: foo::IO a; foo = do{ (x::a) <- bar; return x;}; Error message: A pattern type signature cannot bind scoped type variables `a' unless the pattern has a rigid type context. How can I accomplish my goal of asserting the type of x, as a programmer sanity check, when there are type variables? Thanks, --ken

On Apr 26, 2008, at 2:36 , Ken Takusagawa wrote:
But this does not:
foo::IO a; foo = do{ (x::a) <- bar; return x;};
Error message: A pattern type signature cannot bind scoped type variables `a' unless the pattern has a rigid type context.
This works for me (in a slightly out of date HEAD) if I explicitly forall the declaration as per the ghc manual (see section 8.7.6.3):
bar :: forall b. IO b bar = return undefined -- just want a type for now foo :: forall a. IO a foo = do { (x :: a) <- bar; return x; }
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Fri, Apr 25, 2008 at 11:49 PM, Brandon S. Allbery KF8NH
On Apr 26, 2008, at 2:36 , Ken Takusagawa wrote:
But this does not:
foo::IO a; foo = do{ (x::a) <- bar; return x;};
Error message: A pattern type signature cannot bind scoped type variables `a' unless the pattern has a rigid type context.
Yeah, using the "forall" is exactly what you want to fix this problem. It puts the type variable in scope throughout the definition of the function.
This works for me (in a slightly out of date HEAD) if I explicitly forall the declaration as per the ghc manual (see section 8.7.6.3):
bar :: forall b. IO b bar = return undefined -- just want a type for now foo :: forall a. IO a
foo = do { (x :: a) <- bar; return x; }
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, 26 Apr 2008, Ken Takusagawa wrote:
This works (with -fglasgow-exts):
foo::IO Int; foo = do{ (x::Int) <- bar; return x;};
bar = undefined
But this does not:
foo::IO a; foo = do{ (x::a) <- bar; return x;};
Error message: A pattern type signature cannot bind scoped type variables `a' unless the pattern has a rigid type context.
'asTypeOf' is the answer for Haskell98. You can also write your own helper functions which might simplify that for your use cases.
participants (4)
-
Brandon S. Allbery KF8NH
-
Henning Thielemann
-
Ken Takusagawa
-
Philip Weaver