Commit 5398f31b by Mark Cohen

presentation: content done

parent e6511be5
Showing with 113 additions and 10 deletions
......@@ -1035,6 +1035,83 @@ instance Iterable string where
\end{frame}
\begin{frame}[fragile]{Associated types: formalisms}
\begin{onlyenv}
\begin{lstlisting}[mathescape=true]
class Collectible $\alpha$ where
type item
val empty: $\alpha$
val insert: $\alpha$ $\arrow$ item $\arrow$ $\alpha$
\end{lstlisting}
\end{onlyenv}
\note[item]{
Here we have the definition of the \tt{Collectible} type class using the associated item type.
}
\begin{onlyenv}<2->
\begin{itemize}
\item{
$
\tt{over empty} :: \forall \tt{item} . \forall \alpha . \; \alpha \tt{ in } \ldots
$
}
\item{
$
\tt{over insert} :: \forall \tt{item} . \forall \alpha . \;
\alpha \arrow \tt{item} \arrow \alpha = \ldots \tt{ in } \ldots
$
}
\end{itemize}
\end{onlyenv}
\note[item]{
Since we are not allowing higher-kinded associated types, we just translate away the overloaded
type declaration into an extra generalization.
}
\begin{onlyenv}<3->
\begin{lstlisting}[mathescape=true]
instance Collectible string where
type item = char
val empty = ""
val next = fn xs =>
if xs = ""
then None
else Some (hd xs, tl xs)
\end{lstlisting}
\end{onlyenv}
\note[item]{
Here we have a complete instance of \tt{Collectible} for lists. Notice how here we specify the
item type in terms of the instance type.
}
\begin{onlyenv}<4->
\begin{itemize}
\item{
$
\tt{inst empty} :: \tt{string} = "" \tt{ in } \ldots
$
}
\item{
$
\tt{inst insert} :: \tt{string} \arrow \tt{char} \arrow \tt{string}
= \lambda \tt{s} . \lambda \tt{c} \ldots
\tt{ in } \ldots
$
}
\end{itemize}
\end{onlyenv}
\note[item]{
Now, to translate this, all we have to do is substitute in $\alpha$ everywhere we saw \tt{item}
in the type of the overloaded operator above.
}
\note[item]{
Then, when we go to translate from the intermediate language here to the internal language, we
just check that the annotated type here unifies with the inferred type of the lambda body.
}
\end{frame}
\begin{frame}[fragile]{Iteration: formalisms}
Recall our desired designs:
\begin{lstlisting}[mathescape=true]
......@@ -1162,20 +1239,46 @@ pass
\begin{frame}[fragile]{Iteration: big payoff!}
\begin{lstlisting}[mathescape=true]
val pass = fn _ => None
val collect = fn x => Some x
val for = fn var collection $\iota$ expr =>
case (next collection)
of None => $\iota$
| Some (item, rest) =>
case (expr item)
of Some item' => for var rest (insert $\iota$ item') expr
| None => for var rest $\iota$ expr
let
val pass = None
val collect = fn x => Some x
val for = fn collection $\iota$ expr =>
case (next collection)
of None => $\iota$
| Some (item, rest) =>
case (expr item)
of Some item' => for rest (insert $\iota$ item') expr
| None => for rest $\iota$ expr
in
for <var> <collection> empty (fn <var> => <expr>)
end
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Iteration: big payoff!}
\begin{lstlisting}[mathescape=true]
for id in users $\arrow$ string list
if registered? id
then collect id
else pass
\end{lstlisting}
\begin{onlyenv}<2->
\begin{lstlisting}[mathescape=true]
<expr> $\mapsto$ fn <var> => <expr>
let
val pass = None
val collect = fn x => Some x
val for = fn collection $\iota$ expr =>
case (next collection)
of None => $\iota$
| Some (item, rest) =>
case (expr item)
of Some item' => for rest (insert $\iota$ item') expr
| None => for rest $\iota$ expr
in
for users [] (fn id => if registered? id then collect id else pass)
end
\end{lstlisting}
\end{onlyenv}
\end{frame}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment