Advanced Types A4M36TPJ, 2015/2016 What is a type?

• What is a ?

• 1 is the integer

• true is the boolean

• Data type defines a of possible values

• What is a type of an expression?

• x + 5 : int, if x : int and + : int x int -> int Sets vs. Types

• Q: What is the difference between the set Bool = {true, false} and the Boolean type?

• A: Boolean can be a type of an expression, e.g. true and false. This expression is not a member of the set Bool. On the other hand true is a Boolean and false as well. Hence, the set of Boolean expressions is different from the set of Bool.

• We can construct user-defined types in many languages (Java, C++, …).

• To construct new types from existing types we use type constructors.

• For example Int x Boolean is a type constructor of a new . In Java, class is a constructor of a new class type. First-order Type Systems

• Lack of type parametrization (Java generics)

• Lack type abstraction (Interfaces and abstract classes in Java)

• Contains higher-order function types! Theory, week 6

1Programming Lecture Language Theory, week 6 (“ is a well-formedBasic environment”) Setup •1`⇧ Lecture A (“A is a well-formed type in environment ”) • ` (“ is a well-formed environment”) • `⇧ e :AA(“(“Aeisis a well-formed a well-formed type term in environment of type A”)in environment ”) • •` ` Basic setupe : A (“e is a well-formed term of type A in environment ”) • • ` Basic setup • (1) ;`⇧ (1) ;`⇧ Aa dom() Aa` dom62 () (2) ` 62(a, A) (2) [(a,{ A) } `⇧ [ { } `⇧ `⇧`⇧ (3) (3) x : x(x: )(x) ` ` : A Basic ⇧: 2A Basic (4) ⇧ A 2 (4) ` A ` Function• type • A B ` ` (5) AA B B `` ! ` (5) (x, A) A e : BB [ { `} ` ! (6) (x : A.e):A B ` (x, A) ! e : B (6) e : [A { B } `p : A ` (!x : A.e):` A B (7) ` e(p):B ! ` e : A B p : A Product type ` ! ` (7) • e(p):B A ` A ` 1 ` 2 (8) Product type A A • ` 1 ⇥ 2

e1 : A1 e2 : A2 ` A1 ` A2 (9) (e ,e` ):A `A (8) ` 1 2 A 1 ⇥ A2 ` 1 ⇥ 2 e : A A ` 1 ⇥ 2 (10) efirst1 : A1 e :A e2 : A2 `` 1` (9) (e1,e2):A1 A2 ` e : A1 A2 ⇥ ` ⇥ (11) second e : A ` e : A 2 A ` 1 ⇥ 2 (10) first e : A ` 1 e : A A ` 1 ⇥ 2 (11) second e : A ` 2 1

1 Programming Language Theory, week 6

1 Lecture

(“ is a well-formed environment”) • `⇧ A (“A is a well-formed type in environment ”) • ` e : A (“e is a well-formed term of type A in environment ”) • ` Basic setup •

(1) ;`⇧ Aa dom() ` 62 (2) (a, A) [ { } `⇧ `⇧ (3) x : (x) ` : A Basic ⇧ 2 (4) A Function` Type Function type • A B ` ` (5) A B ` ! (x, A) e : B [ { } ` (6) (x : A.e):A B ` ! e : A B p : A ` ! ` (7) e(p):B ` Product type • A A ` 1 ` 2 (8) A A ` 1 ⇥ 2 e : A e : A ` 1 1 ` 2 2 (9) (e ,e ):A A ` 1 2 1 ⇥ 2 e : A A ` 1 ⇥ 2 (10) first e : A ` 1 e : A A ` 1 ⇥ 2 (11) second e : A ` 2

1 Programming Language Theory, week 6

1 Lecture

(“ is a well-formed environment”) • `⇧ A (“A is a well-formed type in environment ”) • ` e : A (“e is a well-formed term of type A in environment ”) • ` Basic setup •

(1) ;`⇧ Aa dom() ` 62 (2) (a, A) [ { } `⇧ `⇧ (3) x : (x) ` : A Basic ⇧ 2 (4) A ` Function type • A B ` ` (5) A B ` ! (x, A) e : B [ { } ` (6) (x : A.e):A B ` ! e : A B p : A ` ! ` (7) e(p):B Product` Type Product type • A A ` 1 ` 2 (8) A A ` 1 ⇥ 2 e : A e : A ` 1 1 ` 2 2 (9) (e ,e ):A A ` 1 2 1 ⇥ 2 e : A A ` 1 ⇥ 2 (10) first e : A ` 1 e : A A ` 1 ⇥ 2 (11) second e : A ` 2

1 Programming Language Theory, week 6 Tagged Union type • A A ` 1 ` 2 (12) A + A ` 1 2 e : A A ` 1 ` 2 (13) inLeft e : A + A ` A2 1 2 A e : A ` 1 ` 2 (14) inRight e : A1 + A2 ` A1 e : A + A ` 1 2 (15) isLeft e : Boolean ` e : A + A ` 1 2 (16) isRight e : Boolean ` e : A + A ` 1 2 (17) asLeft e : A ` 1 e : A + A ` 1 2 (18) asRight e : A ` 2 Record type • A ... A ` 1 ` n (19) Record(l : A ,...,l : A ) ` 1 1 n n e : A ... e : A ` 1 1 ` n n (20) record(l =e ,...,l =e ):Record(l : A ,...,l : A ) ` 1 1 n n 1 1 n n e : Record(l : A ,...,l : A ,...,l : A ) ` 1 1 j j n n (21) e.lj : Aj Reference type • A ` (22) Ref A ` e : A ` (23) ref e : Ref A ` e : Ref A ` (24) deref e : A `

e : Ref A e0 : A ` ` (25) e = e : ` 0 ⇧ 2 Seminar

Define an API that allows you to represent the above-defined types and their operations.

2 Programming Language Theory, week 6

Union type • A A ` 1 ` 2 (12) Programming Language Theory, week 6 A + A ` 1 2 e : A A ` 1 ` 2 (13) Union type inLeft e : A + A • ` A2 1 2 A A A1 e : A2 ` 1 ` 2 ` (12)` (14) A + A inRightA e : A1 + A2 Tagged` 1 2 Union` Type1 e : A A e : A1 + A2 ` 1 ` 2 ` (13) (15) inLeft e : A + A isLeft e : Boolean ` A2 1 2 ` A e : A e : A1 + A2 ` 1 ` 2 ` (14) (16) inRight e : A1 + A2 isRight e : Boolean ` A1 ` e : A + A e : A1 + A2 ` 1 2 ` (15) (17) isLeft e : Boolean asLeft e : A1 ` ` e : A + A e : A1 + A2 ` 1 2 ` (16) (18) isRight e : Boolean asRight e : A2 ` ` Recorde : A type+ A • ` 1 2 (17) asLeft e : A ` 1 A ... A ` 1 ` n (19) e : A + A Record(l1 : A1,...,ln : An) ` 1 2 ` (18) asRight e : A ` 2 e : A ... e : A ` 1 1 ` n n (20) Record type record(l =e ,...,l =e ):Record(l : A ,...,l : A ) • ` 1 1 n n 1 1 n n A1 ... An e : Record(l : A ,...,l : A ,...,l : A ) ` ` ` 1 1 (19)j j n n (21) Record(l1 : A1,...,ln : An) e.l : A ` j j e : A Reference... typee : A ` 1 •1 ` n n (20) record(l1=e1,...,ln=en):Record(l1 : A1,...,ln : An) ` A ` (22) e : Record(l1 : A1,...,lj : Aj,...,ln : An) Ref A ` ` (21) e.lj : Aj e : A ` (23) Reference type ref e : Ref A • ` A e : Ref A ` ` (22) (24) Ref A deref e : A ` `

e : A e : Ref A e0 : A ` ` (23)` (25) ref e : Ref A e = e : ` ` 0 ⇧ e : Ref A ` (24) 2 Seminarderef e : A ` Define an API that allows you to represent the above-defined types and their e : Ref A e0 : A operations.` ` (25) e = e : ` 0 ⇧ 2 Seminar 2

Define an API that allows you to represent the above-defined types and their operations.

2 Programming Language Theory, week 6

Union type • A A ` 1 ` 2 (12) A + A ` 1 2 e : A A ` 1 ` 2 (13) inLeft e : A + A ` A2 1 2 A e : A ` 1 ` 2 (14) inRight e : A1 + A2 ` A1 e : A + A ` 1 2 (15) isLeft e : Boolean ` e : A + A ` 1 2 (16) isRight e : Boolean ` e : A + A ` 1 2 (17) asLeft e : A ` 1 e : A + A Record` 1 Type2 (18) asRight e : A ` 2 Record type • A ... A ` 1 ` n (19) Record(l : A ,...,l : A ) ` 1 1 n n e : A ... e : A ` 1 1 ` n n (20) record(l =e ,...,l =e ):Record(l : A ,...,l : A ) ` 1 1 n n 1 1 n n e : Record(l : A ,...,l : A ,...,l : A ) ` 1 1 j j n n (21) e.lj : Aj Reference type • A ` (22) Ref A ` e : A ` (23) ref e : Ref A ` e : Ref A ` (24) deref e : A `

e : Ref A e0 : A ` ` (25) e = e : ` 0 ⇧ 2 Seminar

Define an API that allows you to represent the above-defined types and their operations.

2 Programming Language Theory, week 6

Union type • A A ` 1 ` 2 (12) A + A ` 1 2 e : A A ` 1 ` 2 (13) inLeft e : A + A ` A2 1 2 A e : A ` 1 ` 2 (14) inRight e : A1 + A2 ` A1 e : A + A ` 1 2 (15) isLeft e : Boolean ` e : A + A ` 1 2 (16) isRight e : Boolean ` e : A + A ` 1 2 (17) asLeft e : A ` 1 e : A + A ` 1 2 (18) asRight e : A ` 2 Record type • A ... A ` 1 ` n (19) Record(l : A ,...,l : A ) ` 1 1 n n e : A ... e : A ` 1 1 ` n n (20) record(l =e ,...,l =e ):Record(l : A ,...,l : A ) ` 1 1 n n 1 1 n n e : Record(l : A ,...,l : A ,...,l : A ) ` 1 1 j j n n (21) e.lj : Aj Reference type Reference Type • A ` (22) Ref A ` e : A ` (23) ref e : Ref A ` e : Ref A ` (24) deref e : A `

e : Ref A e0 : A ` ` (25) e = e : ` 0 ⇧ 2 Seminar

Define an API that allows you to represent the above-defined types and their operations.

2 Programming Language Theory, week 7

1 Lecture 1.1 Foundations

Disjoint union i=1,...,n Ai of sets A1,...,An is defined as

F A = (x, i): x A . (1) i { 2 i} i=1,...,n i=1,...,n G Type[ Variables 1.2 Types Type variables • X dom() `⇧ 62 (2) X [ { } `⇧ Recursive type • X A [ { } ` (3) µX.A ` e : µX.A ` (4) unfold e : A[X µX.A] ` 7! e : A[X µX.A] ` 7! (5) fold e : µX.A ` Universal type • X A [ { } ` (6) X.A `8 X e : A [ { } ` (7) X.e : X.A ` 8 e : X.A B ` 8 ` (8) e(B):A[X B] ` 7! 1.3 Subtype Polymorphism Basic setup • We define a new binary relation <: on types and a new judgement: A<: B (“A is a subtype of B in environment ”). `

(9) A<: A ` A<: B B<: C ` ` (10) A<: C `

1 Programming Language Theory, week 7

1 Lecture 1.1 Foundations

Disjoint union i=1,...,n Ai of sets A1,...,An is defined as

F A = (x, i): x A . (1) i { 2 i} i=1,...,n i=1,...,n G [ 1.2 Types Type variables • X dom() Recursive`⇧ 62 Type (2) X [ { } `⇧ Recursive type • X A [ { } ` (3) µX.A ` e : µX.A ` (4) unfold e : A[X µX.A] ` 7! e : A[X µX.A] ` 7! (5) fold e : µX.A ` Universal type • X A [ { } ` (6) X.A `8 X e : A [ { } ` (7) X.e : X.A ` 8 e : X.A B ` 8 ` (8) e(B):A[X B] ` 7! 1.3 Subtype Polymorphism Basic setup • We define a new binary relation <: on types and a new judgement: A<: B (“A is a subtype of B in environment ”). `

(9) A<: A ` A<: B B<: C ` ` (10) A<: C `

1 Example Language F1 Syntax Example Language F1 F1 - Basic Types F1 - Basic Types Programming Language Theory, week 7

1 Lecture 1.1 Foundations

Disjoint union i=1,...,n Ai of sets A1,...,An is defined as

F A = (x, i): x A . (1) i { 2 i} i=1,...,n i=1,...,n G [ 1.2 Types Type variables • X dom() `⇧ 62 (2) X [ { } `⇧ Recursive type • X A [ { } ` (3) µX.A ` e : µX.A ` (4) unfold e : A[X µX.A] ` 7! e : A[X µX.A] ` 7! (5) Universal fold e : µX.A Type ` Universal type • X A [ { } ` (6) X.A `8 X e : A [ { } ` (7) X.e : X.A ` 8 e : X.A B ` 8 ` (8) e(B):A[X B] ` 7! 1.3 Subtype Polymorphism Basic setup • We define a new binary relation <: on types and a new judgement: A<: B (“A is a subtype of B in environment ”). `

(9) A<: A ` A<: B B<: C ` ` (10) A<: C `

1 Programming Language Theory, week 7

1 Lecture 1.1 Foundations

Disjoint union i=1,...,n Ai of sets A1,...,An is defined as

F A = (x, i): x A . (1) i { 2 i} i=1,...,n i=1,...,n G [ 1.2 Types Type variables • X dom() `⇧ 62 (2) X [ { } `⇧ Recursive type • X A [ { } ` (3) µX.A ` e : µX.A ` (4) unfold e : A[X µX.A] ` 7! e : A[X µX.A] ` 7! (5) fold e : µX.A ` Universal type • X A [ { } ` (6) X.A `8 X e : A [ { } ` (7) X.e : X.A ` 8 e : X.A B ` 8 ` (8) e(B):A[X B] Subtype` Polymorphism7! 1.3 Subtype Polymorphism

Basic setup• • We define a new binary relation <: on types and a We define anew new judgement: binary relation Γ ⊢ A< :<: on B types(“A is anda subtype a new of judgement: B in A<: B (“A environmentis a subtype Γ of”)B in environment ”). `

(9) A<: A ` A<: B B<: C ` ` (10) A<: C `

1 Programming Language Theory,Subsumption week 7

Subsumption • e : A A<: B ` ` (11) e : B ` Top type • `⇧ (12) Top ` A ` (13) A<: Top ` Subtyping of functions •

A0 <: A B<: B0 ` ` (14) A B<: A B ` ! 0 ! 0 Subtyping of products •

A0 <: A B0 <: B ` ` (15) A B <: A B ` 0 ⇥ 0 ⇥ Subtyping of unions •

A0 <: A B0 <: B ` ` (16) A + B <: A + B ` 0 0 Subtyping of records •

A0 <: A ... A0 <: A A0 ... A0 ` 1 1 ` n n ` n+1 ` n+m Record(l1 : A10 ,...,ln+m : An0 +m) <: Record(l1 : A1,...,ln : An) ` (17) Bounded type variables • AX dom() ` 62 (18) X<: A [ { } `⇧ X<: A [ { } `⇧ (19) X<: A X [ { } ` X<: A [ { } `⇧ (20) X<: A X<: A [ { } ` Subtyping of recursive types • X<: Top A [ { } ` (21) µX.A ` µX.A µY.B Y<: Top,X <: Y A<: B ` ` [ { } ` (22) µX.A <: µY.B `

2 Programming Language Theory, week 7

Subsumption • Tope : A Type A<: B ` ` (11) e : B ` Top type • `⇧ (12) Top ` A ` (13) A<: Top ` Subtyping of functions •

A0 <: A B<: B0 ` ` (14) A B<: A B ` ! 0 ! 0 Subtyping of products •

A0 <: A B0 <: B ` ` (15) A B <: A B ` 0 ⇥ 0 ⇥ Subtyping of unions •

A0 <: A B0 <: B ` ` (16) A + B <: A + B ` 0 0 Subtyping of records •

A0 <: A ... A0 <: A A0 ... A0 ` 1 1 ` n n ` n+1 ` n+m Record(l1 : A10 ,...,ln+m : An0 +m) <: Record(l1 : A1,...,ln : An) ` (17) Bounded type variables • AX dom() ` 62 (18) X<: A [ { } `⇧ X<: A [ { } `⇧ (19) X<: A X [ { } ` X<: A [ { } `⇧ (20) X<: A X<: A [ { } ` Subtyping of recursive types • X<: Top A [ { } ` (21) µX.A ` µX.A µY.B Y<: Top,X <: Y A<: B ` ` [ { } ` (22) µX.A <: µY.B `

2 Programming Language Theory, week 7 Programming Language Theory, week 7 ProgrammingProgramming Language Language Theory, Theory, week week 7 7

SubsumptionSubsumption • • SubsumptionSubsumption • • e e: A: A A

2 Programming Language Theory, week 7

Subsumption • e : A A<: B ` ` (11) e : B ` Top type • `⇧ (12) Top ` A ` (13) A<: Top ` Subtyping of functions •

A0 <: A B<: B0 ` ` (14) A B<: A B ` ! 0 ! 0 Subtyping of products •

A0 <: A B0 <: B ` ` (15) A B <: A B ` 0 ⇥ 0 ⇥ Subtyping of unions •

A0 <: A B0 <: B ` ` (16) A + B <: A + B ` 0 0 Subtyping of records •

A0 <: A ... A0 <: A A0 ... A0 ` 1 1 ` n n ` n+1 ` n+m Record(l1 : A10 ,...,ln+m : An0 +m) <: Record(l1 : A1,...,ln : An) ` Bounded Type Variables (17) Bounded type variables • AX dom() ` 62 (18) X<: A [ { } `⇧ X<: A [ { } `⇧ (19) X<: A X [ { } ` X<: A [ { } `⇧ (20) X<: A X<: A [ { } ` Subtyping of recursive types • X<: Top A [ { } ` (21) µX.A ` µX.A µY.B Y<: Top,X <: Y A<: B ` ` [ { } ` (22) µX.A <: µY.B `

2 Programming Language Theory, week 7

Subsumption • e : A A<: B ` ` (11) e : B ` Top type • `⇧ (12) Top ` A ` (13) A<: Top ` Subtyping of functions •

A0 <: A B<: B0 ` ` (14) A B<: A B ` ! 0 ! 0 Subtyping of products •

A0 <: A B0 <: B ` ` (15) A B <: A B ` 0 ⇥ 0 ⇥ Subtyping of unions •

A0 <: A B0 <: B ` ` (16) A + B <: A + B ` 0 0 Subtyping of records •

A0 <: A ... A0 <: A A0 ... A0 ` 1 1 ` n n ` n+1 ` n+m Record(l1 : A10 ,...,ln+m : An0 +m) <: Record(l1 : A1,...,ln : An) ` (17) Bounded type variables • AX dom() ` 62 (18) X<: A [ { } `⇧ X<: A [ { } `⇧ (19) Subtyping ofX< :RecursiveA X [ { } ` Types X<: A [ { } `⇧ (20) X<: A X<: A [ { } ` Subtyping of recursive types • X<: Top A [ { } ` (21) µX.A ` µX.A µY.B Y<: Top,X <: Y A<: B ` ` [ { } ` (22) µX.A <: µY.B `

2 ProgrammingSubtyping Language Theory, week 7 of Universal Types Subtyping of universal types • X<: A B [ { } ` (23) X<: A.B `8

A0 <: A X<: A0 B<: B0 ` [ { } ` (24) ( X<: A.B) <:( X<: A .B ) ` 8 8 0 0 X<: A e : B [ { } ` (25) X<: A.e : X<: A.B ` 8

e : X<: A.B A0 <: A ` 8 ` (26) e(A ):B[X A ] ` 0 7! 0 2 Seminar

1. Does A<: Top,B <: Top A B<: A B hold? Why? { } ` ! ! 2. Does A<: B,B <: C, C <: Top A + C<: A + B hold? Why? { } ` 3. Does A<: B,B <: Top Ref A<: Ref B hold? Why? { } ` 4. Does X<: Y,Y <: Top,A <: Top µX.(X Top) <: µY.(Y A) hold? Why?{ } ` ⇥ ⇥ 5. Does X<: Y,Y <: Top µX.(X X) <: µY.(Y Y ) hold? Why? { } ` ! ! 6. Does A<: B,B <: Top X.(X A) <: Y.(Y B) hold? Why? { } `8 ⇥ 8 ⇥ 7. Does A<: B,B <: Top X.(X Ref A) <: X.(X Ref B) hold? Why?{ } `8 ⇥ 8 ⇥

3Homework

Your task is to implement the missing methods in the library of types.

3 Example Language F2<: Syntax