CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

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

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

CMSC 330: Organization of Programming Languages

CIS 500 Software Foundations Fall September 25

Type Systems Winter Semester 2006

Chapter 5: The Untyped Lambda Calculus

Concepts of programming languages

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.

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

CMSC 330: Organization of Programming Languages

Lecture 5: The Untyped λ-calculus

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

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

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CMSC 330: Organization of Programming Languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

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

Fundamentals and lambda calculus

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

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

CIS 500 Software Foundations Midterm I

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

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

More Lambda Calculus and Intro to Type Systems

The Untyped Lambda Calculus

dynamically typed dynamically scoped

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

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

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Exercise 1 ( = 18 points)

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

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Elixir, functional programming and the Lambda calculus.

Introduction to the Lambda Calculus

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Exercise 1 ( = 24 points)

Lambda Calculus. Lecture 4 CS /26/10

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Introduction to the λ-calculus

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

Exercise 1 (2+2+2 points)

An Introduction to the Lambda Calculus

The Untyped Lambda Calculus

More Lambda Calculus and Intro to Type Systems

Lambda Calculus. Variables and Functions. cs3723 1

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

Costly software bugs that could have been averted with type checking

CMSC 330: Organization of Programming Languages. Lambda Calculus

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Introduction to Lambda Calculus. Lecture 7 CS /08/09

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

CSE-321 Programming Languages 2012 Midterm

Programming Languages. Dr. Philip Cannata 1

Formal Systems and their Applications

Pure Lambda Calculus. Lecture 17

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Lambda Calculus as a Programming Language

CSE-321 Programming Languages 2010 Midterm

CSE 505: Concepts of Programming Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Programming Languages Fall 2014

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

CIS 500 Software Foundations Fall October 2

Introduction to the Lambda Calculus. Chris Lomont

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

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

CMSC330. Objects, Functional Programming, and lambda calculus

Lecture 9: Typed Lambda Calculus

CS 314 Principles of Programming Languages. Lecture 21

The Untyped Lambda Calculus

The Untyped Lambda Calculus

Lambda Calculus as a Programming Language

Functional Programming

CMSC330 Spring 2017 Midterm 2

Lambda Calculus. Lambda Calculus

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Getting Started with Haskell (10 points)

CSE505, Fall 2012, Midterm Examination October 30, 2012

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

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

Abstract register machines Lecture 23 Tuesday, April 19, 2016

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...

Programming Languages

The University of Nottingham

Exercise 1 ( = 22 points)

Transcription:

CSC173 Lambda Calculus 014 Please write your name on the bluebook. You may use two sides of handwritten notes. There are 90 possible points and 75 is a perfect score. Stay cool and please write neatly. 1. Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation): (((λx. λy. λz.((x y) z) λf. λa.(f a)) λi.i) λj.j) (((λx.λy.λz.((x y) z) λf.λa.(f a)) λi.i) λj.j) => ((λy.λz.((λf.λa.(f a) y) z) λi.i) λj.j) => (λz.((λf.λa.(f a) λi.i) z) λj.j) => ((λf.λa.(f a) λi.i) λj.j) => (λa.(λi.i a) λj.j) => (λi.i λj.j) => λj.j. Function Equivalence (10 min) Show that functions a) and b) are equivalent by applying each to some abstract argument <arg>. Only expand names of functions when really necessary (just before they re applied, and then only if their effect is not obvious). a) identity b) (self-apply (self-apply select-second)) a) (identity <argument>) =>... => <argument> b) where backslash la means λ: ((self-apply (self-apply select-second)) <argument>) =>... => (((self-apply select-second) (self-apply select-second)) <argument> =>... => (((select-second select-second) (select-second select-second)) <argument>) =>... => (((\la f \la s.s select-second) (select-second select-second))) ((\la s.s (select-second select-second) <argument>) =>... => {((select-second select-second) <argument>) =>... => (\la s.s <argument>) => <argument> or I d accept a derivation based on the fact that select-second applied to anything is identity! 1

3. Logical Operators (0 min) Recall we defined logical OR: def or = λx. λy.(((cond true) y) x), which simplified to def or = λx. λy.((x true) y). 3.1 (5 min) : Write a C-language-like x?y:z conditional expression or a simple if-then-else statement that gives the boolean function NOR (that is, (P Q)). Only use the variables x, y, the operator not, and the constant true in your conditional expression. x?(not true):(not y). OR, if x then (not true) else (not y) 3. (5 min) : Convert your expression for NOR into a λ-calculus cond expression: it should have two λs, one cond, and however many true, not, x, ys you need. One λx.λy. (((cond (not true)) (not y)) x). 3.3 (5 min) : Evaluate and simplify (you ll remove cond from) the inner body of this expression to get a λx. λy. (simple, i.e. no-cond, expression) for NOR. One def nor = λx. λy. ((x (not true)) (not y)) Whose inner body came from... (((cond (not true))(not y)) x) => (((λe. λf. la c.((c e) f) (not true)) (not y)) x) => ((λf. λc. (( c (not true)) f ) (not y)) x) => (λc. (( c (not true)) (not y) x) => ((x (not true) (not y)) 3.4 (5 min): Work out cases needed to prove a version of DeMorgan s law by model checking: To Prove: ( A B) = (A B). Using nor, that s NOR (( NOT X) (NOT Y)) = AND (X Y). For each case below apply your simplified expression for NOR from 3.3 to the two arguments and show it evaluates to the correct answer. Since the expression will involve strictly true, false, not, you can rewrite to use only true, false, which are select-first, select-second, and so do each case in one line. No need to expand to λ-level. Here s the first case for free: Let (X,Y) have values (T, T), so (skipping an obvious step) args to NOR are (F,F): (((not true) (not true)) (not (not true))) ==> ((false (not true)) (not false)) ==> (false false true) ==> true, which is right! Now you do the three cases for (T,F), (F,T), and (F,F) TF FT ((false (not true)) false) ==> (false false false) ==> false

FT TF ((true (not true)) false) ==> (true false false) ==> false FF TT ((true (not true)) false) ==> (true false false) ==> false Thus, indeed NOR (( NOT X) (NOT Y)) = AND (X Y). 4. Recursion (15 min) Given: Y = λf.(λx. f(x x)) (λx. f(x x)) and an abstracted high-level definition of factorial: fact = λf.λn. (if n = 0 then 1 else n * (f (n-1))). Now compute factorial of recursively using Y. You don t need to expand any operators except Y and fact (as above), and infix notation is OK (e.g. *3 would reduce to 6). I ll get you started (and finished) (Y fact) => =>...=> (hint: first thing to do is rewrite Y using its definition) (Y fact) => // replacing Y w/ encoding (λf.(λx.f (x x)) (λx.f (x x)) fact) => // beta-reduction: 1st f => fact (λx. fact(x x)) (λx.fact (x x)) => // beta-reduction: 1st x => λx.fact (x x) = (fact ((λx. fact(x x)) (λx.fact (x x)))) => // apply encoding for (Y fact) // ((λx. fact (x x)) (λx.fact (x x))) => (Y fact) => // we know this is the encoding for (Y fact) from 3rd line of work so far (fact (Y fact)) => // apply encoding for fact (λf.λn. if n = 0 then 1 else n * (f (n-1)) (Y fact) => // -reduction: 1st f -> (Y fact) (λn. if n = 0 then 1 else n * ((Y fact) (n-1))) => // beta-reduction: n -> if =0 then 1 else * ((Y fact) (-1)) => // apply if * ((Y fact) 1) =>...=> // evaluate if * 1 => // apply * 5. Number Representations (15 min) 5A. (10 min) Recall Scott s number encoding, which is best understood with applicative semantics, meaning evaluate arguments before applying functions. In particular (using compact notation for -argument functions) zero = λxy.y (i.e. select-second) = λxy.x(λxy.x(λxy.y)) (i.e. sel-1st sel-1st sel-nd). We saw that we can write the predecessor function for Scott as pred = λz.z (λp.p) 0. 3

Why? pred N first copies N to the front with first identity function λz.z. Now N is applied to the last two arguments: N s first λ- expression is sel-1st, which chooses arg1 (λp.p), and ignores arg (0). The identity function (λp.p) is thus substituted in place of N s first sel-1st s body (so the first sel-1st vanishes), and applied to the rest of the original N, yielding the representation for N-1. If N=0 (i.e. sel-nd), copying it up front and applying it to the two arguments as before selects arg (0). Now, using pred above as a template, give a similar-looking one-line implementation of iszero for Scott encoding. (Hint: use true, false.) 5B. (5 min) We wonder: Why do all three of our number representations basically use select-second for succ? Could we just swap the roles of select-first, select-second and get three new mirror-image number representations? (from on-line lectures) 5A: iszero = λz.z (λx. false) true 5B.: No reason, should work fine unless I m missing something: in fact I saw a version of Scott s encoding with this swap. 6. Continuations I (10 min) What gets displayed here? (let ((start #f)) (if (not start) (call/cc (lambda (cc) (set! start cc)))) (display "Going to invoke (start)\n") (start)) Going to invoke (start) Going to invoke (start)...%[forever] 7. Continuations II (10 min) An alternative interface for interacting with continuations consists of two procedures: Let s call them right-now and go-when and define them and some code: (define (right-now) (call-with-current-continuation (lambda (cc) (cc cc)))) 4

(define (go-when then) (then then)) ;;---------------- (define when #f) (define demox (lambda (n) (cond ((< n 4) (set! when (right-now)) (display "small") (newline)) (else (set! when (right-now)) (display "bye") (newline))))) A. (8 min) What gets printed if we now type the following, in order, at the listener? (demox 3): (demox 5): (go-when when): B. ( min). Describe in words what right-now and go-when do. Ans: > (demox 3) small > (demox 5) bye > (go-when when) bye Ans: right-now returns the current point (moment) in the computation. go-when jumps to some point (moment) returned by right-now. 5