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

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

Types and Programming Languages. Lecture 6. Normalization, references, and exceptions

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Lambda Calculi With Polymorphism

Programming Languages Lecture 14: Sum, Product, Recursive Types

Types and Programming Languages. Lecture 8. Recursive type

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

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

Lecture 9: Typed Lambda Calculus

CSE-321 Programming Languages 2012 Midterm

Lecture #23: Conversion and Type Inference

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

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CSCI-GA Scripting Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

Typed Racket: Racket with Static Types

Polymorphism. Lecture 19 CS 565 4/17/08

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

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

The Typed Racket Guide

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

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

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

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

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.

Lecture 14: Recursive Types

Part III. Chapter 15: Subtyping

Data Types The ML Type System

Harvard School of Engineering and Applied Sciences Computer Science 152

CIS 500 Software Foundations Midterm I

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

Homework 3 COSE212, Fall 2018

The Untyped Lambda Calculus

CSE 3302 Programming Languages Lecture 8: Functional Programming

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CSE-321 Programming Languages 2010 Midterm

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Type Systems Winter Semester 2006

Typed Scheme: Scheme with Static Types

CS558 Programming Languages

Exercise 1 (2+2+2 points)

Exercise 1 ( = 24 points)

CIS 500 Software Foundations Fall October 2

Subsumption. Principle of safe substitution

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

CS558 Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Programming in Omega Part 1. Tim Sheard Portland State University

Formal Systems and their Applications

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

Type Checking and Type Inference

Lists. Michael P. Fourman. February 2, 2010

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

Flang typechecker Due: February 27, 2015

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Design Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

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

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

Inductive Data Types

Lambda Calculi With Polymorphism

Part III Chapter 15: Subtyping

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

Simply-Typed Lambda Calculus

CSE-321 Programming Languages 2010 Final

Design Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term

Introduction to OCaml

Denotational Semantics. Domain Theory

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

CIS 500 Software Foundations Fall September 25

Types and Type Inference

Functional Languages and Higher-Order Functions

CSE 341: Programming Languages

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CSE-321 Programming Languages 2011 Final

Derivation for the Necessity of Recursive Types and its Basic Usage

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

Costly software bugs that could have been averted with type checking

4.5 Pure Linear Functional Programming

Subtyping. Lecture 13 CS 565 3/27/06

Programming Languages Fall 2014

Types and Type Inference

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

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Graph reduction. Simple graph reduction with visualisation

Inductive Definitions, continued

Exercise 1 ( = 18 points)

Type Systems, Type Inference, and Polymorphism

The type checker will complain that the two branches have different types, one is string and the other is int

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Second-Order Type Systems

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Dynamic Types , Spring March 21, 2017

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen

Typed Lambda Calculus and Exception Handling

An introduction introduction to functional functional programming programming using usin Haskell

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

Transcription:

Types and Programming Languages Lecture 5. Extensions of simple types Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016

Coming soon Simply typed λ-calculus has enough structure to make its theoretical properties interesting, but it is not yet much of a programming language.

Coming soon Simply typed λ-calculus has enough structure to make its theoretical properties interesting, but it is not yet much of a programming language. Close the gap with more familiar languages by introducing: Base types, Unit type, Pairs, Tuples, Records, Sum, etc.

Coming soon Simply typed λ-calculus has enough structure to make its theoretical properties interesting, but it is not yet much of a programming language. Close the gap with more familiar languages by introducing: Base types, Unit type, Pairs, Tuples, Records, Sum, etc. An important theme throughout the part is the concept of derived forms.

Outline Simple extensions Base types Unit type and sequencing Ascription let bindings Pairs and tuples Records More extensions Sums and variants General recursion

Base types Every programming language provides base types, such as numbers, booleans, or characters, plus appropriate primitive operations for manipulating these values.

Base types Every programming language provides base types, such as numbers, booleans, or characters, plus appropriate primitive operations for manipulating these values. For theoretical purposes, we abstract away from the details of particular base types and their operations. New types. T ::= A where A denotes some base type.

Base types Every programming language provides base types, such as numbers, booleans, or characters, plus appropriate primitive operations for manipulating these values. For theoretical purposes, we abstract away from the details of particular base types and their operations. New types. T ::= A where A denotes some base type.for example, λx : A.x : A A λf : A A.λx : A.f (f (x)) :

Base types Every programming language provides base types, such as numbers, booleans, or characters, plus appropriate primitive operations for manipulating these values. For theoretical purposes, we abstract away from the details of particular base types and their operations. New types. T ::= A where A denotes some base type.for example, λx : A.x : A A λf : A A.λx : A.f (f (x)) : (A A) A A

The Unit type New terms New values New types t ::= unit v ::= unit T ::= Unit New typing rules T-Unit Γ unit : Unit Unit type can be found in the ML family. The main application is in languages with side effects, such as assignments to reference cells. Similar to void in languages like C and Java.

Derived forms: Sequencing and Wildcards Two ways to add sequencing 1. add new syntax, evaluation and typing rules for sequencing: t ::= t 1 ; t 2 E-Seq t 1 t 1 t 1 ; t 2 t 1 ; t 2 E-SeqNext unit; t2 t 2 T-Seq Γ t 1 : Unit Γ t 2 : T t 1 ; t 2 : T 2. Define it as derived forms: t 1 ;t 2 def = (λx : Unit.t 2 ) t 1 where x FV (t 2 )

Derived forms: Sequencing and Wildcards Derived form has another name: syntactic sugar. The advantage is that we can extend the surface syntax without adding any complexity about theorems to be proved. Derived form has been heavily used in modern language definitions. Another derived form: wildcard λ.t def = λx.t where x FV (t).

Ascription Another simple feature is the ability to explicitly ascribe a particular type to a given term. New terms t ::= t as T New evaluation rules t 1 t 1 E-Ascribe E-Ascribe1 v1 as T v 1 t 1 as T t 1 as T Γ t New typing rules 1 : T T-Ascribe Γ t 1 as T : T

Purpose of ascription For documentation and maintenance For controlling the printing of complex types For abstract away some types, especially in PLs with type inference, such as SML. Note that we add new syntax, semantics rules to add ascription. How to consider ascription as derived forms? (See Homework.)

let bindings It is often useful both for avoiding repetition and for increasing readability to give names to some of its subexpressions.

let bindings It is often useful both for avoiding repetition and for increasing readability to give names to some of its subexpressions. let bindings are very common syntax in a lot of PLs, such as ML family, Scheme, but with slightly different scoping rules. New terms t ::= let x = t in t

let bindings It is often useful both for avoiding repetition and for increasing readability to give names to some of its subexpressions. let bindings are very common syntax in a lot of PLs, such as ML family, Scheme, but with slightly different scoping rules. New terms t ::= let x = t in t New evaluation rules E-LetV let x = v1 in t 2 [x v 1 ]t 2 E-Let t 1 t 1 let x = t 1 in t 2 let x = t 1 in t 2 New typing rules T-Let Γ t 1 : T 1 Γ, x : T 1 t 2 : T 2 Γ let x = t 1 in t 2 : T 2

let bindings, as derived forms

let bindings, as derived forms let x = t 1 in t 2 def = (λx : T 1.t 2 ) t 1

let bindings, as derived forms Where T 1 comes from? let x = t 1 in t 2 def = (λx : T 1.t 2 ) t 1

let bindings, as derived forms let x = t 1 in t 2 def = (λx : T 1.t 2 ) t 1 Where T 1 comes from? Type checker! The desugaring of sequencing is a transformation on terms. However, The desugaring of let binding is a transformation on typing derivations. We will NOT treat let bindings as a derived form.

Pairs The simplest compound data structure is pairs, or more generally tuples, of values. New terms t ::= {t, t} t.1 t.2 New values t ::= {v, v} New types t ::= T 1 T 2 product type

Pairs The simplest compound data structure is pairs, or more generally tuples, of values. New terms t ::= {t, t} t.1 t.2 New values t ::= {v, v} New types t ::= T 1 T 2 product type New evaluation rules

Pairs The simplest compound data structure is pairs, or more generally tuples, of values. New terms t ::= {t, t} t.1 t.2 New values t ::= {v, v} New types t ::= T 1 T 2 product type New evaluation rules E-PairBeta1 {v1, v 2 }.1 v 1 E-PairBeta2 {v1, v 2 }.2 v 2 E-Proj1 t 1 t 1 t 1.1 t 1.1 E-Proj2 t 1 t 1 t 1.2 t 1.2 E-Pair1 t 1 t 1 {t 1, t 2 } {t 1, t 2} New typing rules E-Pair1 t 2 t 2 {v 1, t 2 } {v 1, t 2 }

Pairs The simplest compound data structure is pairs, or more generally tuples, of values. New terms t ::= {t, t} t.1 t.2 New values t ::= {v, v} New types t ::= T 1 T 2 product type New evaluation rules E-PairBeta1 {v1, v 2 }.1 v 1 E-PairBeta2 {v1, v 2 }.2 v 2 E-Proj1 t 1 t 1 t 1.1 t 1.1 E-Proj2 t 1 t 1 t 1.2 t 1.2 E-Pair1 t 1 t 1 {t 1, t 2 } {t 1, t 2} E-Pair1 t 2 t 2 {v 1, t 2 } {v 1, t 2 } New typing rules T-Pair Γ t 1 : T 1 Γ t 2 : T 2 Γ {t 1, t 2 } : T 1 T 2 T-Proj1 Γ t 1 : T 1 T 2 Γ t 1.1 : T 1 T-Proj1 Γ t 1 : T 1 T 2 Γ t 1.2 : T 2

Tuples It s easy to generalize pairs to tuples. Pair is a 2-tuple. New terms t ::= {ti i 1..n } t.i New values t ::= vi i 1..n New types t ::= {T i 1..n i } product type New evaluation rules t 1 t 1 E-TupleBeta E-Proj {v i 1..n i }.j v j t 1.j t 1.j t j t j E-Tuple {v i 1..j 1 i, t j, t k j+1..n k } {v i 1..j 1 i, t j, tk j+1..n k } New typing rules i Γ t i : T i T-Tuple Γ {ti i 1..n } : {T i 1..n i } T-Proj Γ t 1 : {T i 1..n i } Γ t 1.j : T j

Records 11.8 Records 129 {} Extendsλ (9-1) New syntactic forms t ::=... terms: i 1..n {l i=t i } record t.l projection v ::=... values: i 1..n {l i=v i } record value T ::=... types: i 1..n {l i:t i } type of records t 1 t 1 t 1.l t 1.l t j t j {l i=v i i 1..j 1,l j=t j,l k=t k k j+1..n } {l i=v i i 1..j 1,l j=t j,lk=tkk j+1..n } New typing rules for eachi Γ t i :T i Γ {l i=t i i 1..n }:{l i:t i i 1..n } (E-Proj) (E-Rcd) Γ t:t (T-Rcd) New evaluation rules t t {l i=v i i 1..n }.l j v j (E-ProjRcd) Γ t 1 :{l i:t i i 1..n } Γ t 1.l j :T j (T-Proj) Figure 11-7: Records have a tuple in which all the fields to the left of field j have already been

Records Surface difference: Fields in records has names, pairs does not. We can treat pairs as special records labeled 1, 2, 3,...

Records Surface difference: Fields in records has names, pairs does not. We can treat pairs as special records labeled 1, 2, 3,... Deeper difference: Compilers may implement them in distinct ways. Pair has strict orders. However, PLs treat the order of fields in records differently, e.g.

Records Surface difference: Fields in records has names, pairs does not. We can treat pairs as special records labeled 1, 2, 3,... Deeper difference: Compilers may implement them in distinct ways. Pair has strict orders. However, PLs treat the order of fields in records differently, e.g. in SML, order does not matter: {l = 2, m = 4} = {m = 4, l = 2}; in rules of Figure 11-7, orders matter, i.e., {l = 2, m = 4} {m = 4, l = 2}

Records Surface difference: Fields in records has names, pairs does not. We can treat pairs as special records labeled 1, 2, 3,... Deeper difference: Compilers may implement them in distinct ways. Pair has strict orders. However, PLs treat the order of fields in records differently, e.g. in SML, order does not matter: {l = 2, m = 4} = {m = 4, l = 2}; in rules of Figure 11-7, orders matter, i.e., {l = 2, m = 4} {m = 4, l = 2} we will latter use subtyping to make records with different field permutations equivalent.

Records Surface difference: Fields in records has names, pairs does not. We can treat pairs as special records labeled 1, 2, 3,... Deeper difference: Compilers may implement them in distinct ways. Pair has strict orders. However, PLs treat the order of fields in records differently, e.g. in SML, order does not matter: {l = 2, m = 4} = {m = 4, l = 2}; in rules of Figure 11-7, orders matter, i.e., {l = 2, m = 4} {m = 4, l = 2} we will latter use subtyping to make records with different field permutations equivalent. Rules for records are similar to those for tuples. Please refer to Figure 11-7 in the textbook.

Outline Simple extensions Base types Unit type and sequencing Ascription let bindings Pairs and tuples Records More extensions Sums and variants General recursion

Variant types We need heterogeneous collections of values in many cases: a node in a tree can be a leaf or an interior node with children; a list cell can be either nil or a cons cell carrying a head and a tail, a node of an abstract syntax tree in a compiler can represent a variable, an abstraction, an application, etc

Variant types We need heterogeneous collections of values in many cases: a node in a tree can be a leaf or an interior node with children; a list cell can be either nil or a cons cell carrying a head and a tail, a node of an abstract syntax tree in a compiler can represent a variable, an abstraction, an application, etc Type-theoretic mechanism that supports this kind of programming is variant types.

Variant types We need heterogeneous collections of values in many cases: a node in a tree can be a leaf or an interior node with children; a list cell can be either nil or a cons cell carrying a head and a tail, a node of an abstract syntax tree in a compiler can represent a variable, an abstraction, an application, etc Type-theoretic mechanism that supports this kind of programming is variant types. A more familiar name for variant type is union, or more precisely, disjoint union. Sum is the binary version of variant type.

Sums Constructors and accessors t ::= inl t inr t case t of inl x t inr x t v ::= inl v inr v T ::= T + T tagging (left) tagging (right) case tagged value (left) tagged value (right) sum type

Sums, example Your score may be an integer number, or a grade (P/F). Types : Score = Int + Char Constructor : t 1 = inl 59 : Score, t 2 = inr F : Score Accessor : a good teacher = λt.case t of inl x inl (max(x, 60)) inr x inr P

Sums, example Your score may be an integer number, or a grade (P/F). Types : Score = Int + Char Constructor : t 1 = inl 59 : Score, t 2 = inr F : Score Accessor : a good teacher = λt.case t of inl x inl (max(x, 60)) inr x inr P Quiz. 1. Give the evaluation rule and typing rule for sum. 2. Does the uniqueness of types still hold for languages with sum? Why?

Sums, semantics E-CaseInl case (inl v 0 ) of inl x 1 t 1 inr x 2 t 2 [x 1 v 0 ]t 1 E-Case t 0 t 0 case t 0 of inl x 1 t 1 inr x 2 t 2 case t 0 of inl x 1 t 1 inr x 2 t 2 E-Inl t 1 t 1 inl t 1 inl t 1 T-Inl Γ t 1 : T 1 Γ inl t 1 : T 1 + T 2 Γ t 0 : T 1 + T 2 Γ, x 1 : T 1 t 1 : T Γ, x 2 : T 2 t 2 : T T-Case case t 0 of inl x 1 t 1 inr x 2 t 2 : T Symmetric rules for inr are omitted.

Sums and uniqueness of types This rule breaks the uniqueness of types: T-Inl Γ t 1 : T 1 Γ inl t 1 : T 1 + T 2 Solutions include: Keep it as a variable which will be instantiated later. Mainly used in PLs with type inference.

Sums and uniqueness of types This rule breaks the uniqueness of types: T-Inl Γ t 1 : T 1 Γ inl t 1 : T 1 + T 2 Solutions include: Keep it as a variable which will be instantiated later. Mainly used in PLs with type inference. Allow any T 2. We will explore this option when discussing subtyping.

Sums and uniqueness of types This rule breaks the uniqueness of types: T-Inl Γ t 1 : T 1 Γ inl t 1 : T 1 + T 2 Solutions include: Keep it as a variable which will be instantiated later. Mainly used in PLs with type inference. Allow any T 2. We will explore this option when discussing subtyping. Ascription: use explicit annotation to tell the compiler or type checker which type T 2 is intended. inl t as T T-Inl Γ t 1 : T 1 Γ inl t 1 as T 1 + T 2 : T 1 + T 2

Variants Like the relation between pair and records. Variants are extensions of sum type with fields. t 1 =< none = unit > as < none : Unit, some : Nat > t 2 =< some = 20 > as < none : Unit, some : Nat > f = λx : < none : Unit, some : Nat >. case x of < none = u > 999 < some = v > v In ML family, this type is called option.

Enumerations and Single-Field variants We can construct enumerations by using variants, each field has type Unit. Weekday =< monday : Unit, tuesday : Unit,, friday : Unit > The access of this enumeration is very annoying. We will have alternatives later. The single-field variants looks silly, but is useful to abstract/hide information. DollarAmount =< dollars : Float > EuroAmount =< euros : Float >

Discussion: variants v.s. Datatypes Variant type is analogous to the ML datatype type T = l 1 of T 1 l n of T n But there are several differences worth noticing For ML datatype, we do not use l i (t i ) as T to explicitly tell the compiler T, instead the constructor l i has type T i T. Enumeration is much easier with datatype, we omit of Unit: type Weekday = monday friday ML datatype has several additional important features: Recursive datatype: type NatList = nil cons of Nat * NatList Parametric datatype: type a List = nil cons of a * a List List is called a type operator.

General recursion Another facility found in most programming languages is the ability to define recursive functions. Here is one way to define a function iseven: ff = λ ie:nat Bool.λ x:nat. if iszero x then true else if iszero (pred x) then false else ie (pred (pred x)); iseven = fix ff; Quiz. What s the type of ff?

General recursion fix itself cannot be defined in the simply typed lambda-calculus. We simply add it as primitives. New terms t ::= fix t New evaluation rules

General recursion fix itself cannot be defined in the simply typed lambda-calculus. We simply add it as primitives. New terms t ::= fix t New evaluation rules E-FixBeta fix (λx : T1.t 2 ) [x fix (λx : T 1.t 2 )]t 2 t 1 t 1 E-Fix fix t 1 fix t 1 New typing rules

General recursion fix itself cannot be defined in the simply typed lambda-calculus. We simply add it as primitives. New terms t ::= fix t New evaluation rules E-FixBeta fix (λx : T1.t 2 ) [x fix (λx : T 1.t 2 )]t 2 t 1 t 1 E-Fix fix t 1 fix t 1 New typing rules T-Fix Γ t 1 : T 1 T 1 Γ fix t 1 : T 1

General recursion fix itself cannot be defined in the simply typed lambda-calculus. We simply add it as primitives. New terms t ::= fix t New evaluation rules E-FixBeta fix (λx : T1.t 2 ) [x fix (λx : T 1.t 2 )]t 2 t 1 t 1 E-Fix fix t 1 fix t 1 New typing rules T-Fix Γ t 1 : T 1 T 1 Γ fix t 1 : T 1 New derived forms letrec x : T 1 = t 1 in t 2 def = let x = fix (λx : T 1.t 1 ) in t 2

More on fix Notice that the type T 1 in rule T-Fix is not restricted to function types. fix implies that every type is inhabited by some term. diverge T = λ : Unit.fix (λx : T.x); diverge T (unit) has type T, and has non-terminating evaluation. The simply typed lambda-calculus with numbers and fix, called PCF (Programming Computable Functions), is the simplest language with a range of subtle semantic phenomena.

List Typing features can be classified into base types such as Bool and Unit type constructors such as and List is also a type constructor: For every T, List T returns a type describing finite length lists whose elements typed T. Please refer to Figure 11-13 for syntax and semantics of List.

Conclusion Real programming languages usually include: Base types, Unit type, Pairs, Tuples, Records, Sum, etc. fix can not be typed in simply typed lambda calculus. Most languages do not have explicit fix, but allow recursive definitions of functions. An important theme throughout the part is the concept of derived forms.

Homework 11.4.1, 11.5.2, 11.8.2, 11.11.1, 11.11.2