Lambda Calculi With Polymorphism

Similar documents
Lambda Calculi With Polymorphism

The Untyped Lambda Calculus

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...

The Untyped Lambda Calculus

CIS 500 Software Foundations Fall December 6

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

The Untyped Lambda Calculus

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

Polymorphism. Lecture 19 CS 565 4/17/08

Lecture 13: Subtyping

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

Second-Order Type Systems

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

Subsumption. Principle of safe substitution

Part III. Chapter 15: Subtyping

Costly software bugs that could have been averted with type checking

CIS 500 Software Foundations Fall December 4

Harvard School of Engineering and Applied Sciences Computer Science 152

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

Programming Languages Lecture 15: Recursive Types & Subtyping

Part III Chapter 15: Subtyping

Subtyping. Lecture 13 CS 565 3/27/06

CIS 500 Software Foundations Midterm I

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Symmetry in Type Theory

Recursive Types and Subtyping

Formal Systems and their Applications

CSE-321 Programming Languages 2012 Midterm

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

Arbitrary-rank polymorphism in (GHC) Haskell

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

Recursive Types and Subtyping

Programming Languages Fall 2014

CIS 500 Software Foundations Fall October 2

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Lesson 11 Universal Types. Universal Types and System F

Let Arguments Go First

Inductive Definitions, continued

Type Systems Winter Semester 2006

The lambda calculus (An introduction)

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Featherweight Java (FJ)

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

Advances in Programming Languages

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

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

The Polymorphic Blame Calculus and Parametricity

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Programming Languages Assignment #7

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

(Advanced) topics in Programming Languages

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

The Untyped Lambda Calculus

Dependent Object Types - A foundation for Scala s type system

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

CSE-321 Programming Languages 2010 Final

COS 320. Compiling Techniques

CMSC 330: Organization of Programming Languages

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

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

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

Calculus of Inductive Constructions

Polymorphism and System-F (OV)

Lecture 9: Typed Lambda Calculus

Programming Languages

Subtyping and Objects

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Dependent Object Types - A foundation for Scala's type system

Consistent Subtyping for All

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

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Mutable References. Chapter 1

CS 320: Concepts of Programming Languages

More Lambda Calculus and Intro to Type Systems

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

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

Concepts of programming languages

Z Notation. June 21, 2018

Where is ML type inference headed?

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Lecture slides & distribution files:

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

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

CSE-321 Programming Languages 2011 Final

Functional Languages and Higher-Order Functions

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CIS 500 Software Foundations Fall September 25

(Refer Slide Time: 4:00)

Type Systems, Type Inference, and Polymorphism

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

CSE 505 Graduate PL. Fall 2013

CSE-321 Programming Languages 2010 Midterm

Basic Foundations of Isabelle/HOL

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1

Transcription:

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... paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook. x(1).!x(1) x.set(1) Programming Language Theory Lambda Calculi With Polymorphism Ralf Lämmel [Järvi] Slides by J. Järvi: Programming Languages, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002

Polymorphism -- Why? What s the identity function? In the simply typed lambda calculus, we need many! Examples λx:bool. x λx:nat. x λx:bool bool. x λx:bool nat. x... In need of polymorphic functions, i.e., functions that accept many types of arguments. 87

Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Font size correlates with coverage in lecture. Existential types ( exists as opposed to for all ) 88

Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 89

System F -- Syntax tax Type variable t ::=x v tt t[t ] v ::=λx :T.t ΛX.t T ::=X T T X.T System F [Girard72,Reynolds74] = (simply-typed) lambda calculus + type abstraction & application Type application Type abstraction Polymorphic type Example: id : X.X X id =ΛX.λx :X.x 90

System F -- Typing rules Type variables are held the context. T-Variable x : T Γ Γ x : T T-Application Γ t : U T T-Abstraction Γ, x : T u : U Γ λx : T.u : T U Γ tu: T Γ u : U Type variables are subject to alpha conversion. T-TypeAbstraction Γ, X t : T Γ ΛX.t : X.T T-TypeApplication Γ t : X.T Γ t[t 1 ]:[T 1 /X ]T 91 Example: id : X.X X id =ΛX.λx :X.x

System F -- Evaluation rules E-AppFun t 1 t 1 t 1 t 2 t 1 t 2 E-TypeApp E-TypeApp t 1 t 1 t 1 [T ] t 1 [T ] E-AppArg t t vt vt E-AppAbs (λx :T.t) v [v/x]t E-TypeAppAbs (ΛX.t)[T ] [T /X ]t Example: id : X.X X id =ΛX.λx :X.x 92

Examples Term id =ΛX.λx :X.x id[bool] id[bool] true id true Type : X.X X : bool bool : bool type error In actual programming languages, type application may be implicit. 93

Instantiated with nat The doubling function double=λx.λf :X X.λx :X.f (f x) double_nat = double [nat] : (nat nat) nat nat Instantiated with nat nat double_nat_arrow_nat = double [nat nat] : ((nat nat) nat nat) (nat nat) nat nat Invoking double double [nat] (λx : nat.succ (succ x)) 5 * 9 94

Functions on polymorphic functions Consider the polymorphic identity function: id : X. X X id = ΛX.λx : X.x Use id to construct a pair of Boolean and String: pairid : (Bool, String) pairid = (id true, id true ) Abstract over id: pairapply : ( X. X X) (Bool, String) pairapply = λf : X. X X. (f true, f true ) Type application left implicit. Argument must be polymorphic! 95

Self application Not typeable in the simply-typed lambda calculus λx :?. x x Typeable in System F selfapp : ( X.X X) ( X.X X) selfapp = λx : X.X X.x [ X.X X] x 96

The fix operator (Y) Not typeable in the simply-typed lambda calculus Extension required Γ t : T T Typeable in System F. Γ fix t : T fix : X.(X X) X Encodeable in System F with recursive types. fix =? See [TAPL] 97

Meaning of all types In the type X...., we quantify over all types. Predicative polymorphism X ranges over simple types. Polymorphic types are type schemes. Type inference is decidable. Impredicative polymorphism X also ranges over polymorphic types. Type inference is undecidable. type:type polymorphism X ranges over all types, including itself. Computations on types are expressible. Type checking is undecidable. Not covered by this lecture Generality is used for selfapp. 98

Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 99

What is subtyping anyway? We say S is a subtype of T. S <: T Liskov substitution principle: For each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2. Practical type checking: Any expression of type S can be used in any context that expects an expression of type T, and no type error will occur. Subtype preserves behavior. Subtype preserves type safety. 100

Why subtyping Function in near-to-c: void foo( struct { int a; } r) { r.a = 0; } Function application in near-to-c: struct K { int a; int b: } K k; foo(k); // error Intuitively, it is safe to pass k. Subtyping allows it. 101

Subsumption (Subsititutability of supertypes by subtypes) Γ t : U Γ t : T U <: T This rule implies, for example, that the actual argument s type in a function application can also be a subtype of the function s argument type. 102

Structural subtyping for records A subtype may have additional label/type pairs. A subtype may permute the label/type pairs. A subtype may use subtypes for label/type pairs. 103

A subtype may have additional label/type pairs. S-RecordNewFields {l i : T i i 1...n+k } <: {l i : T i i 1...n } Example: {key : bool, value : int, map : int int} <: {key : bool, value : int} 104

A subtype may permute the label/type pairs. S-RecordPermutation {l i : T i i 1...n } is a permutation of {k j : U j j 1...n } {l i : T i i 1...n } <: {k j : U j j 1...n } Example: {key : bool, value : int} <: {value : int, key : bool} 105

A subtype may use subtypes for label/type pairs. Example: S-RecordElements for each i T i <: U i {l i : T i i 1...n } <: {l i : U i i 1...n } {field1 : bool, field2 : {val : bool}} <: {field1 : bool, field2 : {}} 106

General rules for subtyping Subsumption Reflexivity of subtyping Transitivity of subtyping Subtyping for function types Supertype of everything Up and down cast 107

General rules for subtyping Reflexivity T <: T Transitivity T <: U T <: V U <: V Example which needs transitivity Prove that {a : bool, b : int, c : {l : int}} <: {c : {}} 108

Subtyping of functions Subsumption provides subtyping for function arguments and results. An additional rule is needed to provide subtyping for function types. Compare this to the need for subtyping rules for record types. Consider the following function application: (λf : {a:int, b:int} {c:int}. f {a:42, b:88}) g Several types can be permitted for g: g : {a:int, b:int} {c:int} g : {a:int} {c:int, d:int}... The actual function may use less fields and return more fields. 109

Subtyping of functions Function subtyping covariant on return types contravariant on parameter types T 2 <: T 1 U 2 <: U 1 T 1 U 2 <: T 2 U 1 110

Supertype of everything T ::=... top The most general type T <: top The supertype of all types 111

Remember type annotation? Syntax: t ::=... t as T Typing rule: Γ t : T Γ t as T : T Evaluation rules: t u t as T u as T v as T v 112

Annotation as up-casting Illustrative type derivation:.. Γ t : U U <: T Γ t : T Γ t as T : T That is, type annotation automagically works as upcast because of the subsumption rule. Example: (λx :bool.{a = x, b = false}) true as {a : bool} 113

Annotation as down-casting Typing rule: Γ t : U Γ t as T : T Potentially too liberal Evaluation rules: t u t as T u as T Runtime type check v : T v as T v 114

Algorithmic subtyping Reminder: A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute. [B.C. Pierce] We violate this definition because some rules are not syntax-driven! 115

Violation of syntax direction Consider an application: t u where t of type U V and u of type S. A type checker would need to figure out that S <: U. This is hard with the rules so far (transitivity, subsumption). The rules need to be redesigned. 116

Analysis of subsumption The term in the conclusion can be anything. It is just a metavariable. T-Subsumption Γ t : U U <: T Γ t : T Example: Which rule should you apply here? Γ (λx :U.t) :? T-Abstraction or T-Subsumption? 117

Analysis of transitivity S-Transitivity T <: U T <: V U does not appear in conclusion. U <: V Thus, to show T <: V, we need to guess a U. For instance, try to show the following: {y :int, x :int} <: {x :int} 118

Analysis of transitivity What is the purpose of transitivity? Chaining together separate subtyping rules for records! S-RecordPermutation {l i : T i i 1...n } is a permutation of {k j : U j j 1...n } {l i : T i i 1...n } <: {k j : U j j 1...n } S-RecordElements for each i T i <: U i {l i : T i i 1...n } <: {l i : U i i 1...n } S-RecordNewFields {l i : T i i 1...n+k } <: {l i : T i i 1...n } 119

Algorithmic subtyping Replace all previous rules by a single rule. S-Record {l i i 1...n } {k j j 1...m } l i = k j implies U i <: T j e d need to prove: {k j : U j i 1...m } <: {l i : T i i 1...n } Correctness / completeness of new rule can be shown. Maintain extra rule for function types. } { S-Function T 1 <: T 2 U 1 <: U 2 T 2 U 1 <: T 1 U 2 120

Algorithmic subtyping The subsumption rule is still not syntax-directed. The rule is essentially used in function application. Express subsumption through an extra premise. Retire subsumption rule. T-Application Γ t : U T Γ u : V V <: U Γ tu: T 121

Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 122

Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 123

Universal versus existential quantification Existential types serve a specific purpose: will not show up in the exam! However, it s very healthy to know this stuff! A means for information hiding (encapsulation). Remember predicate logic. x.p(x) ( x. P(x)) Existential types can be encoded as universal types; see [TAPL]. 124

Overview Syntax of types: T ::= { X, T} Normal forms: v ::= { *T, v } Terms: t ::= { *T, t } as T let {X, x} = t in t Existentially quantified type Hidden type Package Package with type Unpacking 125

Constructing existentials A record: r : { a : nat, b : nat nat } r = { a = 1, b = λ x : nat. pred x } Illustration of using the record: r. b r.a A package with nat as hidden type: p : { X, {a : X, b : X nat}} p = { *nat, r } In fact, we need to assign this or another type. The type system makes sure that nat is inaccessible from outside. 126

Multiple types make sense for the package. Hence, the programmer must provide an annotation upon construction. r : { a : nat, b : nat nat } r = { a = 1, b = λ x : nat. pred x } p = {*nat, r } as { X, {a:x, b:x X}} p has type: { X, {a:x, b:x X}} preferred p = {*nat, r } as { X, {a:x, b:x nat}} p has type: { X, {a:x, b:x nat}} 127

We can have the same existential type with different representation types. p1 = {*nat, {a = 1, b = λx:nat. iszero x}} as { X, {a:x, b:x bool}} p2 = {*bool, {a = false, b = λx:bool. if x then false else true}} as { X, {a:x, b:x bool}} 128

Unpacking existentials (Opening package, importing module) Example -- apply b to a: let {X,x} = p2 in (x.b x.a) * true : bool Syntax: let {X,x} = t in t The value x of the existential becomes available. The representation type is not accessible (only X). 129

Typing rules T-PackExistential Γ t :[U/X ]T Γ { U, t} as { X, T } : { X, T } Substitution checks that the abstracted type of t can be instantiated with the hidden type to the actual type of t. T-UnpackExistential Γ t 1 : { X, T 12 } Γ, X, x : T 12 t 2 : T 2 Only expose abstract type of existential! Γ let {X, x} = t 1 in t 2 : T 2 130

Evaluation rules E-Unpack E-Pack t t { T, t} as U { T, t } as U t 1 t 1 let {X, x} = t 1 in t 2 let {X, x} = t 1 in t 2 E-UnpackPack let {X, x} =({ T, v} as U) in t 2 [T /X ][v/x]t 2 The hidden type is known to the evaluation, but the type system did not expose it; so t2 cannot exploit it. 131

Illustration of information hiding The type can be used in the scope of the unpacked package. let {X, x} = t in (λ y:x. x.b y) x.a * false : bool The representation type must remain abstract. t = {*nat, {a = 1, b = λx:nat. iszero x} as { X, {a:x, b:x bool}}} let {X,x} = t in pred x.a // Type error! The type must not leak into the resulting type: let {X, x} = t in x.a // Type error! 132

Summary: Lambdas with somewhat sexy types Done:,, <:,... Not done: μ,... Prepping: Types and Programming Languages Chapters 15, 16, 22, 23, 24 Outlook: Object calculi Process calculi More paradigms 133