Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Types and Programming Languages Lecture 1 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 5 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 13 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.
CS1Q Computer Systems Lecture 14
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
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.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
CSE-321 Programming Languages Predicative Polymorphic -Calculus POSTECH May 23, 2007 박성우.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
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.
1 Structure and Interpretation of Computer Programs Modularity, Objects, and State (part 1) Abelson & Sussman 2 UC San Diego CSE 294 May 11, 2011 Barry.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
Agenda Object Oriented Programming Reading: Chapter 14.
© 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 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
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 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Types and Programming Languages
Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Types and Programming Languages Lecture 16 Simon Gay Department of Computing Science University of Glasgow 2006/07.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Lecture 9 : Universal Types
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
CSE341: Programming Languages Lecture 7 First-Class Functions
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Using local variable without initialization is an error.
Lesson #6 Modular Programming and Functions.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
Announcements Quiz 6 HW7 due Tuesday, October 30
Lesson #6 Modular Programming and Functions.
CSE341: Programming Languages Lecture 7 First-Class Functions
Inheritance and Polymorphism
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
Scope Rules.
Presentation transcript:

Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07

Types and Programming Languages Lecture 16 - Simon Gay2 Beyond Let-Polymorphism With let-polymorphism, only let-bound values can be used polymorphically. -bound values cannot be used polymorphically. Example: let f = g....g(1)...g(true)... in f( x.x) end is not typable: when typechecking the definition of f, g has type X (a fresh type variable) which is then constrained by X = int Y and X = bool Z (Exercise: check this.) Functions cannot take polymorphic functions as parameters. This is the key limitation of let-polymorphism. Is there anything better? Yes, of course there is...

2006/07Types and Programming Languages Lecture 16 - Simon Gay3 Polymorphic Lambda Calculus We will briefly look at a more powerful polymorphic language, known variously as System F, polymorphic lambda calculus, second-order lambda calculus, or 2. The idea is to introduce abstraction over types, and an explicit operation of applying a polymorphic function to a type to create a specific instance. Syntax of terms: The starting point is the simply typed lambda calculus, plus: X.t type abstraction (this term is also a value) t [T] type application

2006/07Types and Programming Languages Lecture 16 - Simon Gay4 Polymorphic Lambda Calculus Syntax of types: X type variable X.T universal type We also have function types, and base types if we want them. Environments: Environments contain type variables: e.g. = X, x:X X This is to keep track of the scope of type variables.

2006/07Types and Programming Languages Lecture 16 - Simon Gay5 Polymorphic Lambda Calculus Reduction: Typing: The usual rules, plus (R-TApp) ( X.t) [ T ] t [ X T ] (R-TAppTAbs) The usual rules, plus (T-TAbs)(T-TApp)

2006/07Types and Programming Languages Lecture 16 - Simon Gay6 Polymorphic Lambda Calculus Example: double = X. f:X X. a:X. f(f(a)) has type X. (X X) X X which can be instantiated as required: double [int] ( x:int. x+2) 2 double [bool] ( x:bool. x) false This can also be achieved by let-polymorphism, as we have seen. We can go further...

2006/07Types and Programming Languages Lecture 16 - Simon Gay7 Polymorphic Lambda Calculus Example: f = g: X. (X X) X X.... g [int] ( x:int. x+2) g [bool] ( x:bool. x) false... has type ( X. (X X) X X)... A polymorphic function can be passed as a parameter; it can be instantiated as required by the receiving function. Here we have a type in which is not at the top level. This is the crucial difference from let-polymorphism.

2006/07Types and Programming Languages Lecture 16 - Simon Gay8 Facts about Polymorphic Lambda Calculus The basic properties of the simply typed lambda calculus are also true of the polymorphic lambda calculus: type safety and termination (of course, recursive definitions can be added). Polymorphic type constructors (e.g. List) can be encoded in the pure polymorphic lambda calculus. Type reconstruction for polymorphic lambda calculus is undecidable (this was an open question since the 1970s, solved in 1994).

2006/07Types and Programming Languages Lecture 16 - Simon Gay9 Bounded Polymorphism Polymorphism and subtyping can be combined. A natural way to do this is called bounded polymorphism. In polymorphic types, type variables are associated with bounds, for example X<:T. X X is a type in which the type variable X can be instantiated with any type S which is a subtype of T, and then we have a function of type S S. This is different from the subtype polymorphism in standard OO languages, where the best we can do is, in effect, X<:T. X T

2006/07Types and Programming Languages Lecture 16 - Simon Gay10 Bounded Polymorphism vs Subtype Polymorphism Standard OO languages provide subtype polymorphism: we can define (e.g. in Java): T f(T x) {... } and then f can be applied to a value of any subtype of T, say S, but the result has type T even if f actually returns a value of the same type as its parameter. In effect the type of f is X<:T. X T. If we know that the result of f has the same type as its parameter, then a cast is required, with a runtime type check. A typical example of this problem is when working with generic data structures.

2006/07Types and Programming Languages Lecture 16 - Simon Gay11 Hashtable: Subtype Polymorphism class Hashtable {... Object get(Object key) {...} Object put(Object key, Object value) {...}... } Hashtable h = new Hashtable(); MyClass m = new MyClass(); h.put(Simon,m); MyClass n = (MyClass)h.get(Simon);

2006/07Types and Programming Languages Lecture 16 - Simon Gay12 Hashtable: Bounded Polymorphism The recently-released version 1.5 of Java adds bounded polymorphism to the language. It is based on a proposal known as GJ (Generic Java) (Bracha, Odersky, Stoutamire, Wadler). C# also supports bounded polymorphism. class Hashtable {... V get(K key) {...} V put(K key, V value) {...}... } Hashtable h = new Hashtable(); MyClass m = new MyClass(); h.put(Simon,m); MyClass n = h.get(Simon);

2006/07Types and Programming Languages Lecture 16 - Simon Gay13 Reading Pierce: 23 Exercises Pierce: , , , (handout) Now that Java has generics, is there any need for the covariant subtyping rule for arrays? (if T <: U then T[ ] <: U[ ] )