CSE 230: Winter 2010 Principles of Programming Languages Lecture 11: Type Systems News New HW up soon Special Hour to discuss HW? Ranjit Jhala UC San Diego Programming with λ-calculus Encode: bool if-then-else int recursion as functions Encoding Natural Numbers Q: What can we do with a natural number? A: Iterate a number of times over some function Nat: function that takes fun f, starting value s: returns: f applied to s a number of times 0 = def λf. f λs. s 1 = def λf. λs. f s 2 = def λf. λs. f (f s) M Called Church numerals, unary representation Note: (n f s) : apply f to s n times, i.e. f n (s)
Operating on Natural Numbers Testing equality with 0 iszero n = def n (λb. false) true iszero = def λn. n (λ b.false) true The successor function succ n = def λf. λs. f (n f s) succ = def λn. λf. λs. f (n f s) Addition add n 1 n 2 = def n 1 succ n 2 add = def λn 1.λn 2. n 1 succ n 2 Multiplication mult n 1 n 2 = def n 1 (add n 2 ) 0 mult = def λn 1.λn 2. n 1 (add n 2 ) 0 Ex: Computing with Naturals What is the result of add 0? (λn 1. λn 2. n 1 succ n 2 ) 0 β λn 2. 0 succ n 2 = λn 2. (λf. λs. s) succ n 2 β λn 2. n 2 = λx. x Ex: Computing with Naturals mult 2 2 2 (add 2) 0 (add 2) ((add 2) 0) 2 succ (add 2 0) 2 succ (2 succ 0) succ (succ (succ (succ 0))) succ (succ (succ (λf. λs. f (0 f s)))) succ (succ (succ (λf. λs. f s))) succ (succ (λg. λy. g ((λf. λs. f s) g y))) succ (succ (λg. λy. g (g y))) * λg. λy. g (g (g (g y))) = 4 λ Calculus Review Equivalent to Turing machine Encodes several datatypes bool, int, pairs, (HW: lists ) Recursion
Encoding Recursion Write a function find that: takes predicate P, natural n returns: smallest natural larger than n satisfying i P find can encode all recursion but how to write it? Encoding Recursion find satisfies the equation: find p n = if p n then n else find p (succ n) Define: F = λf.λp.λn.(p n) n (f p (succ n)) A fixpoint of F is an x st s.t. x = F x find is a fixpoint of F! as find p n = F find p n so find = F find Q: Given λ-term F, how to write its fixpoint? The Y-Combinator Define: Y = def λf. (λy.f(y y)) (λx. F(x x)) Called the fixpoint combinator as Y F β (λy.f (y y)) (λx. F (x x)) β F ((λx.f (x x))(λz. F (z z))) β F (Y F) ie i.e. Y F = β F (Y F) Can get fixpoint for any λ-calculus function Whoa! Define: F = λf.λp.λn.(p n) n (f p (succ n)) and: find = Y F Whats going on? find p n = β Y F p n = β F (Y F) p n = β F find p n = β (p n) n (find p (succ n))
Fixpoint Combinators Y = def λf. (λy.f(y y)) (λx. F(x x)) How does this mix with Call-by-Value? Y F β (λy.f (y y)) (λx. F (x x)) β F ((λx.f (x x))(λz. F (z z))) β F (F ((λx.f (x x))(λz. F (z z)))) β F (F (F ((λx.f (x x))(λz. F (z z))))) β Many other fixpoint combinators Including those that work for CBV Including Klop s Combinator: Y k = def (L L L L L L L L L L L L L L L L L L L L L L L L L L) where: L = def λaλbλcλdλeλfλgλhλiλjλkλlλmλnλoλpλqλsλtλuλvλwλxλyλzλr. r (t h i s i s a f i x p o i n t c o m b i n a t o r) Expressiveness of λ-calculus Encodings are fun but programming in pure λ-calculus is not Encodings complicate static analysis Type Systems Know the λ-calculus encodes them, so we add 0,1,2,,true,false,if-then-else then else to language Next, we will add types
Types Variables have many values during execution Type: Overapprox bound on set of values Var : bool only takes on boolean values If x : bool then expression not(x) has a sensible meaning during every run Type = property/predicate satisfied by variable at all times during execution i.e. type = Invariant Why types? To prevent classes of execution errors: abc + true if ranjit then else Accessing missing fields Null pointer dereference Using tainted format strings printf(s, ) Writing user pointers inside kernel Preventing Errors by Static Checking Finds errors at compile-time before testing Types provide necessary information Types are invariants Java, C#, ML, For some errors static check difficult Preventing Errors by Dynamic Checking when static checking is difficult e.g. array-bounds checking, divide by 0 Run-time encoding of types (e.g. Lisp) Delays manifestation of errors but better late than never
Safe Languages Types restrict programs to prohibit classes of errors statically or dynamically Safe Typed Static Dynamic ML, Java, C#, F# Lisp, Python Untyped λ-calculus Unsafe C, C++,...? Assembly CSE 230 = statically typed languages