Chapter 3: Church Numerals: Increment
Church Numerals
The most important data in computer programs are integers.
Let's define natural integers.
0 = λ s . λ z . z 1 = λ s . λ z . s ( z ) 2 = λ s . λ z . s ( s ( z ) ) 3 = λ s . λ z . s ( s ( s ( z ) ) ) ⋮ n = λ s . λ z . s n ( z ) 0=\lambda s.\lambda z.z\\
1=\lambda s.\lambda z.s(z)\\
2=\lambda s.\lambda z.s(s(z))\\
3=\lambda s.\lambda z.s(s(s(z)))\\
\vdots\\
n=\lambda s.\lambda z.s^n(z) 0 = λ s . λ z . z 1 = λ s . λ z . s ( z ) 2 = λ s . λ z . s ( s ( z )) 3 = λ s . λ z . s ( s ( s ( z ))) ⋮ n = λ s . λ z . s n ( z )
Why should we define integers like this? What does s s s and z z z mean here?
Let's take 2 2 2 for example to watch how church numerals could be converted back to normal ones. let s = λ x . ( x + 1 ) , z = 0 s=\lambda x.(x+1), z=0 s = λ x . ( x + 1 ) , z = 0 , then:
2 s z = ( λ s . λ z . s ( s ( z ) ) ) s z = s ( s ( z ) ) = s ( s ( 0 ) ) = s ( 0 + 1 ) = 1 + 1 = 2 \begin{array}{}
2sz&=&(\lambda s.\lambda z.s(s(z)))sz\\
&=&s(s(z))\\
&=&s(s(0))\\
&=&s(0+1)\\
&=&1+1\\
&=&2\\
\end{array} 2 sz = = = = = = ( λ s . λ z . s ( s ( z ))) sz s ( s ( z )) s ( s ( 0 )) s ( 0 + 1 ) 1 + 1 2
It is clear now: s s s stands for succussor and z z z stands for zero. It is built base on recursion. And yes, it is exactly how natural numbers are defined mathematically.
Successor
To get the successor of a church numeral, just simply wrap an s s s around it.
S = λ n . λ s . λ z . ( s ( n s z ) ) S n = n + 1 S=\lambda n.\lambda s.\lambda z.(s(nsz))\\
Sn=n+1 S = λn . λ s . λ z . ( s ( n sz )) S n = n + 1
Proof:
S n = ( λ n . λ s . λ z . ( s ( n s z ) ) ) ( λ s . λ z . s n ( z ) ) = ( λ s ′ . λ z ′ . ( s ′ ( ( λ s . λ z . s n ( z ) ) s ′ z ′ ) ) ) = ( λ s ′ . λ z ′ . ( s ′ ( s ′ n ( z ′ ) ) ) ) = ( λ s ′ . λ z ′ . ( s ′ n + 1 ( z ′ ) ) ) = ( λ s . λ z . ( s n + 1 ( z ) ) ) = n + 1 \begin{array}{}
Sn&=&(\lambda n.\lambda s.\lambda z.(s(nsz)))(\lambda s.\lambda z.s^n(z))\\
&=&(\lambda s'.\lambda z'.(s'((\lambda s.\lambda z.s^n(z))s'z')))\\
&=&(\lambda s'.\lambda z'.(s'(s'^n(z'))))\\
&=&(\lambda s'.\lambda z'.(s'^{n+1}(z')))\\
&=&(\lambda s.\lambda z.(s^{n+1}(z)))\\
&=&n+1\\
\end{array} S n = = = = = = ( λn . λ s . λ z . ( s ( n sz ))) ( λ s . λ z . s n ( z )) ( λ s ′ . λ z ′ . ( s ′ (( λ s . λ z . s n ( z )) s ′ z ′ ))) ( λ s ′ . λ z ′ . ( s ′ ( s ′ n ( z ′ )))) ( λ s ′ . λ z ′ . ( s ′ n + 1 ( z ′ ))) ( λ s . λ z . ( s n + 1 ( z ))) n + 1
Addition
A Church numeral n n n is actually a recursion builder repeating a function for n n n times. Making use of this the addition operation of church numerals could be deduced.
add = λ m n . ( m S n ) add m n = m + n \text{add}=\lambda mn.(mSn)\\
\text{add }mn=m+n add = λmn . ( m S n ) add mn = m + n
It reads as if "evaluating m th m^\text{th} m th successor of n n n ".
Proof:
add m n = ( λ m n . ( m S n ) ) m n = ( λ s . λ z . s m ( z ) ) S ( λ s . λ z . s n ( z ) ) = λ z . S m ( z ) ( λ s . λ z . s n ( z ) ) = S m ( λ s . λ z . s n ( z ) ) = λ s . λ z . s m + n ( z ) = m + n \begin{array}{}
\text{add }mn&=&(\lambda mn.(mSn))mn\\
&=&(\lambda s.\lambda z.s^m(z))S(\lambda s.\lambda z.s^n(z))\\
&=&\lambda z.S^m(z)(\lambda s.\lambda z.s^n(z))\\
&=&S^m(\lambda s.\lambda z.s^n(z))\\
&=&\lambda s.\lambda z.s^{m+n}(z)\\
&=&m+n\\
\end{array} add mn = = = = = = ( λmn . ( m S n )) mn ( λ s . λ z . s m ( z )) S ( λ s . λ z . s n ( z )) λ z . S m ( z ) ( λ s . λ z . s n ( z )) S m ( λ s . λ z . s n ( z )) λ s . λ z . s m + n ( z ) m + n
Actually, there is a direct definition of addition.
add = λ m . λ n . λ s . λ z . m s ( n s z ) \text{add}=\lambda m.\lambda n.\lambda s.\lambda z.ms(nsz)\\ add = λm . λn . λ s . λ z . m s ( n sz )
It reads as if "evaluating m th m^\text{th} m th successor of 'n th n^\text{th} n th successor of 0 0 0 '".
Proof is similar to indirect one.
Multiplication
Indirect definition:
mul = λ m n . m ( add n ) 0 mul m n = m × n \text{mul}=\lambda mn.m(\text{add }n)0\\
\text{mul }mn=m\times n mul = λmn . m ( add n ) 0 mul mn = m × n
It reads as if "add n n n for m m m times to 0 0 0 ".
Direct definition:
mul = λ m . λ n . λ s . λ z . m ( n s ) z \text{mul}=\lambda m.\lambda n.\lambda s.\lambda z.m(ns)z mul = λm . λn . λ s . λ z . m ( n s ) z
It reads as if "'evaluating n th n^\text{th} n th successor' for m m m times of 0 0 0 ".
Proofs are similar to the addition operation.
Exponentiation
Indirect definition:
exp = λ m n . n ( mul m ) 1 exp m n = m n \text{exp}=\lambda mn.n(\text{mul }m)1\\
\text{exp }mn=m^n exp = λmn . n ( mul m ) 1 exp mn = m n
It reads as if "multiplies m m m for n n n times to 1 1 1 ".
Proof is similar to the multiplication operation.
Direct definition:
exp = λ m . λ n . λ s . λ z . n m s z \text{exp}=\lambda m.\lambda n.\lambda s.\lambda z.nmsz exp = λm . λn . λ s . λ z . nm sz
Or just
exp = λ m . λ n . n m \text{exp}=\lambda m.\lambda n.nm exp = λm . λn . nm
for short.
Proof:
exp m n = n m = ( λ s . λ z . s n ( z ) ) m = λ z . m n ( z ) = λ s . m n ( s ) = λ s . λ z . m n ( s ) z = λ s . λ z . m ( ⋯ m ( m ⏟ n times s ) ) z = λ s . λ z . m ( ⋯ m ( m ⏟ n − 1 times ( λ z 1 . s m ( z 1 ) ) ⏟ apply s for m times ) ) z = λ s . λ z . m ( ⋯ m ( m ⏟ n − 2 times ( λ z 2 . ( λ z 1 . s m ( z 1 ) ) m ( z 2 ) ) ) ⏟ apply ‘apply s for m times’ for m times ) ) z = λ s . λ z . m ( ⋯ m ( m ⏟ n − 2 times ( λ z 2 . ( λ z 1 . s m ( z 1 ) ) m ( z 2 ) ) ) ⏟ apply s for m × m = m 2 times ) ) z = ⋯ = λ s . λ z . λ z n . ( ⋯ ( λ z 2 . ( λ z 1 . s m ( z 1 ) ) m ( z 2 ) ) ⋯ ) m ( z n ) ⏟ apply s for m × m × ⋯ × m ⏟ n times = m n times z = λ s . λ z . s m n ( z ) = m n \begin{array}{}
\text{exp }mn&=&nm\\
&=&(\lambda s.\lambda z.s^n(z))m\\
&=&\lambda z.m^n(z)\\
&=&\lambda s.m^n(s)\\
&=&\lambda s.\lambda z.m^n(s)z\\
&=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n \text{ times}}s))z\\
&=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n-1 \text{ times}}\underbrace{(\lambda z_1.s^m(z_1))}_{\text{apply } s \text{ for } m \text{ times}}))z\\
&=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n-2 \text{ times}}\underbrace{(\lambda z_2.(\lambda z_1.s^m(z_1))^m(z_2)))}_{\text{apply `apply } s \text{ for } m \text{ times' for } m \text{ times}}))z\\
&=&\lambda s.\lambda z.\underbrace{m(\cdots m(m}_{n-2 \text{ times}}\underbrace{(\lambda z_2.(\lambda z_1.s^m(z_1))^m(z_2)))}_{\text{apply } s \text{ for } m\times m=m^2 \text{ times}}))z\\
&=&\cdots\\
&=&\lambda s.\lambda z.\underbrace{\lambda z_n.(\cdots(\lambda z_2.(\lambda z_1.s^m(z_1))^m(z_2))\cdots)^m(z_n)}_{\text{apply } s \text{ for } \underbrace{m\times m \times\cdots \times m}_{n \text{ times}}=m^n \text{ times}}z\\
&=&\lambda s.\lambda z.s^{m^n}(z)\\
&=&m^n
\end{array} exp mn = = = = = = = = = = = = = nm ( λ s . λ z . s n ( z )) m λ z . m n ( z ) λ s . m n ( s ) λ s . λ z . m n ( s ) z λ s . λ z . n times m ( ⋯ m ( m s )) z λ s . λ z . n − 1 times m ( ⋯ m ( m apply s for m times ( λ z 1 . s m ( z 1 )) )) z λ s . λ z . n − 2 times m ( ⋯ m ( m apply ‘apply s for m times’ for m times ( λ z 2 . ( λ z 1 . s m ( z 1 ) ) m ( z 2 ))) )) z λ s . λ z . n − 2 times m ( ⋯ m ( m apply s for m × m = m 2 times ( λ z 2 . ( λ z 1 . s m ( z 1 ) ) m ( z 2 ))) )) z ⋯ λ s . λ z . apply s for n times m × m × ⋯ × m = m n times λ z n . ( ⋯ ( λ z 2 . ( λ z 1 . s m ( z 1 ) ) m ( z 2 )) ⋯ ) m ( z n ) z λ s . λ z . s m n ( z ) m n
Sequences