%%% 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{5}                            %% Lecture number
\title{IMP: Big-Step and Small-Step Semantics}   %% Title of lecture
%\author{Kevin Markman and Jon Guarino} %% name of scribe
\date{8 September 2006}              %% Date of lecture, e.g., 1 January 2001
%\lecturer{Radu Rugina}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

\begin{document}

\maketitle

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

\section{The IMP Language}

Today we present a very simple imperative language, IMP, along with small-step and big-step rules for evaluation.  We will give
\begin{itemize} % use enumerate for numbered list
\item the IMP language syntax;
\item a small-step semantics for IMP;
\item a big-step semantics for IMP;
\item some notes on why both can be useful.
\end{itemize}

\subsection{Syntax}

There are three types of statements in IMP:

\begin{itemize}
\item arithmetic expressions \nm{AExp} (elements are denoted $a,a_0,a_1,\ldots$)
\item Boolean expressions \nm{BExp} (elements are denoted $b,b_0,b_1,\ldots$)
\item commands \nm{Com} (elements are denoted $c,c_0,c_1,\ldots$)
\end{itemize}
A program in the IMP language is a command in \nm{Com}.

Let \nm{Var} be a countable set of variables.  Elements of \nm{Var} are denoted $x,x_0,x_1\ldots$~.  Let $n,n_0,n_1,\ldots$ denote integers (elements of $\mathbb{Z}=\{\ldots,-2,-1,0,1,2,\ldots\}$).  Let $\overline n$ be an integer constant symbol representing the number $n$.  The BNF grammar for IMP is

\begin{eqnarray*}
\nm{AExp} & ::= & \overline n \bnf x \bnf (a_0~\oplus~a_1) \\
\nm{BExp} & ::= & \TRUE \bnf \FALSE \bnf (a_0~\odot~a_1) \bnf (b_0~\oslash~b_1) \bnf (\neg b)\\
\nm{Com} & ::= & \SKIP \bnf x:=a \bnf (c_0~;~c_1) \bnf (\IF~b~\THEN~c_1~\ELSE~c_2) \bnf (\WHILE~b~\DO~c~) \\
\nm{$\oplus$} &::=& + \bnf * \bnf - \\
\nm{$\odot$} &::=& \leq \bnf = \\
\nm{$\oslash$} &::=& \vee \bnf \wedge
\end{eqnarray*}

\subsection{Stores and Configurations}

A \emph{store} (also known as a \emph{state}) is a function $Var \rightarrow \mathbb{Z}$ that assigns an integer to each variable.  The set of all stores is denoted $\Sigma$.

A \emph{configuration} is a pair $<c,\sigma>$, where $c\in Com$ is a command and $\sigma$ is a store.  Intuitively, the configuration $<c,\sigma>$ represents an instantaneous snapshot of reality during a computation, in which $\sigma$ represents the current values of the variables and $c$ represents the next command to be executed.

\section{Structural Operational Semantics (SOS): Small-Step Semantics}

Small-step semantics specifies the operation of a program one step at a time.  There is a set of rules that we continue to apply to configurations until reaching a final configuration $<\SKIP,\sigma>$ (if ever).  We write $<c,\sigma>\rightarrow<c',\sigma'>$ to indicate that the configuration $<c,\sigma>$ reduces to $<c',\sigma'>$ in one step, and we write $<c,\sigma>\stackrel *\rightarrow<c',\sigma'>$ to indicate that $<c,\sigma>$ reduces to $<c',\sigma'>$ in zero or more steps.  Thus $<c,\sigma>\stackrel *\rightarrow<c',\sigma'>$ iff there is a $k\geq 0$ and configurations $<c_0,\sigma_0>,\ldots,<c_k,\sigma_k>$ such that $<c,\sigma>=<c_0,\sigma_0>$, $<c',\sigma'>=<c_k,\sigma_k>$, and $<c_i,\sigma_i>\rightarrow<c_{i+1},\sigma_{i+1}>$ for $0\leq i\leq k-1$.

To be completely proper, we will define auxiliary small-step operators $\rightarrow_a$ and $\rightarrow_b$ for arithmetic and Boolean expressions, respectively, as well as $\rightarrow$ for commands\footnote{Winskel uses $\rightarrow_1$ instead of $\rightarrow$ to emphasize that only a single step is performed.}.  The types of these operators are
\begin{eqnarray*}
\rightarrow_{\ } &:& (Com\times\Sigma)\rightarrow(Com\times\Sigma) \\
\rightarrow_a &:& (AExp\times\Sigma)\rightarrow \mathbb{Z}\\
\rightarrow_b &:& (BExp\times\Sigma)\rightarrow \mathbf{2}
\end{eqnarray*}
Here $\mathbf{2}$ represents the two-element Boolean algebra consisting of the two truth values $\{\mathit{true},\mathit{false}\}$ with the usual Boolean operations $\wedge,\vee,\neg$.  Intuitively, $<a,\sigma> \stackrel *\rightarrow_a n$ if the expression $a$ evaluates to the integer value $n$ in state $\sigma$.

\subsection{Arithmetic and Boolean Expressions}
\begin{itemize}
\item
Constants:
\begin{tabular}c
\infer{<\overline n,\sigma>\rightarrow_a n}{}
\end{tabular}
\item
Variables:
\begin{tabular}c
\infer{<x,\sigma>\rightarrow_a \sigma(x)}{}
\end{tabular}
\item
Operations:
\begin{tabular}c
\infer{<a_0\oplus a_1,\sigma>\rightarrow_a n_0\oplus n_1}
{<a_0,\sigma>\rightarrow_a n_0 \quad <a_1,\sigma>\rightarrow_a n_1}
\end{tabular}
\end{itemize}
The rules for evaluating Boolean expressions and comparison operators are similar.

One subtle point: in the rule for arithmetic operations $\oplus$, the $\oplus$ appearing in the expression $a_0\oplus a_1$ represents the operation symbol in the IMP language, which is a syntactic object; whereas the $\oplus$ appearing in the expression $n_0\oplus n_1$ represents the actual operation in $\mathbb{Z}$, which is a semantic object.  These are two different things, just as $\overline n$ and $n$ are two different things and $\TRUE$ and $\mathit{true}$ are two different things.  In this case, at the risk of confusion, we have used the same metanotation $\oplus$ for both of them.

\subsection{Commands}

Let $\sigma[n/x]$ denote the store that is identical to $\sigma$ except possibly for the value of $x$, which is $n$.  That is,
\begin{eqnarray*}
\sigma[n/x](y) &\definedas& \left\{\begin{array}{ll} \sigma(y), & \mbox{if $y\neq x$,}\\n, & \mbox{if $y=x$.}\end{array}\right.
\end{eqnarray*}

\begin{itemize}
\item
Assignments:
\begin{tabular}c
\infer{<x:=a,\sigma>\rightarrow<\SKIP,\sigma[n/x]>}
{<a,\sigma>\rightarrow_a n}
\end{tabular}
\item
Sequences:
\begin{tabular}{cc}
\infer{<c_0;c_1,\sigma>\rightarrow<c_0';c_1,\sigma'>}
{<c_0,\sigma>\rightarrow<c_0',\sigma'>} &
\infer{<\SKIP;c_1,\sigma>\rightarrow<c_1,\sigma>}{}
\end{tabular}
\item
Conditionals:
\begin{tabular}{cc}
\infer{<\IF~b~\THEN~c_0~\ELSE~c_1,\sigma>\rightarrow<c_0,\sigma>}
{<b,\sigma>\rightarrow_b \mathit{true}}
&
\infer{<\IF~b~\THEN~c_0~\ELSE~c_1,\sigma>\rightarrow<c_1,\sigma>}
{<b,\sigma>\rightarrow_b \mathit{false}}
\end{tabular}
\item
While statements:
\begin{tabular}{c}
\infer{<\WHILE~b~\DO~c,\sigma>\rightarrow<\IF~b~\THEN~(c;\WHILE~b~\DO~c)~\ELSE~\SKIP,\sigma>}{}
\end{tabular}
\end{itemize}
There is no rule for $\SKIP$, since $<\SKIP,\sigma>$ is a final configuration.

\section{Structural Operational Semantics: Big-Step Semantics}

As an alternative to small-step operational semantics, which specifies the operation of the program one step at a time, we now consider big-step operational semantics, in which we specify the entire transition from a configuration (an <expression, state> pair) to a final value.  This relation is denoted $\stepsto$.  For arithmetic expressions, the final value is an integer; for Boolean expressions, it is a Boolean truth value $\mathit{true}$ or $\mathit{false}$; and for commands, it is a final state.  We write\\[1em]
\begin{tabular}{ll}
$<c,\sigma>\stepsto\sigma'$ & ($\sigma'$ is the store of the final configuration $<\SKIP,\sigma'>$, starting in configuration $<c,\sigma>$)\\
$<a,\sigma>\stepsto n$ & ($n$ is the integer value of arithmetic expression $a$ evaluated in state $\sigma$)\\
$<b,\sigma>\stepsto t$ & ($t\in\{\mathit{true},\mathit{false}\}$ is the truth value of Boolean expression $b$ evaluated in state $\sigma$)
\end{tabular}\\[1em]

The big-step rules for arithmetic and Boolean expressions are the same as the small-step rules.  However, the rules for commands are different:

\begin{itemize}
\item
Skip:
\begin{tabular}{c}
\infer{<\SKIP,\sigma>\stepsto\sigma}{}
\end{tabular}
\item
Assignments:
\begin{tabular}{c}
\infer{<x:=a,\sigma>\stepsto\sigma[n/x]}{<a,\sigma>\stepsto n}
\end{tabular}
\item
Sequences:
\begin{tabular}{c}
\infer{<c_0;c_1,\sigma>\stepsto\sigma''}
 {<c_0,\sigma>\stepsto\sigma'\quad<c_1,\sigma'>\stepsto\sigma''}
\end{tabular}
\item
Conditionals:
\begin{tabular}{cc}
\infer{<\IF~b~\THEN~c_0~\ELSE~c_1,\sigma>\stepsto\sigma'}
 {<b,\sigma>\stepsto\mathit{true}\quad<c_0,\sigma>\stepsto\sigma'} &
\infer{<\IF~b~\THEN~c_0~\ELSE~c_1,\sigma>\stepsto\sigma'}
 {<b,\sigma>\stepsto\mathit{false}\quad<c_1,\sigma>\stepsto\sigma'}
\end{tabular}
\item
While statements:
\begin{tabular}{cc}
\infer{<\WHILE~b~\DO~c,\sigma>\stepsto\sigma}
 {<b,\sigma>\stepsto\mathit{false}} &
\infer{<\WHILE~b~\DO~c,\sigma>\stepsto\sigma''}
 {<b,\sigma>\stepsto\mathit{true}\quad<c,\sigma>\stepsto\sigma'\quad<\WHILE~b~\DO~c,\sigma'>\stepsto\sigma''}
\end{tabular}
\end{itemize}

\section{Comparison of Big-Step vs.\ Small-Step SOS}

\subsection{Small-Step}
\begin{itemize}
\item
Small-step semantics can model more complex features, like programs that run forever and concurrency.
\item
Although one-step-at-a-time evaluation is useful for proving certain properties, in many cases it is unnecessary extra work.
\end{itemize}
\subsection{Big-Step}
\begin{itemize}
\item
Big steps in reasoning make it easier to prove things.
\item
Big-step semantics more closely models an actual recursive interpreter.
\item
Because evaluation skips over intermediate steps, all programs without final configurations (infinite loops,
errors, stuck configurations) look the same.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
