System F System F
Total Page:16
File Type:pdf, Size:1020Kb
Recap CS6848 - Principles of Programming Languages Principles of Programming Languages Extensions to simply typed lambda calculus. V. Krishna Nandivada Pairs, Tuples and records IIT Madras * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 2 / 18 Polymorphism - motivation Polymorphism - variations Type systems allow single piece of code to be used with multiple types are collectively known as polymorphic systems. Variations: f x f f x AppTwiceInt = l : Int ! Int :l : Int : ( ) Parametric polymorphism: Single piece of code to be typed AppTwiceRcd = lf :(l : Int ) ! (l : Int ):lx :(l : Int ):f (f x) generically (also known as, let polymorphism, first-class AppTwiceOther = polymorphism, or ML-style polymorphic). lf :(Int ! Int ) ! (Int ! Int ):lx :(Int ! Int ):f (f x) Restricts polymorphism to top-level let bindings. Disallows functions from taking polymorphic values as arguments. Uses variables in places of actual types and may instantiate with actual types if needed. Example: ML, Java Generics Breaks the idea of abstraction: Each significant piece of (let ((apply lambda f. lambda a (f a))) functionality in a program should be implemented in just one place (let ((a (apply succ 3))) in the source code. (let ((b (apply zero? 3))) ... Ad-hoc polymorphism - allows a polymorphic value to exhibit different behaviors when viewed using different types. Example: function Overloading, Java instanceof operator. subtype polymorphism: A single term may get many types using * subsumption. * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 3 / 18 V.KrishnaJava Nandivada 1.5 onwards (IIT Madras) admits Parametric,CS6848 (IIT Madras) Ad-hoc and subtype 4 / 18 polymorphism. Parametric Polymorphism - System F System F Definition of System F - an extension of simply typed lambda calculus. Lambda calculus recall System F discovered by Jean-Yves Girard (1972) Lambda abstraction is used to abstract terms out of terms. Polymorphic lambda-calculus by John Reynolds (1974) Application is used to supply values for the abstract types. Also called second-order lambda-calculus - allows quantification over types, along with terms. System F A mechanism for abstracting types of out terms and fill them later. A new form of abstraction: lX:e – parameter is a type. Application – e[t] called type abstractions and type applications (or instantiation). * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 5 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 6 / 18 Type abstraction and application Extension Expressions: (lX:e)[t ] ! [X ! t ]e 1 1 e ::= ···jlX:eje[t] Examples Values v ::= ···jlX:e id = lX:lx : X:x Types t ::= ···j8X:t Type of id : 8X:X ! X typing context: A ::= fjA;x : tjA;X applyTwice = lX:lf : X ! X:la : X f (f a) Type of applyTwice : 8X:(X ! X) ! X ! X * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 7 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 8 / 18 Evaluation Typing rules 0 e1 ! e1 A;X ` e1 : t1 type application 1 — type abstraction 0 A ` X:e : 8X:t e1[t1] ! e1[t1] l 1 1 A ` e1 : 8X:t1 type appliation 2 — (lX:e1)[t1] ! [X ! t1]e1 type application A ` e1[t2]:[X ! t2]t1 * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 9 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 10 / 18 Examples Polymorphic lists id = lX:lx : X x id : 8X:X ! X List of uniform members nil : 8X:List X type application: id [Int ]: Int ! Int cons: 8X:X ! List X ! List X value application: id[Int ] 0 = 0 : Int isnil: 8X:List X ! bool applyTwice = lX:lf : X ! X:la : Xf (f a) head: 8X:List X ! X tail: 8X:List X ! List X ApplyTwiceInts = applyTwice [Int ] applyTwice[Int ](lx : Int :succ(succx)) 3 = 7 * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 11 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 12 / 18 Example Church literals Booleans Recall: Simply typed lambda calculus - we cannot type lx:x x. tru = lt:lf :t How about in System F? fls = lt:lf :f selfApp : (8X:X ! X) ! (8X:X ! X) Idea: A predicate will return tru or fls. We can write if pred s1 else s2 as (pred s1 s2) * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 13 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 14 / 18 Building on booleans Building pairs pair = lf :ls:lb:b f s and = lb:lc:b c fls To build a pair: pair v w or = ? lb:lc:b tru c fst = lp:p tru not = ? snd = lp:p fls * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 15 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 16 / 18 Church numerals (Recall) Type inference algorithm (Hindley-Milner) Input: G: set of type equations (derived from a given program). c0 = ls:lz: z Output: Unification s c1 = ls:lz: s z 1 failure = false; s = fg. c2 = ls:lz: s s z 2 while G 6= f and : failure do c3 = ls:lz: s s s z 1 Choose and remove an equation e from G. Say es is (s = t). Intuition 2 If s and t are variables, or s and t are both Int then continue. 3 If s = s ! s and t = t ! t , then G = G [ fs = t ;s = t g: Each number n is represented by a combinator cn. 1 2 1 2 1 1 2 2 4 If (s = Int and t is an arrow type) or vice versa then failure = true. c s z s n takes an argument (for successor) and (for zero) and apply , 5 If s is a variable that does not occur in t, then s = s o [s := t]. n times, to z. 6 If t is a variable that does not occur in s, then s = s o [t := s]. 7 c0 and fls are exactly the same! If s 6= t and either s is a variable that occurs in t or vice versa then failure = true. This representation is similarto the unary representation we studies before. 3 end-while. scc = ln:ls:lz:s (n s z) 4 if (failure = true) then output “Does not type check”. Else o/p s. * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 17 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 18 / 18 Examples - derive the types “Occurs” check a = lx:ly:x Ensures that we get finite types. b = lf : (f 3) If we allow recursive types - the occurs check can be omitted. Say in (s = t), s = A and t = A ! B. Resulting type? c = lx: (+(head x) 3) What if we are interested in System F - what happens to the type d = lf : ((f 3);(f ly: y)) inference? (undecidable in general) appTwice = lf : lx: f f x Self study. * * V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 19 / 18 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 20 / 18.