-calculus kangwon national university 임현승 programming languages these slides are based on the...
TRANSCRIPT
-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
3
Core of functional languages
fun x -> ee1 e2
x
4
-calculus
5
Outline• Brief history• Abstract syntax of the -calculus• Operational semantics of the l-calculus• Substitutions• Programming in the -calculus
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.
7
Can one solve all problems formulated in the universal language?
Need a formalization of the notion of“decidable” or “computable”
Two Models of Computation
• -Calculus (Church 1936)
• Turing machine (Turing 1937)
8
-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.
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.
11
-calculus is Turing-complete
fun x -> ee1 e2
x
Turing machine-calculus
=
-Everywhere• Not only in FPLs…• C++ 11• C#• Java 8• Javascript• PHP• Python• Scala• Apple Swift
12
13
Outline• Brief history V• Abstract syntax of the -calculus• Operational semantics of the l-calculus• Substitutions• Programming in the -calculus
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
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
16
Examples
17
Outline• Brief history V• Abstract syntax of the -calculus V• Operational semantics of the l-calculus• Substitutions• Programming in the -calculus
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.
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
20
Operational Semantics of -Calculus
• Specifies how to transform an expression into a value via a sequence of reductions
Expr ValueE2
reduction reduction reductionEnreduction...
21
Values and Reductions
22
Reductions
redex = reducible expression
: -reduction
24
_____ = Redex
26
-Reduction Not Unique
So we need a reduction strategy.
27
Call-by-name Call-by-value
28
Call-by-name Call-by-value
30
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>
32
Outline• Brief history V• Abstract syntax of the -calculus V• Operational semantics of the l-calculus V• Substitutions• Programming in the -calculus
33
Values and Reductions
redex = reducible expression
: -reduction
34
Call-by-name Call-by-value
35
[e' / x] e• Informally
"substitute e' for every occurrence of x in e."
• Examples
36
Easy Cases First
37
Two Remaining Cases
38
• First (stupid) attempt
• Second attempt
• But wait:
39
• Names of bound variables do not matter.
• Hence
– for a fresh variable y,
Bound Variables
40
One Remaining Case
41
A Naive Attempt
• An anomaly:
something for y
42
Free Variables• Variables that are bound nowhere
FV(e) = set of free variables in e
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
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!
45
Substitution Completed
46
• We have to rename bound variables as necessary.
Capture-Avoiding Substitution in Action
47
-conversion• Renaming bound variables when necessary• Okay because the names of bound variables do not
matter.• Examples
Formalization of -Conversion
• Variable swapping
replace all occurrences of in by and all occurrences of in by .
48
-conversion and Substitution
49
Last case of substitution
50
Outline• Brief history V• Abstract syntax of the -calculus V• Operational semantics of the l-calculus V• Substitutions V• Programming in the -calculus
51
• A boolean value– "Give me two options and I will choose one for
you!"
• Syntactic sugar
Booleans
52
Examples• Under the call-by-name strategy,
53
Logical Operators
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 "
55
Church Numerals
56
Addition• Key observation:
57
Multiplication• Key observation:
• Alternatively
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
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
60
fac to FAC
61
FAC produces a new function from f
62
Now we only need to find a fixed point of:
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)
64
Magic Revealed• Fixed point combinator / Y combinator
(call-by-value)
• fix F returns a fixed point of F.
65
Now we only need to find a fixed point of:
Now we only need to compute:
66
Answer