%%% This is the scribe notes template for CS611
%%% There are several comments preceded by CS611: and boxed in %%%%'s
%%% which indicate where macros should be altered to set up the header
%%% for the paper.  Your Notes should go at the comment SCRIBE NOTES GO HERE!.

%%% In the various .sty files that accompany this .tex file you will
%%% find LaTeX macros that make it easier to typeset inference rules
%%% and programming language constructs.  You must make sure that the
%%% file proof.sty is in a path searched by LaTeX when you try to
%%% use this file.  Take a look to see what macros are available--it
%%% will save you time and make the notes look better.  Feel free to
%%% extend the set of macros--post them to the newsgroup and contact
%%% the course staff if you come up with some good ones so they can be
%%% added to the template.

%%% This template includes examples of how to use some of the macros
%%% to give you an idea of how they work.  (Delete the examples when
%%% you do your scribing.)

\documentclass{article}
\usepackage{611-lecture}
\usepackage{amsthm, amsmath, amssymb, amsfonts}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{25}                  %% Lecture number
\title{Products, Sums, and Other Datatypes}   %% Title of lecture
%\author{David Crandall, Lukas Kroc}       %% name of scribe
\date{27 October 2006}     %% Date of lecture, e.g., 1 January 2001
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% See 611.sty for a variety of macros that will be helpful in
% typesetting the lecture. Here are a few of particular interest:
%
% "x"       x in keyword font (e.g., "if", "#t")
% _x_       x in italics
% \nm{n}    n in slanted font (used for abbreviations)
% <e>       e in angle brackets
% \lt       less-than sign
% \gt       greater-than sign
% \SB{x}    x in semantic brackets
% \Tr x{y}  x[[y]] with x in calligraphic font
%           (if x is more than a single character, use \Tr{x}{y})

\newcommand{\inL}{\ensuremath{\mathsf{inL}}}
\newcommand{\inR}{\ensuremath{\mathsf{inR}}}
\newcommand\caseof[3]{\mathsf{case}~{#1}~\mathsf{of}~{#2}~|~{#3}}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\group}[1]{\left\langle{#1}\right\rangle}

%\theoremstyle{definition}
%\newtheorem*{defn}{Definition}
\newcommand{\nondet}{\left[\!\kern1pt\right]}
\renewcommand\phi\varphi
\renewcommand\wp[2]{\mathsf{wp}~{#1}~{#2}}
\newcommand\wlp[2]{\mathsf{wlp}~{#1}~{#2}}
\renewcommand\({\begin{eqnarray*}}
\renewcommand\){\end{eqnarray*}}

\newcommand\LOOKUP[2]{\mathrm{LOOKUP}~{#1}~{#2}}
\newcommand\UPDATE[3]{\mathrm{UPDATE}~{#1}~{#2}~{#3}}
\newcommand\MALLOC[2]{\mathrm{MALLOC}~{#1}~{#2}}
\newcommand\EMPTY{\mathrm{EMPTY\mbox{-}STORE}}
\renewcommand\dom[1]{\mathrm{dom}\,{#1}}
\newcommand\p[2]{\langle{#1},\,{#2}\rangle}
\newcommand\bigcdot{\mathrel{\raisebox{1pt}{$\scriptscriptstyle\bullet$}}}
\newcommand\holed[1]{[\,#1\,]}
\newcommand\hole{\holed\bigcdot}
\newcommand\context[1]{E\kern1pt\holed{#1}}
\newcommand\contextHole{\context\bigcdot}
\newcommand\goesto[2]{\underset{#2}{\overset{#1}\longrightarrow}}
\newcommand\ifthenelse[3]{\mathsf{if\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\ifpthenelse[3]{\mathsf{ifp\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\whiledo[2]{\mathsf{while\ }#1\mathsf{\ do\ }#2}
\newcommand\letin[3]{\mathsf{let\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\letrec[5]{\mathsf{letrec\ }#1 = #2\mathsf{\ and\ \ldots\ and\ }#3 = #4\mathsf{\ in\ }#5}
\newcommand\letrecone[3]{\mathsf{letrec\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\true{\ensuremath{\mathsf{true}}}
\newcommand\false{\ensuremath{\mathsf{false}}}
\newcommand\error{\ensuremath{\mathsf{error}}}
\newcommand\pca[3]{\{#1\}\kern1pt{#2}\kern1pt\{#3\}}
\newcommand\states{\Set{St}}
\newcommand\rtc{^{\textstyle *}}
\newcommand\sat\vDash
\newcommand\force\vdash

\newcommand\hyphen{\mbox{-}}
\newcommand\lookup[2]{\nm{LOOKUP}~#1~\mquote{#2}}
\newcommand\update[3]{\nm{UPDATE}~#1~\mquote{#2}~#3}
\newcommand\SBk[1]{\SB{#1}k}
\newcommand\fix[1]{\mathsf{fix}\,{#1}}

\newlength\reasonwidth
\setlength\reasonwidth{3cm}
\newcommand\reasoning[1]{\def\longest{#1}\settowidth{\reasonwidth}{$\displaystyle\longest$}\addtolength{\reasonwidth}{5mm}}
\newcommand\reason[2]{\makebox[\reasonwidth][l]{$\displaystyle{#1}$}\mbox{#2}}

\renewcommand\inj[1]{\mathsf{in}_{#1}}
\newcommand\proj[1]{\pi_{#1}}
\newcommand{\dlt}{\sqsubseteq}
\newcommand\floor[1]{\lfloor{#1}\rfloor}
\newcommand\cf[1]{[\kern1pt{#1}\kern1pt]}
\newcommand\SBpr[1]{\SB{#1}\,\phi\,\rho}
\renewcommand\C[3]{\Tr C{#1}\kern1pt{#2}\kern1pt{#3}}
\renewcommand\Cr[1]{\C{#1}\Gamma\rho}

\newcommand\judge[3]{{#1}\force{#2}\ty{#3}}
\newcommand\Gjudge[2]{\judge\Gamma{#1}{#2}}%
\renewcommand\C[3]{\Tr C{#1}\kern1pt{#2}\kern1pt{#3}}
\renewcommand\Cr[1]{\C{#1}\Gamma\rho}
\newcommand\Irred[1]{\ensuremath{\mathrm{Irred}(#1)}}

\begin{document}

\maketitle

\section{Introduction}

In this lecture, we add constructs to the typed $\lambda$-calculus that
allow working with more complicated data structures, such as pairs,
tuples, records, sums and recursive functions.  We also provide
denotational semantics for these new constructs.

\section{Recap -- The Typed $\lambda$-Calculus $\lambda^\to$}

\subsection{Syntax}

\[
\begin{array}{lrcl}
\mbox{terms} & e &::=& n \bnf "true" \bnf "false" \bnf "null" \bnf x \bnf e_1\,e_2 \bnf \lam{x\ty\tau}e\\
\mbox{types} & \tau &::=& "int" \bnf "bool" \bnf "unit" \bnf \tau _1 \rightarrow \tau _2\\
\mbox{values} & v &::=& n \bnf "true" \bnf "false" \bnf "null" \bnf \lam{x\ty\tau}e\ \mbox{closed}
\end{array}
\]

\subsection{Typing Rules}

\[
\begin{array}{c@{\hspace{1cm}}c@{\hspace{1cm}}c@{\hspace{1cm}}c@{\hspace{1cm}}c}
\Gjudge n{"int"} & \Gjudge{"true"}{"bool"} & \Gjudge{"false"}{"bool"} & \Gjudge{"null"}{"unit"} & \judge{\Gamma,\,x\ty\tau}x\tau
\end{array}
\]
\begin{center}
\begin{tabular}{c@{\hspace{1cm}}c}
\infer{\Gjudge{e_0\,e_1}{\tau}}{\Gjudge{e_0}{\sigma\rightarrow\tau} & \Gjudge{e_1}\sigma}
& \infer{\Gjudge{(\lam{x\ty\sigma}e)}{\sigma\to\tau}}{\judge{\Gamma,\,x\ty\sigma}e{\tau}}
\end{tabular}
\end{center}

\section{Simple Data Structures}

Each data structure can be added by extending the syntax of
expressions ($e$), types ($\tau$) and values ($v$). The evaluation
contexts ($E$) will also need to be extended, and evaluation 
and type derivation rules added to work with the new syntax.

\subsection{Pairs}

Syntax:
\begin{eqnarray*}
    e &::=& \cdots \bnf (e_1, e_2) \bnf "\#1"\,e \bnf "\#2"\,e \\
    \tau &::=& \cdots \bnf \tau_1 * \tau_2 \\
    v &::=& \cdots \bnf (v_1, v_2) \\
    E & ::=& \cdots \bnf (\hole,e) \bnf (v,\hole) \bnf "\#1"\,\hole \bnf "\#2"\,\hole
\)
For every added syntactic form, we observe that we have expressions
that {\em introduce} the form, and expressions that {\em eliminate}
the form. In the case of pairs, the introduction expression is
$(e_1,e_2)$, and the elimination expressions are $\#1\,e$ and $\#2\,e$.

\medskip\noindent
Evaluation rules:
\[
"\#1"\,(v_1,v_2) \to v_1 \qquad "\#2"\,(v_1,v_2) \to v_2
\]
Note that these rules define _eager_ evaluation, because we only
select from a pair when both elements are already evaluated to a value.

\medskip\noindent
Typing rules:
\[
\frac{\Gjudge{e_1}{\tau_1} \quad \Gjudge{e_2}{\tau_2}}{\Gjudge{(e_1,e_2)}{\tau_1*\tau_2}} \qquad
\frac{\Gjudge e{\tau_1*\tau_2}}{\Gjudge{"\#1"\,e}{\tau_1}}
\qquad
\frac{\Gjudge e{\tau_1*\tau_2}}{\Gjudge{"\#2"\,e}{\tau_2}}
\]

\subsection{Tuples}

Syntax:
\(
    e &::=& \cdots \bnf (e_1,\ldots, e_n) \bnf "\#n"\,e \\
    \tau &::=& \cdots \bnf \tau_1 *\cdots *\tau_n \\
    v &::=& \cdots \bnf (v_1,\ldots, v_n) \\
    E & ::=& \cdots \bnf (v_1,\ldots,v_{i-1},\hole,e_{i+1},\ldots,e_n) \bnf "\#n"\,\hole
\)

\medskip\noindent
Evaluation rule:
\[
"\#m"\,(v_1,\ldots,v_n) \to v_m\sidecond{$1\le m\le n$}
\]

\medskip\noindent
Typing rules:
\[
\frac{\Gjudge{e_i}{\tau_i} \quad i \in \{1,\ldots,n\}}
{\Gjudge{(e_1,\ldots,e_n)}{\tau_1*\cdots*\tau_n}} \qquad
\frac{\Gjudge e{\tau_1*\cdots*\tau_n}}{\Gjudge{"\#m"\,e}{\tau_m}}\sidecond{$1\le m\le n$}
\]

\subsection{Records}

Syntax:
\(
    e &::=& \cdots \bnf \{x_1=e_1,\ldots, x_n=e_n\} \bnf e.x \\
    \tau &::=& \cdots \bnf \{x_1:\tau_1, \ldots, x_n:\tau_n\} \\
    v &::=& \cdots \bnf \{x_1=v_1,\ldots, x_n=v_n\} \\
    E & ::=& \cdots \bnf \{x_1=v_1,\ldots,x_{i-1}=v_{i-1},x_i=\hole,
             x_{i+1}=e_{i+1},\ldots,x_n=e_n) \bnf \hole.x
\)

\medskip\noindent
Evaluation rule:
\[
\{x_1=v_1,\ldots,x_n=v_n\}.x_i \to v_i\sidecond{$1\le i \le n$}
\]

\medskip\noindent
Typing rules:
\[
\frac{\Gjudge{e_i}{\tau_i},\ 1\le i\le n}
{\Gjudge{\{x_1=e_1,\ldots,x_n=e_n)}{\{x_1:\tau_1, \ldots, x_n:\tau_n\}}}
\qquad
\frac{\Gjudge e{\{x_1:\tau_1, \ldots, x_n:\tau_n\}}}{\Gjudge{e.x_i}{\tau_i}}\sidecond{$1\le i \le n$}
\]

\subsection{Sums}

Sums are useful for representing datatypes that can have multiple
forms. For example, a tail of a list can either be another nonempty
list or "null".

\medskip\noindent
Syntax:
\(
    e &::=& \cdots \bnf \inL_{\tau_1+\tau_2} e \bnf \inR_{\tau_1+\tau_2} e
        \bnf \caseof{e_0}{e_1}{e_2}\\
    \tau &::=& \cdots \bnf \tau_1+\tau_2 \\
    v &::=& \cdots \bnf \inL_{\tau_1+\tau_2}~v \bnf \inR_{\tau_1+\tau_2}~v\\
    E & ::=& \cdots \bnf \inL\,\hole \bnf \inR\,\hole \bnf \caseof{\hole}{e_1}{e_2}
\)
The \inL\ and \inR\ constructs are called _left injection_ and _right injection_, respectively.

\medskip\noindent
Evaluation rules:
\[
\caseof{(\inL_{\tau_1+\tau_2}~v)}{e_1}{e_2} \to e_1~v
\qquad
\caseof{(\inR_{\tau_1+\tau_2}~v)}{e_1}{e_2} \to e_2~v
\]
Here $e_1$ and $e_2$ are functions and
must have the same codomain type in order for the whole "case"
expression to have a type.

\medskip\noindent
Typing rules:
\[
\frac{\Gjudge e{\tau_1}}{\Gjudge{\inL_{\tau_1+\tau_2}~e}{\tau_1+\tau_2}}
\qquad
\frac{\Gjudge e{\tau_2}}{\Gjudge{\inR_{\tau_1+\tau_2}~e}{\tau_1+\tau_2}}
\qquad
\frac{\Gjudge{e_0}{\tau_1+\tau_2} \quad \Gjudge{e_1}{\tau_1\to\tau_3} \quad \Gjudge{e_2}{\tau_2\to\tau_3}}
{\Gjudge{\caseof{e_0}{e_1}{e_2}}{\tau_3}}
\]

To give an example of the sum type, consider the sum of two
"unit" types, $"unit + unit"$. This type has exactly two elements,
namely \inL~"null" and \inR~"null".  We could take this as a
definition of the type "bool" with elements $"true"\definedas\inL~"null"$
and $"false"\definedas\inR~"null"$.  The statement
$\ifthenelse b{e_1}{e_2}$ could then be written as
$\caseof b{\lam{z}{e_1}}{\lam{z}{e_2}}$.

SML has a construct that is a generalization of the sum type:
\(
    e &::=& \cdots \bnf x_i(e) \\
    \tau &::=& \cdots \bnf [x_1:\tau_1,\ldots,x_n:\tau_n]
\)
The SML syntax is
\begin{center}
\verb+datatype t = x1 of t1 | ... | xn of tn+.
\end{center}
Such datatypes are also called _variants_. The \verb|xi| are
constructors, and must be globally (across all types) unique to
avoid confusion as to which type a particular constructor
is referring to (in our sum type, the confusion is alleviated by using
$\tau_1+\tau_2$ subscripts in \inL and \inR).

\section{Denotational Semantics}

We now give the denotational semantics for type domains of
$\lambda^{\rightarrow + *}$, the strongly-typed $\lambda$-calculus
with sum and product types.
\(
\Tr T{\tau \rightarrow \tau'} &\definedas& \Tr T{\tau} \rightarrow \Tr T{\tau'}\\
\Tr T{\tau * \tau'} &\definedas& \Tr T{\tau} \times \Tr T{\tau'}\\
\Tr T{\tau + \tau'} &\definedas& \Tr T{\tau} + \Tr T{\tau'}
\)
As before, our contract for this language is:
\(
\rho\models\Gamma\ \wedge\ \Gjudge e\tau &\Rightarrow& \Cr e\ \in\ \Tr T{\tau}.
\)
The remaining semantic rules are:
\(
\Cr{(e_1, e_2)} &\definedas& {<\Cr{e_1},\Cr{e_2}>}\\
\Cr{"\#1"~e} &\definedas& \proj 1(\Cr{e})\\
\Cr{"\#2"~e} &\definedas& \proj 2(\Cr{e})\\
\Cr{\inL_{\tau_1 + \tau_2}~e} &\definedas& \inj 1(\Cr{e})\\
\Cr{\inR_{\tau_1 + \tau_2}~e} &\definedas& \inj 2(\Cr{e})\\
\Cr{\caseof{e_0}{e_1}{e_2}} &\definedas& \caseof{\Cr{e_0}}{\inL~v \to (\Cr{e_1})~v}{\inR~v\to (\Cr{e_2})~v},
\)
where $\proj n$ is the (mathematical) projection operator that
selects the $n$th element of a product and $\inj n$ is the injection
operator that injects an element into a coproduct.

\section{Adding Recursion}

So far this language is not Turing-complete, because there is no way to do unbounded recursion.  This is true because there is no possibility of nontermination.  The easiest way to add this capability to the language is to add support for recursive functions.

\newcommand\rec[3]{\mathsf{rec}\,{#1}\kern1pt.\kern1pt\lam{#2}{#3}}
To do this, we first extend the definition of an expression:
\(
e &::=& \cdots \bnf \rec{f\ty\sigma\to\tau}{x\ty\sigma}{e}
\)
The new keyword "rec" defines a recursive function named
$f$ such that both $x$ and $f$ are in scope inside $e$.
Intuitively, the meaning of $\rec{f\ty\sigma\rightarrow\tau}{x\ty\sigma}{e}$
is the least fixpoint of the map $f\mapsto \lam{x\ty\sigma}e$, where both
$f$ and $\lam{x\ty\sigma}e$ are of type $\sigma\to\tau$.

For example, we would write the recursive function
\(
f(x) &=& \ifthenelse{x\gt 0}{1}{f(x + 1)}
\)
as
\(
\rec{f\ty"int"\to"int"}{x\ty"int"}{\ifthenelse{x\gt 0}{1}{f(x+1)}}.
\)

The small-step operational semantics evaluation rule for "rec" is:
\(
\rec{f\ty\sigma\to\tau}{x\ty\sigma}{e}\ &\to&\ \lam{x\ty\sigma}{\subst e{(\rec{f\ty\sigma\to\tau}{x\ty\sigma}{e})}f}
\)
and the typing rule for "rec" is
\[
\frac
{\judge{\Gamma,\,f\ty\sigma\to\tau,\,x\ty\sigma}{e}{\tau}}
{\Gjudge{(\rec{f\ty\sigma\to\tau}{x\ty\sigma}{e})}{\sigma\to\tau}}.
\]
The denotational semantics is defined in terms of the "fix" operator on domains:
\(
\Cr{\rec{f\ty\sigma\to\tau}{x\ty\sigma}{e}}
&\definedas&
"fix"~\lam{g\in\Tr T{\sigma\to\tau}}
{\lam{v\in\Tr T\sigma}{\Tr C{e}\,\Gamma[(\sigma\to\tau)/f,\,\sigma/x]\,\rho[v/x,g/f]}}
\)
Of course, whenever we take a fixed point, we have to make sure that
a fixed point exists. We know that the function satisfies continuity
and monotonicity because we are writing in the metalanguage. However,
for a fixed point to exist, $\Tr T{\sigma\to\tau}$  must be
a pointed CPO. But for this to be true, we have to make sure $\bot$ is
in the codomain of the function:
\(
\Tr T{\sigma\to\tau}_\bot &=& \Tr T{\sigma}\to\Tr T{\tau}_{\bot},
\)
We also have to change our contract to account for the possibility of nontermination:
\(
\rho\models\Gamma\ \wedge\ \Gjudge e\tau &\Rightarrow& \Cr{e} \in \Tr T\tau_\bot.
\)

Finally, we have to lift our semantics to take nontermination into account.
For example, we should change the denotation of a pair to:
\(
\Cr{(e_1, e_2)} &\definedas& 
\begin{cases}
{<\Cr{e_1},\Cr{e_2}>}, & \mbox{if both $\Cr{e_1}\neq\bot$ and $\Cr{e_2}\neq\bot$,}\\
\bot, & \mbox{otherwise.}
\end{cases}
\)

\end{document}
