welcome to programming languages...

90
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 15 Recursion (Video ) (1pp ) (1pps ) (8pp ) (07.py ) 1.7 Wed Sep 17 Tree Recursion (Video ) (1pp ) (1pps ) (8pp ) (08.py ) 1.7 Fri Sep 19 Function Examples (Video ) (1pp ) (1pps ) (8pp ) (09.py )

Upload: others

Post on 25-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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)

Page 2: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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, [email protected], 2014 Programming Languages, CSEE Dept., Handong Global University

Page 3: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

3

Page 4: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

4

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

Page 5: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 6: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 7: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 8: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 9: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

9

Functional Programming

Page 10: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 11: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 12: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 13: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 14: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

14

overly simplified

version

Page 15: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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, [email protected], 2014 Programming Languages, CSEE Dept., Handong Global University

Page 16: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 17: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 18: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 19: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 20: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 21: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 22: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 23: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 24: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 25: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

25

I x . x : Identity function definition

Example:

x . x : function abstraction

Page 26: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

26

I x . x : Identity function definition

Example:

x . x : function abstraction

Now consider a variable a to the identity function I:

Page 27: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 28: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 29: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 30: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 31: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 32: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 33: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 34: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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?:

Page 35: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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?:

Page 36: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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?:

Page 37: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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?:

Page 38: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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?:

Page 39: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

39

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

Page 40: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

40

x.y.yxExample:

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

Page 41: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

41

x.y.yxExample:

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

(x.y.yx)ab

Page 42: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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]

Page 43: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 44: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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?

Page 45: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 46: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

46

xy.yx

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

Example:

Page 47: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

47

xy.yx

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

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

Example:

Page 48: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 49: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 50: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 51: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 52: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 53: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 54: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 55: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 56: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 57: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

57

Page 58: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 59: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 60: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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.

Page 61: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 62: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 63: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 64: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 65: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 66: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 67: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 68: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 69: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 70: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

70

x.ffx

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

Page 71: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

71

x.ffx

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

Then, how should we evaluate it?

Page 72: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 73: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 74: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 75: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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)

Page 76: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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)

Page 77: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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)

Page 78: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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)

Page 79: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 80: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 81: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 82: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 83: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 84: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 85: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 86: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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:

Page 87: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 88: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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

Page 89: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

6.89

What else can we do?

Page 90: Welcome to Programming Languages (ITP20005)contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/...ITP 20005 Programming Languages • Lambda Calculus • Lesson 0 –Functional

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