Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Σχετικά έγγραφα
C.S. 430 Assignment 6, Sample Solutions

The Simply Typed Lambda Calculus

ST5224: Advanced Statistical Theory II

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

Example Sheet 3 Solutions

The λ-calculus. Lecturer: John Wickerson. Phil Wadler

derivation of the Laplacian from rectangular to spherical coordinates

Inverse trigonometric functions & General Solution of Trigonometric Equations

Other Test Constructions: Likelihood Ratio & Bayes Tests

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

2 Composition. Invertible Mappings

Section 8.3 Trigonometric Equations

How to register an account with the Hellenic Community of Sheffield.

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

4.6 Autoregressive Moving Average Model ARMA(1,1)

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

Matrices and Determinants

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

Potential Dividers. 46 minutes. 46 marks. Page 1 of 11

Homework 3 Solutions

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

About these lecture notes. Simply Typed λ-calculus. Types

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Uniform Convergence of Fourier Series Michael Taylor

Homomorphism in Intuitionistic Fuzzy Automata

Concrete Mathematics Exercises from 30 September 2016

Models for Probabilistic Programs with an Adversary

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

MathCity.org Merging man and maths

Instruction Execution Times

( ) 2 and compare to M.

THE CALL-BY-NEED LAMBDA CALCULUS, REVISITED

Section 7.6 Double and Half Angle Formulas

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

Approximation of distance between locations on earth given by latitude and longitude

Fractional Colorings and Zykov Products of graphs

TMA4115 Matematikk 3

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

CRASH COURSE IN PRECALCULUS

Section 9.2 Polar Equations and Graphs

F19MC2 Solutions 9 Complex Analysis

Local Approximation with Kernels

VBA ΣΤΟ WORD. 1. Συχνά, όταν ήθελα να δώσω ένα φυλλάδιο εργασίας με ασκήσεις στους μαθητές έκανα το εξής: Version ΗΜΙΤΕΛΗΣ!!!!

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Math 6 SL Probability Distributions Practice Test Mark Scheme

SOLUTIONS TO MATH38181 EXTREME VALUES AND FINANCIAL RISK EXAM

Formal Semantics. 1 Type Logic

SOLUTIONS TO MATH38181 EXTREME VALUES AND FINANCIAL RISK EXAM

SCITECH Volume 13, Issue 2 RESEARCH ORGANISATION Published online: March 29, 2018

Solution Series 9. i=1 x i and i=1 x i.

Trigonometry 1.TRIGONOMETRIC RATIOS

EE512: Error Control Coding

CHAPTER 101 FOURIER SERIES FOR PERIODIC FUNCTIONS OF PERIOD

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

w o = R 1 p. (1) R = p =. = 1

Μορφοποίηση υπό όρους : Μορφή > Μορφοποίηση υπό όρους/γραμμές δεδομένων/μορφοποίηση μόο των κελιών που περιέχουν/

Advanced Subsidiary Unit 1: Understanding and Written Response

Απόκριση σε Μοναδιαία Ωστική Δύναμη (Unit Impulse) Απόκριση σε Δυνάμεις Αυθαίρετα Μεταβαλλόμενες με το Χρόνο. Απόστολος Σ.

Μηχανική Μάθηση Hypothesis Testing

(C) 2010 Pearson Education, Inc. All rights reserved.

PARTIAL NOTES for 6.1 Trigonometric Identities

1) Abstract (To be organized as: background, aim, workpackages, expected results) (300 words max) Το όριο λέξεων θα είναι ελαστικό.

, -.

EE101: Resonance in RLC circuits

( y) Partial Differential Equations

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

Math221: HW# 1 solutions

ECE 308 SIGNALS AND SYSTEMS FALL 2017 Answers to selected problems on prior years examinations

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

Lecture 2. Soundness and completeness of propositional logic

A Lambda Model Characterizing Computational Behaviours of Terms

Bounding Nonsplitting Enumeration Degrees

Numerical Analysis FMN011

Divergence for log concave functions

MATHEMATICS. 1. If A and B are square matrices of order 3 such that A = -1, B =3, then 3AB = 1) -9 2) -27 3) -81 4) 81

Strain gauge and rosettes

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

Part III - Pricing A Down-And-Out Call Option

Jesse Maassen and Mark Lundstrom Purdue University November 25, 2013

UNIVERSITY OF CALIFORNIA. EECS 150 Fall ) You are implementing an 4:1 Multiplexer that has the following specifications:

Problem Set 3: Solutions

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

CE 530 Molecular Simulation

Χρειάζεται να φέρω μαζί μου τα πρωτότυπα έγγραφα ή τα αντίγραφα; Asking if you need to provide the original documents or copies Ποια είναι τα κριτήρια

Quadratic Expressions

Solutions to Exercise Sheet 5

Homework 8 Model Solution Section

Notes on the Open Economy

6.3 Forecasting ARMA processes

AME SAMPLE REPORT James R. Cole, Ph.D. Neuropsychology

An Inventory of Continuous Distributions

ΑΚΑ ΗΜΙΑ ΕΜΠΟΡΙΚΟΥ ΝΑΥΤΙΚΟΥ ΜΑΚΕ ΟΝΙΑΣ ΣΧΟΛΗ ΜΗΧΑΝΙΚΩΝ ΠΤΥΧΙΑΚΗ ΕΡΓΑΣΙΑ

26 28 Find an equation of the tangent line to the curve at the given point Discuss the curve under the guidelines of Section

Transcript:

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Dynamic types, Lambda calculus machines Apr 21 22, 2016 1 Dynamic types and contracts (a) To make sure you understand the operational semantics of dynamic types and exceptions, show the execution of the following program under the semantics of Section 1 of the Lecture 22 notes. g (λx. 42 + x) (λy. (y true) + 42) (λx. 42 + x) ((λx. 42 + x) true) + 42 (42 + true) + 42 Err + 42 Err (b) Modify the program from question (a) by adding appropriate error handlers (i.e., expressions of the form try e 1 catch x. e 2 to catch the type error and return the integer 42 as the final result of the program. There are multiple places in the program where you can insert an error handler to achieve the desired result. Show three variations and their executions. (Note that the semantics for the execution of your programs is from Section 2 of the Lecture 22 notes.) Here is a version where we add an error handler in the body of function f. let f = λx. (try (42 + x) catch z. 0) in let f = λx. (try (42 + x) catch z. 0) in g (λx. (try (42 + x) catch z. 0)) (λy. (y true) + 42) (λx. (try (42 + x) catch z. 0)) ((λx. (try (42 + x) catch z. 0)) true) + 42 (try (42 + true) catch z. 0) + 42 (try (Err 1) catch z. 0) + 42

0 + 42 Here s another version, where we add an error handler in the body of function g. let g = λy. (try (y true) catch z. 0) + 42 in let g = λy. (try (y true) catch z. 0) + 42 in let g = λy. (try (y true) catch z. 0) + 42 in g (λx. 42 + x) (λy. (try (y true) catch z. 0) + 42) (λx. 42 + x) (try (λx. 42 + x) true) catch z. 0) + 42 (try (42 + true) catch z. 0) + 42 (try (Err 1) catch z. 0) + 42 0 + 42 Finally, here is a version where we put the error handling code at the top level. try catch z. 42 try catch z. 42 try g (λx. 42 + x) catch z. 42 try (λy. (y true) + 42) (λx. 42 + x) catch z. 42 try (((λx. 42 + x) true) + 42) catch z. 42 try ((42 + true) + 42) catch z. 42 try ((Err 1) + 42) catch z. 42 try (Err 1) catch z. 42 (c) Modify the program from question (a) by adding appropriate dynamic type checks to raise the error as early as possible. When does your program detect the error? This version of the code adds dynamic type checks on all arguments and on results of function applications. let f = λx. if (is int? x) then 42 + x else raise 3 in let g = λy. if (is fun? y) then let y = (y true) in if (is int? y ) then y + 42 else raise 3 else Page 2 of 5

raise 3 in let a = in if (is int? a) then a else raise 3 The execution detects the error as soon as function f is invoked, i.e., is int? x evaluates to false when x is replaced with true. (d) Modify the program from question (a) by adding contracts that specify the types of the input and output of f and g. Show the execution of the modified program. let f = monitor(λx. 42 + x, is int? is int? ) in let g = monitor(λy. (y true) + 42, (is bool? is int? ) is int? ) in (e) To make sure you understand the operational semantics of contracts, show the execution of the following program. let f = monitor(λx. x + 1, is int? is int? ) in let g = monitor(λx. x (42) + 42, (is int? is int? ) is int? ) in let h = monitor(λx. λy. y 42, is int? ((is int? is bool? ) is bool? )) in h () (λz. z + 42) This answer is not yet done... 2 Lambda calculus machines Consider the following program in the language from the lecture notes (Lecture 23): ((((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43) 44 Show its evaluation trace under CC, SCC, CK and CEK semantics. Page 3 of 5

The CC machine: The SCC machine: ((((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43) 44, [ ] (((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43, [ ] 44 ((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42, ([ ] 43) 44 (λf. λg. λx. λy. f x + g y) λa. 0, (([ ] λb. 42) 43) 44 λg. λx. λy. (λa. 0) x + g y, (([ ] λb. 42) 43) 44 (λg. λx. λy. (λa. 0) x + g y) λb. 42, ([ ] 43) 44 λx. λy. (λa. 0) x + (λb. 42) y, ([ ] 43) 44 (λx. λy. (λa. 0) x + (λb. 42) y) 43, 44 λy. (λa. 0) 43 + (λb. 42) y, [ ] 44 (λy. (λa. 0) 43 + (λb. 42) y) 44, [ ] (λa. 0) 43 + (λb. 42) 44, [ ] (λa. 0) 43, [ ] + (λb. 42) 44 0, [ ] + (λb. 42) 44 0 + (λb. 42) 44 [ ] (λb. 42) 44 0 + [ ] 42 0 + [ ] 0 + 42 [ ] 42 [ ] ((((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43) 44, [ ] (((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43, [ ] 44 ((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42, ([ ] 43) 44 (λf. λg. λx. λy. f x + g y) λa. 0, (([ ] λb. 42) 43) 44 λf. λg. λx. λy. f x + g y, ((([ ] λa. 0) λb. 42) 43) 44 λa. 0, ((((λf. λg. λx. λy. f x + g y) [ ]) λb. 42) 43) 44 λg. λx. λy. (λa. 0) x + g y, ((([ ] λb. 42) 43) 44 λb. 42, ((((λg. λx. λy. (λa. 0) x + g y) [ ]) 43) 44 λx. λy. (λa. 0) x + (λb. 42) y, (([ ] 43) 44 43, (((λx. λy. (λa. 0) x + (λb. 42) y) [ ]) 44 λy. (λa. 0) 43 + (λb. 42) y, [ ] 44 44, (λy. (λa. 0) 43 + (λb. 42) y) [ ] (λa. 0) 43 + (λb. 42) 44, [ ] (λa. 0) 43, [ ] + (λb. 42) 44 λa. 0, ([ ] 43) + (λb. 42) 44 43, ((λa. 0) [ ]) + (λb. 42) 44 0, [ ] + (λb. 42) 44 (λb. 42) 44, 0 + [ ] λb. 42, 0 + ([ ] 44) 44, 0 + ((λb. 42) [ ]) 42, 0 + [ ] 42, [ ] Page 4 of 5

The SK machine (which is exactly analogous to the SCC machine, except the contexts are represented as continuations): ((((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43) 44, mt (((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42) 43, arg, 44, mt ((λf. λg. λx. λy. f x + g y) λa. 0) λb. 42, arg, 43, arg, 44, mt (λf. λg. λx. λy. f x + g y) λa. 0, arg, λb. 42, arg, 43, arg, 44, mt λf. λg. λx. λy. f x + g y, arg, λa. 0, arg, λb. 42, arg, 43, arg, 44, mt λa. 0, fun, λf. λg. λx. λy. f x + g y, arg, λb. 42, arg, 43, arg, 44, mt λg. λx. λy. (λa. 0) x + g y, arg, λb. 42, arg, 43, arg, 44, mt λb. 42, fun, λg. λx. λy. (λa. 0) x + g y, arg, 43, arg, 44, mt λx. λy. (λa. 0) x + (λb. 42) y, arg, 43, arg, 44, mt 43, fun, λx. λy. (λa. 0) x + (λb. 42) y, arg, 44, mt λy. (λa. 0) 43 + (λb. 42) y, arg, 44, mt 44, fun, λy. (λa. 0) 43 + (λb. 42) y, mt (λa. 0) 43 + (λb. 42) 44, mt (λa. 0) 43, +,, (λb. 42) 44, mt λa. 0, arg, 43, +,, (λb. 42) 44, mt 43, fun, λa. 0, +,, (λb. 42) 44, mt 0, +,, (λb. 42) 44, mt (λb. 42) 44, +, 0,, mt λb. 42, arg, 44, +, 0,, mt 44, fun, λb. 42, +, 0,, mt 42, +, 0,, mt 42, mt The answer for the CEK machine is not yet done... Page 5 of 5