David Degea
David Degea

Reputation: 1398

what is [a] as a type in Haskell?

This question is about type in Haskell programming language.

When I type

:t []

I get the return as:

[] :: [a]

What is [a]?

Upvotes: 0

Views: 220

Answers (2)

phipsgabler
phipsgabler

Reputation: 20960

Rewriting it like so might make things clearer:

[] :: forall a. [a]

So, since it doesn't contain any value, haskell can't decide on a type and leaves it open. If you use it in a typed expression, however, like

x = []

f :: [Int] -> [Int]
f l = l

y = f x

it automatically gets resolved to [Int], since it is used as this type.

Upvotes: 12

Lily Ballard
Lily Ballard

Reputation: 185701

It's a placeholder. It basically means any type could go there. In other words, if you had a function

someFunc :: [Int] -> ()

You could pass [] to it because [] is [a], which will match [Int] (by replacing the a placeholder with the concrete Int type).

Now if you look at, say, id, you'll find it has the type a -> a. This means that it's a function that takes an object of any type, and returns an object of the same type. Interestingly, because the function knows absolutely nothing about the object it's given (since it has no constraints on the type), you know that this function has to do either of two things:

  1. Return the input object, or
  2. Return ⊥ (bottom).

Upvotes: 5

Related Questions