Foundations of Computer Science ENGR 3520 Fall 2013 Thursday, Nov 21, 2013

Σχετικά έγγραφα
2 Composition. Invertible Mappings

derivation of the Laplacian from rectangular to spherical coordinates

The Simply Typed Lambda Calculus

C.S. 430 Assignment 6, Sample Solutions

Section 8.3 Trigonometric Equations

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

Finite Field Problems: Solutions

EE512: Error Control Coding

Homework 3 Solutions

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

Every set of first-order formulas is equivalent to an independent set

Section 7.6 Double and Half Angle Formulas

forms This gives Remark 1. How to remember the above formulas: Substituting these into the equation we obtain with

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

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

4.6 Autoregressive Moving Average Model ARMA(1,1)

Math221: HW# 1 solutions

Problem Set 3: Solutions

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

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

TMA4115 Matematikk 3

Partial Differential Equations in Biology The boundary element method. March 26, 2013

Section 9.2 Polar Equations and Graphs

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

Concrete Mathematics Exercises from 30 September 2016

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

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

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

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

Matrices and Determinants

Lecture 2. Soundness and completeness of propositional logic

Example Sheet 3 Solutions

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

Second Order Partial Differential Equations

CRASH COURSE IN PRECALCULUS

Math 6 SL Probability Distributions Practice Test Mark Scheme

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

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

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

Areas and Lengths in Polar Coordinates

Solutions to Exercise Sheet 5

Inverse trigonometric functions & General Solution of Trigonometric Equations

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

Homework 8 Model Solution Section

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

Srednicki Chapter 55

Other Test Constructions: Likelihood Ratio & Bayes Tests

6.3 Forecasting ARMA processes

Areas and Lengths in Polar Coordinates

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

Jesse Maassen and Mark Lundstrom Purdue University November 25, 2013

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

Instruction Execution Times

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

From the finite to the transfinite: Λµ-terms and streams

ST5224: Advanced Statistical Theory II

T λx. (λy. x) F λx. (λy. y) if λpca. pca

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

Assalamu `alaikum wr. wb.

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

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

Statistical Inference I Locally most powerful tests

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

The challenges of non-stable predicates

PARTIAL NOTES for 6.1 Trigonometric Identities

A Lambda Model Characterizing Computational Behaviours of Terms

Bounding Nonsplitting Enumeration Degrees

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

λρ-calculus 1. each λ-variable is a λρ-term, called an atom or atomic term; 2. if M and N are λρ-term then (MN) is a λρ-term called an application;

CHAPTER 48 APPLICATIONS OF MATRICES AND DETERMINANTS

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

( ) 2 and compare to M.

Trigonometric Formula Sheet

D Alembert s Solution to the Wave Equation

Right Rear Door. Let's now finish the door hinge saga with the right rear door

Problem Set 9 Solutions. θ + 1. θ 2 + cotθ ( ) sinθ e iφ is an eigenfunction of the ˆ L 2 operator. / θ 2. φ 2. sin 2 θ φ 2. ( ) = e iφ. = e iφ cosθ.

Second Order RLC Filters

Notes on the Open Economy

Lecture 34 Bootstrap confidence intervals

MA 342N Assignment 1 Due 24 February 2016

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 7η: Consumer Behavior Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

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

Bayesian statistics. DS GA 1002 Probability and Statistics for Data Science.

Chapter 3: Ordinal Numbers

9.09. # 1. Area inside the oval limaçon r = cos θ. To graph, start with θ = 0 so r = 6. Compute dr

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

Econ Spring 2004 Instructor: Prof. Kiefer Solution to Problem set # 5. γ (0)

Finite difference method for 2-D heat equation

Pg The perimeter is P = 3x The area of a triangle is. where b is the base, h is the height. In our case b = x, then the area is

CHAPTER 101 FOURIER SERIES FOR PERIODIC FUNCTIONS OF PERIOD

10.7 Performance of Second-Order System (Unit Step Response)

1) Formulation of the Problem as a Linear Programming Model

( y) Partial Differential Equations

Strain gauge and rosettes

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

Example of the Baum-Welch Algorithm

A Note on Intuitionistic Fuzzy. Equivalence Relation

Durbin-Levinson recursive method

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

ORDINAL ARITHMETIC JULIAN J. SCHLÖDER

Transcript:

Foundations of Computer Science Lecture Notes ENGR 3520 Fall 2013 Thursday, Nov 21, 2013 λ-calculus λ-terms. A λ-term is either: A variable x, y, z,... λx.m M N (M) (where x is a variable and M a λ-term) (where M, N are λ-terms) (where M is a λ-term) Examples: x λx.x λy.(λx.x) λx.(x (λy.y)) Conventions: Application associates to the left, so that M N P should be understood as (M N) P. The body of a function λx.m goes as far to the right as possible. Thus, λx.λy.x should be understood as λx.(λy.x). The scope of λx in λx.m is all of M. A variable x is free in a λ-term if some use of x does not appear in the scope of a λx. Examples: y is free in λx.y; x is free in λy.(x (λx.x)); z is not free in λz.(λx.z) Substitution. Substitution: M[x N] x[x N] = N y[x N] = y (if x y) (M 1 M 2 )[x N] = M 1 [x N] M 2 [x N] (λx.m)[x N] = λx.m (λy.m)[x N] = λy.(m[x N]) (if y is not free in N) We can always rename bound variables to avoid conflict: 1

λx.m = λy.(m[x y]) if y is not free in M. (There is a question what happens when the substitution rules do not apply. For instance, (λx.y)[y x] is not defined, because x, the variable parameter in λx.y, is free in x, the term being substituted for y. One possibility is to return an error. Another possibility is to apply renaming to the problematic variable parameter to get (λz.y)[y x] which by the substitution rules above gives you λz.x. The latter approach is often called capture-avoiding substitution.) Reduction Rules. Reduction (or evaluation): M N (λx.m) N M[x N] M P N P (if M N) P M P N (if M N) λx.m λx.n (if M N) A term of the form (λx.m) N is called a redex. It is the one place where a reduction does any work. Examples: (λx.x) (λy.y) x[x λy.y] = λy.y ((λx.(λy.x)) z 1 ) z 2 (λy.x)[x z 1 ] z 2 = (λy.z 1 ) z 2 z 1 [y z 2 ] = z 1 ((λx.(λy.y)) (λz.z)) (λx.(λy.x)) (λy.y)[x λz.z] (λx.(λy.x)) = (λy.y) (λx.(λy.x)) y[y λx.(λy.x)] = λx.(λy.x) 2

The λ-term could be written more simply as using the conventions above. Similarly, could be written I will use the conventions fully from now on. ((λx.(λy.x)) z 1 ) z 2 (λx.λy.x) z 1 z 2 ((λx.(λy.y)) (λz.z)) (λx.(λy.x)) (λx.λy.y) (λz.z) (λx.λy.x) I will generally expand out substitutions immediately in reductions, skipping the intermediate step of showing what the substitution looks like. A λ-term is in normal form if there is no applicable reduction. Not every λ-term eventually reduces to a normal form: (λx.x x) (λx.x x) (λx.x x) (λx. x) (λx.x x) (λx. x)... There can be more than one redex in a λ-term, meaning that there may be more than one applicable reduction. For instance, ((λx.x) (λy.x)) ((λx.λy.x) z 1 z 2 ). A property of the λ- calculus is that all the ways to reduce a term to a normal form yield the same normal form (up to renaming of bound variables). This is called the Church-Rosser property. It says that the order in which we perform reductions to reach a normal form is not important. Encoding Booleans. Even though the lambda-calculus only has variables and functions, we can encode traditional data types within in. Boolean values encoding (à la Church): true = λx.λy.x false = λx.λy.y In what sense are these encodings of Boolean values? Booleans are useful because they allow you to select one branch or the other of a conditional expression. The trick is that when B reduces to either true or false, then B M N reduces either to M or to N, respectively: 3

If B true, then B M N true M N = (λx.λy.x) M N (λy.m) N M while if B false, then B M N false M N = (λx.λy.y) M N (λy.y) N N We can also define an explicit if expression: if = λc.λx.λy.c x y so that B M N could also be written if B M N. We can define logical operators: and = λm.λn.m n m or = λm.λn.m m n not = λm.λx.λy.m y x Thus: and true false = (λm.λn.m n m) true false (λn.true n true) false true false true = (λx.λy.x) false true (λy.false) true false not false = (λm.λx.λy.m y x) false (λx.λy.false y x) = (λx.λy.(λu.λv.v) y x) (λx.λy.(λv.v) x) (λx.λy.x) = true 4

Encoding Natural Numbers. 0 = λf.λx.x 1 = λf.λx.f x 2 = λf.λx.f (f x) 3 = λf.λx.f (f (f x)) 4 =... In general, natural number n is encoded as λf.λx.f n x. Successor operations: succ = λn.λf.λx.(n f) (f x) succ 1 = (λn.λf.λx.(n f) (f x)) (λf.λx.f x) λf.λx.((λf.λx.f x) f) (f x) λf.λx.(λx.f x) (f x) λf.λx.f (f x) = 2 Other operations: plus = λm.λn.λf.λx.(m f) (n f x) times = λm.λn.λf.λx.m (n f) x iszero? = λn.n (λx.false) true plus 1 2 = (λm.λn.λf.λx.(m f) (n f x)) 1 2 (λn.λf.λx.(1 f) (n f x)) 2 λf.λx.(1 f) (2 f x) = λf.λx.((λf.λx.f x) f) ((λf.λx.f (f x)) f x) λf.λx.(λx.f x) ((λf.λx.f (f x)) f x) λf.λx.f ((λf.λx.f (f x)) f x) λf.λx.f ((λx.f (f x)) x) λf.λx.f (f (f x)) = 3 5

times 2 3 = (λm.λn.λf.λx.m (n f) x) 2 3 (λn.λf.λx.2 (n f) x) 3 λf.λx.2 (3 f) x = λf.λx.(λf.λx.f (f x)) ((λf.λx.f (f (f x))) f) x λf.λx.(λf.λx.f (f x)) (λx.f (f (f x))) x λf.λx.(λx.(λx.f (f (f x))) ((λx.f (f (f x))) x)) x λf.λx.(λx.(λx.f (f (f x))) (f (f (f x)))) x λf.λx.(λx.f (f (f (f (f (f x)))))) x λf.λx.f (f (f (f (f (f x))))) = 6 iszero? 0 = (λn.n (λx.false) true) (λf.λx.x) (λf.λx.x) (λx.false) true (λx.x) true true iszero? 2 = (λn.n (λx.false) true) (λf.λx.f (f x)) (λf.λx.f (f x)) (λx.false) true (λx.(λx.false) ((λx.false) x)) true (λx.false) ((λx.false) true) false More difficult is defining a predecessor function, taking a nonzero natural number n and returning n 1. There are several ways of defining such a function; here is probably the simplest: pred = λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u) pred 2 = (λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u)) (λf.λx.f (f x)) λf.λx.(λf.λx.f (f x)) (λg.λh.h (g f)) (λu.x) (λu.u) λf.λx.(λx.(λg.λh.h (g f)) ((λg.λh.h (g f)) x)) (λu.x) (λu.u) 6

λf.λx.(λg.λh.h (g f)) ((λg.λh.h (g f)) (λu.x)) (λu.u) λf.λx.(λg.λh.h (g f)) (λh.h ((λu.x) f)) (λu.u) λf.λx.(λg.λh.h (g f)) (λh.h x) (λu.u) λf.λx.(λh.h ((λh.h x) f)) (λu.u) λf.λx.(λh.h (f x)) (λu.u) λf.λx.(λu.u) (f x) λf.λx.f x = 1 Note that pred 0 is just 0: pred 0 = (λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u)) (λf.λx.x) λf.λx.(λf.λx.x) (λg.λh.h (g f)) (λu.x) (λu.u) λf.λx.(λx.x) (λu.x) (λu.u) λf.λx.(λu.x) (λu.u) λf.λx.x = 0 Encoding Pairs. pair = λx.λy.λz.z x y first = λp.p (λx.λy.x) second = λp.p (λx.λy.y) first (pair 1 2) = (λp.p (λx.λy.x)) ((λx.λy.λz.z x y) 1 2) (λp.p (λx.λy.x)) ((λy.λz.z 1 y) 2) (λp.p (λx.λy.x)) (λz.z 1 2) (λz.z 1 2) (λx.λy.x) (λx.λy.x) 1 2 (λy.1) 2 1 second (pair 1 2) = (λp.p (λx.λy.y)) ((λx.λy.λz.z x y) 1 2) (λp.p (λx.λy.y)) ((λy.λz.z 1 y) 2) 7

(λp.p (λx.λy.y)) (λz.z 1 2) (λz.z 1 2) (λx.λy.y) (λx.λy.y) 1 2 (λy.y) 2 2 Recursion. With conditionals and basic data types, we are very close to having a Turingcomplete programming language (that is, one that can simulate Turing machines). All that is missing is a way to do iteration: loops. It turns out we can write recursive functions in the λ-calculus, which gives us loops. Consider factorial. Intuitively, we would like to define fact by fact = λn.(iszero? n) 1 (times n (fact (pred n))) but this is not a valid definition, since the right-hand side refers to the term being defined. It is really an equation, the same way x = 3x is an equation. Consider that equation, x = 3x. Define F (t) = 3t. Then, a solution of x = 3x is really a fixed-point of F, namely, a value t 0 for which F (t 0 ) = t 0. And F has only one fixed-point, namely t 0 = 0, which gives us the one solution to x = 3x, namely x = 0. Similarly, if we define F fact = λf.λn.(iszero? n) 1 (times n (f (pred n))) then we see that the definition that we re looking for is a fixed point of F fact, namely, a term f such that F fact f = f. Indeed, if we have such a term, then: f 3 = F fact f 3 = (λf.λn.(iszero? n) 1 (times n (f (pred n)))) f 3 (λn.(iszero? n) 1 (times n (f (pred n)))) 3 (iszero? 3) 1 (times 3 (f (pred 3))) times 3 (f (pred 3)) times 3 (f 2) = times 3 (F fact f 2) times 3 (times 2 (f 1)) = times 3 (times 2 (F fact f 1)) times 3 (times 2 (times 1 (f 1))) = times 3 (times 2 (times 1 (F fact f 1))) times 3 (times 2 (times 1 1)) 8

6 (The notation indicates that one or more reductions have taken place.) Thus, what we need is a way to find fixed points in the λ-calculus. The following function does just that: Y = λf.(λx.f (x x)) (λx.f (x x)) Y F gives us a fixed point of F. (Technically, Y F reduces in one step to a fixed-point of F ): Y F = (λf.(λx.f (x x)) (λx.f (x x))) F (λx.f (x x)) (λx.f (x x)) F ((λx.f (x x)) (λx.f (x x))) F (F ((λx.f (x x)) (λx.f (x x))))... So indeed, (λx.f (x x)) (λx.f (x x)) is a fixed point of F. We can use Y to define our factorial function: fact = Y F fact and we can check: fact 3 = Y F fact 3 = (λf.(λx.f (x x)) (λx.f (x x))) F fact 3 (λx.f fact (x x)) (λx.f fact (x x)) 3 F fact ((λx.f fact (x x)) (λx.f fact (x x))) 3 = F fact fact 3 = (λf.λn.(iszero? n) 1 (times n (f (pred n)))) fact 3 (λn.(iszero? n) 1 (times n (fact (pred n)))) 3 (iszero? 3) 1 (times 3 (fact (pred 3))) times 3 (fact (pred 3)) times 3 (fact 2) = times 3 (F fact fact 2) times 3 (times 2 (fact 1)) = times 3 (times 2 (F fact fact 1)) times 3 (times 2 (times 1 (fact 1))) = times 3 (times 2 (times 1 (F fact fact 1))) 9

times 3 (times 2 (times 1 1)) 6 where fact = (λx.f fact (x x)) (λx.f fact (x x)) is the fixed point of F fact, such that fact = F fact fact. 10