CS 476 – Programming Language Design William Mansky
From Typed Lambda Calculus to OCaml • User-friendly syntax • Basic types, tuples, records • Inductive datatypes and pattern-matching • Local declarations • References ØType inference • Generics/polymorphism 1
Type Inference • 2
Type Inference • Our typing rules have the form: • We’ve been implementing type checkers that take the bottom type as input and return yes/no • We could also read that type as output • First-pass inference algorithm: label literals with their types, then pass types up the AST 3
Direct Type Inference 4
Direct Type Inference 5
Direct Type Inference 6
Direct Type Inference 7
Direct Type Inference 8
Direct Type Inference 9
Direct Type Inference let type_of (e : exp) : typ = … 10
Direct Type Inference 11
Direct Type Inference 12
Direct Type Inference 13
Direct Type Inference 14
Direct Type Inference 15
Direct Type Inference 16
Direct Type Inference 17
Direct Type Inference 18
Direct Type Inference 19
Type Inference • 20
Constraint-Based Type Inference • Not every lambda-term is well typed 21
Constraint-Based Type Inference • Not every lambda-term is well typed 22
Constraint-Based Type Inference • Not every lambda-term is well typed 23
Constraint-Based Type Inference • 24
Type Inference int f(int x, int y){ return x + y; } let f x y = x + y; ; 25