“forall” is the type-level “lambda”

Dec 27, 2018 · 3 min read
mkList :: a -> [a]
mkList :: forall a. a -> [a]

Generic parameters

f :: Read a => String -> [a]
f s = g ("[" ++ s ++ "]")
where
g :: String -> a
g a = read a
f :: Read a => String -> [a]
f s = g ("[" ++ s ++ "]")
where
g :: Read a => String -> a
g a = read a

Scope

Explicit scope

f :: forall a. Read a => String -> [a]
f s = g ("[" ++ s ++ "]")
where
g :: String -> [a]
g a = read a

Analogy to values

f = \x -> x + 1
f = x + 1
At value level:
We write: \x -> Just x
We wish we could write: Just x
At type level:
We write: forall a. Maybe a
We wish we could write: Maybe a