Lambda edits
This commit is contained in:
parent
790cd77e87
commit
b093e368e5
@ -2,7 +2,8 @@
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
singlenumbering,
|
||||
shortwarning
|
||||
]{../../resources/ormc_handout}
|
||||
|
||||
\usepackage{url}
|
||||
@ -26,7 +27,13 @@
|
||||
|
||||
\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}
|
||||
@ -39,7 +46,7 @@
|
||||
\maketitle
|
||||
|
||||
\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}
|
||||
|
||||
|
@ -6,11 +6,9 @@ Consider the following statement:
|
||||
$$
|
||||
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}.
|
||||
|
||||
To apply functions, put them next to their inputs. We'll omit the usual parentheses to save space.
|
||||
|
||||
$$
|
||||
(I~\star) =
|
||||
(\lm \tzm{b}a. a)~\tzmr{a}\star =
|
||||
@ -26,13 +24,10 @@ $$
|
||||
(a.center) to (b.center);
|
||||
\end{tikzpicture}
|
||||
$$
|
||||
|
||||
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
|
||||
In this handout, all types of parentheses ( $(), [~],$ etc ) are equivalent.
|
||||
|
||||
\vfill
|
||||
|
||||
\generic{$\beta$-Reduction:}
|
||||
|
||||
$\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);
|
||||
\end{tikzpicture}
|
||||
$$
|
||||
|
||||
We cannot reduce this any further, so we stop. Our expression is now in \textit{$\beta$-normal form}.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Reduce the following expressions:
|
||||
\begin{itemize}
|
||||
|
@ -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.
|
||||
|
||||
|
||||
|
||||
\definition{The Kestrel}
|
||||
|
||||
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
|
||||
$$
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
What does the Kestrel do? Explain in plain English. \par
|
||||
\hint{What is $(K~\heartsuit~\star)$?}
|
||||
|
@ -49,13 +49,9 @@ How about $(8~NOT~F)$?
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
This handout may remind you of Professor Oleg's handout on Peano's axioms. Good. \par
|
||||
Recall the tools we used to build the natural numbers: \par
|
||||
We had a zero element and a \say{successor} operation so that $1 \coloneqq S(0)$, $2 \coloneqq S(1)$, and so on.
|
||||
|
||||
\vspace{1ex}
|
||||
|
||||
Create a successor operation for the Church numerals. \par
|
||||
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
|
||||
\hint{A good signature for this function is $\lm nfa$, or more clearly $\lm n.\lm fa$. Do you see why?}
|
||||
|
||||
\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]$
|
||||
\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
|
||||
\pagebreak
|
@ -35,21 +35,24 @@ Write an expression that resolves to itself. \par
|
||||
\vspace{1ex}
|
||||
|
||||
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}
|
||||
$\Omega = M~M = (\lm x . xx) (\lm x . xx)$
|
||||
|
||||
\vspace{1ex}
|
||||
\vspace{1mm}
|
||||
|
||||
An uninspired mathematician might call the Mockingbird $\omega$, \say{little omega}.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
This is the \textit{Y-combinator}, easily the most famous $\lm$ expression. \par
|
||||
You may notice that it's just $\Omega$, put to work.
|
||||
This is the \textit{Y-combinator}. You may notice that it's just $\Omega$ put to work.
|
||||
$$
|
||||
Y = \lm f . (\lm x . f(x~x))(\lm x . f(x~x))
|
||||
$$
|
||||
@ -58,5 +61,27 @@ $$
|
||||
What does this thing do? \par
|
||||
Evaluate $Y f$.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\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
|
@ -5,34 +5,19 @@ Do \ref{Yfac} first, then finish the rest in any order.
|
||||
\problem{}<Yfac>
|
||||
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
|
||||
|
||||
\problem{}
|
||||
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.}
|
||||
|
||||
\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}
|
||||
\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 .
|
||||
\Bigl\langle~~
|
||||
\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 .
|
||||
\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
|
||||
$\Bigl\langle
|
||||
\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.
|
||||
\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
|
||||
|
||||
\problem{Bonus}
|
||||
Play with \textit{Lamb}, an automatic lambda expression evaluator. \par
|
||||
\url{https://git.betalupi.com/Mark/lamb}
|
||||
|
||||
\pagebreak
|
Loading…
x
Reference in New Issue
Block a user