Types and Programming Languages. Lecture 5. Extensions of simple types
|
|
- Patience Johnston
- 5 years ago
- Views:
Transcription
1 Types and Programming Languages Lecture 5. Extensions of simple types Xiaojuan Cai BASICS Lab, Shanghai Jiao Tong University Fall, 2016
2 Coming soon Simply typed λ-calculus has enough structure to make its theoretical properties interesting, but it is not yet much of a programming language.
3 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.
4 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.
5 Outline Simple extensions Base types Unit type and sequencing Ascription let bindings Pairs and tuples Records More extensions Sums and variants General recursion
6 Base types Every programming language provides base types, such as numbers, booleans, or characters, plus appropriate primitive operations for manipulating these values.
7 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.
8 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)) :
9 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
10 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.
11 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 )
12 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).
13 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
14 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.)
15 let bindings It is often useful both for avoiding repetition and for increasing readability to give names to some of its subexpressions.
16 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
17 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
18 let bindings, as derived forms
19 let bindings, as derived forms let x = t 1 in t 2 def = (λx : T 1.t 2 ) t 1
20 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
21 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.
22 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
23 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
24 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 }
25 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
26 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
27 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
28 Records Surface difference: Fields in records has names, pairs does not. We can treat pairs as special records labeled 1, 2, 3,...
29 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.
30 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}
31 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.
32 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.
33 Outline Simple extensions Base types Unit type and sequencing Ascription let bindings Pairs and tuples Records More extensions Sums and variants General recursion
34 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
35 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.
36 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.
37 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
38 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
39 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?
40 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.
41 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.
42 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.
43 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
44 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.
45 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 >
46 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.
47 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?
48 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
49 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
50 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
51 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
52 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.
53 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 for syntax and semantics of List.
54 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.
55 Homework , , , ,
CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011
CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e
More informationTypes and Programming Languages. Lecture 6. Normalization, references, and exceptions
Types and Programming Languages Lecture 6. Normalization, references, and exceptions Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon One more language features:
More informationHaskell 98 in short! CPSC 449 Principles of Programming Languages
Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has
More informationLambda Calculi With Polymorphism
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
More informationProgramming Languages Lecture 14: Sum, Product, Recursive Types
CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate
More informationTypes and Programming Languages. Lecture 8. Recursive type
Types and Programming Languages Lecture 8. Recursive type Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 List[T] List[T] is a type constructor whose elements are lists
More informationPolymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy
06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic
More informationReview. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals
Review CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics e ::= λx. e x ee c v ::= λx. e c (λx. e) v e[v/x] e 1 e 2 e 1 e 2 τ ::= int τ τ Γ ::= Γ,x : τ e 2 e 2 ve 2 ve 2 e[e /x]:
More informationLecture 9: Typed Lambda Calculus
Advanced Topics in Programming Languages Spring Semester, 2012 Lecture 9: Typed Lambda Calculus May 8, 2012 Lecturer: Mooly Sagiv Scribe: Guy Golan Gueta and Shachar Itzhaky 1 Defining a Type System for
More informationCSE-321 Programming Languages 2012 Midterm
Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score
More informationLecture #23: Conversion and Type Inference
Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:
More informationConversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";
Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";
More informationCS 4110 Programming Languages & Logics. Lecture 28 Recursive Types
CS 4110 Programming Languages & Logics Lecture 28 Recursive Types 7 November 2014 Announcements 2 Foster office hours 11-12pm Guest lecture by Fran on Monday Recursive Types 3 Many languages support recursive
More informationCSCI-GA Scripting Languages
CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation
More informationProgramming Languages Lecture 15: Recursive Types & Subtyping
CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)
More informationTyped Racket: Racket with Static Types
Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type
More informationPolymorphism. Lecture 19 CS 565 4/17/08
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
More informationn n Try tutorial on front page to get started! n spring13/ n Stack Overflow!
Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!
More informationCS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus
CS 4110 Programming Languages & Logics Lecture 17 Programming in the λ-calculus 10 October 2014 Announcements 2 Foster Office Hours 11-12 Enjoy fall break! Review: Church Booleans 3 We can encode TRUE,
More informationThe Typed Racket Guide
The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce
More informationCIS 500 Software Foundations Midterm I Answer key October 8, 2003
CIS 500 Software Foundations Midterm I Answer key October 8, 2003 Inductive Definitions Review: Recall that the function size, which calculates the total number of nodes in the abstract syntax tree of
More informationThe story so far. Elements of Programming Languages. Pairs in various languages. Pairs
Elements of Programming Languages Lecture 6: Data structures James Cheney University of Edinburgh October 9, 2017 The story so far We ve now covered the main ingredients of any programming language: Abstract
More informationIntroduction to System F. Lecture 18 CS 565 4/20/09
Introduction to System F Lecture 18 CS 565 4/20/09 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
More informationG Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University
G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,
More informationLecture 9: More Lambda Calculus / Types
Lecture 9: More Lambda Calculus / Types CSC 131 Spring, 2019 Kim Bruce Pure Lambda Calculus Terms of pure lambda calculus - M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete
More information- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.
Pure Lambda Calculus Lecture 9: More Lambda Calculus / Types CSC 131 Spring, 2019 Kim Bruce Terms of pure lambda calculus - M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete
More informationLecture 14: Recursive Types
Lecture 14: Recursive Types Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Recursive Types CS546, 2018-2019 1 / 11 Motivation
More informationPart III. Chapter 15: Subtyping
Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule
More informationData Types The ML Type System
7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let
More informationHarvard School of Engineering and Applied Sciences Computer Science 152
Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different
More informationCIS 500 Software Foundations Midterm I
CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking
More information上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )
上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 ) 班级号 (class) 学号 (id) 姓名 (name) 课程名称 程序语言理论 Theory of Programming Languages 成绩 (score) 1. (14 points) (a) Please give the value of a0, a1 after running the following
More informationHomework 3 COSE212, Fall 2018
Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports
More informationThe Untyped Lambda Calculus
Resources: The slides of this lecture were derived from x = 1 [Järvi], with permission of the original author, by copy & let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn
More informationCSE 3302 Programming Languages Lecture 8: Functional Programming
CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages
More informationCS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th
CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th Name: CAS ID (e.g., abc01234@pomona.edu): I encourage you to collaborate. collaborations below. Please record your Each question is worth
More informationCSE-321 Programming Languages 2010 Midterm
Name: Hemos ID: CSE-321 Programming Languages 2010 Midterm Score Prob 1 Prob 2 Prob 3 Prob 4 Total Max 15 30 35 20 100 1 1 SML Programming [15 pts] Question 1. [5 pts] Give a tail recursive implementation
More informationLecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference
Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::
More informationType Systems Winter Semester 2006
Type Systems Winter Semester 2006 Week 9 December 13 December 13, 2006 - version 1.0 Plan PREVIOUSLY: unit, sequencing, let, pairs, sums TODAY: 1. recursion 2. state 3.??? NEXT: exceptions? NEXT: polymorphic
More informationTyped Scheme: Scheme with Static Types
Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and
More informationExercise 1 (2+2+2 points)
1 Exercise 1 (2+2+2 points) The following data structure represents binary trees only containing values in the inner nodes: data Tree a = Leaf Node (Tree a) a (Tree a) 1 Consider the tree t of integers
More informationExercise 1 ( = 24 points)
1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =
More informationCIS 500 Software Foundations Fall October 2
CIS 500 Software Foundations Fall 2006 October 2 Preliminaries Homework Results of my email survey: There was one badly misdesigned (PhD) problem and a couple of others that were less well thought through
More informationSubsumption. Principle of safe substitution
Recap on Subtyping Subsumption Some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. Which can be formalized as by introducing:
More informationHarvard School of Engineering and Applied Sciences CS 152: Programming Languages
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 14 Tuesday, March 24, 2015 1 Parametric polymorphism Polymorph means many forms. Polymorphism is the ability of
More informationCS558 Programming Languages
CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a
More informationCOSE212: Programming Languages. Lecture 3 Functional Programming in OCaml
COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of
More informationProgramming in Omega Part 1. Tim Sheard Portland State University
Programming in Omega Part 1 Tim Sheard Portland State University Tim Sheard Computer Science Department Portland State University Portland, Oregon PSU PL Research at Portland State University The Programming
More informationFormal Systems and their Applications
Formal Systems and their Applications Dave Clarke (Dave.Clarke@cs.kuleuven.be) Acknowledgment: these slides are based in part on slides from Benjamin Pierce and Frank Piessens 1 Course Overview Introduction
More informationResources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...
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
More informationType Checking and Type Inference
Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled
More informationLists. Michael P. Fourman. February 2, 2010
Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce
More informationHarvard School of Engineering and Applied Sciences CS 152: Programming Languages
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations
More informationFlang typechecker Due: February 27, 2015
CMSC 22610 Winter 2015 Implementation of Computer Languages I Flang typechecker Due: February 27, 2015 Project 3 February 9, 2015 1 Introduction The third project is to implement a type checker for Flang,
More informationWhereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions
Whereweare CS-XXX: Graduate Programming Languages Lecture 7 Lambda Calculus Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now: Didn t IMP leave
More informationCS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011
CS152: Programming Languages Lecture 7 Lambda Calculus Dan Grossman Spring 2011 Where we are Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now:
More informationDesign Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014
Design Principles of Programming Languages Recursive Types Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014 Review: Why learn type theories? Art vs. Knowledge Art cannot be
More informationOverview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics
Overview A normal-order language Strictness Recursion Infinite data structures Direct denotational semantics Transition semantics Lazy (call-by-need) evaluation and its semantics A Normal-Order Language
More informationCS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012
CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our
More informationThe Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements
CIS 500 Software Foundations Fall 2004 27 September IS 500, 27 September 1 The Lambda Calculus IS 500, 27 September 3 Announcements Homework 1 is graded. Pick it up from Cheryl Hickey (Levine 502). We
More informationInductive Data Types
Inductive Data Types Lars-Henrik Eriksson Functional Programming 1 Original slides by Tjark Weber Lars-Henrik Eriksson (UU) Inductive Data Types 1 / 42 Inductive Data Types Today Today New names for old
More informationLambda Calculi With Polymorphism
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
More informationPart III Chapter 15: Subtyping
Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule
More informationThe University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS
The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER 2012 2013 MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS Time allowed TWO hours Candidates may complete the front
More informationSimply-Typed Lambda Calculus
#1 Simply-Typed Lambda Calculus #2 Back to School What is operational semantics? When would you use contextual (small-step) semantics? What is denotational semantics? What is axiomatic semantics? What
More informationCSE-321 Programming Languages 2010 Final
Name: Hemos ID: CSE-321 Programming Languages 2010 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 18 28 16 12 36 40 150 There are six problems on 16 pages, including two work sheets, in
More informationDesign Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term
Design Principles of Programming Languages Recursive Types Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term Review: what have we learned so far? λ-calculus: function and data can
More informationIntroduction to OCaml
Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional
More informationDenotational Semantics. Domain Theory
Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning
More informationCS 4110 Programming Languages & Logics. Lecture 27 Recursive Types
CS 4110 Programming Languages & Logics Lecture 27 Recursive Types 4 November 2016 Announcements 2 My office hours are at the normal time today but canceled on Monday Guest lecture by Seung Hee Han on Monday
More informationCIS 500 Software Foundations Fall September 25
CIS 500 Software Foundations Fall 2006 September 25 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial programming language, then the
More informationTypes and Type Inference
CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of
More informationFunctional Languages and Higher-Order Functions
Functional Languages and Higher-Order Functions Leonidas Fegaras CSE 5317/4305 L12: Higher-Order Functions 1 First-Class Functions Values of some type are first-class if They can be assigned to local variables
More informationCSE 341: Programming Languages
CSE 341: Programming Languages Hal Perkins Spring 2011 Lecture 2 Functions, pairs, and lists Hal Perkins CSE341 Spring 2011, Lecture 2 1 What is a programming language? Here are separable concepts for
More informationIntroduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples
Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the
More informationCSE-321 Programming Languages 2011 Final
Name: Hemos ID: CSE-321 Programming Languages 2011 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 15 15 10 17 18 25 100 There are six problems on 18 pages in this exam, including one extracredit
More informationDerivation for the Necessity of Recursive Types and its Basic Usage
Derivation for the Necessity of Recursive Types and its Basic Usage Philipp Koster Supervised by Prof. Dr. Farhad D. Mehta Seminar AT 2016 1 Abstract This paper derives the need for recursive types by
More informationType Systems. Pierce Ch. 3, 8, 11, 15 CSE
Type Systems Pierce Ch. 3, 8, 11, 15 CSE 6341 1 A Simple Language ::= true false if then else 0 succ pred iszero Simple untyped expressions Natural numbers encoded as succ succ
More informationCostly software bugs that could have been averted with type checking
Type Checking Class Notes from Lectures 6 and 7 Lahav Yeffet and Ori Folger The material in these lectures is based on the textbook Types and Programming Languages by Benjamin Pierce. Here is a list of
More information4.5 Pure Linear Functional Programming
4.5 Pure Linear Functional Programming 99 4.5 Pure Linear Functional Programming The linear λ-calculus developed in the preceding sections can serve as the basis for a programming language. The step from
More informationSubtyping. Lecture 13 CS 565 3/27/06
Subtyping Lecture 13 CS 565 3/27/06 Polymorphism Different varieties of polymorphism: Parametric (ML) type variables are abstract, and used to encode the fact that the same term can be used in many different
More informationProgramming Languages Fall 2014
Programming Languages Fall 2014 Lecture 7: Simple Types and Simply-Typed Lambda Calculus Prof. Liang Huang huang@qc.cs.cuny.edu 1 Types stuck terms? how to fix it? 2 Plan First I For today, we ll go back
More informationTypes and Type Inference
Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion
More informationGoal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.
Goal Understand what this interface means and why it matters: CS152: Programming Languages Lecture 15 Parametric Polymorphism Dan Grossman Spring 2011 type a mylist; val mt_list : a mylist val cons : a
More informationData Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.
Data Abstraction An Abstraction for Inductive Data Types Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Introduction This lecture
More informationGraph reduction. Simple graph reduction with visualisation
Simple graph reduction with visualisation Outline The minifp language Graph representation of terms Tying the knot The compiler The reduction engine Literature The minifp language Small functional language,
More informationInductive Definitions, continued
1 / 27 Inductive Definitions, continued Assia Mahboubi Jan 7th, 2016 2 / 27 Last lecture Introduction to Coq s inductive types: Introduction, elimination and computation rules; Twofold implementation :
More informationExercise 1 ( = 18 points)
1 Exercise 1 (4 + 5 + 4 + 5 = 18 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a = Leaf
More informationType Systems, Type Inference, and Polymorphism
6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.
More informationThe type checker will complain that the two branches have different types, one is string and the other is int
1 Intro to ML 1.1 Basic types Need ; after expression - 42 = ; val it = 42 : int - 7+1; val it = 8 : int Can reference it - it+2; val it = 10 : int - if it > 100 then "big" else "small"; val it = "small"
More informationCSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016
CSE-505: Programming Languages Lecture 20.5 Recursive Types Zach Tatlock 2016 Where are we System F gave us type abstraction code reuse strong abstractions different from real languages (like ML), but
More informationCSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures
CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught
More informationSecond-Order Type Systems
#1 Second-Order Type Systems Homework 5 Summary Student : 37.9704 Student : 44.4466 ORIGINAL : 50.2442 Student : 50.8275 Student : 50.8633 Student : 50.9181 Student : 52.1347 Student : 52.1633 Student
More informationQuestions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no
Questions? First exercise is online: http://www.win.tue.nl/~mvdbrand/courses/glt/1112/ Deadline 17 th of October Next week on Wednesday (5 th of October) no lectures!!! Primitive types Primitive value
More informationDynamic Types , Spring March 21, 2017
Dynamic Types 15-312, Spring 2017 March 21, 2017 Announcements Homework 4 will be released shortly. Most of it is new, so it s hard to tell how hard we made it. Please start early! Look at what I made
More informationMLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen
1 MLW Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky Radboud University Nijmegen March 26, 2012 inductive types 2 3 inductive types = types consisting of closed terms built from constructors
More informationTyped Lambda Calculus and Exception Handling
Typed Lambda Calculus and Exception Handling Dan Zingaro zingard@mcmaster.ca McMaster University Typed Lambda Calculus and Exception Handling p. 1/2 Untyped Lambda Calculus Goal is to introduce typing
More informationAn introduction introduction to functional functional programming programming using usin Haskell
An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program
More informationCS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012
CS-XXX: Graduate Programming Languages Lecture 17 Recursive Types Dan Grossman 2012 Where are we System F gave us type abstraction code reuse strong abstractions different from real languages (like ML),
More information