PROGRAMMING PARADIGMS · SESSION 4 · BLOCK 2

Functional programming and control

Block 2 — the formalism: the λ-calculus
SW7 · Aalborg University Copenhagen · Autumn 2026
PART 5

The calculus

Block 1’s answers in one formalism: the smallest language that computes everything, and the one language semantics is written in

Syntax: Three forms

xa variable
λx. ta function of one argument x, with body t
t uan application of t to u
application groups to the leftf 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.

No numbers, no booleans, no let, no if. Everything else is defined from these three forms.

Free and bound variables

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.

β-reduction

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 and capture

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.

Which redex first?

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.

normal order

applicative order

Church and Rosser, 1936: a term has at most one normal form. A path may fail to reach it — the next slide — but no path reaches a different one. The order changes the path, and whether we arrive; never the answer.

Does the order matter?

Ω = (λx. x x) (λx. x x) reduces to itself, forever. Apply a function that ignores its argument to it:

normal order

applicative order

In the calculus, the order never changes the normal form (up to renaming bound variables); it decides whether we get one. Normal order finds the normal form whenever one exists.

Reduction orders and parameter-passing modes

reduction orderthe mode, block 1languages
applicative: arguments firstcall-by-value (weak)C, Java, Python, JavaScript, OCaml, Scheme
normal: arguments when used, each timecall-by-name (weak)Algol 60 — its default
normal, with sharing: each argument oncecall-by-needHaskell
Evaluation strategy: a design decision every language has made, mostly the same way.

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.

Small-step operational semantics: the rules

call by value

e-app1\[\frac{t_1 \to t_1'}{t_1\; t_2 \to t_1'\; t_2}\]
e-app2\[\frac{t_2 \to t_2'}{v\; t_2 \to v\; t_2'}\]
e-appabs\[(\lambda x.\, t)\; v \;\to\; t[x := v]\]

First the function part is reduced, then the argument, until each is a value v — a λ; only then does β apply.

call by name

e-app1\[\frac{t_1 \to t_1'}{t_1\; t_2 \to t_1'\; t_2}\]
e-appabs\[(\lambda x.\, t_1)\; t_2 \;\to\; t_1[x := t_2]\]

Only the function part is reduced, until it is a λ; then β applies with the argument still unevaluated.

No rule reduces inside a λ: both are weak. The lab’s step_cbv and step_cbn are these rules, one match case each.
PART 6

Everything from functions

Booleans, numbers, if, recursion — defined, not built in

Booleans are choices

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-value

Block 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 () ->.

Here, the branches delayed by hand; on the previous slide, the language evaluating by name.

Numbers are repetitions

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)
A number applies a function n times: a number is its own fold — block 1’s The body as a parameter, literally.

Recursion without let rec

The 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 under call-by-value

 

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.

Laziness: by-name with sharing

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.

When the language decides laziness, an infinite list is a value. Session 5: when the program decides it — thunks and streams in a strict language.

Evaluation strategies, stated

The three strategies, side by side: when an argument is evaluated, and what each choice costs.

strategyan argument is evaluatedwhat it costs
call-by-valuebefore the call, oncework on arguments never used; Ω is fatal
call-by-nameat each usethe same argument evaluated many times
call-by-needat first use, oncea thunk per argument; effect order hidden

The second cost is why Haskell keeps effects out of ordinary expressions — a consequence of laziness.

Control as syntax, control as a value

no syntaxThe calculus has no ;, no while, no if. The text says where a step may happen, never which: the order is not in the program.
valuesSo 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 againA 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.
Functions as values are not a feature the calculus happens to have. They are what a language without control syntax must have — and they suffice.
PART 7

Conclusion

The first answer, and the lab

Who decides the next step — after session 4

sessiontopicwho decides the next step
S4evaluationthe language: whether a call keeps a frame, in what order the arguments go — and when an argument is evaluated — its evaluation strategy
S5higher-order functionsthe program, by passing the next step as a function: a loop body, a thunk, a continuation
S6asyncthe program yields; an event loop decides
S7threadsthe operating system — preemption
S8message passingthe other process — communication
S9parallelismnobody needs to — the answer is order-independent

Lab 4: an evaluator for the calculus

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-upreductions on paper; free_vars, subst without capture
Astep under call-by-value and call-by-name, with a trace: Ω and (λx y. y) Ω in our own code
Ba big-step evaluator with environments: a function value is a closure
homeChurch arithmetic as terms; Y and Z in our evaluator; a small ML

What does the closure carry? — session 5's first question.

References

  • Turing, 1936–37. On computable numbers, with an application to the Entscheidungsproblem. Church, 1936. An unsolvable problem of elementary number theory. Kleene, 1936.
  • Church and Rosser, 1936. Some properties of conversion. Curry and Feys, 1958. Barendregt, 1984. The Lambda Calculus.
  • Plotkin, 1975. Call-by-name, call-by-value and the λ-calculus. Wadsworth, 1971. Ariola and Felleisen, 1997. The call-by-need lambda calculus.
  • Landin, 1964. The mechanical evaluation of expressions. Reynolds, 1972. Definitional interpreters for higher-order programming languages.
  • Steele, 1977. Debunking the "expensive procedure call" myth. Clinger, 1998. Proper tail recursion and space efficiency.
  • Wirth, 1976. Algorithms + Data Structures = Programs. Kowalski, 1979. Algorithm = Logic + Control. Backus, 1978. Can programming be liberated from the von Neumann style?
  • Böhm and Berarducci, 1985. Automatic synthesis of typed Λ-programs on term algebras. Hughes, 1989. Why functional programming matters.