62 lines
1.9 KiB
TeX
Raw Normal View History

2023-02-05 21:02:05 -08:00
\section{Recursion}
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
Say we want a function that computes the factorial of a positive integer. Here's one way we could define it:
$$
x! = \begin{cases}
x \times (x-1)! & x \neq 0 \\
1 & x = 0
\end{cases}
$$
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
We cannot re-create this in lambda notation. Functions in lambda calculus are \textit{anonymous}, which means we can't call them before they've been fully defined.
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
As an example, consider the statement $A = \lm a. A~a$ \par
2023-02-05 21:02:05 -08:00
This means \say{write $(\lm a.A~a)$ whenever you see $A$.} However, $A$ is \textit{inside} what we're rewriting. We'd fall into infinite recursion before even starting our $\beta$-reduction!
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin{instructornote}
We're talking about recursion, and \textit{computability} isn't far away. At one point or another, it may be good to give the class a precise definition of \say{computable by lambda calculus:}
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-10-04 09:42:09 -07:00
Say we have a device that reduces a $\lm$ expression to $\beta$-normal form. We give it an expression, and the machine simplifies it as much as it can and spits out the result.
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
An algorithm is \say{computable by lambda calculus} if we can encode its input in an expression that resolves to the algorithm's output.
\end{instructornote}
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
Write an expression that resolves to itself. \par
2023-02-05 21:02:05 -08:00
\note{Your answer should be short and sweet.}
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
This expression is often called $\Omega$, after the last letter of the Greek alphabet. \par
2023-02-05 21:02:05 -08:00
$\Omega$ useless on its own, but gives us a starting point for recursion.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\begin{solution}
$\Omega = M~M = (\lm x . xx) (\lm x . xx)$
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
An uninspired mathematician might call the Mockingbird $\omega$, \say{little omega}.
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
\definition{}
2023-10-04 09:42:09 -07:00
This is the \textit{Y-combinator}, easily the most famous $\lm$ expression. \par
2023-02-05 21:02:05 -08:00
You may notice that it's just $\Omega$, put to work.
$$
Y = \lm f . (\lm x . f(x~x))(\lm x . f(x~x))
$$
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 does this thing do? \par
2023-02-05 21:02:05 -08:00
Evaluate $Y f$.
2022-11-13 13:02:25 -08:00
2023-02-05 21:02:05 -08:00
\vfill
\pagebreak