| x | a variable |
| λx. t | a function of one argument x, with body t |
| t u | an application of t to u |
| application groups to the left | f a b = (f a) b |
| a body extends as far right as it can | λx. x y = λx. (x y) |
| currying: a function of two arguments is a function returning a function (session 5) | λx. λy. t = λx y. t |
Note: In JavaScript the three forms are x, x => e and f(a): λx. 2 + x reads x => 2 + x — except that 2 and + are not in the calculus.
let, no if. Everything else is defined from these three forms.In λx. x y, x is bound by the λ; y is free: nothing in the term says what it is. A term with no free variable is closed.
free (x) = {x}
free (λx. t) = free t − {x}
free (t u) = free t ∪ free u
These three lines are the whole definition; the lab's warm-up writes them in OCaml.
Note: The same name can be bound in one place and free in another: in x (λx. x), the first x is free.
t[x := u] is t with u put in for every free x — substitution. One rule: (λx. t) u → t[x := u]: the body, with the argument substituted for the variable.
An application of a λ is a redex. A term with no redex is in normal form: the computation is over. t →* u: u is reached from t in some number of steps.
In the trace, β is the rule; δ unfolds a name such as Y into its definition — a courtesy, not a rule.
Substitution is defined case by case:
x[x := u] = u
y[x := u] = y if y ≠ x
(t s)[x := u] = t[x := u] s[x := u]
(λy. t)[x := u] = λy. t[x := u] if y ≠ x, y not free in u
The last case carries a condition, and the condition matters: in (λx. λy. x) y, putting y in for x without it gives λy. y — the free y has been captured by the λy, and the meaning has changed.
The solution: a bound variable can be renamed without changing the meaning of the term: α-conversion. Rename the binder first, then substitute: (λx. λy'. x) y → λy'. y.
This term has three redexes, so the rule allows three different first steps. Normal order takes the outermost one, the leftmost among those; applicative order takes the innermost.
Ω = (λx. x x) (λx. x x) reduces to itself, forever. Apply a function that ignores its argument to it:
| reduction order | the mode, block 1 | languages |
|---|---|---|
| applicative: arguments first | call-by-value (weak) | C, Java, Python, JavaScript, OCaml, Scheme |
| normal: arguments when used, each time | call-by-name (weak) | Algol 60 — its default |
| normal, with sharing: each argument once | call-by-need | Haskell |
Programming languages evaluate weakly: a function body runs only when the function is called, never inside a λ. So does the lab's evaluator; the stepper has a switch.
First the function part is reduced, then the argument, until each is a value v — a λ; only then does β apply.
Only the function part is reduced, until it is a λ; then β applies with the argument still unevaluated.
λ: both are weak. The lab’s step_cbv and step_cbn are these rules, one match case each.true = λt. λf. t if = λc. λt. λe. c t e
false = λt. λf. f
A boolean is a function that, given two things, picks one. if only hands the two branches to the condition.
if is a function — under normal order.
if as a function under call-by-valueBlock 1’s my_if failed: both branches are evaluated before if runs. Two repairs — the language evaluates by name, or the branches are delayed by hand:
fact = λf n. if (iszero n) 1 (mul n (f (pred n)))
factv = λf n. if (iszero n) (λd. 1) (λd. mul n (f (pred n))) I
Z = a term that supplies the recursive call — defined three slides on
Never ends: both branches are evaluated before if chooses.
Ends, with 2: only the chosen branch is applied — a thunk, block 1’s fun () ->.
The number n is the function that applies f to x n times; succ adds one more application, add chains them.
0 = λf x. x 1 = λf x. f x 3 = λf x. f (f (f x))
succ = λn f x. f (n f x) add = λm n f x. m f (n f x)
fold — block 1’s The body as a parameter, literally.let recThe calculus has no let rec, so fact cannot call itself by name. Instead it takes the function to call as an argument f: fact = λf n. if (iszero n) 1 (mul n (f (pred n))). Recursion then needs a term fix that hands a function itself: fix g = g (fix g).
Y = λf. (λx. f (x x)) (λx. f (x x)) Y g →* g (Y g)
Y g evaluates the argument x x first — forever. Z wraps it in a thunk, λv. x x v: the repair of if under call-by-value again.
# let y f = (fun x -> f (x x)) (fun x -> f (x x)) Error: The value x has type 'a -> 'b but an expression was expected of type 'a The type variable 'a occurs inside 'a -> 'b
OCaml refuses Y's type before it can run it — a problem on top of the order problem; session 10.
Call-by-name evaluates an argument every time it is used. Call-by-need evaluates it once, when first used, and remembers the result.
OCamllet x = lazy (print_string "once "; 6 * 7);;
Lazy.force x;;
Lazy.force x;;
once - : int = 42 - : int = 42
ghci> (\x -> 1) undefined
1
ghci> fibs = 0:1:zipWith (+) fibs (tail fibs)
ghci> take 10 fibs
[0,1,1,2,3,5,8,13,21,34]
An argument never used is never evaluated; a list with no end is a value.
The three strategies, side by side: when an argument is evaluated, and what each choice costs.
| strategy | an argument is evaluated | what it costs |
|---|---|---|
| call-by-value | before the call, once | work on arguments never used; Ω is fatal |
| call-by-name | at each use | the same argument evaluated many times |
| call-by-need | at first use, once | a thunk per argument; effect order hidden |
The second cost is why Haskell keeps effects out of ordinary expressions — a consequence of laziness.
| no syntax | The calculus has no ;, no while, no if. The text says where a step may happen, never which: the order is not in the program. |
| values | So control lives in values, and the values are functions: a boolean picks, a numeral repeats, Y recurses, a thunk delays, a continuation continues. This part, and session 5. |
| syntax again | A functional language keeps some control syntax — if, &&, let … in, ;. Each fixes an order the calculus left open. Block 1’s my_if is if with the syntax taken away. |
| session | topic | who decides the next step |
|---|---|---|
| S4 | evaluation | the language: whether a call keeps a frame, in what order the arguments go — and when an argument is evaluated — its evaluation strategy |
| S5 | higher-order functions | the program, by passing the next step as a function: a loop body, a thunk, a continuation |
| S6 | async | the program yields; an event loop decides |
| S7 | threads | the operating system — preemption |
| S8 | message passing | the other process — communication |
| S9 | parallelism | nobody needs to — the answer is order-independent |
type term =
| Var of string
| Lam of string * term
| App of term * term
type value =
| Closure of string * term * env
and env = (string * value) list
| warm-up | reductions on paper; free_vars, subst without capture |
| A | step under call-by-value and call-by-name, with a trace: Ω and (λx y. y) Ω in our own code |
| B | a big-step evaluator with environments: a function value is a closure |
| home | Church arithmetic as terms; Y and Z in our evaluator; a small ML |
What does the closure carry? — session 5's first question.