Fixing a type variable inside a function

oleg@pobox.com oleg@pobox.com
Fri, 16 May 2003 11:21:24 -0700 (PDT)


>      foo :: MyTypeClass resultType => resultType
>      foo = fromString (toString (undefined :: resultType))

> but I can't seem to do this -- GHC's scoped type variables don't seem 
> to help either, or at least I couldn't get it working. 

In GHC, type variables in the signature bear no relationship to
(local) type variables in the body of the function. Simon Peyton Jones
and Mark Shields explain that decision in their paper:
	http://research.microsoft.com/~simonpj/papers/scoped-tyvars

They argue that a type signature does not have to immediately precede
the body of the function. A type signature may be far away. In that
case, relying in the body of the function something that was bound far
earlier in the code did not seem reasonable. Incidentally, Chameleon
took an opposite decision. Thus, if you want to use resultType in the
body of the function, you have to bind it somewhere in the body of the
function:

foo :: MyTypeClass resultType => resultType
foo::rt = fromString (toString (undefined :: rt))


Omitting the type signature, as in
	foo = fromString $ toString undefined
works too.

This is almost the same example that has been explained in the thread
   "Solved: Typing problems with polymorphic recursion and typeclasses" 

on this thread two weeks ago.