Conditions, Booleans and Integers

Lambda Calculus · v1.1.1

2026-10-26 12:21:16

Where we are

Scope and prerequisites

  • Unit 2 fixed the language: three grammar productions, and the conversions β, α and η
  • Nothing is added to it here
  • select_first, select_second and make_pair become truth values, conditionals and numbers
  • No primitive booleans and no primitive integers underneath

Every definition below expands to pure λ terms.

Outline of key topics

  • Truth values as selectors, and the conditional cond
  • The boolean operators not, and, or
  • The natural numbers: zero, succ, iszero, pred
  • Simplified bracketing and definition notation
  • The library built so far

From pure functions to things worth computing

The method

  1. choose a representation that makes the required operations easy
  2. define each operation as a λ expression
  3. verify it by reduction
  • true is select_first; false is select_second
  • the logical operations are built from a version of make_pair
  • a number is zero, or a succ wrapping another number

Unit 1 claimed data needs no separate mechanism. This unit makes it good.

Truth values and the conditional

Why selectors represent truth values

<condition> ? <expr> : <expr>

max  = x>y ? x : y
absx = x<0 ? -x : x
  • TRUE selects the first expression, FALSE the second
  • a truth value’s whole job is to choose between two alternatives
  • select_first and select_second do exactly that

Definition — cond

def cond = λe1.λe2.λc.((c e1) e2)

\[((\mathrm{cond}\ E_1)\ E_2) \Rightarrow \lambda c.((c\ E_1)\ E_2)\]

  • a version of make_pair whose last argument is the condition
  • the branches are supplied first, building a function
  • that function is handed the condition, which selects

Reduction — cond selects a branch

\[ \begin{align} &(\lambda c.((c\ E_1)\ E_2)\ \mathrm{select\_first}) \Rightarrow ((\mathrm{select\_first}\ E_1)\ E_2) \Rightarrow E_1 \\[8pt] &(\lambda c.((c\ E_1)\ E_2)\ \mathrm{select\_second}) \Rightarrow ((\mathrm{select\_second}\ E_1)\ E_2) \Rightarrow E_2 \end{align} \]

\(E_1\) and \(E_2\) stand for any two expressions. Selection is the whole of the conditional.

Definition — true and false

def true  = select_first
def false = select_second
  • if is a control structure elsewhere, and booleans a primitive type; here both are ordinary functions
  • the conditional X ? A : B is (((cond A) B) X)
  • which simplifies to ((X A) B) — the condition applied straight to the branches

That simplified form is what every operator below uses.

The boolean operators

Definition — not

X NOT X
FALSE TRUE
TRUE FALSE
  • as a conditional: X ? FALSE : TRUE
  • simplified from λx.(((cond false) true) x)
def not = λx.((x false) true)

\[(\mathrm{not}\ \mathrm{true}) \Rightarrow ((\mathrm{true}\ \mathrm{false})\ \mathrm{true}) \Rightarrow \mathrm{false}\]

Definition — and

X Y X AND Y
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE
  • left operand TRUE: the value is the right operand
  • left operand FALSE: the value is FALSE
  • as a conditional: X ? Y : FALSE
def and = λx.λy.((x y) false)

\[((\mathrm{and}\ \mathrm{true})\ \mathrm{false}) \Rightarrow ((\mathrm{true}\ \mathrm{false})\ \mathrm{false}) \Rightarrow \mathrm{false}\]

Definition — or

X Y X OR Y
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE TRUE
  • first operand TRUE: the value is TRUE
  • otherwise: the value is the second operand
  • as a conditional: X ? TRUE : Y
def or = λx.λy.((x true) y)

\[((\mathrm{or}\ \mathrm{false})\ \mathrm{true}) \Rightarrow ((\mathrm{false}\ \mathrm{true})\ \mathrm{true}) \Rightarrow \mathrm{true}\]

Short-circuiting

def and = λx.λy.((x y) false)
  • a false first operand selects false
  • the second operand is discarded unevaluated
  • nothing in the definition asked for this

Elsewhere short-circuiting is a rule written into the language definition; here it follows from the encoding.

The natural numbers

Definition — zero and succ

def zero = identity
def succ = λn.λs.((s false) n)
  • an integer is that many successors of zero
  • succ n builds a pair: false first, n second
  • nesting depth is the value; no notion of quantity appears anywhere

Example — one and two

\[ \begin{align} \mathrm{one} \equiv (\mathrm{succ}\ \mathrm{zero}) &\Rightarrow \lambda s.((s\ \mathrm{false})\ \mathrm{zero}) \\[6pt] \mathrm{two} \equiv (\mathrm{succ}\ \mathrm{one}) &\Rightarrow \lambda s.((s\ \mathrm{false})\ \lambda s.((s\ \mathrm{false})\ \mathrm{zero})) \end{align} \]

  • three is one layer deeper again
  • every number but zero is a function taking a selector

Definition — iszero

def iszero = λn.(n select_first)

\[ \begin{align} &(\lambda s.((s\ \mathrm{false})\ N)\ \mathrm{select\_first}) \Rightarrow ((\mathrm{select\_first}\ \mathrm{false})\ N) \Rightarrow \mathrm{false} \\[6pt] &(\mathrm{zero}\ \mathrm{select\_first}) \Rightarrow \mathrm{select\_first} \equiv \mathrm{true} \end{align} \]

  • the number is applied to the selector, not the selector to the number
  • zero is identity, so it returns the selector unchanged

Definition — pred

def pred1 = λn.(n select_second)
def pred  = λn.(((iszero n) zero) (n select_second))

\[ \begin{align} &(\lambda s.((s\ \mathrm{false})\ N)\ \mathrm{select\_second}) \Rightarrow N \\[6pt] &(\mathrm{pred1}\ \mathrm{zero}) \Rightarrow \mathrm{select\_second} \equiv \mathrm{false} \quad {\color{#c0392b}\boldsymbol{\times}} \end{align} \]

  • select_second strips one layer of nesting
  • false is not a number, so pred tests for zero and returns zero there

Using pred requires checking for a zero argument.

Notation that keeps it readable

Dropping brackets

\[ \begin{align} &(\ldots((f\ a_1)\ a_2)\ \ldots\ a_N) \\ \text{may be written}\quad &f\ a_1\ a_2\ \ldots\ a_N \end{align} \]

  • a function is applied first to the nearest argument on the right
  • an argument that is itself an application keeps its brackets
  • brackets stay around applications in a function body
def pred = λn.((iszero n) n (n select_second))

n replaces zero in that branch: it is reached only when iszero n holds.

Abbreviating definitions

def <names> = λ<name>.<expression>
def <names> <name> = <expression>
def identity x = x
def self_apply s = s s
def apply func arg = func arg

The sugar is always removable: expand back to pure terms when a derivation confuses.

What you can now build

The library

def cond   = λe1.λe2.λc.((c e1) e2)
def true   = select_first
def false  = select_second
def not    = λx.((x false) true)
def and    = λx.λy.((x y) false)
def or     = λx.λy.((x true) y)
def zero   = identity
def succ   = λn.λs.((s false) n)
def iszero = λn.(n select_first)
def pred   = λn.(((iszero n) zero) (n select_second))

Booleans and integers, built from three productions and three conversions.

What is still missing

  • succ and pred step by one
  • no addition, no multiplication, no comparison
  • each needs an operation repeated a number of times
  • repetition needs recursion

Summary

Summary

  • Truth values are selector functions
  • The conditional is an ordinary function, cond
  • Natural numbers are nested pairs; zero is identity
  • The representation is chosen so the operations become easy
  • Sugar bridges pure terms and readable definitions

Where next

Recursion and Arithmetic — the next unit in this module.

  • A definition that mentions its own name cannot work: a name is an abbreviation that expands
  • The way out is the paradoxical combinator
  • Once it exists, the arithmetic follows quickly