CPSC W1 University of British Columbia

Similar documents
CPSC 311: Definition of Programming Languages: Assignment 4: Stepping

Intro. Scheme Basics. scm> 5 5. scm>

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Programming Languages

Programming Languages

The Substitution Model. Nate Foster Spring 2018

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

An Introduction to Functions

CMSC 330: Organization of Programming Languages

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS115 - Module 10 - General Trees

Variables and Bindings

Lambda Calculus. Concepts in Programming Languages Recitation 6:

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Programming Project #3: Syntax Analysis

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

CPSC 311, 2010W1 Midterm Exam #2

cs173: Programming Languages Midterm Exam

Higher-Order Functions (Part I)

Understanding Recursion

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Functional Programming. Lecture 2: Algebra

Higher-Order Functions

CIS 500 Software Foundations Midterm I

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Comp 311: Sample Midterm Examination

CMSC 330: Organization of Programming Languages. Operational Semantics

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

cs173: Programming Languages Final Exam

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

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

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

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

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

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

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

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Case Study: Undefined Variables

The Substitution Model

How to Design Programs

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

CSE 505: Concepts of Programming Languages

Untyped Lambda Calculus

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Untyped Lambda Calculus

Typing Control. Chapter Conditionals

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Lecture 15 CIS 341: COMPILERS

Project 2: Scheme Interpreter

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

Module 10: General trees

Programming Languages and Compilers (CS 421)

Typing Data. Chapter Recursive Types Declaring Recursive Types

Func+on applica+ons (calls, invoca+ons)

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

CS1102: Adding Error Checking to Macros

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

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Module 5: Lists. Readings: HtDP, Sections 9, 10.

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

CSCI-GA Scripting Languages

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Lecture 2: Big-Step Semantics

Question Points Score

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Lecture 2: SML Basics

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

COMP 105 Homework: Type Systems

CMSC 330: Organization of Programming Languages

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Principles of Programming Languages

Names and Functions. Chapter 2

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

CMSC 330: Organization of Programming Languages

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

Flang typechecker Due: February 27, 2015

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

CS421 Spring 2014 Midterm 1

Parsing. CS321 Programming Languages Ozyegin University

Mutable References. Chapter 1

Bindings & Substitution

CS164: Midterm I. Fall 2003

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

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

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Defining syntax using CFGs

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

Introduction to lambda calculus Part 3

Variables. Substitution

Implementing Continuations

Transcription:

Definition of Programming Languages CPSC 311 2016W1 University of British Columbia Practice Midterm Eamination 26 October 2016, 19:00 21:00 Joshua Dunfield Student Name: Signature: Student Number: CS userid: INSTRUCTIONS This is a CLOSED BOOK CLOSED NOTES eamination. This eamination is PRINTED ON BOTH SIDES of the paper. Write all answers ON THE EXAMINATION PAPER. Try to write your answers in the blanks or boes given. If you can t, try to write them elsewhere on the same page, or on one of the worksheet pages, and LABEL THE ANSWER. We can t give credit for answers we can t find. Blanks are suggestions. You may not need to fill in every blank. This eamination is epected to be worth 15% of the course grade. Notes for the practice eam: The real questions will be on the same general topic as the midterm questions. We are trying to maintain similar difficulty levels, erring on the side of making the practice questions slightly harder than the actual midterm questions. The practice midterm has silly footnotes. The actual midterm doesn t it may have silly question titles, though. Point totals may not correspond eactly to the actual midterm.

Worksheet α 2

Question 1 15 points: A substitutable fo A fo has wandered into the Mathematics Anne, and has brought its own definition of substitution. Fortunately, most of the definition is the same as what you ve seen before. It appears that foes are abstract synta. Id = Id y = Id y if y Fo el er = Fo el er Let e eb = Let e eb Following the above definition, fill in the blank: = = = Let y e eb = Let y e eb if y Fo Id m z Id z Let z Id z Id z Fo Id m z Id z Id m z Let z Id z Id z Fo Id m Let z Id m z Id z Id z Fo Id m Let z Id m Id z It s not mandatory to write the intermediate steps. But you re more likely to get the right answer if you do and you re more likely to get partial credit for a wrong answer. You might be wondering how to evaluate a fo. That s a good question. But you don t need to know how to evaluate a fo to fill in the blank above, because substitution doesn t do evaluation. 3

Question 2 30 points: Energy This question is about a peculiar recursion epression called B-Rec. recursion, or perhaps for battery-powered recursion. The b stands for bounded In place of the Fun recursion epression Rec u e, we write B-Rec k u e for an epression that can nest itself up to k times, where k is a natural number 0, 1, 2,.... This recursion epression will not cause the interpreter to loop forever; instead, it will give up if its recursion depth eceeds k. For eample, in B-Rec 1 u e, the body e can use u recursively but those recursive uses of u must not be recursive themselves. We enforce this in the evaluation rules. If B-Rec k u e tries to call itself more than k times, then it will not evaluate to a value. To represent that situation, we add a judgment e drained. k 1 k = k 1 B-Rec k u e v B-Rec k u e u e v Eval-b-rec k < 1 B-Rec k u e drained The premise k 1 of Eval-b-rec prevents Eval-b-rec from deriving B-Rec 0 u e v. You may need to use the following rules: Num n Num n Eval-num Btrue Btrue Eval-btrue Bfalse Bfalse Eval-bfalse e Btrue ethen v Ite e ethen eelse v Eval-ite-true e Bfalse eelse v Ite e ethen eelse v Eval-ite-false Q2a 15 points Derive the judgment written below the line. If you need it, use: B-Rec 0 u Ite Btrue Num 3 Id u Ite u Btrue Num 3 Id u Ite Btrue Num 3 B-Rec 0 u Ite Btrue Num 3 Id u = } {{ } ebody For convenience, you can write ebody instead of Ite Btrue Num 3 Id u. 1 1 0 = 1 1 Btrue Btrue Eval-Btrue Num 3 Num 3 Eval-num Ite Btrue Num 3 B-Rec 0 u ebody Num 3 B-Rec 1 u Ite Btrue Num 3 Id u Num 3 Eval-ite-true Eval-b-rec 4

Question 2 30 points: Energy, continued Here are the rules for B-Rec, again: k 1 k = k 1 B-Rec k u e v B-Rec k u e u e v Eval-b-rec k < 1 B-Rec k u e drained Q2b 15 points Implement the above two rules by filling in the type-case branch for B-Rec in the function interp: define interp e ; interp : E > E type-case E e ;... Ite econd ethen eelse let vcond interp econd type-case E vcond Btrue interp ethen Bfalse interp eelse else error "Ite on a non-boolean" Rec u eb let v interp subst Rec u eb u eb v B-Rec k u eb if >= k 1 let v interp subst B-Rec - k 1 u eb u eb v error "B-Rec drained" Under eam conditions, it s okay to follow the rules less closely; for eample, by not letbinding v and just writing interp subst B-Rec - k 1 u eb u eb 5

Question 3 20 points: You could call it Curry in a Hurry. 1 Most real languages support functions or procedures, or methods that take more than one argument or maybe zero arguments. Here, we ll add functions that take at least one argument. To add multi-argument functions, we can either add them for real by updating the abstract synta and evaluation rules and the typing rules, if our language is typed, or add them as syntactic sugar. In that case, we only need to change the parser. In this problem, we ll add multi-argument functions as syntactic sugar. We need to update the concrete synta of Fun, as follows: E ::=... {Lam Id... E } {App E E...} The... are read as one or more occurrences of the preceding nonterminal. Thus, all of the following are syntactically valid epressions that match the above BNF: {Lam 5} {Lam } {Lam y 5} {Lam y z 5} {App f 3} {App g a} {App {App 2 3} b 17} {App f 1 c 0} However, {Lam 5} and {App f} are not syntactically valid: they would be functions and applications of zero arguments, which we won t allow. Since we re implementing multi-argument functions as syntactic sugar, the abstract synta of Fun doesn t change. The relevant parts of the define-type are still define-type E Num n number? Id name symbol? Lam name symbol? body E? App function E? argument E? Add code to parse to implement the Lam part of the following desugaring : {Lam 1 2 n E } is parsed as {Lam 1 {Lam 2 {Lam n E } }} {App E0 E1 En } is parsed as {App { {App E0 E1 } } En } For eample: {Lam y {App z 5 y}} is parsed as {Lam {Lam y {App {App z 5} y}}} so parse '{Lam y {app z 5 y}} should return the abstract synta epression Lam Lam y App App Id z Num 5 Id y 1 Words uttered by a customer of India Garden in Pittsburgh, which the restaurant manager met with an icy glare. 6

Question 3 20 points: Curry in a Hurry, continued define parse-lam args if = length args 2 ; single-argument Lam Lam first args parse second args ; multi-argument Lam One approach probably the easiest: Lam first args parse-lam rest args Alternate approach: create concrete synta and pass that to parse. let body parse cons 'Lam rest args Lam first args body define parse-app args if = length args 2 ; App with one argument note: first args is the function being applied App parse first args parse second args ; App with 2 or more arguments let* last-arg last args ecept-last reverse rest reverse args App parse-app ecept-last parse last args define parse sep cond ;... list? sep let* head first sep args rest sep arg-count length args case head ;... Lam if < arg-count 2 error "parse: malformed `Lam'" if symbol? first args parse-lam args error "parse: Lam must be followed by an identifier" App if < arg-count 2 error "parse: App needs at least two subepressions" parse-app args 7

Worksheet β 8

Question 4 15 points: REDACTED In this question, use the following evaluation rules: Num n Num n Eval-num Lam e Lam e Eval-lam e1 Num n1 Num n2 Add e1 Num n1 + n2 Eval-add e1 Num n1 Num n2 Sub e1 Num n1 n2 Eval-sub Recall the epression strategy and the value strategy for evaluating a function application App e1. Both strategies evaluate e1 to Lam eb, but they differ in how they handle : The epression strategy evaluates eb with replaced by. The value strategy evaluates to a value v2, then evaluates eb with replaced by v2. Also recall the evaluation rules that define these strategies: e1 Lam eb v2 App e1 v v2 eb v Eval-app-value e1 Lam eb App e1 v eb v Eval-app-epr The actual questions are redacted, because we couldn t figure out something that wouldn t give away the actual midterm questions. However, we can say: the actual questions have the eact same preamble the rules and tet above, and they fit on the remainder of this page. 9

Question 5 30 points: Demonic Nondeterminism 2 Some languages provide a feature that, given a list of epressions, evaluates one of them at random well, pseudorandomly, if you want to get precise about it. In this question, we etend the concrete synta of the Fun language to include Choose blocks: E ::=... {Choose E E...} Here, E... means zero or more occurrences, so a Choose contains one epression the first E followed by zero or more epressions. We also etend the E define-type, and add an evaluation rule: define-type E. Choose epressions listof E? n 1 1 k n ek vk Eval-choose Choose e1 en vk In Eval-choose, just one of the epressions, the kth epression, is evaluated. Why just one? In the rule, k is a meta-variable, the same as n, so we can choose any integer k such that k is between 1 and n. However, this question deals with typing for Choose epressions, not with evaluation. The typing rules reused from Fun that you may need for this question are: Γ e : A Under assumptions Γ, epression e has type A Γ = A Γ Id : A Type-var Γ Bfalse : bool Type-false Γ Num n : num Type-num Γ Btrue : bool Type-true : A, Γ ebody : B Γ Lam A ebody : A B Type-lam Γ e1 : A B Γ : A Type-app Γ App e1 : B Γ e : bool Γ ethen : A Γ eelse : A Γ Ite e ethen eelse : A Type-ite Now we add a rule for Choose: n 1 Γ e1 : A Γ en : A Γ Choose e1 en : A Type-choose Similar to Type-ite, Type-choose requires all the epressions to have the same type A because we don t know, statically, which epression will be chosen for evaluation. 2 Actual technical term. Yes. Really. 10

Question 5 30 points: Demonic Nondeterminism, continued Q5a 15 points Complete the following derivation. 2 1 : num, Bfalse : bool Type-false : num, Btrue : bool Type-true : num, Choose Bfalse Btrue : bool Lam num Choose Bfalse Btrue : num bool Type-choose Type-lam Q5b 20 points Implement the above rule by filling in the type-case branch for Choose in the typeof function: define typeof tc e ; typeof : Typing-contet E > or false Type type-case E e ;... Ite e ethen eelse and Tbool? typeof tc e let AThen typeof tc ethen AElse typeof tc eelse and type=? AThen AElse AThen ;... Choose echoices if empty? echoices #false let A typeof tc first echoices if empty? rest echoices A and type=? A typeof tc Choose rest echoices A 11