%%% 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}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{8}  %% Lecture number
\title{Evaluation Contexts} %% Title of lecture
%\author{Georgios Piliouras, Raghuram Ramanujan} %% name of scribe
\date{15 September 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\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}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: SCRIBE NOTES GO HERE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Evaluation Contexts}

The rules for structural operational semantics can be classified into two types:
\begin{itemize}
\item
\emph{reduction rules}, which describe the actual computation steps; and
\item
\emph{evaluation order rules}, which constrain the choice of reductions that can be performed next.
\end{itemize}

For example, the CBV reduction strategy for the $\lambda$-calculus was captured in the following rules:
\begin{equation}
  \frac{}{(\lam xe)~v \rightarrow \subst evx}\label{eqn:rule1}\\[-1em]
\end{equation}
\begin{eqnarray}
  \frac{e_1 \rightarrow e'_1}{e_1~e_2 \rightarrow e'_1~e_2} &\quad&
  \frac{e_2 \rightarrow e'_2}{v~e_2 \rightarrow v~e'_2}\label{eqn:rule2}
\end{eqnarray}
Rule (\ref{eqn:rule1}), $\beta$-reduction, is a reduction rule, whereas rules (\ref{eqn:rule2}) are evaluation order rules.  The rules (\ref{eqn:rule2}) say essentially that a reduction may be applied to a redex on the left-hand side of an application anytime, and may be applied to a redex on the right-hand side of an application provided the left-hand side is already fully reduced.

Although there are only two evaluation order rules in the CBV $\lambda$-calculus, there are typically many more in real-world programming languages.  This motivates the desire to find a more compact representation for such rules.  

\emph{Evaluation contexts} provide a mechanism to do just that.  An evaluation context $E$, sometimes written $\contextHole$, is a $\lambda$-term or a metaexpression representing a family of $\lambda$-terms with a special variable $\hole$ called the \emph{hole}.  If $\contextHole$ is an evaluation context, then $\context e$ represents $E$ with the term $e$ substituted for the hole.

Every evaluation context $\contextHole$ represents a _context rule_
\[
  \frac{e \rightarrow e'}{\context e \rightarrow \context{e'}},
\]
which says that we may apply the reduction $e\rightarrow e'$ in the context $\context e$.

For the case of the CBV $\lambda$-calculus, the two evaluation order rules (\ref{eqn:rule2}) are specified by the two evaluation context schemes $\hole\cdot e$ and $v\cdot\hole$.  These are just a compact way of representing the rules (\ref{eqn:rule2}).  Thus we could specify the CBV $\lambda$-calculus simply by writing
\[
\begin{array}{c@{\hspace{1cm}}c@{\hspace{1cm}}c}
  (\lam xe)~v \rightarrow \subst evx & \hole\cdot e & v\cdot\hole.
\end{array}
\]
The CBN $\lambda$-calculus has an equally compact specification:
\[
\begin{array}{c@{\hspace{1cm}}c}
  (\lam xe)~e' \rightarrow \subst e{e'}x & \hole\cdot e.
\end{array}
\]

\section{Nested Contexts}

Note that in CBV, the evaluation contexts $\hole\cdot e$ and $v\cdot\hole$ do not specify \emph{all} contexts in which the reduction rule (\ref{eqn:rule1}) may be applied.  There are also compound contexts obtained from nested applications of the rules (\ref{eqn:rule2}).  For example, the context
\begin{equation}
  (v \cdot \hole) \cdot e\label{eqn:nested}
\end{equation}
is also a valid evaluation context for CBV, since it can be derived from two applications of the rules (\ref{eqn:rule2}):
\begin{equation}
  \frac
  {\displaystyle\frac
 {e_1 \rightarrow e_2}
 {v~e_1 \rightarrow v~e_2}}
  {(v~e_1)~e \rightarrow (v~e_2)~e}.\label{eqn:nested2}
\end{equation}
Here we have applied the right-hand rule of (\ref{eqn:rule2}) in the first step and the left-hand rule of (\ref{eqn:rule2}) in the second.  The evaluation context (\ref{eqn:nested}) represents the abbreviated rule
\[
  \frac
  {e_1 \rightarrow e_2}
  {(v~e_1)~e \rightarrow (v~e_2)~e}
\]
obtained by collapsing the two steps of (\ref{eqn:nested2}).

The set of \emph{all} valid evaluation contexts for the CBV $\lambda$-calculus is represented by the grammar
\begin{eqnarray*}
  E &::=& \hole \bnf E\cdot e \bnf v\cdot E.
\end{eqnarray*}

\section{Annotated Proof Trees}

We can also use evaluation contexts to indicate exactly where a reduction is applied in each step of a proof tree.  For example, consider the annotated proof tree
\[
  \infer[\ (\hole~\lam z{zz})]{(\lam xx)~((\lam xx)~0)~\lam z{zz}\rightarrow(\lam xx)~0~\lam z{zz}}
  {\infer[\ ((\lam xx)~\hole)]{(\lam xx)~((\lam xx)~0)\rightarrow(\lam xx)~0}{(\lam xx)~0 \rightarrow 0}}
\]
We have labeled each step to indicate the context in which the $\beta$-reduction was applied.

As above, we can simplify the tree by collapsing the two steps and annotating the resulting abbreviated tree with the corresponding nested context:
\[
  \infer[\ ((\lam xx)~\hole~\lam z{zz})]{(\lam xx)~((\lam xx)~0)~\lam z{zz}\rightarrow(\lam xx)~0~\lam z{zz}}
  {(\lam xx)~0 \rightarrow 0}
\]

\section{Error Propagation}

Evaluation contexts can be used to define the semantics of error exceptions.  If we have a special error value $\mathsf{error}$, we can very easily propagate it using the evaluation order rule
\[
  \context{\mathsf{error}} \rightarrow \mathsf{error}.
\]
This obviates the need to show in painstaking detail how error propagates up through a series of applications of rewrite rules.  We will revisit this idea later on when we talk about exception handling mechanisms.

The benefits of evaluation contexts will become exceedingly clear in the future as we add more features to the language.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
