[Haskell-cafe] About "Fun with type functions" example

Erik Hesselink hesselink at gmail.com
Thu Nov 18 14:22:40 EST 2010

On Thu, Nov 18, 2010 at 20:17, Arnaud Bailly <arnaud.oqube at gmail.com> wrote:
> Hello,
> I am trying to understand and use the Nat n type defined in the
> aforementioned article. Unfortunately, the given code does not compile
> properly:


> instance (Nat n) => Nat (Succ n) where
>  toInt   _ = 1 + toInt (undefined :: n)


> And here is the error:
> Naturals.hs:16:18:
>    Ambiguous type variable `n' in the constraint:
>      `Nat n' arising from a use of `toInt' at Naturals.hs:16:18-39
>    Probable fix: add a type signature that fixes these type variable(s)

You need to turn on the ScopedTypeVariables extension (using {-#
LANGUAGE ScopedTypeVariables #-} at the top of your file, or
-XScopedTypeVariables at the command line). Otherwise, the 'n' in the
class declaration and in the function definition are different, and
you want them to be the same 'n'.


More information about the Haskell-Cafe mailing list