CSE-321 Programming Languages 2014 Final

Similar documents
CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2010 Final

CSE-321 Programming Languages 2010 Midterm

CSE-321 Programming Languages 2012 Midterm

Mutable References. Chapter 1

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

Programming Languages Lecture 15: Recursive Types & Subtyping

1 Introduction. 3 Syntax

Programming Languages Lecture 14: Sum, Product, Recursive Types

Formal Systems and their Applications

Let Arguments Go First

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

Programming Languages

λ calculus is inconsistent

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Lecture 9: Typed Lambda Calculus

Concepts of programming languages

CSE505, Fall 2012, Midterm Examination October 30, 2012

Constrained Types and their Expressiveness

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

ML F Raising ML to the Power of System F

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Let Arguments Go First

Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018

4.6 Recursive Types. 4.6 Recursive Types 105

Harvard School of Engineering and Applied Sciences Computer Science 152

Subsumption. Principle of safe substitution

Chapter 22: Type Reconstruction (Type Inference)

Introduction to the Lambda Calculus

Part III. Chapter 15: Subtyping

4.5 Pure Linear Functional Programming

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Intersections and Unions of Session Types

Lecture 13: Subtyping

Sémantique des Langages de Programmation (SemLP) DM : Region Types

Lecture Notes on Data Representation

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

If a program is well-typed, then a type can be inferred. For example, consider the program

Part III Chapter 15: Subtyping

CIS 500 Software Foundations Midterm I

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

The Polymorphic Blame Calculus and Parametricity

The Typed λ Calculus and Type Inferencing in ML

Verifying Program Invariants with Refinement Types

Last class. CS Principles of Programming Languages. Introduction. Outline

Programming Languages Fall 2014

Proving OCaml s type system? Jacques Garrigue Nagoya University

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

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

Calculus of Inductive Constructions

Basic Foundations of Isabelle/HOL

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

Theory and Implementation of a Functional Programming Language

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Typed Lambda Calculus and Exception Handling

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

We defined congruence rules that determine the order of evaluation, using the following evaluation

Lambda Calculus and Type Inference

Polymorphism and System-F (OV)

Where is ML type inference headed?

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1

Type Inference for Scripting languages with Implicit Extension

Lecture Notes on Program Equivalence

1.3. Conditional expressions To express case distinctions like

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lambda Calculus. Lecture 4 CS /26/10

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

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

CMSC 330: Organization of Programming Languages

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

Arbitrary-rank polymorphism in (GHC) Haskell

CITS3211 FUNCTIONAL PROGRAMMING

HIGHER-ORDER ABSTRACT SYNTAX IN TYPE THEORY

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Type-indexed functions in Generic Haskell

Coherent Coercion Abstraction with a step-indexed strong-reduction semantics

Finite Model Generation for Isabelle/HOL Using a SAT Solver

Runtime Behavior of Conversion Interpretation of Subtyping

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

CSE 505, Fall 2009, Final Examination 14 December Please do not turn the page until everyone is ready.

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

Flexible Types. Robust type inference for first-class polymorphism. Daan Leijen. Abstract. 1. Introduction. Microsoft Research

Denotational Semantics. Domain Theory

Autosubst Manual. May 20, 2016

Lecture #23: Conversion and Type Inference

CS 421, Fall Sample Final Questions & Solutions

Koka: Programming with Row-polymorphic Effect Types

CSE-321: Assignment 8 (100 points)

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

An Interoperable Calculus for External Object Access

Three Applications of Strictness in the Efficient Implementaton of Higher-Order Terms

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

GADTs meet Subtyping

Type Checking and Type Inference

First-Class Type Classes

Transcription:

Name: Hemos ID: CSE-321 Programming Languages 2014 Final Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 Total Score Max 15 12 14 14 30 15 100 There are six problems on 12 pages in this exam. The maximum score for this exam is 100 points. Be sure to write your name and Hemos ID. You have three hours for this exam. 1

1 Inductive definitions of matched parentheses [15 points] Consider the following system from the course notes where s mparen means that s is a string of matched parentheses. ɛ mparen Meps s mparen (s) mparen Mpar s 1 mparen s 2 mparen s 1 s 2 mparen Mseq In order to show that if s mparen holds, s is indeed a string of matched parentheses, we introduce a new judgment k s where k is a non-negative integer: k s k left parentheses concatenated with s form a string of matched parentheses (( ( s is a string of matched parentheses }{{} k The idea is that we scan a given string from left to right and keep counting the number of left parentheses that have not yet been matched with corresponding right parentheses. Thus we begin with k = 0, increment k each time a left parenthesis is encountered, and decrement k each time a right parenthesis is encountered: 0 ɛ Peps k + 1 s k 1 s k > 0 Pleft Pright k (s k )s The second premise k > 0 in the rule Pright ensures that in any prefix of a given string, the number of right parentheses may not exceed the number of left parentheses. Now a judgment 0 s expresses that s is a string of matched parentheses. Here are a couple of examples: 0 ɛ Peps 1 > 0 Pright 1 ) 2 > 0 2 )) 1 ()) Pleft 0 (()) Pleft Pright (the rule Pright is not applicable because 0 0 ) 0 )( 1 ))( Pright 0 ())( Pleft We wish to prove Theorem 1.2 which states that a string s satisfying 0 s indeed belongs to the syntactic category mparen. We use a lemma 1.1 in the proof. Lemma 1.1. For every natural number k 0, if (( ( s mparen, then (( (()s mparen. }{{}}{{} k k Theorem 1.2. If 0 s, then s mparen. Prove Theorem 1.2. You may use Lemma 1.1 without proving it. In your proof, place conclusion in the left and justification in the right as is conventional in the course notes. If we fail to understand your proof, you get no credit, so try to make your proof as readable as possible. You may write either (( ( }{{} k or ( k for a sequence of k ( s. 2

(Answer sheet for Problem 1) 3

2 Abstract machine N [12 points] In this problem, we design an abstract machine N based on the call-by-need reduction strategy which you implemented in Assignment 6. The call-by-need reduction strategy is a variant of the call-by-name strategy: it reduces a function application without evaluating the argument, but is also designed so that it never evaluates the same argument more than once. To this end, it delays the evaluation of the argument until the result is needed. Once the argument is fully evaluated, it stores the result in the heap so that when it needs the argument again, it does not need to repeat the same evaluation. The heap stores two different kinds of objects: delayed expressions and computed values. When the machine attempts to evaluates a function application, it first allocates a new delayed expression for the argument in the heap and then proceeds to reducing the function application. When the machine later evaluates the variable bound to the argument for the first time, it retrieves the actual argument from the delayed expression to evaluates it. Then it replaces the delayed expression with a computed value in the heap so that all subsequent references to the same variable can directly use the result without repeating the same evaluation. The abstract machine N uses the following definitions: type A ::= P A A expression e ::= x λx:a. e e e value v ::= stored value sv ::= heap h ::= h, l sv environment η ::= η, x l frame φ ::= stack σ ::= σ; φ state s ::= h σ e @ η h σ v A value v contains the result of evaluating an expression. A stored value sv is an object to be stored in the heap h, and thus is either a delayed expression or a computed value. In the definition of state s: h σ e @ η means that the machine with heap h and stack σ is currently analyzing e under environment η. h σ v means that the machine with heap h and stack σ is currently returning v. The reduction judgment for the abstract machine N is as follows: s N s the machine makes a transition from state s to another state s To define the operational semantics, we use a couple of auxiliary functions. First we define the function dom(h) which returns the set of all locations in a given heap h: dom( ) = dom(h, l sv) = dom(h) {l} We also use [l sv]h for updating the contents of l in h with sv: [l sv ](h, l sv) = h, l sv Complete the definitions of value v, stored value sv, and frame φ. Then define transition rules for the abstract machine N. You may introduce as many transition rules as you need. Explain your definitions and transition rules as necessary. 4

(Definitions) value v ::= stored value sv ::= frame φ ::= (Transition rules) 5

3 Subtypes and recursive types [14 points] Question 1. [3 points] The rule of subsumption is a typing rule which enables us to change the type of an expression to its subtype. Complete the rule of subsumption: Question 2. [3 points] Complete the subtyping rule for product types and function types. A B A B Prod A B A B Fun Question 3. [3 points] Consider the following simply typed λ-calculus extended with recursive types. type A ::= unit A A A+A α µα.a expression e ::= x λx:a. e e e () inl A e inr A e case e of inl x. e inr x. e fold C e unfold C e typing context Γ ::= Γ, x : A Γ, α type Given a recursive type C = µα.a, fold C e and unfold C e convert [C/α]A to C and vice versa, respectively. Complete typing rules for fold C e and unfold C e: C = µα.a Γ e : [C/α]A Γ C type Γ fold C e : C Fold C = µα.a Γ e : C Γ unfold C e : [C/α]A Unfold Question 4. [5 points] Translate constructs for a recursive datatype for natural numbers into the above simply typed λ-calculus. datatype nat = Zero Succ of nat nat = µα.unit+α Zero = fold nat inl nat () Succ e = fold nat inr unit e case e of Zero e 1 Succ x e 2 = case unfold nat e of inl. e 1 inr x. e 2 6

4 System F [14 points] Consider the following definitions for System F: type A ::= A A α α.a expression e ::= x λx:a. e e e Λα. e e A value v ::= λx:a. e Λα. e typing context Γ ::= Γ, x : A Γ, α type Note that a typing context Γ is an ordered set of type bindings and type declarations. We use three judgments: a reduction judgment, a type judgment, and a typing judgment. e e e reduces to e Γ A type A is a valid type with respect to typing context Γ Γ e : A e has type A under typing context Γ Question 1. [4 points] Write two reduction rules for type applications e A : Question 2. [4 points] Write the typing rules for type abstractions Λα. e and type applications e A : Question 3. [6 points] The proof of type safety of System F needs three substitution lemmas because there are three kinds of substitutions in System F: type substitution into types, type substitution into expressions, and expression substitution. State the three substitution lemmas for the proof of type safety of System F. 1. For substituting types for type variables in types: 2. For substituting types for type variables in expressions: 3. For substituting expressions for variables in expressions: 7

5 Type reconstruction [30 points] Consider the implicit let-polymorphic type system given in the course notes. monotype A ::= A A α polytype U ::= A α.u expression e ::= x λx. e e e let x = e in e typing context Γ ::= Γ, x : U type substitution S ::= id {A/α} S S type equations E ::= E, A = A We use a typing judgment Γ e : U to express that untyped expression e is typable with a polytype U under typing context Γ. S U and S Γ denote applications of S to U and Γ, respectively. ftv(γ) denotes the set of free type variables in Γ. ftv(u) denotes the set of free type variables in U. An auxiliary function Gen Γ (A) generalizes monotype A to a polytype after taking into account free type variables in typing context Γ: Gen Γ (A) = α 1. α 2. α n.a where α i ftv(γ) and α i ftv(a) for i = 1,, n. We write Γ+x : U for Γ {x : U }, x : U if x : U Γ, and for Γ, x : U if Γ contains no type binding for variable x. The function Unify has a property that if Unify(A 1 = A 1,, A n = A n) = S, then S A i = S A i for i = 1,, n. Question 1. [5 points] Write all the typing rules for the typing judgment Γ e : U. In the rule for specialization, you may use a type judgment Γ A type to mean that A is a valid type with respect to typing context Γ. x : U Γ Γ x : U Var Γ, x : A e : B Γ λx. e : A B I Γ e : A B Γ e : A Γ e e E : B 8

Question 2. [3 points] Fill in the blank: Gen (α α) = α.α α Gen x:α (α α) = α α Gen x:α (α β) = β.α β Gen x:α,y:β (α β) = α β Question 3. [6 points] Complete the unification algorithm Unify: Unify( ) = id Unify(E, α = A) = Unify(E, A = α) = if α = A then Unify(E) else if α ftv(a) then fail else Unify({A/α} E) {A/α} Unify(E, A 1 A 2 = B 1 B 2 ) = Unify(E, A 1 = B 1, A 2 = B 2 ) 9

Question 4. [12 points] Complete the algorithm W. Its soundness means that if W(Γ, e) = (S, A), then S Γ e : A. W(Γ, x) = (id, { β/ α} A) where x : α.a Γ and fresh β W(Γ, λx. e) = let (S, A) = W(Γ + x : α, e) in (fresh α) (S, (S α) A) W(Γ, e 1 e 2 ) = let (S 1, A 1 ) = W(Γ, e 1 ) in let (S 2, A 2 ) = W(S 1 Γ, e 2 ) in let S 3 = Unify(S 2 A 1 = A 2 α) in (fresh α) (S 3 S 2 S 1, S 3 α ) W(Γ, let x = e 1 in e 2 ) = let (S 1, A 1 ) = W(Γ, e 1 ) in let (S 2, A 2 ) = W(S 1 Γ + x : Gen S1 Γ(A 1 ), e 2 ) in (S 2 S 1, A 2 ) Question 5. [4 points] fix x. e is as follows: Complete the case for fix x. e in the algorithm W: Now we add an untyped fixed point construct fix x. e. The typing rule for Γ, x : A e : A Γ fix x. e : A Fix W(Γ, fix x. e) = let (S 1, A 1 ) = W(Γ + x : α, e) in (fresh α) let S 2 = Unify(S 1 α = A 1 ) in (S 2 S 1, S 2 A 1 ) 10

6 Soundness of the algorithm W [15 points] Prove the soundness of the algorithm W. You may use the following three lemmas without proofs: Lemma 6.1. If Γ e : A, then S Γ e : S A for any type substitution S. Lemma 6.2. If Unify(A 1 = A 1,, A n = A n) = S, then S A i = S A i for i = 1,, n. Lemma 6.3. If Γ e : A, then Γ e : Gen Γ (A). Complete the proof of Theorem 6.4. Theorem 6.4 (Soundness of W). If W(Γ, e) = (S, A), then S Γ e : A. Proof. By structural induction on e. We consider three cases shown below. We give a complete proof of the first case and a partial proof of the second case. Case e = λx. e : There exist S 1, A 1, and a fresh type variable α 1 such that: W(Γ + x : α 1, e ) = (S 1, A 1 ) by the definition of W S 1 Γ + x : S 1 α 1 e : A 1 by induction hypothesis on e W(Γ, λx. e ) = (S 1, (S 1 α 1 ) A 1 ) S = S 1 by the definition of W A = (S 1 α 1 ) A 1 S 1 Γ λx. e : (S 1 α 1 ) A 1 by the rule I with S 1 Γ + x : S 1 α 1 e : A 1 S Γ λx. e : A from S = S 1, A = (S 1 α 1 ) A 1 Case e = e 1 e 2 : There exist S 1 and A 1 such that: W(Γ, e 1 ) = (S 1, A 1 ) by the definition of W S 1 Γ e 1 : A 1 by induction hypothesis on e 1 There exist S 2 and A 2 such that: W(S 1 Γ, e 2 ) = (S 2, A 2 ) by the definition of W S 2 S 1 Γ e 2 : A 2 by induction hypothesis on e 2 11

Case e = let x = e 1 in e 2 : 12