2023-02-05 21:02:05 -08:00
\section { Numbers}
2022-11-13 13:02:25 -08:00
2023-10-05 11:23:03 -07: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-10-04 09:42:09 -07: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} . \par
2023-02-05 21:02:05 -08:00
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
$$
2023-10-05 11:23:03 -07:00
If you look closely, you'll find that $ 0 $ is 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}
2023-10-04 09:42:09 -07:00
$ 1 $ calls a function once on its argument: \par
2023-02-05 21:02:05 -08:00
$ 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-10-04 09:42:09 -07:00
Naturally, \par
$ 2 = \lm fa . [ ~f~ ( f~a ) ~ ] $ \par
2023-02-05 21:02:05 -08:00
$ 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-10-05 11:23:03 -07:00
The round parentheses are \textit { essential} . Our lambda calculus is left-associative! \par
Also, note that zero is false and one is the (two-variable) identity.
2023-02-05 21:02:05 -08:00
\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 { }
2023-10-04 09:42:09 -07:00
What is $ ( 3 ~NOT~T ) $ ? \par
2023-02-05 21:02:05 -08:00
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 { }
2023-10-16 15:06:51 -07:00
Peano's axioms state that we only need a zero element and a \say { successor} operation to
build the natural numbers. We've already defined zero.
Now, create a successor operation so that $ 1 \coloneqq S ( 0 ) $ , $ 2 \coloneqq S ( 1 ) $ , and so on. \par
2023-02-05 21:02:05 -08:00
\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-10-04 09:42:09 -07:00
Assume that only Church numerals will be passed to the functions in the following problems. \par
2023-02-05 21:02:05 -08:00
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}
2023-10-04 09:42:09 -07:00
$ \text { ADD } = \lm mn . ( m~S~n ) = \lm mn . ( n~S~m ) $
2023-02-05 21:02:05 -08:00
\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 { }
2023-10-04 09:42:09 -07:00
Design a function MULT that multiplies two numbers. \par
2023-02-05 21:02:05 -08:00
\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 { }
2023-10-04 09:42:09 -07:00
Define the functions $ Z $ and $ NZ $ . $ Z $ should reduce to $ T $ if its input was zero, and $ F $ if it wasn't. \par
2023-02-05 21:02:05 -08:00
$ 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}
2023-10-04 09:42:09 -07:00
$ Z \phantom { N } = \lm n . [ n~ ( \lm a.F ) ~T ] $ \par
2023-02-05 21:02:05 -08:00
$ 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 { }
2023-10-05 11:23:03 -07:00
Design an expression PAIR that constructs two-value tuples. \par
2023-10-04 09:42:09 -07:00
For example, say $ A = \text { PAIR } ~ 1 ~ 2 $ . Then, \par
2023-10-05 11:23:03 -07:00
$ ( A~T ) $ should reduce to $ 1 $ and $ ( 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
2023-10-04 09:42:09 -07:00
From now on, I'll write (PAIR $ A $ $ B $ ) as $ \langle A,B \rangle $ . \par
2023-10-05 11:23:03 -07:00
Like currying, this is only notation. The underlying 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>
2023-10-04 09:42:09 -07:00
Write a function $ H $ , which we'll call \say { shift and add.} \par
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-10-05 11:23:03 -07:00
Given an input pair, it should shift its second argument left, then add one. \par
2023-10-04 09:42:09 -07:00
$ H~ \langle 0 , 1 \rangle $ should reduce to $ \langle 1 , 2 \rangle $ \par
$ H~ \langle 1 , 2 \rangle $ should reduce to $ \langle 2 , 3 \rangle $ \par
$ 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-10-05 11:23:03 -07:00
\problem { } <decrement>
2023-10-04 09:42:09 -07:00
Design a function $ D $ that un-does $ S $ . That means \par
$ D ( 1 ) = 0 $ , $ D ( 2 ) = 1 $ , etc. $ D ( 0 ) $ should be zero. \par
2023-02-05 21:02:05 -08:00
\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
\vfill
\pagebreak