Cheatsheet

A one-page reference for the λ-calculus and the visualizer.

Syntax

xvariable
λx. Mabstraction (function of x)
\x. Msame — ASCII alternative
M Napplication (left-associative)
λx y z. M= λx. λy. λz. M
let x = e1 in e2= (λx. e2) e1
let x = e1; y = e2 in e3chained let
# commentline comment to end-of-line

Reduction rules

α (rename) λx. x ≡ λy. y
β (apply) (λx. M) N → M[x := N]
η (extensional) λx. f x ≡ f  (if x not free in f)

Strategies

normalleftmost-outermost first; finds normal form if it exists
applicativearguments first; can loop where normal terminates
cbnlike normal but stops at λ-bodies
cbvlike applicative but stops at λ-bodies

Combinators

Iλx. xidentity
Kλx y. xconstant (= true)
Sλx y z. x z (y z)"apply, then weave"
Bλf g x. f (g x)compose
Cλf x y. f y xflip args
Wλf x. f x xduplicate arg
Yλf. (λx. f (x x)) (λx. f (x x))fixed point (lazy)
Zλf. (λx. f (λv. x x v)) (λx. f (λv. x x v))fixed point (strict)
ω(λx. x x) (λx. x x)diverges

Booleans

trueλx y. x
falseλx y. y
ifλp a b. p a b
notλb. b false true
andλp q. p q p
orλp q. p p q
xorλp q. p (not q) q

Pairs

pairλx y s. s x y
fstλp. p true
sndλp. p false

Church numerals

0λf x. x
1λf x. f x
nλf x. fⁿ x (apply f n times)
succλn f x. f (n f x)
plusλm n f x. m f (n f x)
multλm n f. m (n f)
powλm n. n m  (yes, just n m)
predλn f x. n (λg h. h (g f)) (λu. x) (λu. u)
iszeroλn. n (λx. false) true
subλm n. n pred m
leqλm n. iszero (sub m n)
eqλm n. and (leq m n) (leq n m)

Lists (Church-encoded right fold)

A list folds with a cons-style function c over a base case n: [x₁, x₂, x₃] = λc n. c x₁ (c x₂ (c x₃ n))

nilλc n. n
consλh t c n. c h (t c n)
isnilλl. l (λh t. false) true
lengthλl. l (λh r. succ r) 0
headλl. l (λh t. h) nil
mapλf l. l (λh t. cons (f h) t) nil
appendλxs ys. xs cons ys
sumλl. l plus 0

Scott-encoded lists

Scott encoding pattern-matches directly: scons h t = λn c. c h t. Easier head/tail but no fold-by-default.

snilλn c. n
sconsλh t n c. c h t
sheadλl. l nil (λh t. h)
stailλl. l snil (λh t. t)

Binary Lambda Calculus

Tromp's tiny binary encoding of closed λ-terms (de Bruijn indices):

00 Mλ M (abstraction)
01 M Napplication
1ⁿ⁺¹ 0variable with de Bruijn index n

E.g. I = λx. x = 0010. Toggle BLC in the visualizer to see any term as a bit string.

Visualizer shortcuts

Ctrl+Enterdraw current expression
Tabaccept autocomplete
Ctrl+Ffind / replace
?show all keyboard shortcuts
Escclose popup / exit fullscreen / exit presentation

Presentation mode

Space / →step
←step back
Rrun
Ppause / resume
0reset
Ffit to viewport
Hhide HUD

Tree mode

Space / →step the tree
Rrun / pause
Sreset (re-parse and replay)
Ffit tree to view
Mtoggle minimap