fouronnes
fouronnes

Reputation: 4038

Type of id function in Haskell

In GHCI, the type of id is:

Prelude> :t id
id :: a -> a

But if I define my own id function, why is the name of the type variable t ? Is there a difference between t and a ?

Prelude> let identity x = x
Prelude> :t identity
identity :: t -> t

Upvotes: 2

Views: 458

Answers (2)

leftaroundabout
leftaroundabout

Reputation: 120741

This has actually the same answer as if I were asking

If I define my own version of your version as

Prelude> let identity' q = q

why is the name of the value variable q? Is there a difference between q and x?

The crucial thing about parameter variables in general is that their names are basically arbitrary. It's a fundamental property of lambda-calculus: α-equivalence. We merely replace \x -> x with \q -> q (or, in lambda-style, λx.x with λq.q). And indeed type variables are parameters as well, though it doesn't really look like they are. But under the hood, Haskell polymorphic signatures should be read as System F, so really we have Λα . α -> α, usually written forall a . a -> a. Which is obviously equivalent to forall t . t -> t.

Upvotes: 6

epsilonhalbe
epsilonhalbe

Reputation: 15959

There is no difference between a and t, they are called Type variables and stand for any type you can have. Note that they are written in lowercase letters where types are written with an uppercase letter at the beginning (except for lists, who have special syntax).

In addition if you write a file and load it into ghci by ghci testmodule.hs

module Testmodule where

identity :: b -> b
identity x = x

then ghci will show you exactly the letter you used in your definition.

Upvotes: 9

Related Questions