16
λ-Calculus - Turing machines are basic symbol-manipulating mechanisms which define a model of computation These machines were described by Alan Turing in 1936. - Von Neumann architecture refers to a computer design model that uses a single storage structure to hold both instructions and data, this kind of computer implements a Turing Machine. -There are different ways one can calculate functions. Alonzo Church -The Church–Turing thesis stated that Turing machines and recursive functions are equivalent. -"Every 'function which would naturally be regarded as computable' can be computed by a Turing machine."-Alonzo Church - Because any computer program can be translated into a Turing machine, and any Turing machine can be translated into any general-purpose programming language, we know that any general- purpose programming language is sufficient to express any algorithm. Calculus -The goal of calculus is to simplify the notion of computable functions. -A calculus is a notation that can be manipulated mechanically to achieve some end. -One reason for developing a calculus is that, by reducing some process to a set of simple mechanical rules, one decreases the chance of making errors. Scope -Scope : where a variable is visible to a range of statements or where the variable can be accessed. - is called the binding operator. -Binding : declared in, accessible, etc -Bound Identifiers : are often called bound variables

COP4020.Lecture11.Spring2014.LambdaCalculus

  • Upload
    brian

  • View
    2

  • Download
    0

Embed Size (px)

DESCRIPTION

lambda calculus

Citation preview

Group 2 Notes

-Calculus - Turing machines are basic symbol-manipulating mechanisms which define a model of computation These machines were described by Alan Turing in 1936.

- Von Neumann architecture refers to a computer design model that uses a single storage structure to hold both instructions and data, this kind of computer implements a Turing Machine.

-There are different ways one can calculate functions.

Alonzo Church

-The ChurchTuring thesis stated that Turing machines and recursive functions are equivalent.

-"Every 'function which would naturally be regarded as computable' can be computed by

a Turing machine."-Alonzo Church

- Because any computer program can be translated into a Turing machine, and any Turing machine can be translated into any general-purpose programming language, we know that any general-purpose programming language is sufficient to express any algorithm.Calculus -The goal of calculus is to simplify the notion of computable functions.-A calculus is a notation that can be manipulated mechanically to achieve some end.

-One reason for developing a calculus is that, by reducing some process to a set of simple mechanical rules, one decreases the chance of making errors.

Scope

-Scope: where a variable is visible to a range of statements or where the variable can be accessed.

- is called the binding operator.

-Binding: declared in, accessible, etc

-Bound Identifiers: are often called bound variables

Ex: i is the bound identifier

-bound identifiers can change without altering the meaning of a formula.

-If we change a free variable we change the meaning of the expression

-The binding site (i.e. ) for an identifier determines the scope of the identifier

Ex:

{x | x > 0} = {y | y > 0}

(x[x + 1 > x] = (y[y + 1 > y]

Ex:

-bound Identifier is j

-free variable is I

-j is bound by the summation operation (() which is called the binding site Scope Ex:

Scope of x

_|_

{x | x > y }

| | |

| | Free occurrence of y

| Bound occurrence of x

Binding site of x

Ex:

Program

|Var x

|Procedure

_

||Var y

|

||

|______Scope of y

||

|

||_

_|

|End

|

|_

EndNested Scopes Ex:

| |________ |

| | |

| Scope of j |

|______________|

|

Scope of I

-Calculus--Calculus is a theory of functions developed by Alonzo Church.

-In 1920 Moses Schonfinkel developed a theory of functions base of combinations

-In 1930 Haskell Curry rediscovered and extended Schonfinkels theory and showed that it was equivalent to -Calculus.

-In 1930 Alonzo Church developed -Calculus.

-In 1950 S.C. Kleene showed that -Calculus was a universal computing system(any computable function can be expressed and evaluated using Calculus).

-In 1950 John Mccarthy who was inspired by -Calculus, invented the programming language LISP.

- -Calculus is equivalent to Turing machines, but -Calculus emphasizes the use of transformation rules and doesnt care about the actual machine implementing them.

- -Calculus is more related to software than hardware.

-The -Calculus is a notation for defining functions.

-Any computable function can be expressed and evaluated using -Calculus.

-The central concept in -Calculus is the expression.

-Each expression denotes a function.

-0 is a -expression to represent the number zero.

-There are just 3 kinds of -expression and using BNF, the syntax of -expressions are:

-If V ranges over the class of and E, E1, E2 range over the syntax of class , then the BNF simplifies to:

-Variable

-Application combination (Procedure call)

-Abstraction

Ex:

- denotes the identity function

-Functions can be applied to expressions - [5/x] means that all occurrences of x in the expression must be replaced by the number 5.

Note that the function:

int function timesthree(x: int) { timesthree = 3 * x } can be expressed in -claculus as:

|

| |

x . * 3 x ( x.* 3 x-Function applications are evaluated by substituting the value of the argument:

(( x.x) E ) ( (x)[E/x] = EEx:(x. (x) E

If we apply

- Arguments are taken from left to right.- The meaning of [E/x] = all occurrences of x are substituted by E in the expression to the left.

Ex:

- Arguments are taken from left to right. Conversion Rules

-An arithmetic expression like (2+3)*5 can be represent as a -expression and its value 25 can also be represented as a -expression.

-The process of simplifying (2+3)*5 is called conversion or reduction.

1)Renaming Rule((-conversion or (-reduction)

-one expression may be replace to another by changing a bound identifier throughout its scope to any other identifier that doesnt occur within that scope.

One Abstraction of the form v.E can be converted to v.E[v/v] provided that the substitution v for v is valid in EEx:

Ex:

Ex:

Substitution Rule (-Reduction or -Conversion)

Any application of the form (V.E1)E2 can be converted to E1 [E2/V] provided the substitution of V in E1 is valid. -Reduction is like the evaluation of a function call in a programming language in (V.E1)E2 the body of the function V.E1 is evaluated in an environment in which the formal parameter V is bound to the actual parameter E2Ex:

Ex:

Non-valid Examples:

Ex1:

(x.y.add xy)(Square y) (

(y.add (Square y)y)

(y.add xy)[(Square y)/x] is not valid because y is free in (Square y) but becomes bound after substitution for x in (x.y.add xy)Ex2:

x((y. y + 2 ) x) + y

y is bound in y + 2 but free in the last occurrence. valid Examples:Ex:

Ex:

Ex:

In -expressions all functions have one variable

Functions of several variables may be expressed as a function of one variable through currying.

With currying we can input one variable at a time into separate functions

The first function takes the first argument, x, and returns a function that will take the second variable, y, and will in turn provide the desired output to create the same function by currying

F:z( (z(z) G:z(z

F maps integers to a function while G maps integers to integers

F(x) returns a function G(x) that provides the appropriate result when supplied with y

F(2) ( G2 when G2(y) ( 2+y

F(2)(3) ( G2(3) ( 2+3 ( 5

In -calculus this function is described as (x.y.x+y)

For function applications

To be able to program significant function in -calculus, it is convenient to have a way to attach names to -expressions

Ex: Plusp ( ( x.x 0)

Minusp ( ( x.x < 0)

Succ ( ( x.x+1)

Square ( ( x.x*x)

Minusp(Succ(2)) (( x.x < 0)(Succ(2)) ((Succ(2) < 0) ((( x.x+1)2 < 0) ( ((2+1) < 0) ( 3 < 0 ( False -Expressions to handle truth valuesFirst of all let us construct a IF expression:

IF ::= c.i.e. c i e where c stands for Condition, i for if and e for elseNow we will introduce the function TRUE which selects the first argument:

TRUE ::= x.y.x

And the function FALSE selects the second argument:

FALSE ::= x.y.y

Example:

TRUE u w ( x.y.x) u w( (y.x) [u/x] w ( (y.u) w ( u[w/y] = u

FALSE u w ( x.y.y) u w( (y.y) [u/x] w ( (y.y) w ( y[w/y] = w

Now thet we have define TRUE and FALSE we can work with the IF function:

If (TRUE) 1 2 ( (c.i.e. c i e) TRUE 1 2

( i.e. c i e) [TRUE/c] 1 2

( i.e. TRUE i e) 1 2

( e. TRUE i e) [1/i] 2 ( e. TRUE 1 e) 2

( TRUE 1 e) [2/e] ( TRUE 1 2 ( x.y.x) 1 2

( (y.x) [1/x] 2 ( (y.1) 2 ( 1[2/y] = 1

Exercise:

If (FALSE) 1 2 ( (c.i.e. c i e) FALSE 1 2

Boolean values and logical operators:

TRUE ::= x.y.x

FALSE ::= x.y.y

AND ::= x.y.x y (t.f.f)

OR ::= x.y.x (t.f.f) y

NOT ::= x.x (u.v.v) (a.b.a)

Example:

NOT TRUE = x.x (u.v.v) (a.b.a) TRUE

( x (u.v.v) (a.b.a) [TRUE/x]

( TRUE (u.v.v) (a.b.a)

( x.y.x (u.v.v) (a.b.a)

( y.x [u.v.v/x] (a.b.a)

( y.(u.v.v) (a.b.a)

( (u.v.v) [a.b.a/y]

( u.v.v = FALSE

Numerals:

Representing integers as -abstractions:

We define a function that calls another function an specific number of times.

0 =Zero = f.x.x

1 = One = f.x.f x

2 = Two = f.x.f (f x)

3 = Three = f.x.f (f (f x))

The abstraction takes two arguments, say s and x. In general x = ZERO

Example:

ZERO s ZERO

(f.x.x) s ZERO

( x.x[s/f] ZERO

( x.x ZERO ( x[ZERO/x] ( ZERO = f.x.x

Exercise:

TWO s ZERO

The successor function:

S = Successor = n.f.x.f (n f x)

Successor ZERO = n.f.x.f (n f x) ZERO

( f.x.f (n f x) [ZERO/n]

( f.x.f (f.x.x f x)

( f.x.f (x.x [f/f] x)

( f.x.f (x.x x)

( f.x.f (x [x/x])

( f.x.f (x) =f.x.f x = 1

Exercise:

Successor ONE The Church numerals that follow just have additional applications of the successor function:2 = C2 = f.x.f (f x)

3 = C3 = f.x.f (f (f x))

4 = C4 = f.x.f (f (f ( f x)))

n = Cn = f.x.f n x

Addition:Ci + j = f.x. Ci f (Cj f x)

Example:

C2 + 3 = f.x. C2 f (C3 f x)

f.x. f2.x2.f2 (f2 x) f (C3 f x)

f.x. f2.x2.f2 (f2 x2) [f/f2] (C3 f x)

f.x. x2.f (f x2) (C3 f x)

f.x. f (f x2) [x2/(C3 f x)]

f.x. f (f (C3 f x)) f.x. f (f (f3.x3.f3(f3 (f3 x3)) f x)) f.x. f (f (x3.f3(f3 (f3 x3)) [f/f3] x))f.x. f (f (x3.f(f (f x3)) x))f.x. f (f (x3.f(f (f x3)) [x/x3]))f.x. f (f (f(f (f x)))) = C5 = 5Now, we can define a function add that takes two Church numerals M and N and returns the

sum of them:add ::= M.N. f.x. M f (N f x)

add C2 C3 = C5Multiplication:

Ci * j = f.x. Ci (Cj f ) x

multiply ::= M.N. f.x. N (M f ) x

multiply C2 C3 = C6Recursion:

The Y fixed point combinatory.

The Y combinator was proposed by Curry Haskell.

Y combinatory is a fixed point combinatory or fixed point operator.A fixed point combinatory is a higher order function that computes a fixed point of other unction.

A fixed point of a function f is a value such that f (x) = x.

In -calculus, fixed points may be defined with the fixed point operator Y.

Y = f.(x.f (x x)) (x.f(x x))

The omega combinatory has a useful generalization called the fixed-point combinator (Y). It is easy to prove that Y is a fixed-point combinator.Let E be an arbitrary -term and let us apply Y to E:

Y E = f.(x.f (x x)) (x.f(x x)) E

= > (x.f (x x)) (x.f(x x)) [ E/f ]

= > (x.E (x x)) (x.E (x x))

= > (x.E (x x)) (x.E (x x))

= > E (x x) [ (x.E (x x)) /E]

= > E (x x) (x.E (x x))

= > E ( (x.E (x x)) (x.E (x x)) ) = > E ( Y E )Thus we have proved that Y E = E ( Y E ) --- Y E is a fixed point of the function E.One can use this definition to repeatedly unfold Y E.

Y E = E ( Y E ) = E (E ( Y E ) ) = E (E (E ( Y E ) ) ) = . . . . .

This might look like another form of an infinite loop, but it is actually quite useful to encode recursive functions if the explicit condition to stop the function is incorporated.

For example we can define factorial by:

factorial = n.If (= n 1) 1

( * n factorial (- n 1))

This is a circular definition and can not be expressed in -calculus. But to resolve the difficulty we can use the Y combinator.

First we define factorial as:

define factorial n = If (= n 1) 1 ( * n (f (- n 1)))

then, using -notation we could define E as:

define E = fn.If (= n 1) 1

( * n (f (- n 1)))

Now we can compute factorial using the Y combinator using: factorial = Y E. For example 2!.factorial 2 = (Y E) 2 = E (Y E) 2

= (fn.If (= n 1) 1 ( * n (f (- n 1))))(Y E) 2

= (n.If (= n 1) 1 ( * n (f (- n 1)))) [Y E/f] 2

= (n.If (= n 1) 1 ( * n (Y E (- n 1)))) 2

= (If (= n 1) 1 ( * n (Y E (- n 1)))) [2/n]

= If (= 2 1) 1 ( * 2 (Y E (- 2 1)))

= ( * 2 (Y E ( 1 ))) = ( * 2 1 ) = 2

note that:

(Y E) 1 = E (Y E) 1

= If (= 1 1) 1 ( * 2 (Y E (- 1 1)))

= 1

In general all recursive function definitions are represented in -calculus as application of the Y combinator.This gives the -calculus the power of turing machine computations.

Y is not the only combinator that finds fixed points of a function. However, this particular fixed-point operator play an important role in the history of -calculus.Notice the behavior of the divergent combinator (omega):

= (x.x x) (x.x x) => ( x x ) [(x.x x)/x] => (x.x x) (x.x x)