\documentclass[11pt]{article}

\input{../latex/compilerstoolbox.sty}
\usepackage{homework}
\usepackage{mathpartir}
\usepackage{latexsym}
\usepackage{graphicx}

\newcommand{\assdate}{Tuesday, February 13, 2024 (11:59PM)}
\newcommand{\assnum}{2}
\newcommand{\asstitle}{L2 Statics as Inference Rules}
\newcommand{\lecturer}{}
\setlength{\headsep}{0.2in}


\begin{document}
\maketitle

\textbf{Reminder:} Assignments are individual assignments, not done in pairs.  The work must be all your own.
Hand in your solutions on Gradescope.
Please read the late policy for written assignments on the course web page.

\section{Inference Rules}

The static semantics of imperative languages can become quite complicated to describe precisely.
For C0 (and L1-L5), the statics ensure three desirable properties of the language:
\begin{itemize}
    \item \textit{type safety}: expressions are well-typed, variables are assigned values of the correct type, \dots
    \item \textit{variable initialization}: variables are always initialized before use.
    \item \textit{proper returns}: every control flow path in a function ends in a return.
\end{itemize}
Specifying the static semantics inductively with inference rules is
useful because the rules lead to a recursive algorithm for the
semantic analysis.

We provide inference rules for type safety in the L2 handout.
In this question, we consider inference rules for the other two properties,
which will help you when implementing the semantic analysis for L2.

It might be helpful to review the \href{https://www.cs.cmu.edu/~janh/courses/411/24/lectures/01-inddef.pdf}{lecture notes on inductive definitions} from Lecture 1.

\clearpage
\mprset{vskip=.2em}
\setlength{\parskip}{.5em}

\section{Variable Initialization}

\newcommand{\sInit}[4]{#1;\; #2 \proves #3 \Rightarrow #4}
\newcommand{\eInit}[2]{#1 \proves #2}

In lecture and recitation we saw a formalization of variable initialization semantics,
using the judgment:
\[ \sInit{\Gamma}{\Delta}{s}{\Delta'} \]
The intended meaning of the judgment is:
\begin{quote}
If the variables in $\Gamma$ are \textit{declared}
and the variables in $\Delta$ are \textit{initialized},
then the statement \(s\) does not use uninitialized variables,
and, after executing \(s\), leaves \(\Delta'\) variables initalized.
\end{quote}
The context \(\Gamma\) here is the same as in the typechecking judgment,
but in this question we omit the types for simplicity.

The complete definition of the judgment is in Figure \ref{fig:sinit}.
We write \( \eInit{\Delta}{e} \) to mean, ``\(e\) uses only variables in \(\Delta\)''.
Note the definition maintains the invariant that \( \Delta \subseteq \Delta' \subseteq \Gamma \) always.

\vspace{1cm}

\begin{figure}[h]
\def \MathparLineskip {\lineskip=1cm}
\begin{mathpar}
    \inferrule
        { \sInit{\Gamma}{\Delta}{s_1}{\Delta'}   \\
          \sInit{\Gamma}{\Delta'}{s_2}{\Delta''} }
        {\sInit{\Gamma}{\Delta}{\sseq{s_1}{s_2}}{\Delta''}}
        \; \textsc{seq}
    
    \inferrule
        { \quad }
        {\sInit{\Gamma}{\Delta}{\snop}{\Delta}}
        \; \textsc{nop}
    \\
    \inferrule
        { \sInit{\Gamma \cup \{x\}}{\Delta}{s}{\Delta'} }
        {\sInit{\Gamma}{\Delta}{\sdecl{x}{\tau}{s}}{\Delta' \setminus \{x\}}}
        \; \textsc{declare}
    
    \inferrule
        {\eInit{\Delta}{e}}
        {\sInit{\Gamma}{\Delta}{\sassn{x}{e}}{\Delta \cup \{ x \}}}
        \; \textsc{assign}
    \\
    \inferrule
        { \eInit{\Delta}{e} \\
          \sInit{\Gamma}{\Delta}{s_1}{\Delta'} \\
          \sInit{\Gamma}{\Delta}{s_2}{\Delta''}}
        {\sInit{\Gamma}{\Delta}{\sif{e}{s_1}{s_2}}{\Delta' \cap \Delta''}}
        \; \textsc{if}
    
    \inferrule
        { \eInit{\Delta}{e} \\
          \sInit{\Gamma}{\Delta}{s}{\Delta'}}
        {\sInit{\Gamma}{\Delta}{\swhile{e}{s}}{\Delta}}
        \; \textsc{while}
    \\
    \inferrule
        { \eInit{\Delta}{e} }
        {\sInit{\Gamma}{\Delta}{\sret{e}}{\Gamma}}
        \; \textsc{return}
\end{mathpar}
\caption{Definition of the \( \sInit{\Gamma}{\Delta}{s}{\Delta'} \) judgment.}
\label{fig:sinit}
\end{figure}

\clearpage
We can use these rules to build derivation trees, where we prove
the premises of each rule by applying more rules. For example, we can prove
the program in Figure \ref{fig:sinit-example} does not use uninitialized variables
with the derivation tree in Figure \ref{fig:sinit-tree}.

\begin{figure}[h!]
    \centering
    \begin{minipage}[t]{.4\textwidth}
        \centering
        \begin{verbatim}
        int main() {
            int x;
            x = 1;
            return x;
        }
        \end{verbatim}
    \end{minipage}
    \begin{minipage}[t]{.4\textwidth}
        \centering
        \vspace{-0.8cm}
        \begin{align*}
          &\sdecl{x}{\tint}{ \\
          &\quad \sseq{ \\
          &\qquad \sassn{x}{1}}{ \\
          &\qquad \sret{x}}}
        \end{align*}
    \end{minipage}
    \label{fig:sinit-example}
    \caption{Example program.}
\end{figure}


\begin{figure}[h]
\[
\inferrule{
    \inferrule{
        \inferrule{
            \eInit{\{\}}{1}
        }{
            \sInit{\{x\}}{\{\}}{\sassn{x}{1}}{\{x\}}
        } \; \textsc{assn}
        \and
        \inferrule{
            \eInit{\{x\}}{x}
        }{
            \sInit{\{x\}}{\{x\}}{\sret{x}}{\{x\}}
        } \; \textsc{ret}
    }{
        \sInit{\{x\}}{\{\}}{\sseq{\sassn{x}{1}}{\sret{x}}}{\{x\}}
    } \; \textsc{assn}
}{
    \sInit{\{\}}{\{\}}{
        \sdecl{x}{\tint}{\sseq{\sassn{x}{1}}{\sret{x}}}
    }{\{\}}
} \; \textsc{decl}
\]
\caption{Derivation tree for the example program.}
\label{fig:sinit-tree}
\end{figure}

For simplicity we are taking the \( \eInit{\Delta}{e} \) premises for granted (so long as they are true!).
Here, \( \eInit{\{\}}{1} \) is true because 1 uses no variables,
and \( \eInit{\{x\}}{x} \) is true because the expression uses one variable \(x\), which is in \(\{x\}\).

\begin{enumerate}
    \item[(a)] Define a variable set \( \Delta' \) so that
    the following judgment is derivable and write a derivation tree for the judgment.
    \[ \sInit{\{x,y\}}{\{\}}{\sif{\efalse}{\sassn{x}{1}}{\sret{2}}}{\Delta'} \]
    %
    You do not need to justify any expression initialization premises (the
    \( \eInit{\Delta}{e} \) premises),
    and you do not need to label the rules you use.

    \item[(b)] Try to derive the following conclusion with a derivation tree.
    \[ \sInit{\{\}}{\{\}}{\sseq{\sret{0}}{\sdecl{x}{\tint}{\sret{x}}}}{\{\}} \]
    Explain informally why the derivation fails, by showing a partial derivation and
    indicating where you got stuck.
\end{enumerate}


\clearpage

\section{Proper Returns}

\newcommand{\sReturns}[1]{#1 \; \textbf{returns}}

For this problem, we define a judgment \(\sReturns{s}\),
meaning that \(s\) is guaranteed to return during execution.
Below is the inference rule for \(\sdecl{x}{\tau}{s}\):

\begin{mathpar}
\inferrule{\sReturns{s}}{\sReturns{\sdecl{x}{\tau}{s}}} \; \textsc{Declare}
\end{mathpar}

\begin{enumerate}
    \item[(a)]
        Complete the definition with rules for
        \( \sassn{x}{e} \), \( \sif{e}{s_1}{s_2} \), \( \swhile{e}{s} \),
        \( \sret{e} \), \( \snop \), and \( \sseq{s_1}{s_2} \).
        Follow the informal definition provided in the L2 handout.

        \textit{Hint:} We express that a statement does \textit{not} return by not including a rule for it.
        So, some of the constructs will not have an associated rule.

    \item[(b)]
        Using your definition, explain why the following program does
        NOT satisfy the always-returns property:
        \[ s \triangleq \sif{\etrue}{\sret{1}}{\sassn{x}{1}} \]
        Specifically, try to derive \( \sReturns{s} \) and show where the derivation fails to
        make progress.
\end{enumerate}


\end{document}
