2023-02-05 21:02:05 -08:00
|
|
|
\section{Definitions}
|
|
|
|
|
|
|
|
\generic{$\lm$ Notation:}
|
2023-10-04 09:42:09 -07:00
|
|
|
$\lm$ notation is used to define functions, and looks like $(\lm ~ \text{input} ~ . ~ \text{output})$. \par
|
2023-02-05 21:02:05 -08:00
|
|
|
Consider the following statement:
|
|
|
|
$$
|
|
|
|
I = \lm a . a
|
|
|
|
$$
|
2023-10-04 09:42:09 -07:00
|
|
|
This tells us that $I$ is a function that takes its input, $a$, to itself. We'll call this the \textit{identity function}.
|
2023-02-05 21:02:05 -08:00
|
|
|
|
|
|
|
To apply functions, put them next to their inputs. We'll omit the usual parentheses to save space.
|
|
|
|
$$
|
2023-04-02 09:20:18 -07:00
|
|
|
(I~\star) =
|
2023-02-05 21:02:05 -08:00
|
|
|
(\lm \tzm{b}a. a)~\tzmr{a}\star =
|
|
|
|
\star
|
|
|
|
\begin{tikzpicture}[
|
|
|
|
overlay,
|
|
|
|
remember picture,
|
|
|
|
out=225,
|
|
|
|
in=315,
|
|
|
|
distance=0.5cm
|
|
|
|
]
|
|
|
|
\draw[->,gray,shorten >=5pt,shorten <=3pt]
|
|
|
|
(a.center) to (b.center);
|
|
|
|
\end{tikzpicture}
|
|
|
|
$$
|
2023-10-04 09:42:09 -07:00
|
|
|
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
|
2023-10-05 11:23:03 -07:00
|
|
|
In this handout, all types of parentheses ( $(), [~],$ etc ) are equivalent.
|
2023-02-05 21:02:05 -08:00
|
|
|
|
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
\problem{}
|
|
|
|
Rewrite the following expressions with as few parentheses as possible, without changing their meaning or structure.
|
|
|
|
Remember that lambda calculus is left-associative.
|
|
|
|
\vspace{2mm}
|
|
|
|
\begin{itemize}[itemsep=2mm]
|
|
|
|
\item $(\lm x. (\lm y. \lm (z. ((xz)(yz)))))$
|
|
|
|
\item $((ab)(cd))((ef)(gh))$
|
|
|
|
\item $(\lm x. ((\lm y.(yx))(\lm v.v)z)u) (\lm w.w)$
|
|
|
|
\end{itemize}
|
|
|
|
|
|
|
|
|
|
|
|
\vfill
|
|
|
|
\pagebreak
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\generic{$\beta$-Reduction:}
|
|
|
|
|
|
|
|
$\beta$-reduction is a fancy name for \say{simplifying an expression.} We've already done it once above,
|
|
|
|
while evaluating $(I \star)$. Let's make another function:
|
2023-02-05 21:02:05 -08:00
|
|
|
$$
|
|
|
|
M = \lm f . f f
|
|
|
|
$$
|
2023-10-16 16:06:39 -07:00
|
|
|
The function $M$ simply repeats its input. What is $(M~I)$?
|
2023-02-05 21:02:05 -08:00
|
|
|
$$
|
|
|
|
(M~I) =
|
|
|
|
((\lm \tzm{b}f.f f)~\tzmr{a}I) =
|
|
|
|
(I~I) =
|
|
|
|
((\lm \tzm{d}a.a)~\tzmr{c}I) =
|
|
|
|
I
|
2022-11-13 13:02:25 -08:00
|
|
|
\begin{tikzpicture}[
|
|
|
|
overlay,
|
|
|
|
remember picture,
|
|
|
|
out=225,
|
|
|
|
in=315,
|
|
|
|
distance=0.5cm
|
|
|
|
]
|
|
|
|
\draw[->,gray,shorten >=5pt,shorten <=3pt]
|
|
|
|
(a.center) to (b.center);
|
2023-02-05 21:02:05 -08:00
|
|
|
\draw[->,gray,shorten >=5pt,shorten <=3pt]
|
|
|
|
(c.center) to (d.center);
|
2022-11-13 13:02:25 -08:00
|
|
|
\end{tikzpicture}
|
2023-02-05 21:02:05 -08:00
|
|
|
$$
|
2023-10-16 16:06:39 -07:00
|
|
|
We cannot go any further, so we stop. Our expression is now in \textit{$\beta$-normal} or \say{reduced} form.
|
|
|
|
|
|
|
|
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
|
|
|
Reduce the following expressions:
|
|
|
|
\begin{itemize}
|
|
|
|
\item $I~I$
|
|
|
|
\item $I~I~I$
|
|
|
|
\item $(\lm a .(a~a~a)) ~ I$
|
|
|
|
\item $(\lm a . (\lm b . a)) ~ M ~ I$
|
|
|
|
\end{itemize}
|
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-16 16:06:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\generic{Currying:}
|
|
|
|
|
|
|
|
In lambda calculus, functions are only allowed to take one argument.
|
2023-10-16 18:54:37 -07:00
|
|
|
However, we can emulate multivariable functions through \textit{currying}\footnotemark{}\hspace{-1ex}.
|
|
|
|
|
|
|
|
\footnotetext{After Haskell Brooks Curry\footnotemark{}\hspace{-1ex}, a logician that contributed to the theory of functional computation.}
|
|
|
|
\footnotetext{
|
|
|
|
There are three programming languages named after him: Haskell, Brook, and Curry. \par
|
|
|
|
Two of these are functional, and one is an oddball GPU language last released in 2007.
|
|
|
|
}
|
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-16 16:06:39 -07:00
|
|
|
The idea behind currying is fairly simple: we make functions that return functions. \par
|
|
|
|
Consider the expression $A$ below. As before, it takes one input $f$ and returns the function $\lm a. f(f(a))$ (underlined).
|
2023-02-05 21:02:05 -08:00
|
|
|
$$
|
2023-10-16 16:06:39 -07:00
|
|
|
A = \lm f .(\tzm{a} ~ \lm a . f(f(a)) ~ \tzm{b})
|
2023-02-05 21:02:05 -08:00
|
|
|
\begin{tikzpicture}[
|
|
|
|
overlay,
|
|
|
|
remember picture
|
|
|
|
]
|
2023-10-16 16:06:39 -07:00
|
|
|
\node[below = 0.8mm] at (a.center) (aa) {};
|
|
|
|
\node[below = 0.8mm] at (b.center) (bb) {};
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\path[draw = gray] (aa) to (bb);
|
|
|
|
\end{tikzpicture}
|
|
|
|
$$
|
2023-10-16 16:06:39 -07:00
|
|
|
When we evaluate $A$ with one input, it constructs a new function with the argument we gave it. \par
|
|
|
|
For example, let's apply $A$ to an arbirary function $N$:
|
|
|
|
$$
|
|
|
|
A~N =
|
|
|
|
\lm \tzm{b}f.(~\lm a.f(f(a))~)~\tzmr{a}N =
|
|
|
|
\lm a.N(N(a))
|
|
|
|
\begin{tikzpicture}[
|
|
|
|
overlay,
|
|
|
|
remember picture,
|
|
|
|
out=225,
|
|
|
|
in=315,
|
|
|
|
distance=0.5cm
|
|
|
|
]
|
|
|
|
\draw[->,gray,shorten >=5pt,shorten <=3pt]
|
|
|
|
(a.center) to (b.center);
|
|
|
|
\end{tikzpicture}
|
|
|
|
$$
|
|
|
|
Above, $A$ replaced every $f$ in its definition with an $N$. \par
|
|
|
|
You can think of $A$ as a \say{factory} that constructs functions using the inputs we gave it.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
\problem{}<firstcardinal>
|
|
|
|
Let $C = \lm f. (\lm g. \lm x. (g(f(x))))$. What does $B$ do? \par
|
|
|
|
Evaluate $(C~a~b~x)$ for arbitary expressions $a$ and $b$. \par
|
|
|
|
\hint{Place parentheses first. Remember, function application is left-associative.}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
\vfill
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
\problem{}
|
|
|
|
Using the definition of $C$ above, evaluate $C~M~I~\star$ \par
|
|
|
|
Then, evaluate $C~I~M~I$ \par
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
\vfill
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
As we saw above, currying allows us to create multivariable functions by nesting single-variable functions.
|
|
|
|
You may have notice that curried expressions can get very long. We'll use a bit of shorthand to make them more palatable:
|
|
|
|
If we have an expression with repeated function definitions, we'll combine their arguments under one $\lm$.
|
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-16 16:06:39 -07:00
|
|
|
For example, $A = \lm f . (\lm a . f(f(a)))$ will become $A = \lm fa . f(f(a))$
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
2023-10-16 16:06:39 -07:00
|
|
|
Rewrite $C = \lm f. (\lm g. \lm x. (g(f(x))))$ from \ref{firstcardinal} using this shorthand.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-16 16:06:39 -07:00
|
|
|
\vspace{25mm}
|
|
|
|
|
|
|
|
Remember that this is only notation. \textbf{Curried functions are not multivariable functions, they are simply shorthand!}
|
|
|
|
Any function presented with this notation must still be evaluated one variable at a time, just like an un-curried function.
|
|
|
|
Substituting all curried variables at once may cause errors, as we'll see below.
|
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-10-16 16:06:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\definition{Equivalence}
|
|
|
|
We say two functions are \textit{equivalent} if they differ only by the names of their variables:
|
|
|
|
$I = \lm a.a = \lm b.b = \lm \heartsuit . \heartsuit = ...$ \par
|
|
|
|
|
|
|
|
\vspace{2mm}
|
|
|
|
|
|
|
|
The idea behind this is very similar to the idea behind \say{equivalent groups} in group theory: \par
|
|
|
|
we do not care which symbols a certain group or function uses, we care about their \textit{structure}. \par
|
|
|
|
|
|
|
|
\vspace{2mm}
|
|
|
|
|
|
|
|
If we have two groups with different elements with the same multiplication table, we look at them as identical groups.
|
|
|
|
The same is true of lambda functions: two lambda functions with different variable names that behave in the same way are identical.
|
|
|
|
|
|
|
|
|
|
|
|
\vspace{4mm}
|
|
|
|
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\generic{$\alpha$-Conversion:}
|
2023-10-16 16:06:39 -07:00
|
|
|
There is one more operation we need to discuss. Those of you that have worked with any programming language may
|
|
|
|
find that this section sounds like something you've seen before.
|
|
|
|
|
|
|
|
\vspace{2mm}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-03 18:17:39 -07:00
|
|
|
Variables inside functions are \say{scoped.} We must take care to keep separate variables separate.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-04 09:42:09 -07:00
|
|
|
For example, take the functions \par
|
|
|
|
$A = \lm a b . a$ \par
|
2023-02-05 21:02:05 -08:00
|
|
|
$B = \lm b . b$
|
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
|
|
|
We could say that $(A~B) = \lm b . (\lm b . b)$, and therefore
|
|
|
|
$$
|
|
|
|
((A~B)~I)
|
|
|
|
= (~ (\lm \tzm{b}b . (\lm b . b))~\tzmr{a}I ~)
|
|
|
|
= \lm I . I
|
|
|
|
\begin{tikzpicture}[
|
|
|
|
overlay,
|
|
|
|
remember picture,
|
|
|
|
out=225,
|
|
|
|
in=315,
|
|
|
|
distance=0.5cm
|
|
|
|
]
|
|
|
|
\draw[->,gray,shorten >=5pt,shorten <=3pt]
|
|
|
|
(a.center) to (b.center);
|
|
|
|
\end{tikzpicture}
|
|
|
|
$$
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
Which is, of course, incorrect. $\lm I . I$ is not a valid function.
|
2023-10-16 16:06:39 -07:00
|
|
|
Both $A$ and $B$ use the input $b$. However, each $b$ is \say{bound} to a different function:
|
|
|
|
One $b$ is bound to $A$, and the other to $B$. They are therefore distinct.
|
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-10-16 16:06:39 -07:00
|
|
|
Let's rewrite $B$ as $\lm b_1 . b_1$ and try again: $(A~B) = \lm b . ( \lm b_1 . b_1) = \lm bb_1 . b_1$ \par
|
2023-02-05 21:02:05 -08:00
|
|
|
Now, we correctly find that $(A~B~I) = (\lm bc . c)~I = \lm c . c = B = I$.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
2023-10-16 16:06:39 -07:00
|
|
|
Let $Q = \lm abc.b$ \par
|
|
|
|
Reduce $(Q~a~c~b)$.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\begin{solution}
|
2023-10-16 16:06:39 -07:00
|
|
|
I'll rewrite $(Q~a~c~b)$ as $(Q~a_1~c_1~b_1)$:
|
2023-02-05 21:02:05 -08:00
|
|
|
\begin{align*}
|
2023-10-16 16:06:39 -07:00
|
|
|
Q = (\lm abc.b) &= (\lm a.\lm b.\lm c.b) \\
|
2023-02-05 21:02:05 -08:00
|
|
|
(\lm a.\lm b.\lm c.b)~a_1 &= (\lm b.\lm c.b) \\
|
|
|
|
(\lm b.\lm c.b)~c_1 &= (\lm c.c_1) \\
|
|
|
|
(\lm c.c_1)~b_1 &= c_1
|
|
|
|
\end{align*}
|
|
|
|
\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{}
|
|
|
|
Reduce $((\lm a.a)~\lm bc.b)~d~\lm eg.g$
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\begin{solution}
|
|
|
|
$((\lm a.a)~\lm bc.b)~d~\lm eg.g$ \\
|
|
|
|
$= (\lm bc.b)~d~\lm eg.g$ \\
|
|
|
|
$= (\lm c.d)~\lm eg.g$ \\
|
|
|
|
$= d$
|
|
|
|
\end{solution}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\vfill
|
|
|
|
\pagebreak
|