-calculus kangwon national university 임현승 programming languages these slides are based on the...
TRANSCRIPT
![Page 1: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/1.jpg)
-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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/4.jpg)
4
-calculus
![Page 5: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/9.jpg)
-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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/12.jpg)
-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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/16.jpg)
16
Examples
![Page 17: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/23.jpg)
24
_____ = Redex
![Page 24: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/27.jpg)
30
![Page 28: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/42.jpg)
45
Substitution Completed
![Page 43: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/43.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/44.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/45.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/46.jpg)
-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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/47.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/48.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/49.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/50.jpg)
53
Logical Operators
![Page 51: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/51.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/52.jpg)
55
Church Numerals
![Page 53: -Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/53.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/54.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/55.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/56.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/57.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/58.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/59.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/60.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/61.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/62.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070418/5697bfec1a28abf838cb896d/html5/thumbnails/63.jpg)
66
Answer