## Homework 1: Denotational Semantics and Rosette

**Due date:** September 22, 6pm

**Grading**: 15% (CS 345H) or 9% (CS 386L) of your course grade

In this homework, we'll continue our exploration of *denotational semantics*, a style of programming
language semantics that uses mathematical functions to define the meaning of programs.

The homework is in three parts (but only two files):

- In Part 1, we'll extend the expression language
`expr`

semantics we studied in lecture to include support for variables, and add some program optimizations to the language. - In Part 2, we'll develop the syntax and semantics for a new
*register machine*language, and then build and prove a compiler from`expr`

s to this language. - In Part 3, we'll bring our semantics over to Rosette and see how we can use automated programming tools to build a compiler without hand-writing it.

### Table of contents

### Preparation

First, make sure you have Coq installed and working, following the instructions from Homework 0.

We'll also be working with Rosette in this homework, so we need to get the Racket programming language and Rosette itself set up on your system. Start by installing Racket (at least v8.1). On a Mac, you can get it from Homebrew:

```
brew install --cask racket
```

On Linux or Windows (or Mac, if you don't use Homebrew), download and run the appropriate installer from from the Racket website. If you're on Linux, don't get Racket from your package manager—most of them have very outdated versions of Racket that won't work.

We'll need access to the `raco`

command-line tool that comes with Racket.
Try running:

```
raco help
```

from a terminal. If it works, jump down to installing Rosette below.
If not, you need to get the directory you installed Racket into onto your `PATH`

.
The Beautiful Racket book has some good instructions on how to do this.

#### Install Rosette

To install Rosette from the command line:

```
raco pkg install rosette
```

You can test that it worked correctly by running something like:

```
racket -I rosette -e 1
```

If you just see `1`

as output, you're good to go. Otherwise, something went wrong when installing Rosette—try to read back through the output of the installation and debug (or ask for help).

#### Choosing a Rosette IDE

Racket comes with the DrRacket IDE, which might be a good place to start, especially if you've never written Racket before. It comes with a bunch of useful features like highlighting the source of bindings (try mousing over stuff!). On a Mac, DrRacket will be in the `/Applications/Racket v8.4`

folder.

The Magic Racket extension for Visual Studio Code is also a pretty good option.

### Get the code

We'll be using GitHub Classroom to check out and submit this homework.
Follow the GitHub Classroom URL on Ed to create your private copy of the homework repository,
and then clone that repository to your machine.
For example, the repository it created for me is called `hw1-jamesbornholt`

, so I would do:

```
git clone git@github.com:utcs345h/hw1-jamesbornholt.git
cd hw1-jamesbornholt
```

To make sure everything's working with your Rosette install, run:

```
raco test Homework.rkt
```

You should see 26 failing tests. You'll know you've finished the Rosette part of the homework when all these tests pass!

### Complete the homework

In your repository, there are two files you'll need to edit.
`Homework.v`

is the Coq file for Parts 1 and 2 of the homework,
and `Homework.rkt`

is the Rosette file for Part 3.
Both files use comments to explain what's going on and list the problems you need to solve.
A few of the problems in both files ask for English answers rather than code;
for those questions, you should just add your answer in the file as a comment.

There is a total of 142 points on this homework, plus 6–12 points of extra credit (and a competition for those points!). As a cross-reference, here's a list of the problems on this homework:

- Part 1 —
`Homework.v`

— 54 points plus 6–12 points extra credit- Problem 1 (2 points): about equality relations
- Problem 2 (4 points): prove
`var_name_goodness_is_not_42`

- Problem 3 (4 points): prove
`set_map_unchanged`

- Problem 4 (4 points): prove
`flip_involutive`

- Problem 5 (4 points): prove
`flip_preserves_count_vars`

- Problem 6 (4 points): prove
`count_vars_bounded_by_size`

- Problem 7 (6 points): prove
`zero_vars_independent`

- Problem 8 (6 points): prove
`rename_preserves_semantics`

- Problem 9 (10 points): prove
`constant_fold_is_effective`

- Problem 10 (10 points): prove
`constant_fold_preserves_semantics`

- Problem 11 (extra credit, 6-12 points): write a shorter proof of
`constant_fold_preserves_semantics`

- Part 2 —
`Homework.v`

— 52 points- Problem 12 (6 points): define
`eval_instr`

- Problem 13 (4 points): define
`eval_prog'`

- Problem 14 (8 points): define
`compile'`

- Problem 15 (6 points): prove
`eval_prog_append`

- Problem 16 (4 points): attempt to prove
`compile_correct`

- Problem 17 (4 points): about the failed proof of
`compile_correct`

- Problem 18 (10 points): prove
`compile'_registers_untouched`

- Problem 19 (10 points): prove
`compile_correct'`

and`compile_correct`

- Problem 12 (6 points): define
- Part 3 —
`Homework.rkt`

— 36 points- Problem 20 (6 points): define
`eval-instr`

- Problem 21 (4 points): define
`reverse`

- Problem 22 (4 points): define
`eval-prog*`

- Problem 23 (4 points): define
`const-ten-instr`

- Problem 24 (10 points): define
`compile`

- Problem 25 (4 points): about the results of
`compile`

- Problem 26 (4 points): feedback on the homework

- Problem 20 (6 points): define

#### Resources for writing Coq

Coq has a slightly steep learning curve. Here are some resources you could reference if you need help understanding how to write Coq or Coq proofs:

- UW's CSE 505 has some great notes on Coq in their first few lectures.
- The first chapter of Software Foundations is a crash course on both Coq programming and proofs.
- If you're trying to find tactics to use in proofs:
- Joe Redmon's Coq Tactics Index is somewhat (in)famous
- Cornell's CS3110 has a Coq tactics cheatsheet
- Coq's standard library documentation is extensive. Use it to find helpful lemmas about data structures you're using from the standard library (which, in this homework, is just
`nat`

from the`Arith`

module). - You can also try Coq's built-in
`Search`

command, as this blog post explains

#### Resources for writing Rosette

Rosette is a language we haven't spent much time talking about, so here are a few references for learning Racket and Rosette:

- The official Racket Guide is very thorough and useful
- The official Racket Reference gives more details about Racket

- The "Explainers" section of
*Beautiful Racket*has nice introductions to Racket language features - For Rosette, the official documentation is pretty good
- There is also a Rosette tutorial from CAV 2019
- How can I resist this opportunity to promote my blog about Rosette to a captive audience?

### What to submit

Submit your solutions by committing your changes in Git and pushing them to the private repository GitHub Classroom created for you in the Get the code step.

The only files you should need to modify are `Homework.v`

and `Homework.rkt`

.

GitHub Classroom will automatically select your most recent pushed commit
*before the deadline* as your submission.
There's no need to manually submit anything else via Canvas or GitHub.

GitHub Classroom also has a simple autograder for Coq proofs and Rosette code using GitHub Actions.
It will pass as long as your `Homework.v`

file compiles and has no remaining `admit`

or `Admitted`

statements, and your `Homework.rkt`

tests pass.
This is only a partial grader, so just because the autograder passes doesn't mean you'll get full points—we will still read your code by hand.
Also, the autograder does not check the extra credit question.