Polymorphism. Lecture 19 CS 565 4/17/08

Similar documents
Introduction to System F. Lecture 18 CS 565 4/20/09

Second-Order Type Systems

Programming Languages Lecture 15: Recursive Types & Subtyping

Harvard School of Engineering and Applied Sciences Computer Science 152

Lambda Calculi With Polymorphism

Lambda Calculi With Polymorphism

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

We defined congruence rules that determine the order of evaluation, using the following evaluation

1 Introduction. 3 Syntax

The Untyped Lambda Calculus

Programming Languages

Girard s System F. Chapter System F. = λ ( f :τ 2 τ 3 ) λ (g:τ 1 τ 2 ) λ (x:τ 1 ) f (g(x)) System F

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Arbitrary-rank polymorphism in (GHC) Haskell

Lesson 11 Universal Types. Universal Types and System F

Subtyping. Lecture 13 CS 565 3/27/06

Types and Programming Languages. Lecture 5. Extensions of simple types

Programming Languages Assignment #7

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

λ calculus is inconsistent

Integration of SMT Solvers with ITPs There and Back Again

Part III. Chapter 15: Subtyping

Gradual Typing with Union and Intersection Types

Costly software bugs that could have been averted with type checking

CS558 Programming Languages

Formal Systems and their Applications

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

The Polymorphic Blame Calculus and Parametricity

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Simply-Typed Lambda Calculus

Structural polymorphism in Generic Haskell

Part III Chapter 15: Subtyping

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

CS558 Programming Languages

CS558 Programming Languages

Inductive Definitions, continued

CSE-321 Programming Languages 2010 Final

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSCI-GA Scripting Languages

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Functional Languages and Higher-Order Functions

Combining Programming with Theorem Proving

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Programming Languages Fall 2014

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Supplementary Notes on Recursive Types

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CSE-321 Programming Languages 2012 Midterm

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

Recitation 6: System F and Existential Types : Foundations of Programming Languages

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Lecture 9: Typed Lambda Calculus

Unifying Nominal and Structural Ad-Hoc Polymorphism

Calculus of Inductive Constructions

Type Systems Winter Semester 2006

CS4215 Programming Language Implementation. Martin Henz

Types, Type Inference and Unification

A New Foundation for Generic Programming

Official Survey. Cunning Plan: Focus On Objects. The Need for a Calculus. Object Calculi Summary. Why Not Use λ-calculus for OO?

Subtyping (cont) Lecture 15 CS 565 4/3/08

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Finite Model Generation for Isabelle/HOL Using a SAT Solver

CS558 Programming Languages

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Why Types Matter. Zheng Gao CREST, UCL

CSE-321 Programming Languages 2010 Midterm

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CIS 500 Software Foundations Midterm I

Type Inference with Inequalities

Subsumption. Principle of safe substitution

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Programming in Omega Part 1. Tim Sheard Portland State University

Types and Type Inference

Types and Type Inference

CS 360: Programming Languages Lecture 12: More Haskell

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

4.5 Pure Linear Functional Programming

Meeting13:Denotations

15 212: Principles of Programming. Some Notes on Continuations

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

References and Exceptions. CS 565 Lecture 14 4/1/08

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Introduction to SML Getting Started

Type Systems, Type Inference, and Polymorphism

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Recursive Types and Subtyping

Transcription:

Polymorphism Lecture 19 CS 565 4/17/08

The Limitations of F 1 (simply-typed λ- calculus) In F 1 each function works exactly for one type Example: the identity function id = λx:τ. x : τ τ We need to write one version for each type Even more important: sort : (τ τ bool) τ array unit The various sorting functions differ only in typing At runtime they perform exactly the same operations We need different versions only to keep the type checker happy Two alternatives: Circumvent the type system (see C, Java,...), or Use a more flexible type system that lets us write only one sorting function

Polymorphism Informal definition A function is polymorphic if it can be applied to many types of arguments Various kinds of polymorphism depending on the definition of many subtype (or bounded) polymorphism many = all subtypes of a given type ad-hoc polymorphism many = depends on the function choose behavior at runtime (depending on types, e.g. sizeof) parametric predicative polymorphism many = all monomorphic types parametric impredicative polymorphism many = all types

Parametric Polymorphism: Types as Parameters (System F) We introduce type variables and allow expressions to have variable types We introduce polymorphic types τ ::= b τ 1 τ 2 t t. τ e ::= x λx:τ.e e 1 e 2 Λt. e e[τ] Λt. e is type abstraction (or generalization) e[τ] is type application (or instantiation) Examples: id = Λt.λx:t. x : t.t t id[int] = λx:int. x : int int id[bool] = λx:bool. x : bool bool id 5 is invalid. Use id [int] 5 instead

Impredicative Polymorphism The typing rules:

Impredicative Polymorphism (Cont.) Verify that id [int] 5 has type int Note the side-condition in the rule for type abstraction Prevents ill-formed terms like: λx:t.λt.x The evaluation rules are just like those of F 1 This means that type abstraction and application are all performed at compile time We do not evaluate under Λ (Λt. e is a value) We do not have to operate on types at run-time This is called phase separation: type checking and execution

Observations Based on the type of a term we can prove properties of that term There is only one value of type t.t t The polymorphic identity function There is no value of type t.t Take the function: reverse : t. t List t List This function cannot inspect the elements of the list It can only produce a permutation of the original list If L 1 and L 2 have the same length and let match be a function that compares two lists element-wise according to an arbitrary predicate then match L 1 L 2 match (reverse L 1 ) (reverse L 2 )!

Expressiveness of Impredicative Polymorphism This calculus is called F2 system F second-order λ-calculus polymorphic λ-calculus Polymorphism is extremely expressive We can encode many base and structured types in F2

Encoding Base Types in F 2 Booleans bool = t.t t t (given any two things, select one) There are exactly two values of this type! Naturals true = Λt. λx:t.λy:t. x false = Λt. λx:t.λy:t. y not = λb:bool. Λt.λx:t.λy:t. b [t] y x nat = t. (t t) t t (given a successor and a zero element, compute a natural number) 0 = Λt. λs:t t.λz:t. z n = Λt. λs:t t.λz:t. s (s (s...s(n))) add = λn:nat. λm:nat. Λt. λs:t t.λz:t. n [t] s (m [t] s z) mul = λn:nat. λm:nat. Λt. λs:t t.λz:t. n [t] (m [t] s) z

Expressiveness of F 2 Polymorphic application selfapp = λx:t. t t.λy:t. x [t. t t] x : (t. t t) (t. t t) double = Λt. λf:t t. λa:t.f(f(a)) : (t. t t) t t quadruple = Λt. double[t t] (double[t]) : (t. t t) t t Recursive types? We can encode primitive recursion but not full recursion

Assessment Impredicative variant of System F very expressive: can express complex polymorphic functions type abstraction and application limited form of self-application prove interesting theorems based on type structure complicated semantics termination proof type reconstruction

Predicative Polymorphism Restriction: type variables can be instantiated only with monomorphic types This restriction can be expressed syntactically τ ::= b τ 1 τ 2 t σ ::= τ t. σ σ 1 σ 2 e ::= x e 1 e 2 λx:σ. e Λt.e e [τ] Type application is restricted to mono types Cannot apply id to itself anymore Same typing rules Simple semantics and termination proof Type reconstruction still undecidable Must restrict further!

Prenex Predicative Polymorphism Restriction: polymorphic type constructor at top level only This restriction can also be expressed syntactically τ ::= b τ 1 τ 2 t σ ::= τ t. σ e ::= x e 1 e 2 λx:τ. e Λt.e e [τ] Type application is restricted to mono types (i.e., predicative) Abstraction only on mono types The only occurrences of are at the top level of a type (t. t t) (t. t t) is not a valid type Same typing rules Simple semantics and termination proof Decidable type inference!