\documentclass[11pt]{article}

\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{graphicx, color}
\usepackage{moreverb}
\usepackage{fancyvrb}
\usepackage{mathrsfs}
\usepackage{listings}
\usepackage{amsmath,proof,amsthm,amssymb,enumerate}
\usepackage{math-cmds}

\lstset{
   language=Java,
   basicstyle=\footnotesize\ttfamily,
   keywordstyle=\bf,
}

\textwidth 16.0cm \textheight 23.5cm
\oddsidemargin 0.1in
\evensidemargin 0.1in
\topmargin -0.5in
\parskip 1ex
\columnsep 0.35in
\sloppy

\newcommand{\hfile}[1]{{\it HOMEDIR}{\tt /#1}}
\newcommand{\afile}[1]{{\it ASSTDIR}{\tt /#1}}
\newcommand{\TODO}[1]{\textbf{TODO: #1}}

\newcommand{\question}[2]
  {\bigskip \noindent
  {\bf Question \arabic{qnum}}, {#1}, {\it(#2 points).}
  \addtocounter{qnum}{1}
  }

\newcounter{qnum}
\setcounter{qnum}{1}

\newlength{\figratio}
\setlength{\figratio}{1.0in}

\newcommand{\centerfig}[2]{
   \centerline{\resizebox{#2in}{!}{\psfig{file=#1.eps}}}}

\begin{document}

\begin{center}
{\Large 17-396/17-696/17-960: Language Design and Prototyping, Spring 2020}\\[1ex]
{\Large Assignment 3: Language Semantics}\\[1ex]
{\large Assigned: Wednesday, January 29th}\\
{\large Due: Tuesday, February 4th, 11:59pm}
\end{center}


\section*{Policy}

The Warm-up Questions are to be done individually.  The Project Semantics Sketch may be done in groups of up to 2.  Team members should state that they are collaborating and turn in the same thing for the second part of their homework.

%For fairness, expectations for the coverage and level of detail in the assignment will be modestly increased for groups of two---but not doubled, since there are coordination costs.\footnote{Perhaps this is obvious, but just in case: although you can divide up the work to some extent, we would also like you to talk with each other about your design as part of your process, and not work completely independently on different aspects of of it.}

\section*{Logistics}

All clarifications (if any) to this assignment will be posted on the class discussion board on
\href{http://piazza.com/cmu/spring2020/17396/}{Piazza}.
Any revisions will be uploaded to the ``assignments'' page on the class web page.

\section{Learning Objectives of This Assignment}

In this assignment, you will apply what you have learned about programming language semantics to model the execution and typechecking of programs, and to reason about program properties and extensions.  We emphasize using formalism as a way to clarify how a language operates.

\section{Warm-up Questions}

\question{Typing}{10}

Take the class definitions for Pair, A, and B given in lecture (they are also in the FJ paper).
Consider the following class and method definition:

\begin{lstlisting}
class Bar {
    Bar() {}
    Pair foo(Pair p) {
        return ((Pair) (p.snd).setfst(new B())
    }
}
\end{lstlisting}

Construct an appropriate context for typechecking the body of the method \texttt{foo}, and give a typing derivation for the term in the method body.  It is OK if you just show the part of the deriviation that involves term typing rules and their premises.  You don't have to show the application of helper rules such as field lookup or method type, but you should show the judgment you get from these rules (e.g. \textit{fields}(C) = ...)

\question{Execution}{10}

Assume there are classes called \texttt{Hello} and \texttt{World}, each with no fields.
Show the execution steps for the following program:

\begin{lstlisting}
((Pair) new Pair(new A(), new Pair(new A(), new World())).fst).setfst(new Hello())
\end{lstlisting}

You do not have to construct a derivation tree for each step; just show what the program looks like at each execution step.

\question{Soundness}{10}

FJ includes the following rule for typechecking method invocation terms:

\[
\infer[\textit{T-Invk}]
    {\Gamma \vdash t_0.m(\overline{t}) : C}
    {\Gamma \vdash t_0 : C_0 & mtype(m, C_0) = \overline{D} \rightarrow C & \Gamma \vdash \overline{t} : \overline{C} & \overline{C} <: \overline{D}}
\]

Imagine a modified version of the rule--call it \textit{T-Invk$'$}--that is missing the last premise $\overline{C} <: \overline{D}$  Demonstrate that \textit{T-Invk$'$} is unsound.  Do this by writing a program that would typecheck with the modified \textit{T-Invk$'$} rule, but when it is run, gets stuck before evaluating to a value.

You can use the \texttt{Pair} example code if you want, but you will probably need to define at least one additional class and method in order to solve the problem.  Your answer should include the definition of any new classes as well as the term that represents the ``program.''

\question{Soundness (Extra Credit Challenge)}{5}

FJ has a rule for checking that a method definition such as $C_0~m(\overline{C}~\overline{x})~\{~\texttt{return}~t_0;~\}$ is well-typed.  Let's assume that that the class C that defines this method inherits from class D.  This rule includes the following check:

if \textit{mtype}$(m, D) = \overline{D} \rightarrow D_0 ~\textrm{then}~ \overline{C} = \overline{D} ~\textrm{and}~ C_0 = D_0$

This check enforces that when you override a method, the argument and return types in the subclass C are the same as they were in the superclass D.  Java actually has a more flexible rule:

if \textit{mtype}$(m, D) = \overline{D} \rightarrow D_0 ~\textrm{then}~ \overline{C} = \overline{D} ~\textrm{and}~ C_0 <: D_0$

This allows the return type in a subclass method to return a subtype.  For example, we might have a class \texttt{Dog} that is a subtype of \texttt{Animal}, and a class \texttt{Bark} that is a subtype of class \texttt{Sound}.  If \texttt{Animal.makeNoise()} returns a \texttt{Sound} object, it's OK for \texttt{Dog.makeNoise()} to return a \texttt{Bark} object.

Your friend suggests that we make the rule even more flexible:

if \textit{mtype}$(m, D) = \overline{D} \rightarrow D_0 ~\textrm{then}~ \overline{C} <: \overline{D} ~\textrm{and}~ C_0 <: D_0$

That way, if we have class \texttt{DogFood} extends \texttt{Food}, we can have a method \texttt{Animal.feed(Food)} and override it with \texttt{Dog.feed(DogFood)}.

Your friend's suggestion may seem like a good idea--the programming language Eiffel even implemented it--but it's flawed.  Can you come up with a program that demonstrates the flaw: it typechecks with your friend's rule, but when you run it, the program gets stuck without reducing to a value?


\question{Translation}{15}

We define a statement language:
\[
\begin{array}{rcl}
S &\bnfdef& \texttt{let x := new C(y..)} \\
  &\bnfalt& \texttt{let x := y.m(z..)} \\
  &\bnfalt& \texttt{let x := y.f} \\
\end{array}
\]

and some rules for transforming FJ terms into the statement language.  The transformation judgment is:

$t \leadsto \overline{S}; x$

and is read ``we translate term $t$ into the sequence of statements $\overline{S}$, with the result in variable $x$.''  Here are the rules:

\[
\begin{array}{c}
\infer[\textit{Trans-Var}]
    {x \leadsto []; x}
    {}
\\[2ex]

\infer[\textit{Trans-New}]
    {\texttt{new}~ C(\overline{t}) \leadsto \overline{(\overline{S_i})}, \texttt{let}~ y := \texttt{new}~ C(\overline{x}); y}
    {t_i \leadsto \overline{S_i}; x_i & y~\textrm{is a fresh variable}}
\\[2ex]

\infer[\textit{Trans-Invk}]
    {t_0.m(\overline{t}) \leadsto \overline{S'}, \overline{(\overline{S_i})}, \texttt{let}~ y := x_0.m(\overline{x}); y}
    {t_0 \leadsto \overline{S'}; x_0 & t_i \leadsto \overline{S_i}; x_i & y~\textrm{is a fresh variable}}
    
\end{array}
\]

In the rules above, $\overline{(\overline{S})}$ means concatenate all the $\overline{S_i}$ lists into a longer list.  ``$y$ is a fresh variable'' means we just choose a name for a variable that we haven't used in the translation before. [] means the empty list of statements.

Show how the following expression translates, using the rules above:

\begin{lstlisting}
new Pair(new A(), x).setfst(new B())
\end{lstlisting}

\question{Translation Extension}{10}

Write a rule for translating FJ field read terms into statements.

\section{Project Semantics Skietch}

\question{AST}{15}

Define an abstract syntax for a small part of your language in BNF form.  We recommend you include 5-10 constructs, though if you have time you can go a little bit beyond this.  Recall that FJ has 5 term forms (plus a few additional forms, e.g. for classes, methods, and constructors) so while 5 is small, you can often say interesting things even in small models.

For FJ, the answer to this question might be the term syntax in BNF form:

$t \bnfdef x \bnfalt t.f \bnfalt t.m(\overline{t}) \bnfalt (C)~t \bnfalt \texttt{new}~ C(\overline{t})$

\question{Judgment Form}{15}

Write at least one judgment form for your semantics.  This could show how your language is typechecked, or how it evaluates (e.g. like FJ's small-step evaluation rules), or how it translates into some other language (like the rules above).  If you choose a translation semantics, also give an AST for the output language of your translation rules.

For FJ, the answer to this question could be either the typing judgment:

$\Gamma \vdash t : C$

or the evaluation judgment:

$t \rightarrow t$

\question{Sample Rules}{15}

Write 2 sample rules for the judgment form given above.

For FJ, this would just be two of the evaluation rules, or two of the typing rules.

\textit{Note: I want you to think creatively and write some draft rules for this question.  Your first draft may not be completely correct or sensible; that's OK.  The goal of this assignment is to get something down on paper; I'll grade on that basis, and give you feedback for improving your semantics in a future assignment.}

\section*{Handins}

Turn in a PDF file on Canvas.  This can be produced via LaTeX, by printing a .txt file to PDF, or by neatly writing on paper and scanning the result.

\end{document}
