Lambda edits

This commit is contained in:
mark 2023-10-16 15:06:51 -07:00
parent 790cd77e87
commit b093e368e5
6 changed files with 121 additions and 81 deletions

View File

@ -2,7 +2,8 @@
% use [solutions] flag to show solutions. % use [solutions] flag to show solutions.
\documentclass[ \documentclass[
solutions, solutions,
singlenumbering singlenumbering,
shortwarning
]{../../resources/ormc_handout} ]{../../resources/ormc_handout}
\usepackage{url} \usepackage{url}
@ -26,7 +27,13 @@
\newcommand{\lm}{\lambda} \newcommand{\lm}{\lambda}
% Notice that I a b = 1 a b!
% TODO:
% Lazy evaluation (alternate Y)
% Add a few theorems
% Better ending -> applications?
% - nix, comparison to imperitive
\uptitlel{Advanced 2} \uptitlel{Advanced 2}
@ -39,7 +46,7 @@
\maketitle \maketitle
\begin{minipage}{8cm} \begin{minipage}{8cm}
Beware of the Turing tar-pit in which everything is possible but nothing of interest is easy. Beware of the Turing tar pit, in which everything is possible but nothing of interest is easy.
\vspace{2ex} \vspace{2ex}

View File

@ -6,11 +6,9 @@ Consider the following statement:
$$ $$
I = \lm a . a I = \lm a . a
$$ $$
This tells us that $I$ is a function that takes its input, $a$, to itself. We'll call this the \textit{identity function}. This tells us that $I$ is a function that takes its input, $a$, to itself. We'll call this the \textit{identity function}.
To apply functions, put them next to their inputs. We'll omit the usual parentheses to save space. To apply functions, put them next to their inputs. We'll omit the usual parentheses to save space.
$$ $$
(I~\star) = (I~\star) =
(\lm \tzm{b}a. a)~\tzmr{a}\star = (\lm \tzm{b}a. a)~\tzmr{a}\star =
@ -26,13 +24,10 @@ $$
(a.center) to (b.center); (a.center) to (b.center);
\end{tikzpicture} \end{tikzpicture}
$$ $$
Functions are left-associative: If $A$ and $B$ are functions, $(A~B~\star)$ is equivalent to $((A~B)~\star)$. Functions are left-associative: If $A$ and $B$ are functions, $(A~B~\star)$ is equivalent to $((A~B)~\star)$.
As usual, we'll use parentheses to group terms if we want to override this order: $(A~(B~\star)) \neq (A~B~\star)$ \par As usual, we'll use parentheses to group terms if we want to override this order: $(A~(B~\star)) \neq (A~B~\star)$ \par
In this handout, all types of parentheses ( $(), [~],$ etc ) are equivalent. In this handout, all types of parentheses ( $(), [~],$ etc ) are equivalent.
\vfill
\generic{$\beta$-Reduction:} \generic{$\beta$-Reduction:}
$\beta$-reduction is a fancy name for \say{simplifying an expression.} We've already done it once above. $\beta$-reduction is a fancy name for \say{simplifying an expression.} We've already done it once above.
@ -61,12 +56,8 @@ $$
(c.center) to (d.center); (c.center) to (d.center);
\end{tikzpicture} \end{tikzpicture}
$$ $$
We cannot reduce this any further, so we stop. Our expression is now in \textit{$\beta$-normal form}. We cannot reduce this any further, so we stop. Our expression is now in \textit{$\beta$-normal form}.
\vfill
\pagebreak
\problem{} \problem{}
Reduce the following expressions: Reduce the following expressions:
\begin{itemize} \begin{itemize}

View File

@ -6,6 +6,8 @@ For example, $b$ is a free variable in $\lm a. b$. The same is true of $\star$ i
A \textit{combinator} is a function with no free variables. A \textit{combinator} is a function with no free variables.
\definition{The Kestrel} \definition{The Kestrel}
Notable combinators are often named after birds.\hspace{-0.5ex}\footnotemark{} We've already met a few: \par Notable combinators are often named after birds.\hspace{-0.5ex}\footnotemark{} We've already met a few: \par
@ -21,6 +23,9 @@ Another notable combinator is $K$, the \textit{Kestrel}:
$$ $$
K = \lm ab . a K = \lm ab . a
$$ $$
\problem{} \problem{}
What does the Kestrel do? Explain in plain English. \par What does the Kestrel do? Explain in plain English. \par
\hint{What is $(K~\heartsuit~\star)$?} \hint{What is $(K~\heartsuit~\star)$?}

View File

@ -49,13 +49,9 @@ How about $(8~NOT~F)$?
\pagebreak \pagebreak
\problem{} \problem{}
This handout may remind you of Professor Oleg's handout on Peano's axioms. Good. \par Peano's axioms state that we only need a zero element and a \say{successor} operation to
Recall the tools we used to build the natural numbers: \par build the natural numbers. We've already defined zero.
We had a zero element and a \say{successor} operation so that $1 \coloneqq S(0)$, $2 \coloneqq S(1)$, and so on. Now, create a successor operation so that $1 \coloneqq S(0)$, $2 \coloneqq S(1)$, and so on. \par
\vspace{1ex}
Create a successor operation for the Church numerals. \par
\hint{A good signature for this function is $\lm nfa$, or more clearly $\lm n.\lm fa$. Do you see why?} \hint{A good signature for this function is $\lm nfa$, or more clearly $\lm n.\lm fa$. Do you see why?}
\begin{solution} \begin{solution}
@ -175,36 +171,5 @@ $D(1) = 0$, $D(2) = 1$, etc. $D(0)$ should be zero. \par
$D = \lm n . \Bigl[(~n~H~\langle 0, 0 \rangle~)~T\Bigr]$ $D = \lm n . \Bigl[(~n~H~\langle 0, 0 \rangle~)~T\Bigr]$
\end{solution} \end{solution}
\begin{solution}
Here's a different solution. \par
Can you figure out how it works?
\vspace{1ex}
$
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
$
\vspace{1ex}
$
D = \lm nfa .
\Bigl(
n D_0 \Bigl\langle T, \langle f, a \rangle \Bigr\rangle
\Bigr)~F~F
$
\end{solution}
\vfill \vfill
\pagebreak \pagebreak

View File

@ -35,21 +35,24 @@ Write an expression that resolves to itself. \par
\vspace{1ex} \vspace{1ex}
This expression is often called $\Omega$, after the last letter of the Greek alphabet. \par This expression is often called $\Omega$, after the last letter of the Greek alphabet. \par
$\Omega$ useless on its own, but it gives us a starting point for recursion. $\Omega$ useless on its own, but it gives us a starting point for recursion. \par
\begin{solution} \begin{solution}
$\Omega = M~M = (\lm x . xx) (\lm x . xx)$ $\Omega = M~M = (\lm x . xx) (\lm x . xx)$
\vspace{1ex} \vspace{1mm}
An uninspired mathematician might call the Mockingbird $\omega$, \say{little omega}. An uninspired mathematician might call the Mockingbird $\omega$, \say{little omega}.
\end{solution} \end{solution}
\vfill \vfill
\pagebreak
\definition{} \definition{}
This is the \textit{Y-combinator}, easily the most famous $\lm$ expression. \par This is the \textit{Y-combinator}. You may notice that it's just $\Omega$ put to work.
You may notice that it's just $\Omega$, put to work.
$$ $$
Y = \lm f . (\lm x . f(x~x))(\lm x . f(x~x)) Y = \lm f . (\lm x . f(x~x))(\lm x . f(x~x))
$$ $$
@ -58,5 +61,27 @@ $$
What does this thing do? \par What does this thing do? \par
Evaluate $Y f$. Evaluate $Y f$.
\vfill \vfill
\pagebreak
\definition{}
We say $x$ is a \textit{fixed point} of a function $f$ if $f(x) = x$.
\problem{}
Show that $Y F$ is a fixed point of $F$.
\vfill
\problem{}
Let $\theta = (\lm xy . y(xxy))$ and $\Theta = \theta \theta$. \par
Let $N = \Theta F$ for an arbitrary lambda expression $F$. \par
Show that $F N = N$.
\vfill
\problem{Bonus}
Find a fixed-point combinator that isn't $Y$ or $\Theta$.
\vfill
\pagebreak

View File

@ -5,34 +5,19 @@ Do \ref{Yfac} first, then finish the rest in any order.
\problem{}<Yfac> \problem{}<Yfac>
Design a recursive factorial function using $Y$. Design a recursive factorial function using $Y$.
\begin{solution}
$\text{FAC} = \lm yn.[Z~n][1][\text{MULT}~n~(y~(\text{D}~n))]$
To compute the factorial of 5, evaluate $(\text{Y}~\text{FAC}~5)$.
\end{solution}
\vfill \vfill
\problem{} \problem{}
Design a non-recursive factorial function. \par Design a non-recursive factorial function. \par
\note{This one is easier than \ref{Yfac}, but I don't think it will help you solve it.} \note{This one is easier than \ref{Yfac}, but I don't think it will help you solve it.}
\problem{}
Using pairs, make a \say{list} data structure. Define a GET function, so that $\text{GET}~L~n$ reduces to the nth item in the list.
$\text{GET}~L~0$ should give the first item in the list, and $\text{GET}~L~1$, the \textit{second}. \par
Lists have a defined length, so you should be able to tell when you're on the last element.
\problem{}
Solve \ref{decrement} without using $H$.
\problem{}
Write a MOD $a$ $b$ function that reduces to the remainder of $a \div b$.
\begin{solution} \begin{solution}
\textbf{Factorial with recursion:}
\vspace{3ex}
$\text{FAC} = \lm yn.[Z~n][1][\text{MULT}~n~(y~(\text{D}~n))]$
\linehack{}
\textbf{Factorial without recursion:}
\vspace{3ex}
$\text{FAC}_0 = \lm p . $\text{FAC}_0 = \lm p .
\Bigl\langle~~ \Bigl\langle~~
\Bigl[D~(p~t)\Bigr] \Bigl[D~(p~t)\Bigr]
@ -47,12 +32,55 @@ Write a MOD $a$ $b$ function that reduces to the remainder of $a \div b$.
\text{FAC} = \lm n . \text{FAC} = \lm n .
\bigl( n~\text{FAC}_0~\langle n, 1 \rangle \bigr) \bigl( n~\text{FAC}_0~\langle n, 1 \rangle \bigr)
$ $
\end{solution}
\linehack{} \vfill
\textbf{Lists:}
\vspace{3ex}
\problem{}
Solve \ref{decrement} without using $H$.
\begin{solution}
One solution is below. Can you figure out how it works? \par
\vspace{1ex}
$
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
$
\vspace{1ex}
$
D = \lm nfa .
\Bigl(
n D_0 \Bigl\langle T, \langle f, a \rangle \Bigr\rangle
\Bigr)~F~F
$
\end{solution}
\vfill
\problem{}
Using pairs, make a \say{list} data structure. Define a GET function, so that $\text{GET}~L~n$ reduces to the nth item in the list.
$\text{GET}~L~0$ should give the first item in the list, and $\text{GET}~L~1$, the \textit{second}. \par
Lists have a defined length, so you should be able to tell when you're on the last element.
\begin{solution}
One possible implementation is One possible implementation is
$\Bigl\langle $\Bigl\langle
\langle \text{is last} ~,~ \text{item} \rangle \langle \text{is last} ~,~ \text{item} \rangle
@ -75,8 +103,27 @@ Write a MOD $a$ $b$ function that reduces to the remainder of $a \div b$.
This will break if $n$ is out of range. This will break if $n$ is out of range.
\end{solution} \end{solution}
\vfill
\pagebreak
\problem{}
Write a lambda expression that represents the Fibonacci function: \par
$f(0) = 1$, $f(1) = 1$, $f(n + 2) = f(n + 1) + f(n)$.
\vfill
\problem{}
Write a lambda expression that evaluates to $T$ if a number $n$ is prime, and to $F$ otherwise.
\vfill
\problem{}
Write a MOD $a$ $b$ function that reduces to the remainder of $a \div b$.
\vfill \vfill
\problem{Bonus} \problem{Bonus}
Play with \textit{Lamb}, an automatic lambda expression evaluator. \par Play with \textit{Lamb}, an automatic lambda expression evaluator. \par
\url{https://git.betalupi.com/Mark/lamb} \url{https://git.betalupi.com/Mark/lamb}
\pagebreak