2023-02-05 21:02:05 -08:00
\section { Numbers}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Since the only objects we have in $ \lm $ -calculus are functions, it's natural to think of quantities as \textit { adverbs} (once, twice, thrice,...) rather than \textit { nouns} (one, two, three ...) \\
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
We'll start with zero. If our numbers are \textit { once,} \textit { twice,} and \textit { twice} , it may make sense to make zero \textit { don't} . \\
Here's our \textit { don't} function: given a function and an input, don't apply the function to the input.
$$
0 = \lm fa.a
$$
If you look closely, you'll find that $ 0 $ is $ \alpha $ -equivalent to the false function $ F $ .
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Write $ 1 $ , $ 2 $ , and $ 3 $ . We will call these \textit { Church numerals} .\hspace { -0.5ex} \footnotemark { } \\
\note { \textit { Note:} This problem read aloud is \say { Define \textit { once} , \textit { twice} , and \textit { thrice} .} }
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\footnotetext { after Alonzo Church, the inventor of lambda calculus and these numerals. He was Alan Turing's thesis advisor.}
\begin { solution}
$ 1 $ calls a function once on its argument: \\
$ 1 = \lm fa . ( f~a ) $ .
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Naturally, \\
$ 2 = \lm fa . [ ~f~ ( f~a ) ~ ] $ \\
$ 3 = \lm fa . [ ~f~ ( f~ ( f~a ) ) ~ ] $
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
The round parentheses are \textit { essential} . Our lambda calculus is left-associative!
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 2ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Also, notice that $ 1 $ is very similar to $ I $ :
$$
I~\heartsuit ~\star = 1~\heartsuit ~\star
$$
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Zero is false, and one is the identity. Isn't that wonderful?
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
What is $ ( 4 ~I ) ~ \star $ ?
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
What is $ ( 3 ~NOT~T ) $ ? \\
How about $ ( 8 ~NOT~F ) $ ?
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\pagebreak
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
This handout may remind you of Professor Oleg's handout on Peano's axioms. Good. \\
2023-04-02 09:20:18 -07:00
Recall the tools we used to build the natural numbers: \\
2023-02-05 21:02:05 -08:00
We had a zero element and a \say { successor} operation so that $ 1 \coloneqq S ( 0 ) $ , $ 2 \coloneqq S ( 1 ) $ , and so on.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Create a successor operation for the Church numerals. \\
\hint { A good signature for this function is $ \lm nfa $ , or more clearly $ \lm n. \lm fa $ . Do you see why?}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ S = \lm n. [ \lm fa . f~ ( n~f~a ) ] = \lm nfa . [ f~ ( n~f~a ) ] $
2022-11-13 13:02:25 -08:00
\vspace { 1ex}
2023-02-05 21:02:05 -08:00
Do $ f $ $ n $ times, then do $ f $ one more time.
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Verify that $ S ( 0 ) = 1 $ and $ S ( 1 ) = 2 $ .
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
\pagebreak
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Assume that only Church numerals will be passed to the functions in the following problems. \\
We make no promises about their output if they're given anything else.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Define a function ADD that adds two Church numerals.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ \text { ADD } = \lm mn . ( m~S~n ) = \lm mn . ( n~S~m ) $ \\
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { instructornote}
Defining \say { equivalence} is a bit tricky. The solution above illustrates the problem pretty well.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\note { Note: The notions of \say { extensional} and \say { intentional} equivalence may be interesting in this context. Do some reading on your own.
}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 4ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
These two definitions of ADD are equivalent if we apply them to Church numerals. If we were to apply these two versions of ADD to functions that behave in a different way, we'll most likely get two different results! \\
As a simple example, try applying both versions of ADD to the Kestrel and the Kite.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
To compare functions that aren't $ \alpha $ -equivalent, we'll need to restrict our domain to functions of a certain form, saying that two functions are equivalent over a certain domain. \\
\end { instructornote}
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Adding is nice, but we can do better. \\
Design a function MULT that multiplies two numbers. \\
\hint { The easy solution uses ADD, the elegant one doesn't. Find both!}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ \text { MULT } = \lm mn . [ m~ ( \text { ADD } ~n ) ~m ] $
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
$ \text { MULT } = \lm mnf . [ m~ ( n~f ) ] $
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
\pagebreak
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Define the functions $ Z $ and $ NZ $ . $ Z $ should reduce to $ T $ if its input was zero, and $ F $ if it wasn't. \\
$ NZ $ does the opposite. $ Z $ and $ NZ $ should look fairly similar.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ Z \phantom { N } = \lm n . [ n~ ( \lm a.F ) ~T ] $ \\
$ NZ = \lm n . [ n~ ( \lm a.T ) ~F ] $
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Data structures will be useful. Design an expression PAIR that constructs two-value tuples. \\
For example, say $ A = \text { PAIR } ~ 1 ~ 2 $ . Then, \\
$ ( A~T ) $ should reduce to $ 1 $ \\
$ ( A~F ) $ should reduce to $ 2 $
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ \text { PAIR } = \lm ab . \lm i . ( i~a~b ) = \lm abi.iab $
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
From now on, I'll write (PAIR $ A $ $ B $ ) as $ \langle A,B \rangle $ . \\
Like currying, this is only notation. The underlying $ \lm $ logic remains the same.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\pagebreak
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { } <shiftadd>
Write a function $ H $ , which we'll call \say { shift and add.} \\
It does exactly what it says on the tin: \\
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Given an input pair, it should shift its right argument left, then add one. \\
$ H~ \langle 0 , 1 \rangle $ should reduce to $ \langle 1 , 2 \rangle $ \\
$ H~ \langle 1 , 2 \rangle $ should reduce to $ \langle 2 , 3 \rangle $ \\
$ H~ \langle 10 , 4 \rangle $ should reduce to $ \langle 4 , 5 \rangle $ \\
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ H = \lm p . \Bigl \langle ~ ( p~F ) ~,~S ( p~F ) ~ \Bigr \rangle $
2022-11-13 13:02:25 -08:00
\vspace { 1ex}
2023-02-05 21:02:05 -08:00
Note that $ H~ \langle 0 , 0 \rangle $ reduces to $ \langle 0 , 1 \rangle $
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\problem { }
Design a function $ D $ that un-does $ S $ . That means \\
$ D ( 1 ) = 0 $ , $ D ( 2 ) = 1 $ , etc. $ D ( 0 ) $ should be zero. \\
\hint { $ H $ will help you make an elegant solution.}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
$ D = \lm n . \Bigl [ ( ~n~H~ \langle 0 , 0 \rangle ~ ) ~T \Bigr ] $
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin { solution}
Here's a different solution. \\
Can you figure out how it works? \\
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
$
D_ 0 =
\lm p . \Bigl [p~T\Bigr]
\Bigl \langle
F ~,~ p~F
\Bigr \rangle
\Bigl \langle
F
~,~
\bigl \langle
p~F~T ~,~ ( (p~F~T)~(P~F~F) )
\bigr \rangle
\Bigr \rangle
$
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vspace { 1ex}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
$
D = \lm nfa .
\Bigl (
n D_ 0 \Bigl \langle T, \langle f, a \rangle \Bigr \rangle
\Bigr )~F~F
$
\end { solution}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
\pagebreak