-calculus kangwon national university 임현승 programming languages these slides are based on the...

63
-Calculus Kangwon National University 임임임 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.

Upload: winfred-hicks

Post on 29-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

-Calculus

Kangwon National University

임현승

Programming Languages

These slides are based on the slides by Prof. Sungwoo Park at POSTECH.

Page 2: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

2

What is a core of functional languages?

fun x -> e

e1 e2

0, 1, 2, ..., +, -, ...

true, false, if e then e else e

patterns

datatypes

exceptions

structures

functors

let f x = evariables

Page 3: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

3

Core of functional languages

fun x -> ee1 e2

x

Page 4: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

4

-calculus

Page 5: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

5

Outline• Brief history• Abstract syntax of the -calculus• Operational semantics of the l-calculus• Substitutions• Programming in the -calculus

Page 6: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

Liebniz’s Ideal

Gottfried Wilhelm von Leibniz

(1646-1716)

6

1) Create a “universal language” in which all possible problems can be stated.

2) Find a decision method to solve all the problem stated in the universal language.

Page 7: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

7

Can one solve all problems formulated in the universal language?

Need a formalization of the notion of“decidable” or “computable”

Page 8: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

Two Models of Computation

• -Calculus (Church 1936)

• Turing machine (Turing 1937)

8

Page 9: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

-Calculus (1936)

Alonzo Church

(1903-1995)

9

• Minimalist

• Three expression types:

x [variables]

x.e [anonymous function]

e1 e2 [function application]

• Foundations of functional languages: Lisp, ML, Haskell, etc.

Page 10: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

Turing Machine (1937)

Alan Turing

(1912-1954)

10

• A hypothetical device that manipulates symbols on a strip of tape according to a table of rules

• The first accepted definition of a general-purpose computer

• Foundations of imperative languages: Java, C/C++, C#, Fortran, Pascal, assembler languages, etc.

Page 11: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

11

-calculus is Turing-complete

fun x -> ee1 e2

x

Turing machine-calculus

=

Page 12: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

-Everywhere• Not only in FPLs…• C++ 11• C#• Java 8• Javascript• PHP• Python• Scala• Apple Swift

12

Page 13: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

13

Outline• Brief history V• Abstract syntax of the -calculus• Operational semantics of the l-calculus• Substitutions• Programming in the -calculus

Page 14: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

14

Syntax for a Programming Language

Concrete syntax• program =

string of characters• specifies rules for parsing.

– operator precedence– associativity– keywords, ...

1 + 2 * 31 + (2 * 3)1 + (2 * (3))

Abstract syntax• abstracts away from details of parsing.• focuses on the high-level structure of programs.• suitable for studying the semantics

Page 15: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

15

• x– variable– z, s, t, f, arg, accum, ...

• x. e– -abstraction– x = formal argument, e = body– fun x -> e

• e1 e2

– application– left-associative (as in OCaml):

• e1 e2 e3 = (e1 e2 ) e3

• e1 e2 e3 e1 (e2 e3 )

Abstract Syntax of the -Calculus

Page 16: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

16

Examples

Page 17: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

17

Outline• Brief history V• Abstract syntax of the -calculus V• Operational semantics of the l-calculus• Substitutions• Programming in the -calculus

Page 18: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

18

Semantics of Languages• Answers "what is the meaning of a given program?"

– ML has a formal semantics.– What about C?

• Three styles– denotational semantics– axiomatic semantics– operational semantics

• The 1990s saw the renaissance of operational semantics.

Page 19: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

19

Operational Semantics• Specifies how to transform a program into a value

via a sequence of operations

Program ValueP2

operation operation operationPnoperation...

let rec fac = function 1 -> 1 | n -> n * fac (n - 1)in fac 4

24

Page 20: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

20

Operational Semantics of -Calculus

• Specifies how to transform an expression into a value via a sequence of reductions

Expr ValueE2

reduction reduction reductionEnreduction...

Page 21: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

21

Values and Reductions

Page 22: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

22

Reductions

redex = reducible expression

: -reduction

Page 23: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

24

_____ = Redex

Page 24: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

26

-Reduction Not Unique

So we need a reduction strategy.

Page 25: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

27

Call-by-name Call-by-value

Page 26: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

28

Call-by-name Call-by-value

Page 27: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

30

Page 28: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

31

Call-by-name Call-by-value• Used in Haskell• Lazy or non-strict

functional languages• The implementation uses

call-by-need.

• Superb!

• Used in OCaml• Eager or strict

functional languages

• Superb!

(fn x => 0) <some horrible computation>

(fn x => 0) <non-terminating computation>

Page 29: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

32

Outline• Brief history V• Abstract syntax of the -calculus V• Operational semantics of the l-calculus V• Substitutions• Programming in the -calculus

Page 30: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

33

Values and Reductions

redex = reducible expression

: -reduction

Page 31: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

34

Call-by-name Call-by-value

Page 32: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

35

[e' / x] e• Informally

"substitute e' for every occurrence of x in e."

• Examples

Page 33: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

36

Easy Cases First

Page 34: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

37

Two Remaining Cases

Page 35: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

38

• First (stupid) attempt

• Second attempt

• But wait:

Page 36: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

39

• Names of bound variables do not matter.

• Hence

– for a fresh variable y,

Bound Variables

Page 37: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

40

One Remaining Case

Page 38: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

41

A Naive Attempt

• An anomaly:

something for y

Page 39: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

42

Free Variables• Variables that are bound nowhere

FV(e) = set of free variables in e

Page 40: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

43

Free Variables Remain Free• From the point of view of an outside observer,

a free variable remains free until it is explicitly

replaced.

outside observer

? variable capture

Page 41: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

44

Capture-Avoiding Substitution

• What happens if– the free variable y is captured and becomes a

bound variable. – To an outside observer, it suddenly disappears!

Page 42: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

45

Substitution Completed

Page 43: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

46

• We have to rename bound variables as necessary.

Capture-Avoiding Substitution in Action

Page 44: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

47

-conversion• Renaming bound variables when necessary• Okay because the names of bound variables do not

matter.• Examples

Page 45: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

Formalization of -Conversion

• Variable swapping

replace all occurrences of in by and all occurrences of in by .

48

Page 46: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

-conversion and Substitution

49

Last case of substitution

Page 47: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

50

Outline• Brief history V• Abstract syntax of the -calculus V• Operational semantics of the l-calculus V• Substitutions V• Programming in the -calculus

Page 48: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

51

• A boolean value– "Give me two options and I will choose one for

you!"

• Syntactic sugar

Booleans

Page 49: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

52

Examples• Under the call-by-name strategy,

Page 50: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

53

Logical Operators

Page 51: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

54

Natural Numbers• A natural number n

– has the capability to repeat a given process n

times.– "Give me a function f and I will return

f n = f o f ... f o f "

Page 52: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

55

Church Numerals

Page 53: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

56

Addition• Key observation:

Page 54: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

57

Multiplication• Key observation:

• Alternatively

Page 55: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

58

Recursive Functions in -Calculus• Plan of attack

1. Assume a recursive function construct

2. Rewrite it as an expression in the -calculusi.e., show that it is syntactic sugar.

fun f x. e

Page 56: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

59

• Plan of attack

1. Assume a recursive function construct

2. Rewrite it as an expression in the -calculusi.e., show that it is syntactic sugar.

Example: Factorial

Page 57: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

60

fac to FAC

Page 58: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

61

FAC produces a new function from f

Page 59: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

62

Now we only need to find a fixed point of:

Page 60: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

63

Fixed Point• Fixed point of function f

V such that V = f (V)

• Ex.

f (x) = 2 - x

fixed point of f = 1

1 = f (1)

Page 61: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

64

Magic Revealed• Fixed point combinator / Y combinator

(call-by-value)

• fix F returns a fixed point of F.

Page 62: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

65

Now we only need to find a fixed point of:

Now we only need to compute:

Page 63: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH

66

Answer