360 likes | 550 Views
CSE-321 Programming Languages -Calculus (II). 박성우. POSTECH March 26, 2007. : -reduction. redex = reducible expression. Values and Reductions. Call-by-name Call-by-value. Outline. Abstract syntax of the -calculus V Operational semantics of the l -calculus V Substitutions
E N D
CSE-321 Programming Languages-Calculus (II) 박성우 POSTECH March 26, 2007
: -reduction redex = reducible expression Values and Reductions
Outline • Abstract syntax of the -calculus V • Operational semantics of the l-calculus V • Substitutions • Programming in the -calculus
[e' / x] e • Informally"substitute e' for every occurrence of x in e." • Examples
First (stupid) attempt • Second attempt • But wait:
Bound Variables • Names of bound variables do not matter. • Hence • for a fresh variable y,
A Naive Attempt • An anomaly: something for y
Free Variables • Variables that are bound nowhere FV(e) = set of free variables in e
? Free Variables Remain Free • From the point of view of an outside observer,a free variable remains free until it is explicitly replaced. variable capture outside observer
Capture-Avoiding Substitution • What happens if • the free variable y is captured and becomes a bound variable. • To an outside observer, it suddenly disappears!
Capture-Avoiding Substitution in Action • We have to rename bound variables as necessary.
-Conversion • Renaming bound variables when necessary • Okay because the names of bound variables do not matter. • Examples
Formalization of -Conversion • See the course notes! • It's more interesting than you might think.
Outline • Abstract syntax of the -calculus V • Operational semantics of the l-calculus V • Substitutions V • Programming in the -calculus
Booleans • A boolean value • "Give me two options and I will choose one for you!" • Syntactic sugar
Examples • Under the call-by-name strategy,
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 "
Addition • Key observation:
Multiplication • Key observation: • Alternatively
Recursive Functions in -Calculus • Plan of attack • Assume a recursive function constructfun f x. e • Rewrite it as an expression in the -calculusi.e., show that it is syntactic sugar.
Example: Factorial • Plan of attack • Assume a recursive function construct • Rewrite it as an expression in the -calculusi.e., show that it is syntactic sugar.
Fixed Point • Fixed point of function f V such that V = f (V) • Ex. f (x) = 2 - x fixed point of f = 11 = f (1)
Magic Revealed • Fixed point combinator / Y combinator (call-by-value) • fix F returns a fixed point of F.
Now we only need to find a fixed point of: Now we only need to compute: