# ImpSimple Imperative Programs

In this chapter, we begin a new direction that will continue for the rest of the course. Up to now most of our attention has been focused on various aspects of Coq itself, while from now on we'll mostly be using Coq to formalize other things. (We'll continue to pause from time to time to introduce a few additional aspects of Coq.)
Our first case study is a simple imperative programming language called Imp, embodying a tiny core fragment of conventional mainstream languages such as C and Java. Here is a familiar mathematical function written in Imp.
Z ::= X;;
Y ::= 1;;
WHILE not (Z = 0) DO
Y ::= Y × Z;;
Z ::= Z - 1
END
This chapter looks at how to define the syntax and semantics of Imp; the chapters that follow develop a theory of program equivalence and introduce Hoare Logic, a widely used logic for reasoning about imperative programs.

### Sflib

Note that we import earlier definitions from Sflib here, not Logic:

Require Export SfLib.

# Arithmetic and Boolean Expressions

We'll present Imp in three parts: first a core language of arithmetic and boolean expressions, then an extension of these expressions with variables, and finally a language of commands including assignment, conditions, sequencing, and loops.

## Syntax

Abstract syntax trees for arithmetic and boolean expressions:

Inductive aexp : Type :=
| ANum : nat aexp
| APlus : aexp aexp aexp
| AMinus : aexp aexp aexp
| AMult : aexp aexp aexp.

Inductive bexp : Type :=
| BTrue : bexp
| BFalse : bexp
| BEq : aexp aexp bexp
| BLe : aexp aexp bexp
| BNot : bexp bexp
| BAnd : bexp bexp bexp.

For comparison, here's a conventional BNF (Backus-Naur Form) grammar defining the same abstract syntax:
a ::= nat
| a + a
| a - a
| a × a

b ::= true
| false
| a = a
| a ≤ a
| b and b
| not b

## Evaluation

Evaluating an arithmetic expression produces a number.

Fixpoint aeval (a : aexp) : nat :=
match a with
| ANum nn
| APlus a1 a2 ⇒ (aeval a1) + (aeval a2)
| AMinus a1 a2 ⇒ (aeval a1) - (aeval a2)
| AMult a1 a2 ⇒ (aeval a1) × (aeval a2)
end.

Example test_aeval1:
aeval (APlus (ANum 2) (ANum 2)) = 4.
Proof. reflexivity. Qed.

Similarly, evaluating a boolean expression yields a boolean.

Fixpoint beval (b : bexp) : bool :=
match b with
| BTruetrue
| BFalsefalse
| BEq a1 a2beq_nat (aeval a1) (aeval a2)
| BLe a1 a2ble_nat (aeval a1) (aeval a2)
| BNot b1negb (beval b1)
| BAnd b1 b2andb (beval b1) (beval b2)
end.

What does the following expression evaluate to?
aeval (APlus (ANum 3) (AMinus (ANum 4) (ANum 1)))
(1) true
(2) false
(3) 0
(4) 3
(5) 6

## Optimization

Fixpoint optimize_0plus (a:aexp) : aexp :=
match a with
| ANum n
ANum n
| APlus (ANum 0) e2
optimize_0plus e2
| APlus e1 e2
APlus (optimize_0plus e1) (optimize_0plus e2)
| AMinus e1 e2
AMinus (optimize_0plus e1) (optimize_0plus e2)
| AMult e1 e2
AMult (optimize_0plus e1) (optimize_0plus e2)
end.

Example test_optimize_0plus:
optimize_0plus (APlus (ANum 2)
(APlus (ANum 0)
(APlus (ANum 0) (ANum 1))))
= APlus (ANum 2) (ANum 1).
Proof. reflexivity. Qed.

Theorem optimize_0plus_sound: a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros a. induction a.
Case "ANum". reflexivity.
Case "APlus". destruct a1.
SCase "a1 = ANum n". destruct n.
SSCase "n = 0". simpl. apply IHa2.
SSCase "n ≠ 0". simpl. rewrite IHa2. reflexivity.
SCase "a1 = APlus a1_1 a1_2".
simpl. simpl in IHa1. rewrite IHa1.
rewrite IHa2. reflexivity.
SCase "a1 = AMinus a1_1 a1_2".
simpl. simpl in IHa1. rewrite IHa1.
rewrite IHa2. reflexivity.
SCase "a1 = AMult a1_1 a1_2".
simpl. simpl in IHa1. rewrite IHa1.
rewrite IHa2. reflexivity.
Case "AMinus".
simpl. rewrite IHa1. rewrite IHa2. reflexivity.
Case "AMult".
simpl. rewrite IHa1. rewrite IHa2. reflexivity. Qed.

# Coq Automation

That last proof was getting a little repetitive. Let's pause to learn a few more Coq tricks.

## Tacticals

Tacticals is Coq's term for tactics that take other tactics as arguments — "higher-order tactics," if you will.

### The repeat Tactical

The repeat tactical takes another tactic and keeps applying this tactic until the tactic fails. Here is an example showing that 100 is even using repeat.

Theorem ev100 : ev 100.
Proof.
repeat (apply ev_SS). (* applies ev_SS 50 times,
until apply ev_SS fails *)

apply ev_0.
Qed.
(* Print ev100. *)

### The try Tactical

If T is a tactic, then try T is a tactic that is just like T except that, if T fails, try T successfully does nothing at all (instead of failing).

Theorem silly1 : ae, aeval ae = aeval ae.
Proof. try reflexivity. (* this just does reflexivity *) Qed.

Theorem silly2 : (P : Prop), P P.
Proof.
intros P HP.
try reflexivity. (* just reflexivity would have failed *)
apply HP. (* we can still finish the proof in some other way *)
Qed.

### The ; Tactical (Simple Form)

In its most commonly used form, the ; tactical takes two tactics as argument: T;T' first performs the tactic T and then performs the tactic T' on each subgoal generated by T.
For example:

Lemma foo : n, ble_nat 0 n = true.
Proof.
intros.
destruct n.
(* Leaves two subgoals, which are discharged identically...  *)
Case "n=0". simpl. reflexivity.
Case "n=Sn'". simpl. reflexivity.
Qed.

We can simplify this proof using the ; tactical:

Lemma foo' : n, ble_nat 0 n = true.
Proof.
intros.
destruct n; (* destruct the current goal *)
simpl; (* then simpl each resulting subgoal *)
reflexivity. (* and do reflexivity on each resulting subgoal *)
Qed.

Using try and ; together, we can get rid of the repetition in the proof that was bothering us a little while ago.

Theorem optimize_0plus_sound': a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros a.
induction a;
(* Most cases follow directly by the IH *)
try (simpl; rewrite IHa1; rewrite IHa2; reflexivity).
(* The remaining cases -- ANum and APlus -- are different *)
Case "ANum". reflexivity.
Case "APlus".
destruct a1;
(* Again, most cases follow directly by the IH *)
try (simpl; simpl in IHa1; rewrite IHa1;
rewrite IHa2; reflexivity).
(* The interesting case, on which the try... does nothing,
is when e1 = ANum n. In this case, we have to destruct
n (to see whether the optimization applies) and rewrite
with the induction hypothesis. *)

SCase "a1 = ANum n". destruct n;
simpl; rewrite IHa2; reflexivity. Qed.

This proof can be further improved by removing the trivial first case (for e = ANum n).

Theorem optimize_0plus_sound'': a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros a.
induction a;
(* Most cases follow directly by the IH *)
try (simpl; rewrite IHa1; rewrite IHa2; reflexivity);
(* ... or are immediate by definition *)
try reflexivity.
(* The interesting case is when a = APlus a1 a2. *)
Case "APlus".
destruct a1; try (simpl; simpl in IHa1; rewrite IHa1;
rewrite IHa2; reflexivity).
SCase "a1 = ANum n". destruct n;
simpl; rewrite IHa2; reflexivity. Qed.

## Defining New Tactic Notations

Coq also provides several ways of "programming" tactic scripts:
• Ltac: scripting language for tactics
• Tactic Notation: syntax extension for tactics
• OCaml tactic scripting API (only for wizards)
An example Tactic Notation:

Tactic Notation "simpl_and_try" tactic(c) :=
simpl;
try c.

### Bulletproofing Case Analyses

Tactic Notation "aexp_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "ANum" | Case_aux c "APlus"
| Case_aux c "AMinus" | Case_aux c "AMult" ].

(Case_aux implements the common functionality of Case, SCase, SSCase, etc. For example, Case "foo" is defined as Case_aux Case "foo".)
For example, if a is a variable of type aexp, then doing
aexp_cases (induction aCase
will perform an induction on a (the same as if we had just typed induction a) and also add a Case tag to each subgoal generated by the induction, labeling which constructor it comes from. For example, here is yet another proof of optimize_0plus_sound, using aexp_cases:

Theorem optimize_0plus_sound''': a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros a.
aexp_cases (induction a) Case;
try (simpl; rewrite IHa1; rewrite IHa2; reflexivity);
try reflexivity.
(* At this point, there is already an "APlus" case name
in the context.  The Case "APlus" here in the proof
text has the effect of a sanity check: if the "Case"
string in the context is anything _other_ than "APlus"
(for example, because we added a clause to the definition
of aexp and forgot to change the proof) we'll get a
helpful error at this point telling us that this is now
the wrong case. *)

Case "APlus".
aexp_cases (destruct a1) SCase;
try (simpl; simpl in IHa1;
rewrite IHa1; rewrite IHa2; reflexivity).
SCase "ANum". destruct n;
simpl; rewrite IHa2; reflexivity. Qed.

## The omega Tactic

The omega tactic implements a decision procedure for a subset of first-order logic called Presburger arithmetic. It is based on the Omega algorithm invented in 1992 by William Pugh.
If the goal is a universally quantified formula made out of
• numeric constants, addition (+ and S), subtraction (- and pred), and multiplication by constants (this is what makes it Presburger arithmetic),
• equality (= and ) and inequality (), and
• the logical connectives , , ¬, and ,
then invoking omega will either solve the goal or tell you that it is actually false.

Example silly_presburger_example : m n o p,
m + nn + o o + 3 = p + 3
mp.
Proof.
intros. omega.
Qed.

## A Few More Handy Tactics

Finally, here are some miscellaneous tactics that you may find convenient.
• clear H: Delete hypothesis H from the context.
• subst x: Find an assumption x = e or e = x in the context, replace x with e throughout the context and current goal, and clear the assumption.
• subst: Substitute away all assumptions of the form x = e or e = x.
• rename... into...: Change the name of a hypothesis in the proof context. For example, if the context includes a variable named x, then rename x into y will change all occurrences of x to y.
• assumption: Try to find a hypothesis H in the context that exactly matches the goal; if one is found, behave just like apply H.
• contradiction: Try to find a hypothesis H in the current context that is logically equivalent to False. If one is found, solve the goal.
• constructor: Try to find a constructor c (from some Inductive definition in the current environment) that can be applied to solve the current goal. If one is found, behave like apply c.

# Evaluation as a Relation

We have presented aeval and beval as functions defined by Fixpoints. Another way to think about evaluation — one that we will see is often more flexible — is as a relation between expressions and their values. This leads naturally to Inductive definitions like the following one for arithmetic expressions...

Inductive aevalR : aexp nat Prop :=
| E_ANum : (n: nat),
aevalR (ANum n) n
| E_APlus : (e1 e2: aexp) (n1 n2: nat),
aevalR e1 n1
aevalR e2 n2
aevalR (APlus e1 e2) (n1 + n2)
| E_AMinus: (e1 e2: aexp) (n1 n2: nat),
aevalR e1 n1
aevalR e2 n2
aevalR (AMinus e1 e2) (n1 - n2)
| E_AMult : (e1 e2: aexp) (n1 n2: nat),
aevalR e1 n1
aevalR e2 n2
aevalR (AMult e1 e2) (n1 × n2).

A standard notation for "evaluates to":

Notation "e '' n" := (aevalR e n) : type_scope.

If we "reserve" the notation in advance, we can also use it in the definition:

Reserved Notation "e '' n" (at level 50, left associativity).

Inductive aevalR : aexp nat Prop :=
| E_ANum : (n:nat),
(ANum n) n
| E_APlus : (e1 e2: aexp) (n1 n2 : nat),
(e1 n1) (e2 n2) (APlus e1 e2) (n1 + n2)
| E_AMinus : (e1 e2: aexp) (n1 n2 : nat),
(e1 n1) (e2 n2) (AMinus e1 e2) (n1 - n2)
| E_AMult : (e1 e2: aexp) (n1 n2 : nat),
(e1 n1) (e2 n2) (AMult e1 e2) (n1 × n2)

where "e '' n" := (aevalR e n) : type_scope.

Tactic Notation "aevalR_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "E_ANum" | Case_aux c "E_APlus"
| Case_aux c "E_AMinus" | Case_aux c "E_AMult" ].

Write down a term with the following type:
(AMinus (ANum 3) (AMinus (ANum 2) (ANum 1)))  2

## Inference Rule Notation

For example, the constructor E_APlus...
| E_APlus : (e1 e2aexp) (n1 n2nat),
aevalR e1 n1
aevalR e2 n2
aevalR (APlus e1 e2) (n1 + n2)
...would be written like this as an inference rule:
 e1 ⇓ n1 e2 ⇓ n2 (E_APlus) APlus e1 e2 ⇓ n1+n2
There is nothing very deep going on here:
• rule name corresponds to a constructor name
• above the line are premises
• below the line is conclusion
• metavariables like e1 and n1 are implicitly universally quantified
• the whole collection of rules is implicitly wrapped in an Inductive (sometimes we write this slightly more explicitly, as "...closed under these rules...")
For example, is the smallest relation closed under these rules:
 (E_ANum) ANum n ⇓ n
 e1 ⇓ n1 e2 ⇓ n2 (E_APlus) APlus e1 e2 ⇓ n1+n2
 e1 ⇓ n1 e2 ⇓ n2 (E_AMinus) AMinus e1 e2 ⇓ n1-n2
 e1 ⇓ n1 e2 ⇓ n2 (E_AMult) AMult e1 e2 ⇓ n1*n2

Here, again, is the Coq definition of the beval function:
Fixpoint beval (e : bexp) : bool :=
match e with
| BTrue       ⇒ true
| BFalse      ⇒ false
| BEq a1 a2   ⇒ beq_nat (aeval a1) (aeval a2)
| BLe a1 a2   ⇒ ble_nat (aeval a1) (aeval a2)
| BNot b1     ⇒ negb (beval b1)
| BAnd b1 b2  ⇒ andb (beval b1) (beval b2)
end.
Write out a corresponding definition of boolean evaluation as a relation (in inference rule notation).

 (E_BTrue) BTrue ⇓ true
 (E_BFalse) BFalse ⇓ false
 e1 ⇓ n1 e2 ⇓ n2 beq_nat n1 n2 = b (E_BEq) BEq e1 e2 ⇓ b
 e1 ⇓ n1 e2 ⇓ n2 (E_BLe) BEq e1 e2 ⇓ ble_nat n1 n2
 e1 ⇓ b1 (E_BNot) BNot e1 ⇓ negb b1
 e1 ⇓ b1 e2 ⇓ b2 (E_BAnd) BAnd e1 e2 ⇓ andb b1 b2

## Equivalence of the Definitions

It is straightforward to prove that the relational and functional definitions of evaluation agree on all possible arithmetic expressions...

Theorem aeval_iff_aevalR : a n,
(a n) aeval a = n.
Proof.
split.
Case "".
intros H.
aevalR_cases (induction H) SCase; simpl.
SCase "E_ANum".
reflexivity.
SCase "E_APlus".
rewrite IHaevalR1. rewrite IHaevalR2. reflexivity.
SCase "E_AMinus".
rewrite IHaevalR1. rewrite IHaevalR2. reflexivity.
SCase "E_AMult".
rewrite IHaevalR1. rewrite IHaevalR2. reflexivity.
Case "".
generalize dependent n.
aexp_cases (induction a) SCase;
simpl; intros; subst.
SCase "ANum".
apply E_ANum.
SCase "APlus".
apply E_APlus.
apply IHa1. reflexivity.
apply IHa2. reflexivity.
SCase "AMinus".
apply E_AMinus.
apply IHa1. reflexivity.
apply IHa2. reflexivity.
SCase "AMult".
apply E_AMult.
apply IHa1. reflexivity.
apply IHa2. reflexivity.
Qed.

We can make the proof quite a bit shorter by making more use of tacticals...

Theorem aeval_iff_aevalR' : a n,
(a n) aeval a = n.
Proof.
(* WORK IN CLASS *) Admitted.

## Computational vs. Relational Definitions

For the definitions of evaluation for arithmetic and boolean expressions, the choice of whether to use functional or relational definitions is mainly a matter of taste. In general, Coq has somewhat better support for working with relations. On the other hand, in some sense function definitions carry more information, because functions are necessarily deterministic and defined on all arguments; for a relation we have to show these properties explicitly if we need them. Functions also take advantage of Coq's computations mechanism.
However, there are circumstances where relational definitions of evaluation are preferable to functional ones.

For example, suppose that we wanted to extend the arithmetic operations by considering also a division operation:

Inductive aexp : Type :=
| ANum : nat aexp
| APlus : aexp aexp aexp
| AMinus : aexp aexp aexp
| AMult : aexp aexp aexp
| ADiv : aexp aexp aexp. (* <--- new *)

Extending the definition of aeval to handle this new operation would not be straightforward (what should we return as the result of ADiv (ANum 5) (ANum 0)?). But extending aevalR is straightforward.

Inductive aevalR : aexp nat Prop :=
| E_ANum : (n:nat),
(ANum n) n
| E_APlus : (a1 a2: aexp) (n1 n2 : nat),
(a1 n1) (a2 n2) (APlus a1 a2) (n1 + n2)
| E_AMinus : (a1 a2: aexp) (n1 n2 : nat),
(a1 n1) (a2 n2) (AMinus a1 a2) (n1 - n2)
| E_AMult : (a1 a2: aexp) (n1 n2 : nat),
(a1 n1) (a2 n2) (AMult a1 a2) (n1 × n2)
| E_ADiv : (a1 a2: aexp) (n1 n2 n3: nat),
(a1 n1) (a2 n2) (mult n2 n3 = n1) (ADiv a1 a2) n3

where "a '' n" := (aevalR a n) : type_scope.

Suppose, instead, that we want to extend the arithmetic operations by a nondeterministic number generator any:

Inductive aexp : Type :=
| AAny : aexp (* <--- NEW *)
| ANum : nat aexp
| APlus : aexp aexp aexp
| AMinus : aexp aexp aexp
| AMult : aexp aexp aexp.

Again, extending aeval would be tricky (because evaluation is not a deterministic function from expressions to numbers), but extending aevalR is no problem:

Inductive aevalR : aexp nat Prop :=
| E_Any : (n:nat),
AAny n (* <--- new *)
| E_ANum : (n:nat),
(ANum n) n
| E_APlus : (a1 a2: aexp) (n1 n2 : nat),
(a1 n1) (a2 n2) (APlus a1 a2) (n1 + n2)
| E_AMinus : (a1 a2: aexp) (n1 n2 : nat),
(a1 n1) (a2 n2) (AMinus a1 a2) (n1 - n2)
| E_AMult : (a1 a2: aexp) (n1 n2 : nat),
(a1 n1) (a2 n2) (AMult a1 a2) (n1 × n2)

where "a '' n" := (aevalR a n) : type_scope.

# Expressions With Variables

Let's turn our attention back to defining Imp. The next thing we need to do is to enrich our arithmetic and boolean expressions with variables. To keep things simple, we'll assume that all variables are global and that they only hold numbers.

## Identifiers

To begin, we'll need to formalize identifiers such as program variables. We could use strings for this — or, in a real compiler, fancier structures like pointers into a symbol table. But for simplicity let's just use natural numbers as identifiers.

We define a new inductive datatype Id so that we won't confuse identifiers and numbers. We use sumbool to define a computable equality operator on Id.

Inductive id : Type :=
Id : nat id.

Theorem eq_id_dec : id1 id2 : id, {id1 = id2} + {id1id2}.
Proof.
intros id1 id2.
destruct id1 as [n1]. destruct id2 as [n2].
destruct (eq_nat_dec n1 n2) as [Heq | Hneq].
Case "n1 = n2".
left. rewrite Heq. reflexivity.
Case "n1 ≠ n2".
right. intros contra. inversion contra. apply Hneq. apply H0.
Defined.

The following lemmas will be useful for rewriting terms involving eq_id_dec.

Lemma eq_id : (T:Type) x (p q:T),
(if eq_id_dec x x then p else q) = p.
Proof.
intros.
destruct (eq_id_dec x x).
Case "x = x".
reflexivity.
Case "x ≠ x (impossible)".
apply ex_falso_quodlibet; apply n; reflexivity. Qed.

#### Exercise: 1 star, optional (neq_id)

Lemma neq_id : (T:Type) x y (p q:T), xy
(if eq_id_dec x y then p else q) = q.
Proof.
(* FILL IN HERE *) Admitted.

## States

A state represents the current values of all the variables at some point in the execution of a program.

Definition state := id nat.

Definition empty_state : state :=
fun _ ⇒ 0.

Definition update (st : state) (x : id) (n : nat) : state :=
fun x'if eq_id_dec x x' then n else st x'.

For proofs involving states, we'll need several simple properties of update.

#### Exercise: 1 star (update_eq)

Theorem update_eq : n x st,
(update st x n) x = n.
Proof.
(* FILL IN HERE *) Admitted.

#### Exercise: 1 star (update_neq)

Theorem update_neq : x2 x1 n st,
x2x1
(update st x2 n) x1 = (st x1).
Proof.
(* FILL IN HERE *) Admitted.

Theorem update_shadow : n1 n2 x1 x2 (st : state),
(update (update st x2 n1) x2 n2) x1 = (update st x2 n2) x1.
Proof.
(* FILL IN HERE *) Admitted.

#### Exercise: 2 stars (update_same)

Theorem update_same : n1 x1 x2 (st : state),
st x1 = n1
(update st x1 n1) x2 = st x2.
Proof.
(* FILL IN HERE *) Admitted.

#### Exercise: 3 stars (update_permute)

Theorem update_permute : n1 n2 x1 x2 x3 st,
x2x1
(update (update st x2 n1) x1 n2) x3 = (update (update st x1 n2) x2 n1) x3.
Proof.
(* FILL IN HERE *) Admitted.

## Syntax

We can add variables to the arithmetic expressions we had before by simply adding one more constructor:

Inductive aexp : Type :=
| ANum : nat aexp
| AId : id aexp (* <----- NEW *)
| APlus : aexp aexp aexp
| AMinus : aexp aexp aexp
| AMult : aexp aexp aexp.

Tactic Notation "aexp_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "ANum" | Case_aux c "AId" | Case_aux c "APlus"
| Case_aux c "AMinus" | Case_aux c "AMult" ].

Defining a few variable names as notational shorthands will make examples easier to read:

Definition X : id := Id 0.
Definition Y : id := Id 1.
Definition Z : id := Id 2.

The definition of bexps is the same as before (using the new aexps):

Inductive bexp : Type :=
| BTrue : bexp
| BFalse : bexp
| BEq : aexp aexp bexp
| BLe : aexp aexp bexp
| BNot : bexp bexp
| BAnd : bexp bexp bexp.

Tactic Notation "bexp_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "BTrue" | Case_aux c "BFalse" | Case_aux c "BEq"
| Case_aux c "BLe" | Case_aux c "BNot" | Case_aux c "BAnd" ].

## Evaluation

Add an st parameter to both evaluation functions.

Fixpoint aeval (st : state) (a : aexp) : nat :=
match a with
| ANum nn
| AId xst x (* <----- NEW *)
| APlus a1 a2 ⇒ (aeval st a1) + (aeval st a2)
| AMinus a1 a2 ⇒ (aeval st a1) - (aeval st a2)
| AMult a1 a2 ⇒ (aeval st a1) × (aeval st a2)
end.

Fixpoint beval (st : state) (b : bexp) : bool :=
match b with
| BTruetrue
| BFalsefalse
| BEq a1 a2beq_nat (aeval st a1) (aeval st a2)
| BLe a1 a2ble_nat (aeval st a1) (aeval st a2)
| BNot b1negb (beval st b1)
| BAnd b1 b2andb (beval st b1) (beval st b2)
end.

# Commands

Now we are ready define the syntax and behavior of Imp commands (often called statements).

## Syntax

Informally, commands c are described by the following BNF grammar:
c ::= SKIP
| x ::= a
| c ;; c
| WHILE b DO c END
| IFB b THEN c ELSE c FI
For example, here's the factorial function in Imp.
Z ::= X;;
Y ::= 1;;
WHILE not (Z = 0) DO
Y ::= Y × Z;;
Z ::= Z - 1
END
When this command terminates, the variable Y will contain the factorial of the initial value of X.

Inductive com : Type :=
| CSkip : com
| CAss : id aexp com
| CSeq : com com com
| CIf : bexp com com com
| CWhile : bexp com com.

Tactic Notation "com_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "SKIP" | Case_aux c "::=" | Case_aux c ";;"
| Case_aux c "IFB" | Case_aux c "WHILE" ].

As usual, we can use a few Notation declarations to make things more readable. We need to be a bit careful to avoid conflicts with Coq's built-in notations, so we'll keep this light — in particular, we won't introduce any notations for aexps and bexps to avoid confusion with the numerical and boolean operators we've already defined. We use the keyword IFB for conditionals instead of IF, for similar reasons.

Notation "'SKIP'" :=
CSkip.
Notation "x '::=' a" :=
(CAss x a) (at level 60).
Notation "c1 ;; c2" :=
(CSeq c1 c2) (at level 80, right associativity).
Notation "'WHILE' b 'DO' c 'END'" :=
(CWhile b c) (at level 80, right associativity).
Notation "'IFB' c1 'THEN' c2 'ELSE' c3 'FI'" :=
(CIf c1 c2 c3) (at level 80, right associativity).

For example, here is the factorial function again, written as a formal definition to Coq:

Definition fact_in_coq : com :=
Z ::= AId X;;
Y ::= ANum 1;;
WHILE BNot (BEq (AId Z) (ANum 0)) DO
Y ::= AMult (AId Y) (AId Z);;
Z ::= AMinus (AId Z) (ANum 1)
END.

## Examples

Assignment:

Definition plus2 : com :=
X ::= (APlus (AId X) (ANum 2)).

Definition XtimesYinZ : com :=
Z ::= (AMult (AId X) (AId Y)).

Definition subtract_slowly_body : com :=
Z ::= AMinus (AId Z) (ANum 1) ;;
X ::= AMinus (AId X) (ANum 1).

Loops:

Definition subtract_slowly : com :=
WHILE BNot (BEq (AId X) (ANum 0)) DO
subtract_slowly_body
END.

Definition subtract_3_from_5_slowly : com :=
X ::= ANum 3 ;;
Z ::= ANum 5 ;;
subtract_slowly.

An infinite loop:

Definition loop : com :=
WHILE BTrue DO
SKIP
END.

# Evaluation

Next we need to define what it means to evaluate an Imp command. The fact that WHILE loops don't necessarily terminate makes defining an evaluation function tricky...

## Evaluation as a Function (Failed Attempt)

Here's an attempt at defining an evaluation function for commands, omitting the WHILE case.

Fixpoint ceval_fun_no_while (st : state) (c : com) : state :=
match c with
| SKIP
st
| x ::= a1
update st x (aeval st a1)
| c1 ;; c2
let st' := ceval_fun_no_while st c1 in
ceval_fun_no_while st' c2
| IFB b THEN c1 ELSE c2 FI
if (beval st b)
then ceval_fun_no_while st c1
else ceval_fun_no_while st c2
| WHILE b DO c END
st (* bogus *)
end.

In a traditional functional programming language like ML or Haskell we could write the WHILE case as follows:
Fixpoint ceval_fun (st : state) (c : com) : state :=
match c with
...
| WHILE b DO c END =>
if (beval st b1)
then ceval_fun st (c1; WHILE b DO c END)
else st
end.
Coq doesn't accept such a definition ("Error: Cannot guess decreasing argument of fix") because the function we want to define is not guaranteed to terminate. Indeed, it doesn't always terminate: for example, the full version of the ceval_fun function applied to the loop program above would never terminate. Since Coq is not just a functional programming language, but also a consistent logic, any potentially non-terminating function needs to be rejected. Here is an (invalid!) Coq program showing what would go wrong if Coq allowed non-terminating recursive functions:
Fixpoint loop_false (n : nat) : False := loop_false n.
That is, propositions like False would become provable (e.g. loop_false 0 would be a proof of False), which would be a disaster for Coq's logical consistency.
Thus, because it doesn't terminate on all inputs, the full version of ceval_fun cannot be written in Coq — at least not without additional tricks (see chapter ImpCEvalFun if curious).

## Evaluation as a Relation

Here's a better way: we define ceval as a relation rather than a function — i.e., we define it in Prop instead of Type, as we did for aevalR above.
This is an important change. Besides freeing us from the awkward workarounds that would be needed to define evaluation as a function, it gives us a lot more flexibility in the definition. For example, if we added concurrency features to the language, we'd want the definition of evaluation to be non-deterministic — i.e., not only would it not be total, it would not even be a partial function!
We'll use the notation c / st st' for our ceval relation: c / st st' means that executing program c in a starting state st results in an ending state st'. This can be pronounced "c takes state st to st'".
 (E_Skip) SKIP / st ⇓ st
 aeval st a1 = n (E_Ass) x := a1 / st ⇓ (update st x n)
 c1 / st ⇓ st' c2 / st' ⇓ st'' (E_Seq) c1;;c2 / st ⇓ st''
 beval st b1 = true c1 / st ⇓ st' (E_IfTrue) IF b1 THEN c1 ELSE c2 FI / st ⇓ st'
 beval st b1 = false c2 / st ⇓ st' (E_IfFalse) IF b1 THEN c1 ELSE c2 FI / st ⇓ st'
 beval st b1 = false (E_WhileEnd) WHILE b DO c END / st ⇓ st
 beval st b1 = true c / st ⇓ st' WHILE b DO c END / st' ⇓ st'' (E_WhileLoop) WHILE b DO c END / st ⇓ st''

Reserved Notation "c1 '/' st '' st'" (at level 40, st at level 39).

Inductive ceval : com state state Prop :=
| E_Skip : st,
SKIP / st st
| E_Ass : st a1 n x,
aeval st a1 = n
(x ::= a1) / st (update st x n)
| E_Seq : c1 c2 st st' st'',
c1 / st st'
c2 / st' st''
(c1 ;; c2) / st st''
| E_IfTrue : st st' b c1 c2,
beval st b = true
c1 / st st'
(IFB b THEN c1 ELSE c2 FI) / st st'
| E_IfFalse : st st' b c1 c2,
beval st b = false
c2 / st st'
(IFB b THEN c1 ELSE c2 FI) / st st'
| E_WhileEnd : b st c,
beval st b = false
(WHILE b DO c END) / st st
| E_WhileLoop : st st' st'' b c,
beval st b = true
c / st st'
(WHILE b DO c END) / st' st''
(WHILE b DO c END) / st st''

where "c1 '/' st '' st'" := (ceval c1 st st').

Tactic Notation "ceval_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "E_Skip" | Case_aux c "E_Ass" | Case_aux c "E_Seq"
| Case_aux c "E_IfTrue" | Case_aux c "E_IfFalse"
| Case_aux c "E_WhileEnd" | Case_aux c "E_WhileLoop" ].

The cost of defining evaluation as a relation instead of a function is that we now need to construct proofs that some program evaluates to some result state, rather than just letting Coq's computation mechanism do it for us.

Example ceval_example1:
(X ::= ANum 2;;
IFB BLe (AId X) (ANum 1)
THEN Y ::= ANum 3
ELSE Z ::= ANum 4
FI)
/ empty_state
(update (update empty_state X 2) Z 4).
Proof.
(* We must supply the intermediate state *)
apply E_Seq with (update empty_state X 2).
Case "assignment command".
apply E_Ass. reflexivity.
Case "if command".
apply E_IfFalse.
reflexivity.
apply E_Ass. reflexivity. Qed.

Is the following proposition provable?
c st st',
(SKIP ; c) / st  st'
c / st  st'
(1) Yes
(2) No
(3) Not sure
Is the following proposition provable?
c1 c2 st st',
(c1;c2) / st  st'
c1 / st  st
c2 / st  st'
(1) Yes
(2) No
(3) Not sure
Is the following proposition provable?
b c st st',
(IFB b THEN c ELSE c FI) / st  st'
c / st  st'
(1) Yes
(2) No
(3) Not sure
Is the following proposition provable?
b,
(stbeval st b = true
c st,
~(st', (WHILE b DO c END) / st  st')
(1) Yes
(2) No
(3) Not sure
Is the following proposition provable?
b c st,
~(st', (WHILE b DO c END) / st  st'
st''beval st'' b = true
(1) Yes
(2) No
(3) Not sure

## Determinism of Evaluation

Theorem ceval_deterministic: c st st1 st2,
c / st st1
c / st st2
st1 = st2.
Proof.
intros c st st1 st2 E1 E2.
generalize dependent st2.
ceval_cases (induction E1) Case;
intros st2 E2; inversion E2; subst.
Case "E_Skip". reflexivity.
Case "E_Ass". reflexivity.
Case "E_Seq".
assert (st' = st'0) as EQ1.
SCase "Proof of assertion". apply IHE1_1; assumption.
subst st'0.
apply IHE1_2. assumption.
Case "E_IfTrue".
SCase "b1 evaluates to true".
apply IHE1. assumption.
SCase "b1 evaluates to false (contradiction)".
rewrite H in H5. inversion H5.
Case "E_IfFalse".
SCase "b1 evaluates to true (contradiction)".
rewrite H in H5. inversion H5.
SCase "b1 evaluates to false".
apply IHE1. assumption.
Case "E_WhileEnd".
SCase "b1 evaluates to false".
reflexivity.
SCase "b1 evaluates to true (contradiction)".
rewrite H in H2. inversion H2.
Case "E_WhileLoop".
SCase "b1 evaluates to false (contradiction)".
rewrite H in H4. inversion H4.
SCase "b1 evaluates to true".
assert (st' = st'0) as EQ1.
SSCase "Proof of assertion". apply IHE1_1; assumption.
subst st'0.
apply IHE1_2. assumption. Qed.

We'll get much deeper into systematic techniques for reasoning about Imp programs in the following chapters, but we can do quite a bit just working with the bare definitions.

Theorem plus2_spec : st n st',
st X = n
plus2 / st st'
st' X = n + 2.
Proof.
intros st n st' HX Heval.
(* Inverting Heval essentially forces Coq to expand one
step of the ceval computation - in this case revealing
that st' must be st extended with the new value of X,
since plus2 is an assignment *)

inversion Heval. subst. clear Heval. simpl.
apply update_eq. Qed.

#### Exercise: 3 stars (stack_compiler)

HP Calculators, programming languages like Forth and Postscript, and abstract machines like the Java Virtual Machine all evaluate arithmetic expressions using a stack. For instance, the expression
(2*3)+(3*(4-2))
would be entered as
2 3 * 3 4 2 - * +
and evaluated like this:
[]            |    2 3 * 3 4 2 - * +
[2]           |    3 * 3 4 2 - * +
[3, 2]        |    * 3 4 2 - * +
[6]           |    3 4 2 - * +
[3, 6]        |    4 2 - * +
[4, 3, 6]     |    2 - * +
[2, 4, 3, 6]  |    - * +
[2, 3, 6]     |    * +
[6, 6]        |    +
[12]          |
The task of this exercise is to write a small compiler that translates aexps into stack machine instructions.
The instruction set for our stack language will consist of the following instructions:
• SPush n: Push the number n on the stack.
• SLoad x: Load the identifier x from the store and push it on the stack
• SPlus: Pop the two top numbers from the stack, add them, and push the result onto the stack.
• SMinus: Similar, but subtract.
• SMult: Similar, but multiply.

Inductive sinstr : Type :=
| SPush : nat sinstr
| SPlus : sinstr
| SMinus : sinstr
| SMult : sinstr.

Write a function to evaluate programs in the stack language. It takes as input a state, a stack represented as a list of numbers (top stack item is the head of the list), and a program represented as a list of instructions, and returns the stack after executing the program. Test your function on the examples below.
Note that the specification leaves unspecified what to do when encountering an SPlus, SMinus, or SMult instruction if the stack contains less than two elements. In a sense, it is immaterial what we do, since our compiler will never emit such a malformed program.

Fixpoint s_execute (st : state) (stack : list nat)
(prog : list sinstr)
: list nat :=
(* FILL IN HERE *) admit.

Example s_execute1 :
s_execute empty_state []
[SPush 5; SPush 3; SPush 1; SMinus]
= [2; 5].
(* FILL IN HERE *) Admitted.

Example s_execute2 :
s_execute (update empty_state X 3) [3;4]
[SPush 4; SLoad X; SMult; SPlus]
= [15; 4].
(* FILL IN HERE *) Admitted.

Next, write a function which compiles an aexp into a stack machine program. The effect of running the program should be the same as pushing the value of the expression on the stack.

Fixpoint s_compile (e : aexp) : list sinstr :=
(* FILL IN HERE *) admit.

After you've defined s_compile, uncomment the following to test that it works.

(*
Example s_compile1 :
s_compile (AMinus (AId X) (AMult (ANum 2) (AId Y)))