welcome to programming languages...

Post on 25-Aug-2020

7 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Welcome to Programming Languages (ITP20005)

Video Lectures: • Recursion, Tree Recursion, Function Examples – Review

These topic above will be covered in our class, but we get to know them.• Available from www.cs61a.org, CS61A Course Fall 2014.• They are the best resources for your mid term exam.

Mon Sep 15Recursion

(Video) (1pp) (1pps) (8pp) (07.py)1.7

Wed Sep 17Tree Recursion

(Video) (1pp) (1pps) (8pp) (08.py)1.7

Fri Sep 19Function Examples

(Video) (1pp) (1pps) (8pp) (09.py)

ITP 20005 Programming Languages• Lambda Calculus

• Lesson 0 – Functional Programming and Lambda Calculus

• Lesson 1 – Introduction to the Lambda Calculus• Lesson 2 – Boolean Logic in the Lambda Calculus• Lesson 3 – Function reduction and evaluation• Lesson 4 – Arithmetic in the Lambda Calculus• Lesson 5 – Advanced Logic and Arithmetic in the

Lambda Calculus• Functional Programming and Scheme

Prof. Youngsup Kim, idebtor@handong.edu, 2014 Programming Languages, CSEE Dept., Handong Global University

3

4

1903–1995Professor at Princeton (1929–1967)and UCLA (1967–1990)Invented the Lambda Calculus

5

Had a few successful graduate students, including• Stephen Kleene (Regular expressions)• Michael O. Rabin† (Nondeterministic automata) – Turing award winners• Dana Scott† (Formal programming language semantics)• Alan Turing (Turing machines)

1903–1995Professor at Princeton (1929–1967)and UCLA (1967–1990)Invented the Lambda Calculus

6

Had a few successful graduate students, including• Stephen Kleene (Regular expressions)• Michael O. Rabin† (Nondeterministic automata) – Turing award winners• Dana Scott† (Formal programming language semantics)• Alan Turing (Turing machines)

1903–1995Professor at Princeton (1929–1967)and UCLA (1967–1990)Invented the Lambda Calculus

Church says: “There may, indeed, be other applications of the system than its use as a logic.”• Influenced design of Lisp, ML, Haskell, and other languages• See Boost Lambda Library for C++ function objects• Important part of CS history and foundations

7

• In 1937, Turing proved that the two models were equivalent, i.e., that they define the same class of computable functions.

• Modern processors are just overblown Turing machines.

• Functional languages are just the lambda calculus with a more palatable syntax.

In 1936,• Alan Turing invented the Turing

machine• Alonzo Church invented the

lambda calculus

8

Essentially every full-scale programming language has some notion of function.

The (pure) lambda calculus is a language composed entirely of functions.

We use the lambda calculus to study the essence of computation.

It is just as fundamental as Turing Machines.

Many functional programming languages can be viewed as elaborations on the lambda calculus.

Thus, to study lambda calculus is to learn the foundation of many modern functional programming languages such as Hope, Rex, Common Lisp, Scheme, Clojure, Racket, Erlang, OCaml, Haskell, and F#.

Haskell is named for Haskell Brooks Curry, whose work in mathematical logic serves as a foundation for functional languages. Haskell is based on the lambda calculus, hence their logo looks like this:

9

Functional Programming

10

In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Often we say that functional programming is a programming without assignment statement.

Spreadsheet and SQL are good examples of functional programming. In a spreadsheet, one specifies the value of each cell in terms of the values of other cells. The focus is on what is to be computed, not how it should be computed. The SQL query says what relation should be computed, without saying how it should be computed. Indeed, the query can be evaluated in any convenient order.

11

Functional programming has become more and more popular in recent years because it introduce you to new ways to think about and structure your programs:

new abstractions

new design patterns

new algorithms

safe, concise, and elegant code

Furthermore, technology trends point to increasing parallelism:

multicore, gpu, data center

it makes it easier to write parallel code.

its techniques such as map-reduce provide a plausible way forward for many applications

12

Haskell to synthesize hardware

F# in Visual Studio

map-reduce in their data centers

Erlang for concurrency,Haskell for managing PHP

O’Caml for reliability

http://gregosuri.com/how-facebook-uses-erlang-for-real-time-chathttp://labs.google.com/paper/mapreduce.htmlhttp://www.Haskell.org/haskell/wiki/Haskell_in_industryhttp://www.janestcapital.com/technology/ocaml.phphttp://msdn.microsoft.com/en-us/fsharp/cc742182www.artima.com/scalazine/articles/twitter_on_scala.html

13

• F# in Microsoft Visual Studio• Scala combines ML (a functional language) with Objects

• runs on the JVM• C# includes “delegates”

• delegates == functions• Python includes “lambda”

• lambda == more functions• Javascript

• find tutorials online about using functional programming techniques to write more elegant code

• C++ libraries for map-reduce• enabled functional parallelism at Google

• Java has generics and GC(Garbage Collection)• …

Elements of functional programming are showing up all over:

14

overly simplified

version

ITP 20005 Programming Languages• Lambda Calculus

• Lesson 0 – Functional Programming and Lambda Calculus

• Lesson 1 – Introduction to the Lambda Calculus• Lesson 2 – Boolean Logic in the Lambda Calculus• Lesson 3 – Function reduction and evaluation• Lesson 4 – Arithmetic in the Lambda Calculus• Lesson 5 – Advanced Logic and Arithmetic in the

Lambda Calculus• Functional Programming and Scheme

Prof. Youngsup Kim, idebtor@handong.edu, 2014 Programming Languages, CSEE Dept., Handong Global University

16

• Imperative• oriented around assignment to variables and simple control flow• C/C++, Pascal, Java, Fortran, C#

• Object-oriented• oriented around classes and objects• uses encapsulation, modularity, polymorphism, and inheritance• Java, C#, C++, Java, OCaml, Smalltalk

• Markup• One designed for the presentation of text• usually not Turing complete• HTML, XHTML, XML

• Logic • oriented around logical formulae, unification and search• Prolog

17

• Functional• oriented around functions and immutable data structures• SML, O’Caml, F#, Coq, Scheme, Map-Reduce, Erlang, Haskell

• Parallel• allows a computation to run concurrently on multiple processors• CUDA, Cilk, MPI, POSIX threads, X10

• Scripting• An interpreted language with high-level operators for “gluing

together” computations• Awk, Perl, PHP, Python, Ruby

• von Neumann• One whose computational model is based on the von Neumann

architecture Computation is done by modifying variables• C, C++. C#, Fortran, Java

18

• Big Data• C++, Python, R, SQL, and Hadoop-based languages

• Scientific computing• Fortran, C/C++, Python

• Scripting applications• Awk, Perl, Python, Tcl

• Specialized applications• LaTex for typesetting• SQL for database applications• VB macros for spreadsheets

19

• Symbolic programming• F#, Haskell, Lisp, ML, Ocaml

• Systems programming• C, C++, C#, Java, Objective-C

• Web programming• CGI• HTML• JavaScript• Ruby on Rails

• Countless other application areas

20

In mathematical notation that we are already familiar with to manipulate and reason about functions are given: f(x) = x2

In -calculus, the same function, except it is anonymous, representsx. x2

To represent the function evaluation f(2) = 4,

we use the following -calculus syntax: (x. x2 2) 22 4

21

e ::= x variable| x.e abstraction| 𝒆𝟏 𝒆𝟐 application (i.e., applying a function to an argument)

(i.e., definition)

The syntax of the pure -calculus is defined as follows:Syntax:

22

e ::= x variable| x.e abstraction| 𝒆𝟏 𝒆𝟐 application (i.e., applying a function to an argument)

(i.e., definition)

x.e a function abstraction

variable x is the argument

expression e is the body of the function

the function x.e doesn't have a name - anonymous

Terminology:

The syntax of the pure -calculus is defined as follows:Syntax:

23

Semantics: The semantics of a -calculus expression is as follows:

(x.E) M E {M/x} [M/x] E E [M/x]

• {M/x} is the term in which all free occurrences of x in E are replaced with M.

• This replacement operation is called substitution. We will define it carefully later in the lecture.

different notations

Expressions

x + y x + 2*y + z

Function Abstraction

x. (x+y)

z. (x + 1 + z)

x. x

Function Application

(y.(x.x))a

(f. x. f (f x)) (y. y+x)

Expression, function abstraction, and application

apply twice add x to argument

Example:

an abstraction called the identity function

an abstraction that ignores its argumentand returns the identity function

25

I x . x : Identity function definition

Example:

x . x : function abstraction

26

I x . x : Identity function definition

Example:

x . x : function abstraction

Now consider a variable a to the identity function I:

27

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

Now consider a variable a to the identity function I:

28

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

Now consider a variable a to the identity function I:

29

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

Now consider a variable a to the identity function I:

30

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

31

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

E[M/x] all free occurrences of x in E are replaced with M

32

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

E[M/x] all free occurrences of x in E are replaced with M

33

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

= I

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

E[M/x] all free occurrences of x in E are replaced with M

34

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

= I

E[M/x] all free occurrences of x in E are replaced with M

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

How about evaluating like this?:

35

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

= I

E[M/x] all free occurrences of x in E are replaced with M

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

II = (x.x)I

How about evaluating like this?:

36

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

= I

E[M/x] all free occurrences of x in E are replaced with M

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

II = (x.x)I

x[I/x]

How about evaluating like this?:

37

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

= I

E[M/x] all free occurrences of x in E are replaced with M

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

II = (x.x)I

x[I/x]

= I

How about evaluating like this?:

38

I x . x : Identity function definition

Example:

x . x : function abstraction

Ia : function application

= ( x . x)a : a long form

= a

II = (x.x)(x.x)

x[x.x/x]

= x.x

= I

E[M/x] all free occurrences of x in E are replaced with M

Now consider applying the identity function I to itself I:

Now consider a variable a to the identity function I:

II = (x.x)I

x[I/x]

= I

a kind of the lazy evaluation

How about evaluating like this?:

39

Exercise: Evaluate 𝜔 I, where 𝜔 = ( x . x x ).

40

x.y.yxExample:

Now apply this function to two variables a and b, then we get:

41

x.y.yxExample:

Now apply this function to two variables a and b, then we get:

(x.y.yx)ab

42

x.y.yxExample:

Now apply this function to two variables a and b, then we get:

(x.y.yx)ab

(y.yx)b[a/x]

43

x.y.yxExample:

Now apply this function to two variables a and b, then we get:

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

44

x.y.yxExample:

Now apply this function to two variables a and b, then we get:

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Q: What is the meaning of this function in words?

45

x.y.yxExample:

Now apply this function to two variables a and b, then we get:

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Q: What is the meaning of this function in words?

A: Two functions take one argument each, returns them in opposite order.

46

xy.yx

Q: What is the meaning of this function in words?

Example:

47

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

48

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

49

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

50

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

(xy.yx)ab

= ba

51

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

(xy.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Let's compare this with the previous

example.

52

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

(xy.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Let's compare this with the previous

example.

53

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

(xy.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Let's compare this with the previous

example.

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Haskell Curry proved that when dealing with functions of multiple variables, it is always possible to decompose said function into multiple functions, each of a single variable.

54

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

(xy.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Let's compare this with the previous

example.

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Haskell Curry proved that when dealing with functions of multiple variables, it is always possible to decompose said function into multiple functions, each of a single variable.

(xy.yx) (x.y.yx)currying

55

xy.yx

Q: What is the meaning of this function in words?

A: Surely, it is a multivariate function – two variables.

Example:

It takes two arguments and returns them in the opposite order.

Now apply this function to two variables a and b, then we get:

(xy.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Let's compare this with the previous

example.

(x.y.yx)ab

(y.yx)b[a/x]

=(y.ya)b

= ba

Haskell Curry proved that when dealing with functions of multiple variables, it is always possible to decompose said function into multiple functions, each of a single variable.

(xy.yx) (x.y.yx)currying

uncurrying

6.56

The lambda calculus can only represent functions of one variable. It turns out that one-variable functions are sufficient to represent multiple-

variable functions, using a strategy called currying.

Since a lambda abstraction only binds a single variable, functions with multiple parameters must be modelled as Curried higher-order functions.

E.g., given the mathematical function:h(x,y) = x+y

We can represent h as h’ of type such that h(x,y) = h’(x)(y) = x+y

For example, h’(2) = g, where g(y) = 2+y

We say that h’ is the curried version of h.

As we have seen, to improve readability, multiple lambdas are suppressed, so:

x y . x = x . y . x

b x y . b x y = b . x . y . ( b x ) y

Most lambda expressions, function abstraction and application can be implemented straight forward in Python:

57

Most lambda expressions, function abstraction and application can be implemented straight forward in Python:

58

# λx.λy.xy λxy.xy

lambda x : lambda y : x (y)

Example : Function abstraction and application

Most lambda expressions, function abstraction and application can be implemented straight forward in Python:

59

# λx.λy.xy λxy.xy

lambda x : lambda y : x (y)

# apply x to the print, y to "hello world"

(lambda x : lambda y : (x)(y))(print)("hello world")

Example : Function abstraction and application

Most lambda expressions, function abstraction and application can be implemented straight forward in Python:

60

# λx.λy.xy λxy.xy

lambda x : lambda y : x (y)

# apply x to the print, y to "hello world"

(lambda x : lambda y : (x)(y))(print)("hello world")

Example : Function abstraction and application

Exercise : add(2, 3) using a lambda application.

Most lambda expressions, function abstraction and application can be implemented straight forward in Python:

61

# λx.λy.xy λxy.xy

lambda x : lambda y : x (y)

# apply x to the print, y to "hello world"

(lambda x : lambda y : (x)(y))(print)("hello world")

Example : Function abstraction and application

Exercise : add(2, 3) using a lambda application.

# apply x, y to add

>>> from operator import add

>>> (lambda w : lambda x, y : w (x, y)) (add)(2, 3)

5

Most lambda expressions, function abstraction and application can be implemented straight forward in Python:

62

# λx.λy.xy λxy.xy

lambda x : lambda y : x (y)

# apply x to the print, y to "hello world"

(lambda x : lambda y : (x)(y))(print)("hello world")

Example : Function abstraction and application

Exercise : add(2, 3) using a lambda application.

# apply x, y to add

>>> from operator import add

>>> (lambda w : lambda x, y : w (x, y)) (add)(2, 3)

5

63

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

64

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

65

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

66

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

(f.fx) (x.x+2)

(f.f(5)) (x.x+2)

def f():

def adder(x):

return add(x, 2)

return adder

f()(5)

7

67

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

(f.fx) (x.x+2)

(f.f(5)) (x.x+2)

def f():

def adder(x):

return add(x, 2)

return adder

f()(5)

7

68

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

(f.fx) (x.x+2)

(f.f(5)) (x.x+2)

def f():

def adder(x):

return add(x, 2)

return adder

f()(5)

7

69

Example: a function application

def f(x):

return x+2

Python:

f(5)

7

Lambda Calculus

x.x+2

(x.x+2)5

5+2

7

(f.fx) (x.x+2)

(f.f(5)) (x.x+2)

def f():

def adder(x):

return add(x, 2)

return adder

f()(5)

7

70

x.ffx

Let 's suppose that we have a given Lambda expression such as

71

x.ffx

Let 's suppose that we have a given Lambda expression such as

Then, how should we evaluate it?

72

x.ffx

Let 's suppose that we have a given Lambda expression such as

x.f(fx) x.(ff)x

Then, how should we evaluate it?

vs

73

x.ffx

Let 's suppose that we have a given Lambda expression such as

x.f(fx) x.(ff)x

Then, how should we evaluate it?

vs

74

x.ffx

Let 's suppose that we have a given Lambda expression such as

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

Then, how should we evaluate it?

vs

75

The body in an abstraction extends as far as possible to the right.

Application is left-associative and binds tighter than period.

f.xy = f.(xy)

x.x z.z = x.(x(z.z)) != (x.x)(z.z)

76

The body in an abstraction extends as far as possible to the right.

Application is left-associative and binds tighter than period.

f.xy = f.(xy)

x.x z.z = x.(x(z.z)) != (x.x)(z.z)

77

The body in an abstraction extends as far as possible to the right.

Application is left-associative and binds tighter than period.

xyz = (x y) z

x.ffx = x.(f f) x

x.f(fx) = x.(f (f (x)))wyx.y(wyx) = wyx.y((wy)x)

(w.y.xy)z.z = (x.(y.(xy)))z.z

f.xy = f.(xy)

x.x z.z = x.(x(z.z)) != (x.x)(z.z)

78

The body in an abstraction extends as far as possible to the right.

Application is left-associative and binds tighter than period.

xyz = (x y) z

x.ffx = x.(f f) x

x.f(fx) = x.(f (f (x)))wyx.y(wyx) = wyx.y((wy)x)

(w.y.xy)z.z = (x.(y.(xy)))z.z

f.xy = f.(xy)

x.x z.z = x.(x(z.z))

In general, use parentheses to make the parsing of a lambda expression clear if you are in doubt.

!= (x.x)(z.z)

79

The body in an abstraction extends as far as possible to the right.

Application is left-associative and binds tighter than period.

f g . x f . g . x

f.xy = f.(xy)

x.x z.z = x.(x(z.z))

In general, use parentheses to make the parsing of a lambda expression clear if you are in doubt.

Multiple lambdas may be suppressed. (Review)

!= (x.x)(z.z)

xyz = (x y) z

x.ffx = x.(f f) x

x.f(fx) = x.(f (f (x)))wyx.y(wyx) = wyx.y((wy)x)

(w.y.xy)z.z = (x.(y.(xy)))z.z

80

The body in an abstraction extends as far as possible to the right.

currying

Application is left-associative and binds tighter than period.

f g . x f . g . x

f.xy = f.(xy)

x.x z.z = x.(x(z.z))

In general, use parentheses to make the parsing of a lambda expression clear if you are in doubt.

Multiple lambdas may be suppressed. (Review)

!= (x.x)(z.z)

xyz = (x y) z

x.ffx = x.(f f) x

x.f(fx) = x.(f (f (x)))wyx.y(wyx) = wyx.y((wy)x)

(w.y.xy)z.z = (x.(y.(xy)))z.z

81

The body in an abstraction extends as far as possible to the right.

currying

uncurrying

a simpler notation

Application is left-associative and binds tighter than period.

f g . x f . g . x

f.xy = f.(xy)

x.x z.z = x.(x(z.z))

In general, use parentheses to make the parsing of a lambda expression clear if you are in doubt.

Multiple lambdas may be suppressed. (Review)

!= (x.x)(z.z)

xyz = (x y) z

x.ffx = x.(f f) x

x.f(fx) = x.(f (f (x)))wyx.y(wyx) = wyx.y((wy)x)

(w.y.xy)z.z = (x.(y.(xy)))z.z

82

Operational Semantics: a simple introduction – stay tuned

The lambda calculus can be viewed as the simplest possible pure functional programming language.

conversion (renaming):

x.x y.y

reduction (substitution):

(x.x+y)z z+y

reduction: ( x.y)x y

83

Beta reduction is the computational engine of the lambda calculus:

(x.xx)(y.y)

xx[y.y/x]

= (y.y)(y.y)

y[y.y/y]

= y.y

Example:

84

Beta reduction is the computational engine of the lambda calculus:

(x.xx)(y.y)

xx[y.y/x]

= (y.y)(y.y)

y[y.y/y]

= y.y

Example:

(x.xx)(x.xx)

xx[x.xx/x]

= (x.xx)(x.xx)

Example:

85

Beta reduction is the computational engine of the lambda calculus:

(x.xx)(y.y)

xx[y.y/x]

= (y.y)(y.y)

y[y.y/y]

= y.y

Example:

(x.xx)(x.xx)

xx[x.xx/x]

= (x.xx)(x.xx)

Example:

Lazy evaluation:

(x.xx)I

xx[I/x]

= II

=(y.y)I

y[I/y]

= I

86

Beta reduction is the computational engine of the lambda calculus:

(x.xx)(y.y)

xx[y.y/x]

= (y.y)(y.y)

y[y.y/y]

= y.y

Example:

(x.xx)(x.xx)

xx[x.xx/x]

= (x.xx)(x.xx)

Example:

Lazy evaluation:

(x.xx)I

xx[I/x]

= II

=(y.y)I

y[I/y]

= I

(x.xx)(x.xx)

= (x.xx)𝜔

xx[𝜔/x]

= 𝜔𝜔

Lazy evaluation:

87

הגה 𝜔 = (x.xx)

Q: What is the meaning of 𝜔 function in words?

A: 𝜔 takes a single variable, and produces two of that variable. In other words, the variable x applied to itself.

Example: 𝜔𝐼

𝜔𝐼=(x.xx)I

= II

=(x.x)I

= I

𝐼 = (x.x)

𝜔 takes I, andproduces two of I. I simply repeats

what it is given.what it is given

lazy evaluation applied

88

הגה 𝜔 = (x.xx) 𝐼 = (x.x)

𝜴 ≡ 𝝎𝝎Definition: Ω is 𝜔 applied to itself. What is the normal form of Ω ?

Ω=𝜔𝜔= (x.xx)𝜔= 𝜔𝜔= (x.xx)𝜔= 𝜔𝜔= ...

non terminating computations –infinite loop

Such a function is said to have no normal form.

𝜴 Property: Ω never respond to another function being applied to it.

ΩI=𝜔𝜔I= (𝜔𝜔)I= (𝜔𝜔)I= ...

Example:

due to the left-association rule

6.89

What else can we do?

90

What else can we do?

We can do everything:It is considered just as fundamental as Turing Machines.

We will challenge for Boolean logical operations.

ITP 20005 Programming Languages• Lambda Calculus

• Lesson 0 – Functional Programming and Lambda Calculus

• Lesson 1 – Introduction to the Lambda Calculus• Lesson 2 – Boolean Logic in the Lambda Calculus• Lesson 3 – Function reduction and evaluation• Lesson 4 – Arithmetic in the Lambda Calculus• Lesson 5 – Advanced Logic and Arithmetic in the

Lambda Calculus• Functional Programming and Scheme

top related