Decidable type checking Parametric polymorphism = Generics in Java (type erasure) | Templates in C++ (compile time metaprogramming) Turing complete type checking. let root t = match t with | Node(v, _, _) -> v;; Principal type of an Ocaml Theorem: Every expression in Ocaml has a unique principal type. Theorem: Algorithm W computes the principal type of each expression. (Hindley-Milner type inference) int foo(int x) { return foo(x + 1); } ========== Tail recursion ============================ Tail call optimization let rec sum n = if (n == 0) then 0 else n + sum (n - 1);; sum 5 ==> if (5 == 0) then 0 else 5 + sum(5 - 1) ==> if (false) then 0 else 5 + sum(5 - 1) ==> 5 + sum(5 - 1) ==> 5 + sum 4 ==> 5 + (if (4 == 0) then 0 else 4 + sum(4 - 1)) ==> ... ==> 5 + (4 + sum 3) ==> ... ==> 5 + (4 + 3 + sum 2) ==> ... ==> 5 + (4 + (3 + (2 + (1 + 0)))) Context is stored on the stack. The context describes how to use the results of these deeply nested computations. The stack is a limited resource. let rec count_sum acc n = if (n == 0) then acc else count_sum (acc + n) (n - 1) let sum n = count_sum 0 n Finish reading Chapter 2: Variables and Functions Chapter 3: Lists and Patterns Chapters 5, 6 : Records and Variants On Tuesday: Higher order functions.