-calculus: A Basis for Functional Languages Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 14, 2006 September 14, 2006 http://www.csg.csail.mit.edu/6.827 L03-1

Download Report

Transcript -calculus: A Basis for Functional Languages Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 14, 2006 September 14, 2006 http://www.csg.csail.mit.edu/6.827 L03-1

-calculus:
A Basis for Functional Languages
Arvind
Computer Science and Artificial Intelligence Laboratory
M.I.T.
September 14, 2006
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-1
Functions
.
.
.
..
.
Domain
Range
f:D
R
f may be viewed as
• a set of ordered pairs < d , r > where d  D
and r  R
• a method of computing value r corresponding
to argument d
some important notations
– -calculus (Church)
– Turing machines (Turing)
– Partial recursive functions
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-2
The -calculus:
a simple type-free language
• to express all computable functions
• to directly express higher-order functions
• to study evaluation orders, termination,
uniqueness of answers...
• to study various typing systems
• to serve as a kernel language for functional
languages
– However, -calculus extended with constants and letblocks is more suitable
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-3
-notation
• a way of writing and applying functions
without having to give them names
• a syntax for making a function expression
from any other expression
• the syntax distinguishes between the
integer "2” and the function "always_two"
which when applied to any integer returns 2
always_two x = 2;
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-4
Pure-calculus: Syntax
E = x | x.E | E E
variable
1. application
abstraction
application
E1 E 2
function
argument
- application is left associative
E1 E2 E3 E4  ((( E1 E2 ) E3 ) E4 )
2. abstraction
x.E
bound variable
body
or formal parameter
- the scope of the dot in an abstraction extends as
far to the right as possible
x.x y x.(x y) (x.(x y))  (x.x y)  (x.x) y
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-5
Free and Bound Variables
•
-calculus follows lexical scoping rules
• Free variables of an expression
FV(x)
= {x}
FV(E1 E2)
= FV(E1) U FV(E2)
?
FV(x.E)
= FV(E) – {x}
?
• A variable occurrence which is not free in an
expression is said to be a bound variable of the
expression
• combinator: a -expression without free variables,
aka closed -expression
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-6
ß-substitution
(x.E) Ea  E[Ea /x]
replace all free occurrences of x in E with Ea
E[A/x] is defined as follows by case on E:
variable
y[Ea/x]= Ea
y[Ea/x]= y
application
if x  y
otherwise
(E1 E2 )[Ea/x] = (E1[Ea/x]
abstraction
E2[Ea/x])
?
?
(y.E1)[Ea/x]
(y.E1)[Ea/x]
September 14, 2006
= y.E1
if x  y
= (y.E
z.((E
?
/x])
otherwise
a/x])
1[E1a[z/y])[E
otherwise
What
if Ea contains
y)?U
where
z

FV(E
)
U
FV(E
1
a
http://www.csg.csail.mit.edu/6.827
L03-7
ß-substitution: an example
(p.p (p q)) [(a p b) / q]
September 14, 2006

(z.z (z q)) [(a p b) / q]

(z.z (z (a p b)))
http://www.csg.csail.mit.edu/6.827
L03-8
-Calculus as a Reduction System
Syntax
E = x | x.E | E E
Reduction Rule
 -rule:x.E  y.E [y/x]
if y  FV(E)
 -rule: (x.E) Ea  E [Ea/x]
-rule: (x.E x)  E
if x  FV(E)
Redex
(x.E) Ea
Normal Form
An expression without redexes
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-9
 and  Rules
-rule says that the bound variables can be
renamed systematically:
(x.x (x.a x)) b  (y.y (x.a x)) b
-rule can turn any expression, including a
constant, into a function:
x.a x

a
-rule does not work in the presence of types
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-10
A Sample Reduction
C  x.y.f.f x y
H f.f (x.y. x)
T f.f (x.y. y)
What is H (C a b)





f.f (x.y.x)) (C a b)
(C a b) (x.y.x)
(x.y.x) a b
(y.a) b
a
H (C a b)
T (C a b)
September 14, 2006
?




a
b
http://www.csg.csail.mit.edu/6.827
L03-11
Integers: Church's Representation
0  x.y. y
1  x.y. x y
2 x.y. x (x y)
...
n x.y. x (x...(x y)...)
succ ?
If n is an integer, then (n a b) gives n
nested a’s followed by b

the successor of n should be a (n a b)
succ
plus
mul
September 14, 2006
 n.a.b.a (n a b)
 m.n.m succ n
 m.n.m (plus n) 0
http://www.csg.csail.mit.edu/6.827
?
?
L03-12
Booleans and Conditionals
True
False
 x.y.x
 x.y.y
zero?
n. n (y.False) True
zero? 0 
 x.y.y) (y.False) True
?
 x.y.x y) (y.False) True
zero? 1 
?
cond
 b.x.y. b x y
 E1
cond True E1 E2 
 E2
cond False E1 E2 
?
?
 y. y) True
 True
 (y.False) True
 False
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-13
Recursion ?
fact n = if (n == 0) then 1
else n * fact (n-1)
• Assuming suitable combinators, fact can
be rewritten as:
fact = n. cond (zero? n) 1 (mul n (fact (sub n 1)))
• How do we get rid of the fact on the RHS?
Suppose
H = f.n.cond (zero? n) 1 (mul n (f (sub n 1)))
then
fact = H fact
--- fact is a solution of this equation???
more on recursion in the next lecture
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-14
Choosing Redexes
1. ((x.M) A) ((x.N) B)
------ ------
------ ------
2. ((x.M) ((y.N)B))
------ ------
------------- -----------
Does  followed by  produce the same
expression as  followed by ?
Notice in the second example  can destroy
or duplicate 
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-15
Church-Rosser Property
A reduction system is said to have the
 E1 and
Church-Rosser property, if E 
 E2 then there exits a E3 such that
E 
 E3 and E2 
 E3.
E1 
E
E1
E2
E3
also known as CR or Confluence
Theorem:
September 14, 2006
The -calculus is CR.
(Martin-Lof & Tate)
http://www.csg.csail.mit.edu/6.827
L03-16
Interpreters
An interpreter for the -calculus is a program to
reduce -expressions to “answers”.
It requires:
• the definition of an answer
• a reduction strategy
- a method to choose redexes in an expression
• a criterion for terminating the reduction
process
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-17
Definitions of “Answers”
• Normal form (NF): an expression without redexes
• Head normal form (HNF):
x is HNF
(x.E) is in HNF if E is in HNF
(x E1 ... En) is in HNF
Semantically most interesting- represents the
information content of an expression
• Weak head normal form (WHNF):
An expression in which the left most application is not a
redex.
x is in WHNF
(x.E) is in WHNF
(x E1 ... En) is in WHNF
Practically most interesting  “Printable Answers”
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-18
Reduction Strategies
Two common strategies
• applicative order: left-most innermost redex
aka call by value evaluation
• normal order: left-most (outermost) redex
aka call by name evaluation
( x.y) (( x.x x) ( x.x x))
1
September 14, 2006
2
http://www.csg.csail.mit.edu/6.827
applicative order
normal order
L03-19
Facts
1. Every -expression does not have an answer
i.e., a NF or HNF or WHNF
(x.x x) (x.x x) = 
      
2. CR implies that if NF exists it is unique
3. Even if an expression has an answer, not all
reduction strategies may produce it
(x.y.y) 
leftmost redex: (x.y.y)   y.y
innermost redex: (x.y.y)   (x.y. y)   ...
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-20
Normalizing Strategy
A reduction strategy is said to be normalizing
if it terminates and produces an answer of an
expression whenever the expression has an
answer.
aka the standard reduction
Theorem: Normal order (left-most) reduction
strategy is normalizing for the -calculus.
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-21
A Call-by-name Interpreter
Apply the function
before evaluating
the arguments
Answers:
Strategy:
WHNF
leftmost redex
cn(E):
Definition by cases on E
E
=
x |
x.E | E E
cn(x)
= x
cn(x.E) = x.E
cn(E1 E2) = let f = cn(E )
1
in
case f of
x.E3 = cn(E3[E2/x])
_ = (f E2)
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-22
Better syntax ...
[[ ....]] represents syntax
E
=
x |
cn([[x]])
cn([[x.E]])
cn([[E1 E2]])
x.E | E E
= x
= x.E
= let f = cn([[E1]])
in
case f of
[[x.E3]] = cn(E3[E2/x])
_ = (f E2)
Meta syntax
September 14, 2006
http://www.csg.csail.mit.edu/6.827
still messy
L03-23
A Call-by-value Interpreter
Answers:
Strategy:
WHNF
leftmost-innermost redex but not
inside a -abstraction
cv(E):
Definition by cases on E
E
=
x |
Evaluate the
x.E | E Eargument before
applying the
function
cv(x)
= x
cv(x.E) = x.E
cv( E1 E2 ) = let f = cv(E1)
a = cv(E2)
in
case f of
x.E3 = cv(E3[a/x])
_
= (f a)
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-24
Normalizing?
( x.y) (( x.x x) ( x.x x))
call by value
reduction
call by name
reduction
( x.y) (( x.x x) ( x.x x))
y
Which interpreters (if any) are normalizing
for computing WHNF ?
Clearly not
call-by-value
May be
call-by-name
The proof to show that the call-by-name
interpreter is normalizing is non-trivial
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-25
Big Step Semantics
• Consider the following rule
E1
E1 E2


x.Eb
Eb [E2 / x]
•Can we compute using this rule?
•What does it compute?
•Will it compute every thing that the –calculus can?
September 14, 2006
http://www.csg.csail.mit.edu/6.827
L03-26