CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Similar documents
Programming Languages Lecture 14: Sum, Product, Recursive Types

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

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

Mutable References. Chapter 1

Handout 2 August 25, 2008

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

4.5 Pure Linear Functional Programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Denotational Semantics. Domain Theory

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Graphical Untyped Lambda Calculus Interactive Interpreter

CIS 500 Software Foundations Midterm I

Type Checking and Type Inference

Functional Programming. Lecture 2: Algebra

Principles of Programming Languages 2017W, Functional Programming

CS558 Programming Languages

CSE505, Fall 2012, Midterm Examination October 30, 2012

L3 Programming September 19, OCaml Cheatsheet

CSE341 Autumn 2017, Final Examination December 12, 2017

Programming Languages Assignment #7

Lab 7: OCaml 12:00 PM, Oct 22, 2017

CSCI-GA Scripting Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Programming Languages Lecture 15: Recursive Types & Subtyping

CSE 505: Concepts of Programming Languages

CS558 Programming Languages

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

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

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CS 320: Concepts of Programming Languages

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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

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

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

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

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

The Substitution Model. Nate Foster Spring 2018

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Typed Lambda Calculus and Exception Handling

Harvard School of Engineering and Applied Sciences Computer Science 152

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Introduction to OCaml

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Mini-ML. CS 502 Lecture 2 8/28/08

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

The Substitution Model

1 Introduction. 3 Syntax

Metaprogramming assignment 3

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

SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis

CS558 Programming Languages

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

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

CMSC 330: Organization of Programming Languages. Operational Semantics

CSE 341: Programming Languages

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

Type Systems Winter Semester 2006

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

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

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

CIS 500 Software Foundations Fall September 25

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

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CS 11 Haskell track: lecture 1

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

λ calculus is inconsistent

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Typed Racket: Racket with Static Types

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

CMSC 330: Organization of Programming Languages

Lambda Calculi With Polymorphism

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CMSC330 Spring 2017 Midterm 2

How to Design Programs

Functional Programming. Pure Functional Languages

Documentation for LISP in BASIC

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

CPSC W1 University of British Columbia

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

CMSC 330 Exam #2 Fall 2008

List Functions, and Higher-Order Functions

Shell CSCE 314 TAMU. Haskell Functions

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

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Sample Final Exam Questions

Functional Programming

At build time, not application time. Types serve as statically checkable invariants.

Transcription:

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 one point, except problems marked with a (C) are challenge problems go ahead and test your mettle, but these are longer or harder than anything I d put on an exam and worth no course credit. Please turn in your work as a printout of this sheet, not on separate paper. If you would rather typeset your work, I can give you the L A TEX... but you ll learn more by writing it by hand. Collaborators:

1 Lambda calculus with booleans t ::= bool t 1 t 2 e ::= x e 1 e 2 λx:t. e true false if e 1 then e 2 else e 3 Γ ::= Γ, x:t Γ(x) = t Γ x : t Γ e 1 : t 1 t 2 Γ e 2 : t 1 Γ e 1 e 2 : t 2 Γ, x:t 1 e : t 2 Γ λx:t 1. e : t 1 t 2 Γ true : bool 1.1 Type hunting Γ false : bool Γ e 1 : bool Γ e 2 : t Γ e 3 : t Γ if e 1 then e 2 else e 3 : t For each term e, find a context Γ and type t that makes that term well typed, i.e., Γ e : t. 1. Γ if x then x else y : t Γ = t = 2. Γ x y : t Γ = t = 3. Γ λx:bool. x : t Γ = t = 4. Γ λx:bool bool. y x true : t Γ = t = 5. Γ λx:bool bool. y (x true) : t Γ = t = 6. Γ λx:t 1. if y then x else y : t Γ = t = t 1 = 7. Γ λx:t 1 y:t 2 z:bool. if z then x else y : t Γ = t = t 1 = t 2 =

1.2 Term hunting For each type t, find a closed term e that has that type, i.e., e : t. 1. e : bool bool 2. e : bool bool bool 3. e : (bool bool) bool 4. e : (bool bool bool) bool

1.3 A failed hunt Explain why there are no t 1, t 2, and t 3 such that (λx:t 1. x x) (λx:t 2. x x) : t 3. 1.4 A type you can count on How many semantically different closed values are there of type bool? That is, we can write an infinite number of closed programs with type bool, but how many different values can we get out? List them as lambda calculus terms. How many semantically different values are there of type bool bool? There are infinitely many syntactically different values of type bool bool, but many of them behave the same. How many different behaviors can a value typed at bool bool exhibit? List them as (typed) lambda calculus terms.

1.5 We make our own rules, here Suppose we extended our grammar with a forms e 1 e 2 (conjunction, read e 1 and e 2 ), e 1 e 2 (disjunction, read e 1 or e 2 ), and e (negation, read not e ). Write typing rules for these forms.

1.6 Conditional love (C) Suppose we extend our grammar with a multi-branch conditional, of the form: cond {e 11 e 12 ; e 21 e 22 ;... ; e d } Here are small-step evaluation rules for it: e 11 e 11 cond {e 11 e 12 ; e 21 e 22 ;... ; e d } cond {e 11 e 12 ; e 21 e 22 ;... ; e d } cond {true e 12 ; e 21 e 22 ;... ; e d } e 12 cond {false e 12 ; e 21 e 22 ;... ; e d } cond {e 21 e 22 ;... ; e d } cond { e d } e d In English, a multi-branch conditional evaluates each of its branches e i1 e i2 in turn; if e i1 yields true, then it executes e i2 ; otherwise, it keeps checking other branches. If none of the branches match, it runs the default branch e d. Write a typing rule for cond.

2 Tuples 2.1 Two s company Write the typing rules for a lambda calculus extended with pair types (t 1, t 2 ), pairs (e 1, e 2 ) and projections fst e and snd e.

2.2 The trouble with triples Write the typing rules for a lambda calculus extended with triples, i.e., the type (t 1, t 2, t 3 ) and the terms (e 1, e 2, e 3 ), first e, second e, and third e. 2.3 It s a twofer (C) Devise a syntactic sugar for encoding triples in terms of pairs. That is, write down four pieces of syntactic sugar that take the triple type and expression syntax of Problem 2.2 to a program in the pair syntax of Problem 2.1. Make sure you syntactic sugar: (a) has the right behavior; and (b) preserves types appropriately, i.e., if (e 1, e 2, e 3 ) is well typed per your rules in Problem 2.2, its encoding should be well typed per your rules in Problem 2.1.

2.4 No limits (C) Write typing rules for tuples of arbitrary length, i.e., types (t 1,..., t n ), tuples (e 1,..., e n ), and projections π i e which get the ith element of a tuple. Be sure to allow n to be 0.

3 Other extensions 3.1 List of demands Suppose we have integers (type int) in the lambda calculus. Add lists of integers to the simply typed lambda calculus, i.e., a type intlist and terms nil, cons e 1 e 2, and case e 1 of {nil e 2 ; cons x 1 x 2 e 3 }. Here are evaluation rules for case: e 1 e 1 case e 1 of {nil e 2 ; cons x y e 3 } case e 1 of {nil e 2 ; cons x y e 3 } case nil of {nil e 2 ; cons x y e 3 } e 2 case (cons v 1 v 2 ) of {nil e 2 ; cons x 1 x 2 e 3 } e 3 [ v1 /x 1][ v2 /x 2] Write typing rules for nil, cons e 1 e 2, and case e 1 of {nil e 2 ; cons x 1 x 2 e 3 }. This problem s title was inspired by the inimitable Saul Williams (see https://www.youtube.com/watch?v=zdmtaicrfq0).

3.2 Sum more than others Add the Haskell Either datatype to the simply typed lambda calculus. That is, extend the rules of the simply typed lambda calculus to include so-called sum types t 1 + t 2 and terms left e, right e, and a pattern matching form like case e 1 of {left x 1 e 2 ; right x 2 e 3 }. You ll need to write typing rules for each of these new syntactic forms. What changes would you make, if any, to implement these rules in a type checker? Fun fact: pairs are also called product types, and are sometimes written t 1 t 2 or t 1 t 2 to emphasize this fact. The sum/product analogies are why datatypes are sometimes called algebraic datatypes.

3.3 Get your fix In HW07, we introduced recursion by adding let rec. We could have instead added recursive functions directly. Suppose we extend the simply typed lambda calculus with an expression form fix f(x:t 1 ) : t 2 = e Here f is the name of the function, x is its argument and t 1 is the argument s type, t 2 is the return type, and e is the body. It evaluates as follows: (fix f(x:t 1 ) : t 2 = e) v e[ v /x][ fix f(x:t1) : t2 = e /f] For example, (fix fact(n:int) : int = if n = 0 then 1 else n fact (n 1)) 5 120 Write a typing rule for fix.

3.4 That s an order (C) Suppose we have a simply typed lambda calculus with let x = e 1 in e 2. Let e 1 ; e 2 be syntactic sugar for let = e 1 in e 2, i.e., it runs e 1, throws away the result, and then runs e 2. Let s extend the simply typed lambda calculus with state, i.e., a type ref t and terms new e (which allocates a new reference with e as its initial value), read e 1 (which looks up the current value of the reference in e 1 ), and write e 1 e 2 (which sets the reference in e 1 to have a new value in e 2. For example, we have: let x = new true in read x true let x = new true in write x false () let x = new true in write x( (read x)); read x false Write typing rules for new, read, and write. Don t allow strong updates, which change the type of a variable. That is, let x = new true in write x (λx:bool. x) should be ill typed. You can assume that there is a rule saying Γ () : (). Write small-step reduction rules for these features. Note that you ll need to add something to the step relation to keep track of the values of each reference. (How did we handle mutation for the While language?) You might need another page.