Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

Similar documents
Programming Languages

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

The Lambda Calculus. Stephen A. Edwards. Fall Columbia University

Type Systems. Lecture 2 Oct. 27th, 2004 Sebastian Maneth.

Lambda-Calculus (cont): Fixpoints, Naming. Lecture 10 CS 565 2/10/08

Type Systems. Today. 1. What is the Lambda Calculus. 1. What is the Lambda Calculus. Lecture 2 Oct. 27th, 2004 Sebastian Maneth

Models of computation

Lambda-Calculus (I) 2nd Asian-Pacific Summer School on Formal Methods Tsinghua University, August 23, 2010

Extending the Lambda Calculus: An Eager Functional Language

3.2 Equivalence, Evaluation and Reduction Strategies

Typing λ-terms. Types. Typed λ-terms. Base Types. The Typing Relation. Advanced Formal Methods. Lecture 3: Simply Typed Lambda calculus

Typed Arithmetic Expressions

COMP6463: λ-calculus

λ Slide 1 Content Exercises from last time λ-calculus COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

CSE 505, Fall 2009, Midterm Examination 5 November Please do not turn the page until everyone is ready.

Element x is R-minimal in X if y X. R(y, x).

Lambda Calculus. Andrés Sicard-Ramírez. Semester Universidad EAFIT

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

Categories, Proofs and Programs

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

Programming Language Concepts: Lecture 18

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Introduction to λ-calculus

Principles of Programming Languages

Introduction to lambda calculus Part 2

CSE 505, Fall 2005, Midterm Examination 8 November Please do not turn the page until everyone is ready.

Type Systems Winter Semester 2006

CMSC 631 Program Analysis and Understanding Fall Type Systems

1. Object Calculus. Object calculus is to OO languages what lambda calculus is to functional languages

Functional Programming with Coq. Yuxin Deng East China Normal University

Recursion Theorem. Ziv Scully Ziv Scully Recursion Theorem / 28

Lecture 2. Lambda calculus. Iztok Savnik, FAMNIT. March, 2018.

(2) (15pts) Using Prolog, implement a type-checker for the following small subset of System F:

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

Simply Typed Lambda-Calculi (II)

CIS 500 Software Foundations Final Exam Answer key December 20, 2004

CS294-9 September 14, 2006 Adam Chlipala UC Berkeley

Lambda Calculus! Gunnar Gotshalks! LC-1

CMSC 336: Type Systems for Programming Languages Lecture 10: Polymorphism Acar & Ahmed 19 February 2008

First-Order Logic First-Order Theories. Roopsha Samanta. Partly based on slides by Aaron Bradley and Isil Dillig

Domain theory and denotational semantics of functional programming

Static Program Analysis

ITP Programming Languages Lambda Calculus Lesson 0 Functional Programming and Lambda Calculus Lesson 1 Introduction to the Lambda Calculus

Hoare Logic: Reasoning About Imperative Programs

Jones-Optimal Partial Evaluation by Specialization-Safe Normalization

Type Inference. For the Simply-Typed Lambda Calculus. Peter Thiemann, Manuel Geffken. Albert-Ludwigs-Universität Freiburg. University of Freiburg

Reasoning About Imperative Programs. COS 441 Slides 10b

Equivalent Computers. Lecture 39: Lambda Calculus. Lambda Calculus. What is Calculus? Real Definition. Why?

Origin in Mathematical Logic

Simply Typed Lambda Calculus

Lecture 3. Lambda calculus. Iztok Savnik, FAMNIT. October, 2015.

Tutorial on Semantics Part I

Type Systems Winter Semester 2006

Type Systems. Lecture 9: Classical Logic. Neel Krishnaswami University of Cambridge

Consequence Relations and Natural Deduction

Operational Semantics

Formal Techniques for Software Engineering: Denotational Semantics

Models of Computation,

Continuations, Processes, and Sharing. Paul Downen, Luke Maurer, Zena M. Ariola, Daniele Varacca. September 8, 2014

Logic and Probability Lecture 3: Beyond Boolean Logic

1 Problem 1. (20 pts)

Lecture 2: Self-interpretation in the Lambda-calculus

Charles Wells 1. February 25, 1999

Computability Theory. CS215, Lecture 6,

Semantics of Higher-Order Functional Programming

Non deterministic classical logic: the λµ ++ -calculus

Programming Languages

Beyond First-Order Logic

Introduction to lambda calculus Part 6

Denotational semantics

CS156: The Calculus of Computation

Lecture Notes on Data Abstraction

CBV and CBN. Eduardo Bonelli. TP para LP 2012C1 1/55

A λ-calculus with Constants and Let-blocks

Informal Statement Calculus

Programming Language Concepts: Lecture 16

The Lambda-Calculus Reduction System

Quantum Functional Programming Language & Its Denotational Semantics

Computational Models Lecture 8 1

Computational Models Lecture 8 1

Elaborating evaluation-order polymorphism

CSE 311 Lecture 28: Undecidability of the Halting Problem. Emina Torlak and Kevin Zatloukal

Axiomatic Semantics. Lecture 9 CS 565 2/12/08

an efficient procedure for the decision problem. We illustrate this phenomenon for the Satisfiability problem.

Simple Type Extensions

The Knaster-Tarski Fixed Point Theorem for Complete Partial Orders

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 10 Nancy Lynch

The complexity of recursive constraint satisfaction problems.

Logic. Propositional Logic: Syntax. Wffs

Undecibability. Hilbert's 10th Problem: Give an algorithm that given a polynomial decides if the polynomial has integer roots or not.

Origin in Mathematical Logic

Refined Environment Classifiers

CS558 Programming Languages

Functional Programming with F# Overview and Basic Concepts

The Safe λ-calculus. William Blum. Joint work with C.-H. Luke Ong. Lunch-time meeting, 14 May Oxford University Computing Laboratory

(a) Definition of TMs. First Problem of URMs

Advanced Lambda Calculus. Henk Barendregt & Giulio Manzonetto ICIS Faculty of Science Radboud University Nijmegen, The Netherlands

Nunchaku: Flexible Model Finding for Higher-Order Logic

PROOFS IN PREDICATE LOGIC AND COMPLETENESS; WHAT DECIDABILITY MEANS HUTH AND RYAN 2.3, SUPPLEMENTARY NOTES 2

Transcription:

CSE 230: Winter 2007 Principles of Programming Languages Lecture 12: The λ-calculus Ranjit Jhala UC San Diego Review The lambda calculus is a calculus of functions: e := x λx. e e 1 e 2 Several evaluation strategies exist based on β-reduction: (λx.e) e β [e /x] e The Diamond Property Languages defined by nondeterministic rules: Logic programming languages Expert systems Constraint satisfaction systems Make Useful to know if systems have diamond property! If so, a unique result is guaranteed Equality = β : symmetric, reflexive, transitive closure of β = β is ( β β ) * So e 1 = β e 2 if e 1 converts to e 2 via a seq of forward and backward β : e 1 e 2 The Church-Rosser Theorem If e 1 = β e 2 then exists e s.t. e 1 β* e and e 2 β* e e Proof (informal): apply the diamond property as many times as necessary. e 1 e 2 Corollaries If e 1 = β e 2 and e 1, e 2 are normal forms then e 1 e 2 Proof: From CR we have e. e 1 * β e and e 2 * β e As e 1, e 2 are normal forms they are e If e * β e 1 and e * β e 2 and e 1, e 2 are normal forms then e 1 e 2 Proof? Significance: Each term reduces to one normal form 1

Evaluation Strategies Q: Which β-redex should be picked? Good news, Church-Rosser Theorem: independent of strategy, there is at most one normal form Bad news, some strategies may fail to find a normal form: (λx. y) ((λy.y y) (λy.y y)) (λx. y) ((λy.y y) (λy.y y)) (λx. y) ((λy.y y) (λy.y y)) y Normal-Order Reduction outermost redex: a redex not contained inside another redex (λe. λf. e) ((λa.λb. a) x y) ((λc.λd. c) u v) Outermost: We consider three strategies: normal call-by-name call-by-value Normal order: leftmost outermost redex first Theorem: If e has a normal form e then normal order reduction will reduce e to e. Weak vs Strong Reduction In most PL, functions considered fully evaluated Weak Reduction: No reduction done under lambdas i.e. inside a function body Strong Reduction: Reduction is done under lambdas Partial evaluation of function, other optimizations Normal order reduces under lambda λx.((λy.y y) (λy.y y)) λx.((λy.y y) (λy.y y)) Not always desired Call-by-Name Reduction Don t reduce under λ Don t evaluate the argument to a function call Directly substitute; evaluate when reducing body Demand-driven an expression is not evaluated unless needed in body Normalizing it converges whenever normal order converges but does not always evaluate to a normal form Call-by-Name Example (λy. (λx. x) y) ((λu. u) (λv. v)) β (λy. y) ((λu. u) (λv. v)) β (λu. u) (λv. v) β λv. v Call-by-Value Reduction Don t reduce under lambda Do evaluate argument to function call Most languages are call-by-value Not normalizing (λx. y) ((λy.y y) (λy.y y)) diverges, but normal order (or CBN) converges 2

Call-by-Value Example: Evaluate arg (λy. (λx. x) y) ((λu. u) (λv. v)) β (λy. (λx. x) y) (λv. v) β (λy. y) (λv. v) β λv. v CBV vs. CBN Call-by-value: Easy to implement May diverge Call-by-name: More difficult to implement must pass unevaluated expressions Args multiply-evaluated inside function body Simpler theory than call-by-value Terminates more often (always if normal form exists) e.g. if arg is non-terminating, but not used Various other reduction strategies Programming with the λ-calculus How does the λ-calculus relate to real programming languages? Bools? If-then-else? Integers? Recursion? Functions: well, those we have Functional Programming λ-calculus = prototypical functional PL: no side effects, several evaluation strategies, lots of functions, nothing but functions Q: How can we program with functions? Are they a sufficient abstraction? Functional Programming Lisp, Scheme, ML, Haskell Pure: No locations, update, (side) effects Functions as args to/results from other functions: Higher-order programming Some impure functional languages permit side-effects (e.g., Lisp, Scheme, ML, OCaml) references (pointers), arrays, exceptions Variables in Functional Languages We can introduce new variables: let x = e 1 in e 2 x is bound by let i.e., x is statically scoped in e 2 essentially like (λx. e 2 ) e 1 Variables are never updated just names for expressions e.g., x is a name for the value denoted by e 1 in e 2 Equivalent to meaning of let in mathematics 3

Why? Referential Transparency Enables reasoning equationally, by substitution: let x = e 1 in e 2 [e 1 /x]e 2 Imp. langs: side-effects in e 1 invalidate equation evaluation of e_1 can alter semantics of e_2 Expressiveness of λ-calculus λ calculus can express: data types (ints, bools, pairs, lists, trees, ) branching Recursion (f e_1) and (f e_2) can produce different results even if e_1 and e 2 evaluate to the same thing! FP: function s behavior depends only on arguments Doesn t matter how function was called/used before! No state, like a mathematical function Localizes, simplifies understanding, reasoning about FP Enough to encode Turing machines Corollary: e = β e is undecidable But how to encode using only λ? Idea: encode the behavior of values Encoding Booleans in λ-calculus Q: What can we do with a boolean? A: Make a binary choice Q: So, how can you view this as a function? A: Bool = fun that takes two choices, returns one true = def λx. λy. x false = def λx. λy. y if E 1 then E 2 else E 3 = def E 1 E 2 E 3 Example: if true then u else v is (λx. λy. x) u v β (λy. u) v β u Boolean Operations Boolean operations: not Function takes b: returns function takes x,y: returns opposite of b s return not = def λb.(λx.λy. b y x) Boolean operations: or Function takes b 1, b 2 : returns function takes x,y: returns (if b 1 then x else (if b 2 then x else y)) or = def λb 1.λb 2.(λx.λy. b 1 x (b 2 x y)) Encoding Pairs (and so, Records) Q: What can we do with a pair? A: We can select one of its elements Pair: function takes a bool, returns the left or the right element mkpair e 1 e 2 = def λb. b e 1 e 2 Note: pair encoded as λ-abstraction, waiting for bool fst p = def p true snd p = def p false Ex: fst (mkpair x y) (mkpair x y) true true x y x Encoding Natural Numbers Q: What can we do with a natural number? A: Iterate a number of times over some function Nat: function that takes fun f, starting value s: returns: f applied to s a number of times 0 = def λf. λs. s 1 = def λf. λs. f s 2 = def λf. λs. f (f s) M Called Church numerals, unary representation Note: (n f s) : apply f to s n times, i.e. f n (s) 4

Operating on Natural Numbers Testing equality with 0 iszero n = def n (λb. false) true iszero = def λn.(λ b.false) true The successor function succ n = def λf. λs. f (n f s) succ = def λn. λf. λs. f (n f s) Addition add n 1 n 2 = def n 1 succ n 2 add = def λn 1.λn 2. n 1 succ n 2 Multiplication mult n 1 n 2 = def n 1 (add n 2 ) 0 mult = def λn 1.λn 2. n 1 (add n 2 ) 0 Ex: Computing with Naturals What is the result of add 0? (λn 1. λn 2. n 1 succ n 2 ) 0 β λn 2. 0 succ n 2 = λn 2. (λf. λs. s) succ n 2 β λn 2. n 2 = λx. x Ex: Computing with Naturals mult 2 2 2 (add 2) 0 (add 2) ((add 2) 0) 2 succ (add 2 0) 2 succ (2 succ 0) succ (succ (succ (succ 0))) succ (succ (succ (λf. λs. f (0 f s)))) succ (succ (succ (λf. λs. f s))) succ (succ (λg. λy. g ((λf. λs. f s) g y))) succ (succ (λg. λy. g (g y))) * λg. λy. g (g (g (g y))) = 4 Encoding Recursion Write a function find that: takes predicate P, natural n, returns smallest nat., larger than n, satisfying P find can encode all recursion but how to write it? Encoding Recursion find satisfies the equation: find p n = if p n then n else find p (succ n) Define: F = λf.λp.λn.(p n) n (f p (succ n)) A fixpoint of F is an x s.t. x = F x find is a fixpoint of F! as find p n = F find p n so find = F find Q: Given λ-term F, how to write its fixpoint? The Y-Combinator Define: Y = def λf. (λy.f(y y)) (λx. F(x x)) Called the fixpoint combinator because Y F β (λy.f (y y)) (λx. F (x x)) β F ((λx.f (x x))(λz. F (z z))) β F(YF) i.e. Y F = β F (Y F) Can write fixpoint for any λ-calculus function Define: F = λf.λp.λn.(p n) n (f p (succ n)) find = Y F 5

Whoa! Define: F = λf.λp.λn.(p n) n (f p (succ n)) and: find = Y F Whats going on? find p n = β Y F p n = β F (Y F) p n = β F find p n = β (p n) n (find p (succ n)) Fixpoint Combinators Y = def λf. (λy.f(y y)) (λx. F(x x)) How does this mix with Call-by-Value? Y F β (λy.f (y y)) (λx. F (x x)) β F ((λx.f (x x))(λz. F (z z))) β F (F ((λx.f (x x))(λz. F (z z)))) β F (F (F ((λx.f (x x))(λz. F (z z))))) β Many other fixpoint combinators Including those that work for CBV See upcoming HW Including Klop s Combinator: Y k = def (L L L L L L L L L L L L L L L L L L L L L L L L L L) where: L = def λaλbλcλdλeλfλgλhλiλjλkλlλmλnλoλpλqλsλtλuλvλwλxλyλzλr. r (t h i s i s a f i x p o i n t c o m b i n a t o r) Expressiveness of λ-calculus Encodings are fun but programming in pure λ-calculus is not Encodings complicate static analysis Know the λ-calculus encodes them, so we add 0,1,2,,true,false,if-then-else to language Next, we will add types 6