## Homework 0: Warmup

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

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

Coq is an *interactive theorem prover* or *proof assistant* widely used for
formalizing programming languages, mathematics, and even entire software systems. In an interactive
theorem prover, you write your program or model (often as a functional program), and then can write
and complete proofs about those programs. These proofs are written as a series of simple deduction
steps called tactics, and the proof assistant will automatically check that each deduction step you
write is logically sound.

In this course, we'll be using Coq to formalize most of our proofs, including all homework proofs and the final exam. We choose to use a proof assistant instead of pen-and-paper proofs for a few reasons. First, the proof assistant forces us to be precise in our reasoning—Coq will not be amused by hand-waving explanations we might make on paper. Second, the proof assistant helps us check our understanding of a proof—we don't need to worry about having missed a case of a proof, or making some wrong logical step. Finally, I just think proof assistants are pretty neat, and a cool application of programming languages ideas that are worth seeing.

This is a short warmup homework, intended mostly to get you up and running with Coq on your local machine, as well as introducing the GitHub Classroom platform we'll be using for homeworks in this class.

### Table of contents

### Preparation

First, we'll need to install Coq, as well as a development environment for it. I've tested these instructions on Mac and Linux, but everyone's system is a little different. Our friends at UW have written some very detailed installation instructions that you can try out if the more terse ones here don't work. If you run into trouble, please post on the Ed discussion board, as it's likely others have had the same problem!

#### Mac

On a Mac with Homebrew installed, installing Coq is as easy as:

```
brew install coq
```

Once that's done, skip along to Choosing an IDE.

#### Linux

I don't recommend using your distribution's package manager to install Coq, as most package managers tend to be several Coq versions behind (it's a fairly niche package). Instead, install Opam (the package manager for the OCaml programming language that Coq is implemented in) and then use it to install Coq. On Ubuntu 20.04 (and possibly other versions), this should be enough to get everything you need:

```
sudo apt install build-essential unzip bubblewrap
sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
opam init -a
eval $(opam env)
opam pin add coq 8.15.2
```

When you set up your IDE, you might need to tell it where Coq is installed.
These steps will install it to `$HOME/.opam/default/bin`

.

Coq is also available on the UTCS public Linux hosts, but it's a very outdated version. That should work for this homework, but for later homeworks you'll likely want a newer version.

#### Windows

You're on your own, sorry! The Windows Subsystem for Linux might work, or you can try the UW installation instructions linked above.

#### Choosing an IDE

Coq proofs are just text files like any other programming language, so you're free to use whatever text editor you prefer. But proofs are a *lot* easier to write with IDE support for showing you the proof context, exploring available lemmas, etc. I *strongly* recommend setting up a good IDE for Coq proofs. I have three suggestions here:

- My preference is Visual Studio Code with the VSCoq extension. This is what I use in lectures.
- For Emacs users, people like Proof General, optionally with the Company-Coq extensions, though I have no experience with them.
- Coq has its own CoqIDE tool, which is not the prettiest option, but will get the job done.

As we saw in lecture, most people write Coq proofs interactively. The core idea is to step backwards and forwards through the Coq definitions using your IDE. For VSCoq, look for "Step Backwards", "Step Forwards", and "Interpret to Point" commands (on my Mac, they're Ctrl+Option and Up, Down, and Right arrows, respectively).

### Get the code

We'll be using GitHub Classroom to check out and submit homework and the final exam this semester. I'm assuming you already have some experience with Git as a version control system. If not, Chapters 1 and 2 of the Git book are a good tutorial, as is GitHub's quickstart guide.

To get started, you'll need to create a GitHib account if you don't already have one. Then you need to use the GitHub Classroom URL from Ed to create your own private copy of the homework repository. Along the way, GitHub Classroom will ask you to associate your GitHub account with your UT EID—if you make a mistake here, you'll need my help to undo it, so please get in touch via Ed.

Once you have a repository created by GitHub Classroom,
clone it to your machine.
For example, the repository it created for me is called `hw0-jamesbornholt`

,
so I would do:

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

### Complete the homework

In your repository, there's just a single Coq file, `Homework.v`

.
That file contains all the tasks you need to complete,
and uses comments to explain what's going on.

Just as a cross-reference, here's a list of problems in that file:

- (2 points) Fill in the definition for
`length`

. - (2 points) Fill in the definition for
`append`

. - (4 points) Complete the proof of the
`append_length_sum`

theorem. - (1 point extra credit) Define the
`rev`

function and write the`rev_preserves_length`

theorem and proof.

#### Some 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

### 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 file you should need to modify is `Homework.v`

.

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 using GitHub Actions.
It will pass as long as your `Homework.v`

file compiles and has no remaining `admit`

or `Admitted`

statements.
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 proofs by hand.
Also, the autograder does not check the extra credit question.