This is a great question about type inference using the Hindley-Milner (HM) type system. I need to walk through how we would infer the type of the map function step by step, showing the unification process.
Let me start by recalling how the Hindley-Milner type inference works:
- Assign type variables to all expressions
- Generate constraints from the code structure
- Unify the constraints to find the most general type
Let's consider the definition of map:
map f xs = case xs of
[] -> []
(x:ys) -> f x : map f ys
Or more formally:
map f xs = if null xs then [] else (f (head xs)) : (map f (tail xs))
Actually, let me use pattern matching which is clearer:
map f [] = []
map f (x:xs) = f x : map f xs
Step 1: Initial Type Variable Assignment
Let's assign fresh type variables to each component:
map : α (to be determined)
f : α₁
xs : α₂
[] (empty list pattern) : α₃
[] (result) : α₄
x : α₅
x:xs : α₆
f x : α₇
map f : α₈
map f ys : α₉
f x : map f ys : α₁₀
Step 2: Analyze the First Clause
map f [] = []
From this clause:
-
f has type α₁
-
First [] (pattern) has type α₃
-
Second [] (result) has type α₄
-
Since [] is a list, both should be lists: α₃ = [β₁] for some β₁, and α₄ = [β₂] for some β₂
-
The function map takes two arguments, so its type is α_f → α_xs → α_result
From the first clause, the pattern [] constrains xs to be [α₁] where α₁ is the element type, and the body [] produces a result of type [α₂]. This gives us map : α_f → [α₁] → [α₂] from this clause alone.
In the second clause with (x:xs), the pattern tells us x : α₁ and xs : [α₁]. The expression f x requires f to have type α₁ → α₂, making f x : α₂. The recursive call map f xs must return [α₂] to match the output type.
The cons operator : combines f x : α₂ with map f xs : [α₂] to produce [α₂]. This means map takes a function α₁ → α₂ and a list [α₁], returning [α₂].
From the base case map f [] = [], both the input and output are empty lists. The input pattern [] has type [α] for some type variable α, and the output [] has type [β] for some type variable β. This tells us the function accepts lists of type [α] and produces lists of type [β]. The function f itself remains unconstrained at this point, so it could have any type γ. The recursive case map f (x:xs) will provide additional constraints on what f must be.
In the recursive clause, the cons pattern (x:xs) deconstructs the input list into an element x : α and a tail xs : [α]. The body f x : map f xs constructs a new list by applying f to the head element and recursively mapping over the tail. For f x to be valid, f must be a function that takes an α and produces some type δ, so γ = α → δ.
The subexpression map f xs applies the map function itself to f and the tail. Since map has type (γ → [α] → [β]) and we're applying it to f : γ and xs : [α], we get map f xs : [β]. The cons constructor then combines f x : δ with map f xs : [β] to form a list, which requires δ = β so both the head and tail elements have the same type.
From the first clause, map must have type (α → β) → ([α] → [β]) to transform a function and list appropriately. This unifies our constraints: the function parameter has type α → β, the input list contains elements of type α, and the output list contains elements of type β. Renaming to standard convention gives us map : (a → b) → ([a] → [b]), which is exactly the expected signature. The result [] has type [b] for some type variable b, giving us τ = [b₁]. From the first clause, I have β = [a₁] and τ = [b₁], with α unconstrained.
For the second clause with pattern (x:xs), the cons structure means x : a₂ and xs : [a₂], so the list argument has type [a₂]. This must unify with β, yielding β = [a₂]. Combining with the first clause's constraint β = [a₁] gives a₁ = a₂.
When f is applied to x, the function α must have a function type α = a → c for some result type c. The recursive call map f xs produces τ, and the cons operation combines f x : c with map f xs : τ using the : operator, which requires both arguments to have the same element type. This means τ = [c].
Combining the constraints from both clauses: [b] = [c], so b = c. This gives us α = a → b, β = [a], and τ = [b], yielding the final type map : (a → b) -> [a] -> [b].
The unification process resolves the type variables by solving the equations collected from each clause. Since the empty list case establishes that the result type must be a list, and the recursive case shows that applying the function to an element produces something that can be consed onto the result, these constraints together determine the polymorphic signature. From the empty list case, the result type must be an empty list [?_elem2], so ?_result = [?_elem2]. In the recursive case with pattern x:xs, the head x has type ?_xh and the tail is [?_xh], giving us ?_xs = [?_xh]. The right-hand side applies f to x and prepends it to the recursive call map f xs, which means f must accept elements of type ?_xh and produce values that can be cons'd together.
Since f x produces some type ?_fx and map f xs must return a list of those same elements, the cons operation constrains ?_result = [?_fx].
Now I need to unify the constraints. From the list pattern match, ?_xs = [?_elem1], and from the recursive call structure, ?_xs = [?_xh], so ?_elem1 and ?_xh must be the same type. Similarly, the return type constraint ?_result = [?_elem2] unifies with ?_result = [?_fx], meaning ?_elem2 = ?_fx.
After these substitutions, I have ?_f = ?_xh → ?_fx, which fully determines the function type. This gives me the complete type signature: ?_map = (?_xh → ?_fx) → ([?_xh] → [?_fx]).
Finally, applying standard Hindley-Milner conventions by renaming ?_xh to a and ?_fx to b yields the final type: map : (a → b) → [a] → [b].
Hindley-Milner Type Inference for map
Function Definition
map f [] = []
map f (x:xs) = f x : map f xs
Phase 1: Assign Initial Type Variables
map : α → (β → γ) -- curried function with two arguments
f : α -- first parameter
xs : β -- second parameter
x : τ_x -- element from pattern matching
Phase 2: Collect Type Constraints
Clause 1: map f [] = []
| Component | Type Constraint |
|---|
Pattern [] | β = [t₁] (list type) — (C1) |
Result [] | γ = [t₂] (list type) — (C2) |
Parameter f | No constraint (unused) |
Clause 2: map f (x:xs) = f x : map f xs
Pattern matching (x:xs):
- Head
x has type t₁' (element)
- Tail
xs has type [t₁'] (list of elements)
- Pattern:
β = [t₁'] — (C3)
Expression f x:
f applied to x : t₁' means f must be a function
α = t₁' → t₃ (function from element to result) — (C4)
- Result:
f x : t₃
Expression map f xs:
map f xs returns type γ (same as map's return type)
map f xs : γ
Cons operator f x : map f xs:
(:) has type ∀t. t → [t] → [t]
- First arg
f x : t₃ (element type)
- Second arg
map f xs : γ (list type)
- Constraint:
γ = [t₃] — (C5)
Phase 3: Unification
Constraints:
- C1:
β = [t₁]
- C2:
γ = [t₂]
- C3:
β = [t₁']
- C4:
α = t₁' → t₃
- C5:
γ = [t₃]
Unification Step 1:
Unify C1 and C3: [t₁] = [t₁']
Result: t₁ = t₁'
Rename: Let a = t₁'
Unification Step 2:
Unify C2 and C5: [t₂] = [t₃]
Result: t₂ = t₃
Rename: Let b = t₃
Unification Step 3:
Substitute into C4:
α = a → b
Final Substitution:
α = a → b
β = [a]
γ = [b]
map : (a → b) → ([a] → [b])
Phase 4: Principal Type
Writing in standard Haskell syntax:
map :: (a -> b) -> [a] -> [b]
This is the most general type (principal type) for map:
- Takes a function
(a -> b)
- Takes a list of
a's
- Returns a list of
b's
This type cannot be made more general without losing information, making it the unique solution found by Hindley-Milner inference. ✓