Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
The lambda calculus David Walker CS 441. the (untyped) lambda calculus a language of functions e ::= x | \x.e | e1 e2 v ::= \x.e there are several different.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CSE-321 Programming Languages Predicative Polymorphic -Calculus POSTECH May 23, 2007 박성우.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.
Semantics for MinML COS 441 Princeton University Fall 2004.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Propositional Calculus Math Foundations of Computer Science.
Chapter 4: A Universal Program 1. Coding programs Example : For our programs P we have variables that are arranged in a certain order: Y 1 X 1 Z 1 X 2.
Functional Programming Element of Functional Programming.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
Type Safety Kangwon National University 임현승 Programming Languages.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Specifying Languages Our aim is to be able to specify languages for use in the computer. The sketch of an FSA is easy for us to understand, but difficult.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Lecture 9 : Universal Types
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
CS5205: Foundations in Programming Languages
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Principles of programming languages 12: Functional programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
ML: a quasi-functional language with strong typing
A lightening tour in 45 minutes
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Typed Arithmetic Expressions
PROGRAMMING IN HASKELL
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
CSE S. Tanimoto Lambda Calculus
Modern Programming Languages
Presentation transcript:

Universal Types Report by Matthias Horbach

Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity

Types of Polymorphism

polymorphism universal (true) ad hoc (apparent) parametric inclusion (new) overloading coercion according to Strachey (1967) and Cardelli/Wegner (1985) and others and in more complex relations…

Ad Hoc Polymorphism Overloading one name for different functions just a convenient syntax abbreviation example: +: int  int z.B : real  real z.B Coercion and Casts convert argument to fulfill requirements examples: ((real) 1) or Operators only seem to be polymorphic!

Universal Polymorphism Inclusion Polymorphism one “object” belongs to many “classes” used in object oriented languages modeled by subtypes example: Dog, Bird  Animal

Universal Polymorphism Parametric Polymorphism Uniformity of type structure achieved by type parameters examples: length (1::2::3::nil)//parameter: int length (true::true::false::nil)//parameter: bool length (“hello”::“, ”::“world”::nil)//parameter: string let-polymorphism: - ML, no polymorphic arguments - automatic type reconstruction possible (see Sven) We will look at a system with explicit type annotation.

System F

System F (Context) Polymorphic -calculus Second order -calculus Idea: do lambda abstraction over type variables, define functions over types Girard (1972), motivation: logics Reynolds (1974), motivation: programming

System F (What’s new?) Extension of the simply typed -calculus: Abstraction and application also for types: t ::=… X.t (type abstraction) t [T] (type application) A new value: v ::=… X.t (type abstraction value) Added types: T ::=… X (type variable)  X.T (universal type) Adjusted contexts:  ::=… , X (type variable binding)

System F (Rules, 1) New typing rules: type abstractiontype application New evaluation rules: type application (1)type application (2) t  t’ t [T]  t’ [T] ( X.t) [T]  [T/X] t ,X  t : T   X.t :  X.T   t :  X.T’   t [T] : [T/X] T’

System F (Rules, 2) Needed restriction: Types in these rules have to be closed, or free type variables have to be bound: X    X :  ,X  T :     X.T :    T 1 :    T 2 :    T 1  T 2 : 

System F (Examples) The polymorphic identity function (System F and ML) id = X. x:X. xval id = fn x => x > id:  X. X  X> ‘a id: ‘a  ‘a is applied as follows: id [Nat] 5id 5 which is evaluated as ( X. x:X. x) [Nat] 5  [Nat/X]( x:X. x) 5  ( x:Nat. x) 5  [5/x](x)  5

System F (Further Examples) Double application ( f(f(x)) ): double = X. f.X  X. x. f (f x) > double:  X. (X  X)  X  X (ML: val double = fn f => fn x => f(f x) > ‘a double: (‘a  ’a)  ‘a  ‘a ) doubleFun = double [Nat  Nat] > doubleFun: ((Nat  Nat)  Nat  Nat)  (Nat  Nat)  Nat  Nat doubleFun ( x. x+1) 3 > 5

System F (Further Examples) Self application: In simply typed -calculus, you cannot type x. x x. Now: selfApp = f.f f selfApp = f:  X.X  X.f [  X.X  X] f > selfApp: (  X.X  X)  (  X.X  X) evaluation: selfApp id  ( f:  X.X  X. f [  X.X  X] f) id  ( X. x:X. x) [  X.X  X] id  ( x:  X.X  X. x) id  id

Basic Properties

Type Uniqueness, Type Preservation and Progress Theorem [Uniqueness]: Every well-typed system F term has exactly one type. Theorem [Preservation]:   t : T and t  t’ implies   t’ : T. Theorem [Progress]: If t is closed and well founded, then either t is a value or t  t’ for some t’. Proofs: straightforward structural induction

Normalization Theorem: Every well-typed System F term is normalizing, i.e. the evaluation of well-typed programs terminates. Proof: very hard (Girard 1972, doctoral thesis) (simplified later on to about 5 pages) Amazing: Normalization holds although we can code many things. to sorting function

Normalization – Simple Application There are untypable terms! Example: ( x. x x) ( x. x x) cannot be typable, since this term has no normal form.

Erasure

Erasure and Type Reconstruction See System F as extension of untyped -calculus: erase(x)= x erase( x:T. t)= x.erase(t) erase(t t‘)= (erase(t))(erase(t‘)) erase( X.t)= erase(t) erase(t[T])= erase(t) Theorem (Wells, 1994): Let m be a closed term. It is undecidable, whether there is a well typed System F term t such that m = erase(t). Are there solutions for weaker erasure?

Erasure and Evaluation Erasure operational semantics: Throw away types. Assume existence of divergence, side effects… Then let f = X. diverge in 0 diverges, but let f = diverge in 0 does. So another reasonable erasure is: erase(x)= x erase( x:T. t)= x.erase(t) erase(t t‘)= (erase(t)) (erase(t‘)) erase( X.t)= _.erase(t) erase(t[T])= erase(t) () Type reconstruction is still undecidable (Pfenning 1992).

Impredicativity

Impredicativity System F is impredicative: Polymorphic types are defined by universal quantification over the universe of all types. This includes polymorphic types themselves. Polymorphic types are „1st class“ in the world of types. example: ( f:  X.X  X. f) id universally quantified type

Impredicativity ML-polymorphism is predicative: Polymorphic types are 2nd class, arguments do not have polymorphic types! („prenex polymorphism“) example: (fn f => fn x => f x) id 3 only one type / instanciated

Parametricity

Parametricity Evaluation of polymorphic applications does not depend on the type that is supplied. This is a strong invariant!

Parametricity Examples of easy results from parametricity: There is exactly one function of type  X.X  X, namely the identity function. There are exactly two functions of type  X.X  X  X behaving differently, namely those denoted by X. a:X. b:X. a X. a:X. b:X. b These do not (and cannot) alter their behavior depending on X!

Church Encodings: Booleans System F has hidden structure: CBool =  X. X  X  X contains (as already seen) tru = X. t:X. f:X. t(> tru: CBool) fls = X. t:X. f:X. f(> fls: CBool) and other terms, but it is intuitively clear that they all behave like either tru or fls. One function on CBool is not = b:CBool. ( X. t:X. f:X. b [X] f t)

Church Encodings: Nat Elements of Nat could be encoded as The corresponding type is CNat =  X. (X  X)  X  X and a term encoding the successor function is csucc = n:CNat. ( X. s:X  X. z:X. s (n [X] s z) ) c0 = X. s:X  X. z:X. z c1 = X. s:X  X. z:X. s z c2 = X. s:X  X. z:X. s (s z) c0 = s. z. z c1 = s. z. s z c2 = s. z. s (s z)

Summary  System F is highly expressive!  Still, it is strongly normalizing!!!  Types must not be omitted. In practice: Trade-off between convenience (e.g. automated type checking) and expressivity.

References Barendregt: Lambda Calculi with Types Handbook of Computer Science, Vol. 2, 1992 Cardelli, Wegner: On Understanding Types, Data Abstraction, and Polymorphism Computing Surveys, Vol. 17, No. 4, p , 1985 MacQueen: Lecture Notes Chicago, 2003 Pfenning: On the Undecidability of Partial Polymorphic Type Reconstruction Fundamentae Informaticae, Vol 19, No. 1-2, p , 1993 Pierce: Types and Programming Languages, Chapter 22 MIT Press, 2002

Questions / The END

A Sorting Function in System F (Reynolds 1985) List X =  R. (X  R  R)  R  R insert = X. leq:X  X  bool. l:List X. e:X. let res = l [List X * List X] ( hd:X. acc:List X * List X. let rest = acc.1 in let newrest = hd::rest in let restwithe = acc.2 in let newrestwithe = if leq e hd then e::hd::rest else hd::restwithe in (newrest, newrestwithe) ) (nil[X], e::nil[X]) in res.2 sort = X. leq:X  X  bool. l:List X. l [List X] ( hd:X. rest:List X. insert [X] leq rest hd) (nil [X])  only pure calculus, w/o fix or recursion back to normalization

System F (Polymorphic Lists) List X =  R. (X  R  R)  R  R (Church encoding) nil:  X. List X X. ( R. c:X  R  R. n:R. n) cons:  X. X -> List X -> List X isnil:  X. List X -> Bool head:  X. List X -> X tail:  X. List X -> List X map:  X.  Y. (X -> Y) -> List X -> List Y