Lambda
Lambda
J. Carette
McMaster University
Fall 2023
Concrete Syntax
▶ The “surface syntax” used by programmers
Abstract Syntax
▶ Often a tree, sometimes a Directed Acyclic Graph (DAG)
▶ The “internal representation” that’s nicer for programs to compute with.
Concrete to Abstract:
Nice-to-have but redundant constructs removed (aka desugaring)
Missing information is added (type inference and elaboration)
i.e. (s t) u
∃x | x ̸= y (1)
(λx.x y ) x (2)
These terms reduce by substituting the abstracted variable with the term
applied to the function. In other words:
(λx.t1 ) t2 → [x 7→ t2 ] t1 (3)
(λx.x) y → y (4)
Normal order begins with the leftmost, outermost redex, and proceeds until
there are no more redexes to evaluate.
The call by name strategy is more restrictive than normal order. You can’t
evaluate anything under a lambda.
Most languages use call by value, where only the outermost redexes are
reduced, and a redex is only reduced when the right-hand-side has already
been reduced to a value.
Here, as elsewhere, a value is a term in normal form.
More operations.
and = λb.λc. b c fls (14)
c0 = λs.λz. z (18)
c1 = λs.λz. s z (19)
c2 = λs.λz. s (s z) (20)
c3 = λs.λz. s (s (s z)) (21)
..
.
You might have noticed that c0 has the same definition as fls.
This is sometimes called a pun in computer science.
The same thing occurs in lower level languages, where the interpretation
of a sequence of bits is context dependant.
In C, the bit arrangement 0x00000000 corresponds to:
▶ Zero (Integer)
▶ False (Boolean)
▶ ”\0\0\0\0” (Character Array)
This is not a good thing.
Adding one:
succ = λn.λs.λz. s (n s z) (22)
Successor of Two
succ c2
→ (λn.λs.λz. s (n s z)) c2
→ λs.λz. s (c2 s z)
→ λs.λz. s ((λs.λz. s (s z)) s z)
→ λs.λz. s ((λz. s (s z)) z)
→ λs.λz. s (s (s z))
→ c3
↛
plus c2 c2
→ (λm.λn.λs.λz. m s (n s z))c2 c2
→ (λn.λs.λz. c2 s (n s z))c2
→ λs.λz. c2 s (c2 s z)
→ λs.λz. (λs.λz. s (s z)) s ((λs.λz. s (s z)) s z)
→ λs.λz. (λz. s (s z)) ((λs.λz. s (s z)) s z)
→ λs.λz. (s (s ((λs.λz. s (s z)) s z)))
→ λs.λz. (s (s ((λz. s (s z)) z)))
→ λs.λz. (s (s (s (s z))))
→ c4
↛
3 × 2 =?
times c3 c2
→ (λm.λn. m (plus n) c0 ) c3 c2
→ (λn. c3 (plus n) c0 ) c2
→ (λs.λz. s (s (s z))) (plus c2 ) c0
→ (plus c2 ) ((plus c2 ) ((plus c2 ) c0 ))
Technically this is cheating, since we don’t have a rule for this type of
substitution in the semantic, and it violates our evaluation strategy.
plus c2
→ (λm.λn.λs.λz. m s (n s z)) (λs.λz. s (s z))
→ (λn.λs.λz. (λs.λz. s (s z)) s (n s z))
→ (λn.λs.λz. (λz. s (s z)) (n s z))
→ (λn.λs.λz. (s (s (n s z))))