19

I've been messing around with the fix function, and I happened across this:

λ let fix f = let x = f x in x
λ fix (+)

<interactive>:15:5:
    Occurs check: cannot construct the infinite type: t ~ t -> t
    Expected type: t -> t
      Actual type: t -> t -> t
    Relevant bindings include it :: t (bound at <interactive>:15:1)
    In the first argument of ‘fix’, namely ‘(+)’
    In the expression: fix (+)

I know full well why this error is occurring, but I noticed a funny type signature up there: t ~ t -> t. What does this type mean? What do tilde mean in type signatures within haskell? Where are they used?

AJF
  • 11,767
  • 2
  • 37
  • 64

1 Answers1

22

Tilde (~) in that error means type equality. It is telling you that it cannot deduce t to be t -> t. The symbol is also used for irrefutable patterns, but that's a completely different context.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Whereabouts would it be used, not including type errors? – AJF Dec 27 '14 at 13:31
  • 12
    @AJFarmar the `TypeFamilies` extension lets you use `~` in your own type signatures to mean type equality, in the same way it's used in this error message. It's something you find you want when working with type synonym families, but can be useful on its own for doing various type-level tricks (the one example off the top of my head: you can get "default" instances of a class with `OverlappingInstances` with `instance Foo X` and `instance (y ~ Y)=> Foo y`) – jberryman Dec 27 '14 at 21:44