
Advanced Types A4M36TPJ, 2015/2016 What is a type? • What is a data type? • 1 is the integer • true is the boolean • Data type defines a set 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. Type Constructor • 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 product type. 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! Programming Language 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 ` 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 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 ... Γ AΓn 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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages29 Page
-
File Size-