0% found this document useful (0 votes)
75 views

COMP302: Programming Languages and Paradigms: Jacob Errington Prof. Brigitte Pientka

This document discusses type inference in the context of the Damas-Hindley-Milner type system. It introduces type variables and differentiates between type checking (A), where all substitutions of a term must be well-typed, and type inference (B), where the goal is to find some substitution that makes a term well-typed. Examples are provided to illustrate type variables in functions and how they allow general types like α → α to be inferred rather than requiring concrete types.

Uploaded by

Cheng Shou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

COMP302: Programming Languages and Paradigms: Jacob Errington Prof. Brigitte Pientka

This document discusses type inference in the context of the Damas-Hindley-Milner type system. It introduces type variables and differentiates between type checking (A), where all substitutions of a term must be well-typed, and type inference (B), where the goal is to find some substitution that makes a term well-typed. Examples are provided to illustrate type variables in functions and how they allow general types like α → α to be inferred rather than requiring concrete types.

Uploaded by

Cheng Shou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

COMP302: Programming Languages and Paradigms

Jacob Errington Prof. Brigitte Pientka


[email protected] [email protected]

School of Computer Science


McGill University
Week 12-2 , Fall 2019
COMP302: Programming Languages and Paradigms 2 / 20
Course Evaluations – It’s important!
• You and other students can view course
evaluation results in Minerva - but only if
more than 30% fill out the course evaluations!
• The more people fill out the course
evaluations, the more representative the
results.
• It is a way to give back to other students,
since the results are shared.
• Feedback improves courses.
• What contributed most to your learning?
You can do better!
• What aspect did you enjoy the most?
• Did you like LearnOCaml?

COMP302: Programming Languages and Paradigms 3 / 20


Recap: typing with contexts

Operations op ::= + | − | ∗ |<|=


Expressions e ::= n | e1 op e2 | true | false | if e then e1 else e2
| x | let x = e1 in e2 end
Types T ::= int | bool
Context Γ ::= · | Γ, x : T

Γ`e:T Expression e has type T given the typing context Γ.

Γ ` e1 : int Γ ` e2 : int
t-num t-plus
Γ ` n : int Γ ` e1 + e2 : int
Γ ` e : bool Γ ` e1 : T Γ ` e2 : T Γ(x) = T
t-if t-var
Γ ` if e then e1 else e2 : T Γ`x :T
Γ ` e1 : T1 Γ, x:T1 ` e2 : T
t-let x must be new
Γ ` let x = e1 in e2 end : T
COMP302: Programming Languages and Paradigms 4 / 20
Generalizing to functions and function application

Γ, x:T1 ` e : T2 Γ ` e1 : T2 → T Γ ` e2 : T2
t-fn t-app
Γ ` fn x => e : T1 → T2 Γ ` e1 e2 : T

Read t-fn rule as :

Expression fn x => e has type T1 → T2 in a typing context Γ, if


expression e has type T2 in the extended context Γ, x : T1

Read t-app rule as :

Expression e1 e2 has type T in a typing context Γ, if


- expression e1 has type T2 → T in the context Γ
- expression e2 has type T2 in the context Γ
COMP302: Programming Languages and Paradigms 5 / 20
Generalizing to functions and function application

Γ, x:T1 ` e : T2 Γ ` e1 : T2 → T Γ ` e2 : T2
t-fn t-app
Γ ` fn x => e : T1 → T2 Γ ` e1 e2 : T

Read t-fn rule as :

Expression fn x => e has type T1 → T2 in a typing context Γ, if


expression e has type T2 in the extended context Γ, x : T1

The rule t-fn cannot be used for type inference...


where is T1 coming from?

COMP302: Programming Languages and Paradigms 6 / 20


Generalizing to functions and function application

Γ, x:T1 ` e : T2 Γ ` e1 : T2 → T Γ ` e2 : T2
t-fn t-app
Γ ` fn x => e : T1 → T2 Γ ` e1 e2 : T

Read t-fn rule as :

Expression fn x => e has type T1 → T2 in a typing context Γ, if


expression e has type T2 in the extended context Γ, x : T1

The rule t-fn cannot be used for type inference...


where is T1 coming from?

Last class we solved this using type annotations on functions.

COMP302: Programming Languages and Paradigms 6 / 20


But...

COMP302: Programming Languages and Paradigms 7 / 20


But...

COMP302: Programming Languages and Paradigms 7 / 20


But...

Damas-Hindley-Milner type inference


COMP302: Programming Languages and Paradigms 7 / 20
Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.
fn x => x has type α→α

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.
fn x => x has type α→α
fn f => fn x => f (f (x)) has type (α → α) → α → α

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.
fn x => x has type α→α
fn f => fn x => f (f (x)) has type (α → α) → α → α
fn f => f x has type (α → β) → β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.
` fn x => x has type α→α
fn f => fn x => f (f (x)) has type (α → α) → α → α
fn f => f x has type (α → β) → β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.
` fn x => x has type α→α
` fn f => fn x => f (f (x)) has type (α → α) → α → α
fn f => f x has type (α → β) → β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
A. Are all substitution instances of e well-typed? That is for every type
substitution σ, we have [σ]Γ ` e : [σ]T . Type checking

Examples for A.
` fn x => x has type α→α
` fn f => fn x => f (f (x)) has type (α → α) → α → α
x : α ` fn f => f x has type (α → β) → β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

fn x => x + 1 has type α→α choosing int for α (i.e. int/α)

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

fn x => x + 1 has type α→α choosing int for α (i.e. int/α)


fn x => x + 1 has type α→β choosing int for α
choosing int for β
(i.e. int/α, int/β)

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

fn x => x + 1 has type α→α choosing int for α (i.e. int/α)


fn x => x + 1 has type α→β choosing int for α
choosing int for β
(i.e. int/α, int/β)
fn f => f x has type β→γ choosing (α → γ) for β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

` fn x => x + 1 has type α→α choosing int for α (i.e. int/α)


fn x => x + 1 has type α→β choosing int for α
choosing int for β
(i.e. int/α, int/β)
fn f => f x has type β→γ choosing (α → γ) for β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

` fn x => x + 1 has type α→α choosing int for α (i.e. int/α)


` fn x => x + 1 has type α→β choosing int for α
choosing int for β
(i.e. int/α, int/β)
fn f => f x has type β→γ choosing (α → γ) for β

COMP302: Programming Languages and Paradigms 8 / 20


Type Variables – Two Different Views

Types T ::= int | bool | T1 → T2 | α

Γ`e:T “Expression e has type T in the context Γ”


where T and Γ may contain type variables
B. Is some substitution instance of e well-typed? That is we can find a
type substitution σ, such that [σ]Γ ` e : [σ]T . Type inference

Examples for B.

` fn x => x + 1 has type α→α choosing int for α (i.e. int/α)


` fn x => x + 1 has type α→β choosing int for α
choosing int for β
(i.e. int/α, int/β)
x : α ` fn f => f x has type β→γ choosing (α → γ) for β

COMP302: Programming Languages and Paradigms 8 / 20


Look at this typing derivation under the checking view

x: α , f : α → β ` f : α → β x: α , f : α → β ` x : α
x: α , f : α → β ` f x : β
x: α ` fn f => f x : ( α → β) → β
` fn x => fn f => f x : α → ( α → β) → β

This derivation remains valid no matter what we choose for α and β.

COMP302: Programming Languages and Paradigms 9 / 20


Look at this typing derivation under the checking view

x:int, f :int → β ` f : int → β x:int, f :int → β ` x : int


x:int, f :int → β ` f x : β
x:int ` fn f => f x : (int → β) → β
` fn x => fn f => f x : int → (int → β) → β

For example, choosing int for α.

COMP302: Programming Languages and Paradigms 9 / 20


Which substitution to pick, under the inference view?

x : α ` fn f => f x has type β→γ choosing (α → γ) for β


(i.e. (α → γ)/β)

What about choosing int/α, (int → γ)/β?

COMP302: Programming Languages and Paradigms 10 / 20


Which substitution to pick, under the inference view?

x : α ` fn f => f x has type β→γ choosing (α → γ) for β


(i.e. (α → γ)/β)

What about choosing int/α, (int → γ)/β?

This gives us that


fn f => f x has type (int → γ) → γ under the assumption x : int
which is a solution.

But it’s not the most general solution!

COMP302: Programming Languages and Paradigms 10 / 20


COMP302: Programming Languages and Paradigms 11 / 20
Damas-Hindley-Milner Style Type Inference - Recipe

Given a typing context Γ and an expression e,


infer a type T (and some constraints) as follows:
• Analyze e as before following the given typing rules
• When we analyze e recursively and we miss type information,
introduce a type variable and generate possible constraints
• The type T is a skeleton that may contain type variables.
• To determine whether e is well-typed, solve the constraints!
• Solving constraints generates a type substitution σ
• Consequently, we now have expression e has type [σ]T .

COMP302: Programming Languages and Paradigms 12 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)
• Look up the type of y as β

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)
• Look up the type of y as β
• Infer the type of 2 + 2 as int.

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)
• Look up the type of y as β
• Infer the type of 2 + 2 as int.
• Add constraint β = int
(Since the branches must have the same type.)

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)
• Look up the type of y as β
• Infer the type of 2 + 2 as int.
• Add constraint β = int
(Since the branches must have the same type.)

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)
• Look up the type of y as β
• Infer the type of 2 + 2 as int.
• Add constraint β = int
(Since the branches must have the same type.)
fn x => fn y => if x then y else 2 + 2 has type α → β → β
given the constraints α = bool and β = int

COMP302: Programming Languages and Paradigms 13 / 20


Inferring Types and Constraints by Example

How to infer the type of fn x => fn y => if x then y else 2 + 2?

Assume x has type α and y has type β.


Infer the type of if x then y else 2 + 2
• Look up the type of x as α and add constraint α = bool
(Since the condition must be a boolean.)
• Look up the type of y as β
• Infer the type of 2 + 2 as int.
• Add constraint β = int
(Since the branches must have the same type.)
fn x => fn y => if x then y else 2 + 2 has type α → β → β
given the constraints α = bool and β = int

Therefore ....
fn x => fn y => if x then y else 2 + 2 has type bool → int → int
COMP302: Programming Languages and Paradigms 13 / 20
Type Inference (more formally)

Γ ` e ⇒ T /C Infer type T for expression e in the typing environ-


ment Γ modulo the set of constraints C.

Constraints, (the elements of C)


are equations between types, of the form T1 = T2 .

For example:

Γ ` e ⇒ T /C Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-if
Γ ` if e then e1 else e2 ⇒ T1 /C ∪ C1 ∪ C2 ∪ {T = bool, T1 = T2 }

Inputs are green – outputs are blue

COMP302: Programming Languages and Paradigms 14 / 20


Let’s read this ...

Γ ` e ⇒ T /C Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-if
Γ ` if e then e1 else e2 ⇒ T1 /C ∪ C1 ∪ C2 ∪ {T = bool, T1 = T2 }
Read t-if rule as :
Given the typing context Γ and expression if e then e1 else e2 , infer
a type T1 and a set of constraints C ∪ C1 ∪ C2 ∪ {T = bool, T1 = T2 }
as follows
• given the typing context Γ and expression e,
infer the type T together with the constraint C
• given the typing context Γ and expression e1 ,
infer the type T1 together with the constraint C1
• given the typing context Γ and expression e2 ,
infer the type T2 together with the constraint C2
Inputs are green – outputs are blue
COMP302: Programming Languages and Paradigms 15 / 20
Inferring Types ...

see board ...

COMP302: Programming Languages and Paradigms 16 / 20


Inferring Types ...

... and now functions!

Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C

COMP302: Programming Languages and Paradigms 16 / 20


Inferring Types ...

... and now functions!

Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C

Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}

COMP302: Programming Languages and Paradigms 16 / 20


Inferring Types ...

... and now functions!

Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C

Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}

Examples, examples, example .... please!


• fn x => x + 1 has type α → int/{α = int}
• fn x => if x then x + 1 else 2
• fn f => fn x => f (f x)

COMP302: Programming Languages and Paradigms 16 / 20


Inferring Types ...

... and now functions!

Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C

Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}

Examples, examples, example .... please!


• fn x => x + 1 has type α → int/{α = int}
• fn x => if x then x + 1 else 2 .../{α = bool, α = int}
• fn f => fn x => f (f x)

COMP302: Programming Languages and Paradigms 16 / 20


Inferring Types ...

... and now functions!

Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C

Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}

Examples, examples, example .... please!


• fn x => x + 1 has type α → int/{α = int}
• fn x => if x then x + 1 else 2 .../{α = bool, α = int}
• fn f => fn x => f (f x) has type α → β → α1
{α = β → α0 , α = α0 → α1 }
COMP302: Programming Languages and Paradigms 16 / 20
Inferring Types ...

see board ...

Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C

Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}

Examples, examples, example .... please!


• fn x => x + 1 has type α → int/{α = int}
• fn x => if x then x + 1 else 2 .../{α = bool, α = int}
• fn f => fn x => f (f x) has type α → β → α1
{α = β → α0 , α = α0 → α1 }
COMP302: Programming Languages and Paradigms 16 / 20
How to solve constraints?

Examples ...Can we solve the following constraints?

• {α = int, α → β = int → bool}

COMP302: Programming Languages and Paradigms 17 / 20


How to solve constraints?

Examples ...Can we solve the following constraints?

• {α = int, α → β = int → bool}


• {α1 → α2 = int → β, β = bool}

COMP302: Programming Languages and Paradigms 17 / 20


How to solve constraints?

Examples ...Can we solve the following constraints?

• {α = int, α → β = int → bool}


• {α1 → α2 = int → β, β = bool}
• {α1 → α2 = int → β, β = α2 → α2 }

Constraint Solving via Unification

Two types T1 and T2 are unifiable


if there exists an instantiation σ for the type variables in T1 and T2
s.t. [σ]T1 = [σ]T2 , i.e [σ]T1 is syntactically equal to [σ]T2 .

COMP302: Programming Languages and Paradigms 17 / 20


Unification via Rewriting Constraints

Given as set of constraints C try to simplify the set until we derive the
empty set.

We write C for C1 , . . . , Cn and we assume constraints can be reordered.

COMP302: Programming Languages and Paradigms 18 / 20


Unification via Rewriting Constraints

Given as set of constraints C try to simplify the set until we derive the
empty set.

We write C for C1 , . . . , Cn and we assume constraints can be reordered.

{C , int = int} =⇒ {C }

COMP302: Programming Languages and Paradigms 18 / 20


Unification via Rewriting Constraints

Given as set of constraints C try to simplify the set until we derive the
empty set.

We write C for C1 , . . . , Cn and we assume constraints can be reordered.

{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }

COMP302: Programming Languages and Paradigms 18 / 20


Unification via Rewriting Constraints

Given as set of constraints C try to simplify the set until we derive the
empty set.

We write C for C1 , . . . , Cn and we assume constraints can be reordered.

{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
{C , α = α} =⇒ {C }

COMP302: Programming Languages and Paradigms 18 / 20


Unification via Rewriting Constraints

Given as set of constraints C try to simplify the set until we derive the
empty set.

We write C for C1 , . . . , Cn and we assume constraints can be reordered.

{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
{C , α = α} =⇒ {C }
{C , (T1 → T2 ) = (S1 → S2 )} =⇒ {C , T1 = S1 , T2 = S2 }

COMP302: Programming Languages and Paradigms 18 / 20


Unification via Rewriting Constraints

Given as set of constraints C try to simplify the set until we derive the
empty set.

We write C for C1 , . . . , Cn and we assume constraints can be reordered.

{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
{C , α = α} =⇒ {C }
{C , (T1 → T2 ) = (S1 → S2 )} =⇒ {C , T1 = S1 , T2 = S2 }
{C , α = T} =⇒ {[T /α]C } provided that α 6∈ FV(T )
{C , T = α} =⇒ {[T /α]C } provided that α ∈6 FV(T )

Example : α = β → γ, β = α =⇒ ?

COMP302: Programming Languages and Paradigms 18 / 20


To summarize ...

Unification is a fundamental algorithm to determine whether two ob-


jects can be made syntactically equal.

COMP302: Programming Languages and Paradigms 19 / 20


Let-Polymorphism

1 let double f x = f ( f x )

• What is the type of double?


• What is the type of double (fn x -> x) false
• What is the type of double (fn x -> x + 1) 3
Recall ...

Γ ` e1 : T1 Γ, x:T1 ` e2 : T
t-let x must be new
Γ ` let x = e1 in e2 end : T

COMP302: Programming Languages and Paradigms 20 / 20

You might also like