StackMachine.v 42.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
(* Copyright (c) 2008, Adam Chlipala
 * 
 * This work is licensed under a
 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
 * Unported License.
 * The license text is available at:
 *   http://creativecommons.org/licenses/by-nc-nd/3.0/
 *)

Adam Chlipala's avatar
Adam Chlipala committed
10
(* begin hide *)
11 12 13
Require Import List.

Require Import Tactics.
14 15

Set Implicit Arguments.
Adam Chlipala's avatar
Adam Chlipala committed
16
(* end hide *)
17 18


19 20 21
(** %\chapter{Some Quick Examples}% *)


Adam Chlipala's avatar
Adam Chlipala committed
22
(** I will start off by jumping right in to a fully-worked set of examples, building certified compilers from increasingly complicated source languages to stack machines.  We will meet a few useful tactics and see how they can be used in manual proofs, and we will also see how easily these proofs can be automated instead.  I assume that you have installed Coq and Proof General.  The code in this book is tested with Coq version 8.1pl3, though parts may work with other versions.
23

24 25 26 27 28
As always, you can step through the source file %\texttt{%#<tt>#StackMachine.v#</tt>#%}% for this chapter interactively in Proof General.  Alternatively, to get a feel for the whole lifecycle of creating a Coq development, you can enter the pieces of source code in this chapter in a new %\texttt{%#<tt>#.v#</tt>#%}% file in an Emacs buffer.  If you do the latter, include a line [Require Import List Tactics.] at the start of the file, to match some code hidden in this rendering of the chapter source, and be sure to run the Coq binary %\texttt{%#<tt>#coqtop#</tt>#%}% with the command-line argument %\texttt{%#<tt>#-I SRC#</tt>#%}%, where %\texttt{%#<tt>#SRC#</tt>#%}% is the path to a directory containing the source for this book.  In either case, you will need to run %\texttt{%#<tt>#make#</tt>#%}% in the root directory of the source distribution for the book before getting started.  If you have installed Proof General properly, it should start automatically when you visit a %\texttt{%#<tt>#.v#</tt>#%}% buffer in Emacs.

There are some minor headaches associated with getting Proof General to pass the proper command line arguments to the %\texttt{%#<tt>#coqtop#</tt>#%}% program.  The best way to add settings that will be shared by many source files is to add a custom variable setting to your %\texttt{%#<tt>#.emacs#</tt>#%}% file, like this:
%\begin{verbatim}%#<pre>#(custom-set-variables
  ...
29
  '(coq-prog-args '("-I" "/path/to/cpdt/src"))
30 31 32
  ...
)#</pre>#%\end{verbatim}%
The extra arguments demonstrated here are the proper choices for working with the code for this book.  The ellipses stand for other Emacs customization settings you may already have.  It can be helpful to save several alternate sets of flags in your %\texttt{%#<tt>#.emacs#</tt>#%}% file, with all but one commented out within the %\texttt{%#<tt>#custom-set-variables#</tt>#%}% block at any given time.
Adam Chlipala's avatar
Adam Chlipala committed
33 34

With Proof General, the portion of a buffer that Coq has processed is highlighted in some way, like being given a blue background.  You step through Coq source files by positioning the point at the position you want Coq to run to and pressing C-C C-RET.  This can be used both for normal step-by-step coding, by placing the point inside some command past the end of the highlighted region; and for undoing, by placing the point inside the highlighted region. *)
35 36


Adam Chlipala's avatar
Adam Chlipala committed
37
(** * Arithmetic Expressions Over Natural Numbers *)
38

Adam Chlipala's avatar
Adam Chlipala committed
39
(** We will begin with that staple of compiler textbooks, arithmetic expressions over a single type of numbers. *)
40

Adam Chlipala's avatar
Adam Chlipala committed
41
(** ** Source Language *)
42 43

(** We begin with the syntax of the source language. *)
44

Adam Chlipala's avatar
Adam Chlipala committed
45
Inductive binop : Set := Plus | Times.
46

47 48
(** Our first line of Coq code should be unsurprising to ML and Haskell programmers.  We define an algebraic datatype [binop] to stand for the binary operators of our source language.  There are just two wrinkles compared to ML and Haskell.  First, we use the keyword [Inductive], in place of %\texttt{%#<tt>#data#</tt>#%}%, %\texttt{%#<tt>#datatype#</tt>#%}%, or %\texttt{%#<tt>#type#</tt>#%}%.  This is not just a trivial surface syntax difference; inductive types in Coq are much more expressive than garden variety algebraic datatypes, essentially enabling us to encode all of mathematics, though we begin humbly in this chapter.  Second, there is the [: Set] fragment, which declares that we are defining a datatype that should be thought of as a constituent of programs.  Later, we will see other options for defining datatypes in the universe of proofs or in an infinite hierarchy of universes, encompassing both programs and proofs, that is useful in higher-order constructions. *)

Adam Chlipala's avatar
Adam Chlipala committed
49 50 51
Inductive exp : Set :=
| Const : nat -> exp
| Binop : binop -> exp -> exp -> exp.
52

53 54 55 56 57 58 59 60
(** Now we define the type of arithmetic expressions.  We write that a constant may be built from one argument, a natural number; and a binary operation may be built from a choice of operator and two operand expressions.

A note for readers following along in the PDF version: coqdoc supports pretty-printing of tokens in LaTeX or HTML.  Where you see a right arrow character, the source contains the ASCII text %\texttt{%#<tt>#->#</tt>#%}%.  Other examples of this substitution appearing in this chapter are a double right arrow for %\texttt{%#<tt>#=>#</tt>#%}% and the inverted 'A' symbol for %\texttt{%#<tt>#forall#</tt>#%}%.  When in doubt about the ASCII version of a symbol, you can consult the chapter source code.

%\medskip%

Now we are ready to say what these programs mean.  We will do this by writing an interpreter that can be thought of as a trivial operational or denotational semantics.  (If you are not familiar with these semantic techniques, no need to worry; we will stick to "common sense" constructions.) *)

Adam Chlipala's avatar
Adam Chlipala committed
61 62 63 64 65
Definition binopDenote (b : binop) : nat -> nat -> nat :=
  match b with
    | Plus => plus
    | Times => mult
  end.
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
(** The meaning of a binary operator is a binary function over naturals, defined with pattern-matching notation analogous to the %\texttt{%#<tt>#case#</tt>#%}% and %\texttt{%#<tt>#match#</tt>#%}% of ML and Haskell, and referring to the functions [plus] and [mult] from the Coq standard library.  The keyword [Definition] is Coq's all-purpose notation for binding a term of the programming language to a name, with some associated syntactic sugar, like the notation we see here for defining a function.  That sugar could be expanded to yield this definition:

[[
Definition binopDenote : binop -> nat -> nat -> nat := fun (b : binop) =>
  match b with
    | Plus => plus
    | Times => mult
  end.

In this example, we could also omit all of the type annotations, arriving at:

[[
Definition binopDenote := fun b =>
  match b with
    | Plus => plus
    | Times => mult
  end.

Languages like Haskell and ML have a convenient %\textit{%#<i>#principal typing#</i>#%}% property, which gives us strong guarantees about how effective type inference will be.  Unfortunately, Coq's type system is so expressive that any kind of "complete" type inference is impossible, and the task even seems to be hard heuristically in practice.  Nonetheless, Coq includes some very helpful heuristics, many of them copying the workings of Haskell and ML type-checkers for programs that fall in simple fragments of Coq's language.

This is as good a time as any to mention the preponderance of different languages associated with Coq.  The theoretical foundation of Coq is a formal system called the %\textit{%#<i>#Calculus of Inductive Constructions (CIC)#</i>#%}%, which is an extension of the older %\textit{%#<i>#Calculus of Constructions (CoC)#</i>#%}%.  CIC is quite a spartan foundation, which is helpful for proving metatheory but not so helpful for real development.  Still, it is nice to know that it has been proved that CIC enjoys properties like %\textit{%#<i>#strong normalization#</i>#%}%, meaning that every program (and, more importantly, every proof term) terminates; and %\textit{%#<i>#relative consistency#</i>#%}% with systems like versions of Zermelo Fraenkel set theory, which roughly means that you can believe that Coq proofs mean that the corresponding propositions are "really true," if you believe in set theory.

Adam Chlipala's avatar
Adam Chlipala committed
89
Coq is actually based on an extension of CIC called %\textit{%#<i>#Gallina#</i>#%}%.  The text after the [:=] and before the period in the last code example is a term of Gallina.  Gallina adds many useful features that are not compiled internally to more primitive CIC features.  The important metatheorems about CIC have not been extended to the full breadth of these features, but most Coq users do not seem to lose much sleep over this omission.
90 91 92 93 94 95 96 97 98

Commands like [Inductive] and [Definition] are part of %\textit{%#<i>#the vernacular#</i>#%}%, which includes all sorts of useful queries and requests to the Coq system.

Finally, there is %\textit{%#<i>#Ltac#</i>#%}%, Coq's domain-specific language for writing proofs and decision procedures. We will see some basic examples of Ltac later in this chapter, and much of this book is devoted to more involved Ltac examples.

%\medskip%

We can give a simple definition of the meaning of an expression: *)

Adam Chlipala's avatar
Adam Chlipala committed
99 100 101 102 103
Fixpoint expDenote (e : exp) : nat :=
  match e with
    | Const n => n
    | Binop b e1 e2 => (binopDenote b) (expDenote e1) (expDenote e2)
  end.
104

105 106
(** We declare explicitly that this is a recursive definition, using the keyword [Fixpoint].  The rest should be old hat for functional programmers. *)

Adam Chlipala's avatar
Adam Chlipala committed
107 108 109
(** It is convenient to be able to test definitions before starting to prove things about them.  We can verify that our semantics is sensible by evaluating some sample uses. *)

Eval simpl in expDenote (Const 42).
Adam Chlipala's avatar
Adam Chlipala committed
110 111 112
(** [[
= 42 : nat
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
113
Eval simpl in expDenote (Binop Plus (Const 2) (Const 2)).
Adam Chlipala's avatar
Adam Chlipala committed
114 115 116
(** [[
= 4 : nat
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
117
Eval simpl in expDenote (Binop Times (Binop Plus (Const 2) (Const 2)) (Const 7)).
Adam Chlipala's avatar
Adam Chlipala committed
118 119 120
(** [[
= 28 : nat
]] *)
121

Adam Chlipala's avatar
Adam Chlipala committed
122
(** ** Target Language *)
123

124 125
(** We will compile our source programs onto a simple stack machine, whose syntax is: *)

Adam Chlipala's avatar
Adam Chlipala committed
126 127 128 129 130 131 132
Inductive instr : Set :=
| IConst : nat -> instr
| IBinop : binop -> instr.

Definition prog := list instr.
Definition stack := list nat.

133 134 135 136
(** An instruction either pushes a constant onto the stack or pops two arguments, applies a binary operator to them, and pushes the result onto the stack.  A program is a list of instructions, and a stack is a list of natural numbers.

We can give instructions meanings as functions from stacks to optional stacks, where running an instruction results in [None] in case of a stack underflow and results in [Some s'] when the result of execution is the new stack [s'].  [::] is the "list cons" operator from the Coq standard library. *)

Adam Chlipala's avatar
Adam Chlipala committed
137 138 139 140 141 142 143 144 145 146
Definition instrDenote (i : instr) (s : stack) : option stack :=
  match i with
    | IConst n => Some (n :: s)
    | IBinop b =>
      match s with
        | arg1 :: arg2 :: s' => Some ((binopDenote b) arg1 arg2 :: s')
        | _ => None
      end
  end.

147 148
(** With [instrDenote] defined, it is easy to define a function [progDenote], which iterates application of [instrDenote] through a whole program. *)

Adam Chlipala's avatar
Adam Chlipala committed
149 150 151 152 153 154 155 156 157
Fixpoint progDenote (p : prog) (s : stack) {struct p} : option stack :=
  match p with
    | nil => Some s
    | i :: p' =>
      match instrDenote i s with
        | None => None
        | Some s' => progDenote p' s'
      end
  end.
158

159 160
(** There is one interesting difference compared to our previous example of a [Fixpoint].  This recursive function takes two arguments, [p] and [s].  It is critical for the soundness of Coq that every program terminate, so a shallow syntactic termination check is imposed on every recursive function definition.  One of the function parameters must be designated to decrease monotonically across recursive calls.  That is, every recursive call must use a version of that argument that has been pulled out of the current argument by some number of [match] expressions.  [expDenote] has only one argument, so we did not need to specify which of its arguments decreases.  For [progDenote], we resolve the ambiguity by writing [{struct p}] to indicate that argument [p] decreases structurally. *)

161

162
(** ** Translation *)
163

164 165
(** Our compiler itself is now unsurprising.  [++] is the list concatenation operator from the Coq standard library. *)

Adam Chlipala's avatar
Adam Chlipala committed
166 167 168 169 170
Fixpoint compile (e : exp) : prog :=
  match e with
    | Const n => IConst n :: nil
    | Binop b e1 e2 => compile e2 ++ compile e1 ++ IBinop b :: nil
  end.
171 172


Adam Chlipala's avatar
Adam Chlipala committed
173 174 175
(** Before we set about proving that this compiler is correct, we can try a few test runs, using our sample programs from earlier. *)

Eval simpl in compile (Const 42).
Adam Chlipala's avatar
Adam Chlipala committed
176 177 178
(** [[
= IConst 42 :: nil : prog
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
179
Eval simpl in compile (Binop Plus (Const 2) (Const 2)).
Adam Chlipala's avatar
Adam Chlipala committed
180 181 182
(** [[
= IConst 2 :: IConst 2 :: IBinop Plus :: nil : prog
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
183
Eval simpl in compile (Binop Times (Binop Plus (Const 2) (Const 2)) (Const 7)).
Adam Chlipala's avatar
Adam Chlipala committed
184 185 186
(** [[
= IConst 7 :: IConst 2 :: IConst 2 :: IBinop Plus :: IBinop Times :: nil : prog
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
187

Adam Chlipala's avatar
Adam Chlipala committed
188
(** We can also run our compiled programs and check that they give the right results. *)
Adam Chlipala's avatar
Adam Chlipala committed
189 190

Eval simpl in progDenote (compile (Const 42)) nil.
Adam Chlipala's avatar
Adam Chlipala committed
191 192 193
(** [[
= Some (42 :: nil) : option stack
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
194
Eval simpl in progDenote (compile (Binop Plus (Const 2) (Const 2))) nil.
Adam Chlipala's avatar
Adam Chlipala committed
195 196 197
(** [[
= Some (4 :: nil) : option stack
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
198
Eval simpl in progDenote (compile (Binop Times (Binop Plus (Const 2) (Const 2)) (Const 7))) nil.
Adam Chlipala's avatar
Adam Chlipala committed
199 200 201
(** [[
= Some (28 :: nil) : option stack
]] *)
Adam Chlipala's avatar
Adam Chlipala committed
202 203


Adam Chlipala's avatar
Adam Chlipala committed
204
(** ** Translation Correctness *)
205

Adam Chlipala's avatar
Adam Chlipala committed
206 207
(** We are ready to prove that our compiler is implemented correctly.  We can use a new vernacular command [Theorem] to start a correctness proof, in terms of the semantics we defined earlier: *)

Adam Chlipala's avatar
Adam Chlipala committed
208
Theorem compile_correct : forall e, progDenote (compile e) nil = Some (expDenote e :: nil).
Adam Chlipala's avatar
Adam Chlipala committed
209 210 211
(* begin hide *)
Abort.
(* end hide *)
Adam Chlipala's avatar
Adam Chlipala committed
212
(* begin thide *)
Adam Chlipala's avatar
Adam Chlipala committed
213 214 215 216

(** Though a pencil-and-paper proof might clock out at this point, writing "by a routine induction on [e]," it turns out not to make sense to attack this proof directly.  We need to use the standard trick of %\textit{%#<i>#strengthening the induction hypothesis#</i>#%}%.  We do that by proving an auxiliary lemma:
*)

Adam Chlipala's avatar
Adam Chlipala committed
217
Lemma compile_correct' : forall e p s, progDenote (compile e ++ p) s = progDenote p (expDenote e :: s).
Adam Chlipala's avatar
Adam Chlipala committed
218 219 220 221 222 223 224

(** After the period in the [Lemma] command, we are in %\textit{%#<i>#the interactive proof-editing mode#</i>#%}%.  We find ourselves staring at this ominous screen of text:

[[
1 subgoal
  
 ============================
225 226
  forall (e : exp) (p : list instr) (s : stack),
   progDenote (compile e ++ p) s = progDenote p (expDenote e :: s)  
Adam Chlipala's avatar
Adam Chlipala committed
227 228 229 230 231 232 233 234 235
]]

Coq seems to be restating the lemma for us.  What we are seeing is a limited case of a more general protocol for describing where we are in a proof.  We are told that we have a single subgoal.  In general, during a proof, we can have many pending subgoals, each of which is a logical proposition to prove.  Subgoals can be proved in any order, but it usually works best to prove them in the order that Coq chooses.

Next in the output, we see our single subgoal described in full detail.  There is a double-dashed line, above which would be our free variables and hypotheses, if we had any.  Below the line is the conclusion, which, in general, is to be proved from the hypotheses.

We manipulate the proof state by running commands called %\textit{%#<i>#tactics#</i>#%}%.  Let us start out by running one of the most important tactics:
*)

Adam Chlipala's avatar
Adam Chlipala committed
236 237
  induction e.

Adam Chlipala's avatar
Adam Chlipala committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
(** We declare that this proof will proceed by induction on the structure of the expression [e].  This swaps out our initial subgoal for two new subgoals, one for each case of the inductive proof:

[[
2 subgoals
  
 n : nat
 ============================
 forall (s : stack) (p : list instr),
   progDenote (compile (Const n) ++ p) s =
   progDenote p (expDenote (Const n) :: s)
]]
[[
 subgoal 2 is:
  forall (s : stack) (p : list instr),
    progDenote (compile (Binop b e1 e2) ++ p) s =
    progDenote p (expDenote (Binop b e1 e2) :: s)
]]

The first and current subgoal is displayed with the double-dashed line below free variables and hypotheses, while later subgoals are only summarized with their conclusions.  We see an example of a free variable in the first subgoal; [n] is a free variable of type [nat].  The conclusion is the original theorem statement where [e] has been replaced by [Const n].  In a similar manner, the second case has [e] replaced by a generalized invocation of the [Binop] expression constructor.  We can see that proving both cases corresponds to a standard proof by structural induction.

We begin the first case with another very common tactic.
*)

Adam Chlipala's avatar
Adam Chlipala committed
261
  intros.
Adam Chlipala's avatar
Adam Chlipala committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

(** The current subgoal changes to:
[[

 n : nat
 s : stack
 p : list instr
 ============================
 progDenote (compile (Const n) ++ p) s =
 progDenote p (expDenote (Const n) :: s)
]]

We see that [intros] changes [forall]-bound variables at the beginning of a goal into free variables.

To progress further, we need to use the definitions of some of the functions appearing in the goal.  The [unfold] tactic replaces an identifier with its definition.
*)

Adam Chlipala's avatar
Adam Chlipala committed
279
  unfold compile.
Adam Chlipala's avatar
Adam Chlipala committed
280 281 282 283 284 285 286 287 288 289 290

(** [[

 n : nat
 s : stack
 p : list instr
 ============================
 progDenote ((IConst n :: nil) ++ p) s =
 progDenote p (expDenote (Const n) :: s)
]] *)

Adam Chlipala's avatar
Adam Chlipala committed
291
  unfold expDenote.
Adam Chlipala's avatar
Adam Chlipala committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

(** [[

 n : nat
 s : stack
 p : list instr
 ============================
 progDenote ((IConst n :: nil) ++ p) s = progDenote p (n :: s)
]]

We only need to unfold the first occurrence of [progDenote] to prove the goal: *)

  unfold progDenote at 1.

(** [[

 n : nat
 s : stack
 p : list instr
 ============================
  (fix progDenote (p0 : prog) (s0 : stack) {struct p0} : 
    option stack :=
      match p0 with
      | nil => Some s0
      | i :: p' =>
          match instrDenote i s0 with
          | Some s' => progDenote p' s'
          | None => None (A:=stack)
          end
      end) ((IConst n :: nil) ++ p) s =
   progDenote p (n :: s)
]]

This last [unfold] has left us with an anonymous fixpoint version of [progDenote], which will generally happen when unfolding recursive definitions.  Fortunately, in this case, we can eliminate such complications right away, since the structure of the argument [(IConst n :: nil) ++ p] is known, allowing us to simplify the internal pattern match with the [simpl] tactic:
*)

Adam Chlipala's avatar
Adam Chlipala committed
328
  simpl.
Adam Chlipala's avatar
Adam Chlipala committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

(** [[

 n : nat
 s : stack
 p : list instr
 ============================
 (fix progDenote (p0 : prog) (s0 : stack) {struct p0} : 
  option stack :=
    match p0 with
    | nil => Some s0
    | i :: p' =>
        match instrDenote i s0 with
        | Some s' => progDenote p' s'
        | None => None (A:=stack)
        end
    end) p (n :: s) = progDenote p (n :: s)
]]

Now we can unexpand the definition of [progDenote]:
*)

  fold progDenote.

(** [[

 n : nat
 s : stack
 p : list instr
 ============================
 progDenote p (n :: s) = progDenote p (n :: s)
]]

It looks like we are at the end of this case, since we have a trivial equality.  Indeed, a single tactic finishes us off:
*)

Adam Chlipala's avatar
Adam Chlipala committed
365 366
  reflexivity.

Adam Chlipala's avatar
Adam Chlipala committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
(** On to the second inductive case:

[[

  b : binop
  e1 : exp
  IHe1 : forall (s : stack) (p : list instr),
         progDenote (compile e1 ++ p) s = progDenote p (expDenote e1 :: s)
  e2 : exp
  IHe2 : forall (s : stack) (p : list instr),
         progDenote (compile e2 ++ p) s = progDenote p (expDenote e2 :: s)
  ============================
   forall (s : stack) (p : list instr),
   progDenote (compile (Binop b e1 e2) ++ p) s =
   progDenote p (expDenote (Binop b e1 e2) :: s)
]]

We see our first example of hypotheses above the double-dashed line.  They are the inductive hypotheses [IHe1] and [IHe2] corresponding to the subterms [e1] and [e2], respectively.

We start out the same way as before, introducing new free variables and unfolding and folding the appropriate definitions.  The seemingly frivolous [unfold]/[fold] pairs are actually accomplishing useful work, because [unfold] will sometimes perform easy simplifications. *)

Adam Chlipala's avatar
Adam Chlipala committed
388 389 390 391 392
  intros.
  unfold compile.
  fold compile.
  unfold expDenote.
  fold expDenote.
Adam Chlipala's avatar
Adam Chlipala committed
393

Adam Chlipala's avatar
Adam Chlipala committed
394
(** Now we arrive at a point where the tactics we have seen so far are insufficient.  No further definition unfoldings get us anywhere, so we will need to try something different.
Adam Chlipala's avatar
Adam Chlipala committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

[[

  b : binop
  e1 : exp
  IHe1 : forall (s : stack) (p : list instr),
         progDenote (compile e1 ++ p) s = progDenote p (expDenote e1 :: s)
  e2 : exp
  IHe2 : forall (s : stack) (p : list instr),
         progDenote (compile e2 ++ p) s = progDenote p (expDenote e2 :: s)
  s : stack
  p : list instr
  ============================
   progDenote ((compile e2 ++ compile e1 ++ IBinop b :: nil) ++ p) s =
   progDenote p (binopDenote b (expDenote e1) (expDenote e2) :: s)
]]

What we need is the associative law of list concatenation, available as a theorem [app_ass] in the standard library. *)

Check app_ass.

(** [[

app_ass
     : forall (A : Type) (l m n : list A), (l ++ m) ++ n = l ++ m ++ n
]]

We use it to perform a rewrite: *)

Adam Chlipala's avatar
Adam Chlipala committed
424
  rewrite app_ass.
Adam Chlipala's avatar
Adam Chlipala committed
425 426 427 428 429 430 431 432 433

(** changing the conclusion to: [[

   progDenote (compile e2 ++ (compile e1 ++ IBinop b :: nil) ++ p) s =
   progDenote p (binopDenote b (expDenote e1) (expDenote e2) :: s)
]]

Now we can notice that the lefthand side of the equality matches the lefthand side of the second inductive hypothesis, so we can rewrite with that hypothesis, too: *)

Adam Chlipala's avatar
Adam Chlipala committed
434
  rewrite IHe2.
Adam Chlipala's avatar
Adam Chlipala committed
435 436 437 438 439 440 441 442 443

(** [[

   progDenote ((compile e1 ++ IBinop b :: nil) ++ p) (expDenote e2 :: s) =
   progDenote p (binopDenote b (expDenote e1) (expDenote e2) :: s)
]]

The same process lets us apply the remaining hypothesis. *)

Adam Chlipala's avatar
Adam Chlipala committed
444 445
  rewrite app_ass.
  rewrite IHe1.
Adam Chlipala's avatar
Adam Chlipala committed
446 447 448 449 450 451 452 453 454 455 456

(** [[

   progDenote ((IBinop b :: nil) ++ p) (expDenote e1 :: expDenote e2 :: s) =
   progDenote p (binopDenote b (expDenote e1) (expDenote e2) :: s)
]]

Now we can apply a similar sequence of tactics to that that ended the proof of the first case.
*)

  unfold progDenote at 1.
Adam Chlipala's avatar
Adam Chlipala committed
457
  simpl.
Adam Chlipala's avatar
Adam Chlipala committed
458
  fold progDenote.
Adam Chlipala's avatar
Adam Chlipala committed
459
  reflexivity.
Adam Chlipala's avatar
Adam Chlipala committed
460 461 462 463 464 465 466 467 468

(** And the proof is completed, as indicated by the message:

[[
Proof completed.

And there lies our first proof.  Already, even for simple theorems like this, the final proof script is unstructured and not very enlightening to readers.  If we extend this approach to more serious theorems, we arrive at the unreadable proof scripts that are the favorite complaints of opponents of tactic-based proving.  Fortunately, Coq has rich support for scripted automation, and we can take advantage of such a scripted tactic (defined elsewhere) to make short work of this lemma.  We abort the old proof attempt and start again.
*)

Adam Chlipala's avatar
Adam Chlipala committed
469 470
Abort.

Adam Chlipala's avatar
Adam Chlipala committed
471
Lemma compile_correct' : forall e s p, progDenote (compile e ++ p) s =
Adam Chlipala's avatar
Adam Chlipala committed
472 473 474 475
  progDenote p (expDenote e :: s).
  induction e; crush.
Qed.

Adam Chlipala's avatar
Adam Chlipala committed
476 477 478 479
(** We need only to state the basic inductive proof scheme and call a tactic that automates the tedious reasoning in between.  In contrast to the period tactic terminator from our last proof, the semicolon tactic separator supports structured, compositional proofs.  The tactic [t1; t2] has the effect of running [t1] and then running [t2] on each remaining subgoal.  The semicolon is one of the most fundamental building blocks of effective proof automation.  The period terminator is very useful for exploratory proving, where you need to see intermediate proof states, but final proofs of any serious complexity should have just one period, terminating a single compound tactic that probably uses semicolons.

The proof of our main theorem is now easy.  We prove it with four period-terminated tactics, though separating them with semicolons would work as well; the version here is easier to step through. *)

Adam Chlipala's avatar
Adam Chlipala committed
480
Theorem compile_correct : forall e, progDenote (compile e) nil = Some (expDenote e :: nil).
Adam Chlipala's avatar
Adam Chlipala committed
481 482 483 484 485 486 487 488 489
  intros.

(** [[

  e : exp
  ============================
   progDenote (compile e) nil = Some (expDenote e :: nil)
]]

Adam Chlipala's avatar
Adam Chlipala committed
490
At this point, we want to massage the lefthand side to match the statement of [compile_correct'].  A theorem from the standard library is useful: *)
Adam Chlipala's avatar
Adam Chlipala committed
491 492 493 494 495 496 497 498 499

Check app_nil_end.

(** [[

app_nil_end
     : forall (A : Type) (l : list A), l = l ++ nil
]] *)

Adam Chlipala's avatar
Adam Chlipala committed
500
  rewrite (app_nil_end (compile e)).
Adam Chlipala's avatar
Adam Chlipala committed
501 502 503 504 505 506 507 508 509 510 511 512

(** This time, we explicitly specify the value of the variable [l] from the theorem statement, since multiple expressions of list type appear in the conclusion.  [rewrite] might choose the wrong place to rewrite if we did not specify which we want.

[[

  e : exp
  ============================
   progDenote (compile e ++ nil) nil = Some (expDenote e :: nil)
]]

Now we can apply the lemma. *)

Adam Chlipala's avatar
Adam Chlipala committed
513
  rewrite compile_correct'.
Adam Chlipala's avatar
Adam Chlipala committed
514 515 516 517 518 519 520 521 522 523

(** [[

  e : exp
  ============================
   progDenote nil (expDenote e :: nil) = Some (expDenote e :: nil)
]]

We are almost done.  The lefthand and righthand sides can be seen to match by simple symbolic evaluation.  That means we are in luck, because Coq identifies any pair of terms as equal whenever they normalize to the same result by symbolic evaluation.  By the definition of [progDenote], that is the case here, but we do not need to worry about such details.  A simple invocation of [reflexivity] does the normalization and checks that the two results are syntactically equal. *)

Adam Chlipala's avatar
Adam Chlipala committed
524 525
  reflexivity.
Qed.
Adam Chlipala's avatar
Adam Chlipala committed
526
(* end thide *)
527 528


Adam Chlipala's avatar
Adam Chlipala committed
529
(** * Typed Expressions *)
530 531 532

(** In this section, we will build on the initial example by adding additional expression forms that depend on static typing of terms for safety. *)

Adam Chlipala's avatar
Adam Chlipala committed
533
(** ** Source Language *)
534

535 536
(** We define a trivial language of types to classify our expressions: *)

537 538
Inductive type : Set := Nat | Bool.

539 540
(** Now we define an expanded set of binary operators. *)

541 542 543 544 545 546
Inductive tbinop : type -> type -> type -> Set :=
| TPlus : tbinop Nat Nat Nat
| TTimes : tbinop Nat Nat Nat
| TEq : forall t, tbinop t t Bool
| TLt : tbinop Nat Nat Bool.

547 548 549 550 551 552
(** The definition of [tbinop] is different from [binop] in an important way.  Where we declared that [binop] has type [Set], here we declare that [tbinop] has type [type -> type -> type -> Set].  We define [tbinop] as an %\textit{%#<i>#indexed type family#</i>#%}%.  Indexed inductive types are at the heart of Coq's expressive power; almost everything else of interest is defined in terms of them.

ML and Haskell have indexed algebraic datatypes.  For instance, their list types are indexed by the type of data that the list carries.  However, compared to Coq, ML and Haskell 98 place two important restrictions on datatype definitions.

First, the indices of the range of each data constructor must be type variables bound at the top level of the datatype definition.  There is no way to do what we did here, where we, for instance, say that [TPlus] is a constructor building a [tbinop] whose indices are all fixed at [Nat].  %\textit{%#<i>#Generalized algebraic datatypes (GADTs)#</i>#%}% are a popular feature in GHC Haskell and other languages that removes this first restriction.

Adam Chlipala's avatar
Adam Chlipala committed
553
The second restriction is not lifted by GADTs.  In ML and Haskell, indices of types must be types and may not be %\textit{%#<i>#expressions#</i>#%}%.  In Coq, types may be indexed by arbitrary Gallina terms.  Type indices can live in the same universe as programs, and we can compute with them just like regular programs.  Haskell supports a hobbled form of computation in type indices based on multi-parameter type classes, and recent extensions like type functions bring Haskell programming even closer to "real" functional programming with types, but, without dependent typing, there must always be a gap between how one programs with types and how one programs normally.
554 555 556 557
*)

(** We can define a similar type family for typed expressions. *)

558 559 560 561 562
Inductive texp : type -> Set :=
| TNConst : nat -> texp Nat
| TBConst : bool -> texp Bool
| TBinop : forall arg1 arg2 res, tbinop arg1 arg2 res -> texp arg1 -> texp arg2 -> texp res.

563 564
(** Thanks to our use of dependent types, every well-typed [texp] represents a well-typed source expression, by construction.  This turns out to be very convenient for many things we might want to do with expressions.  For instance, it is easy to adapt our interpreter approach to defining semantics.  We start by defining a function mapping the types of our languages into Coq types: *)

565 566 567 568 569 570
Definition typeDenote (t : type) : Set :=
  match t with
    | Nat => nat
    | Bool => bool
  end.

571 572 573 574 575
(** It can take a few moments to come to terms with the fact that [Set], the type of types of programs, is itself a first-class type, and that we can write functions that return [Set]s.  Past that wrinkle, the definition of [typeDenote] is trivial, relying on the [nat] and [bool] types from the Coq standard library.

We need to define a few auxiliary functions, implementing our boolean binary operators that do not appear with the right types in the standard library.  They are entirely standard and ML-like, with the one caveat being that the Coq [nat] type uses a unary representation, where [O] is zero and [S n] is the successor of [n].
*)

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
Definition eq_bool (b1 b2 : bool) : bool :=
  match b1, b2 with
    | true, true => true
    | false, false => true
    | _, _ => false
  end.

Fixpoint eq_nat (n1 n2 : nat) {struct n1} : bool :=
  match n1, n2 with
    | O, O => true
    | S n1', S n2' => eq_nat n1' n2'
    | _, _ => false
  end.

Fixpoint lt (n1 n2 : nat) {struct n1} : bool :=
  match n1, n2 with
    | O, S _ => true
    | S n1', S n2' => lt n1' n2'
    | _, _ => false
  end.

597 598
(** Now we can interpret binary operators: *)

599 600 601 602 603 604 605 606 607 608
Definition tbinopDenote arg1 arg2 res (b : tbinop arg1 arg2 res)
  : typeDenote arg1 -> typeDenote arg2 -> typeDenote res :=
  match b in (tbinop arg1 arg2 res) return (typeDenote arg1 -> typeDenote arg2 -> typeDenote res) with
    | TPlus => plus
    | TTimes => mult
    | TEq Nat => eq_nat
    | TEq Bool => eq_bool
    | TLt => lt
  end.

609 610
(** This function has just a few differences from the denotation functions we saw earlier.  First, [tbinop] is an indexed type, so its indices become additional arguments to [tbinopDenote].  Second, we need to perform a genuine %\textit{%#<i>#dependent pattern match#</i>#%}% to come up with a definition of this function that type-checks.  In each branch of the [match], we need to use branch-specific information about the indices to [tbinop].  General type inference that takes such information into account is undecidable, and Coq avoids pursuing special heuristics for the problem, instead asking users to write annotations, like we see above on the line with [match].

Adam Chlipala's avatar
Adam Chlipala committed
611
The [in] annotation restates the type of the term being case-analyzed.  Though we use the same names for the indices as we use in the type of the original argument binder, these are actually fresh variables, and they are %\textit{%#<i>#binding occurrences#</i>#%}%.  Their scope is the [return] clause.  That is, [arg1], [arg2], and [arg3] are new bound variables bound only within the return clause [typeDenote arg1 -> typeDenote arg2 -> typeDenote res].  By being explicit about the functional relationship between the type indices and the match result, we regain decidable type inference.
612 613 614 615

The same tricks suffice to define an expression denotation function in an unsurprising way:
*)

616 617 618 619 620 621 622
Fixpoint texpDenote t (e : texp t) {struct e} : typeDenote t :=
  match e in (texp t) return (typeDenote t) with
    | TNConst n => n
    | TBConst b => b
    | TBinop _ _ _ b e1 e2 => (tbinopDenote b) (texpDenote e1) (texpDenote e2)
  end.

623 624 625
(** We can evaluate a few example programs to convince ourselves that this semantics is correct. *)

Eval simpl in texpDenote (TNConst 42).
Adam Chlipala's avatar
Adam Chlipala committed
626 627 628
(** [[
= 42 : typeDenote Nat
]] *)
629
Eval simpl in texpDenote (TBConst true).
Adam Chlipala's avatar
Adam Chlipala committed
630 631 632
(** [[
= true : typeDenote Bool
]] *)
633
Eval simpl in texpDenote (TBinop TTimes (TBinop TPlus (TNConst 2) (TNConst 2)) (TNConst 7)).
Adam Chlipala's avatar
Adam Chlipala committed
634 635 636
(** [[
= 28 : typeDenote Nat
]] *)
637
Eval simpl in texpDenote (TBinop (TEq Nat) (TBinop TPlus (TNConst 2) (TNConst 2)) (TNConst 7)).
Adam Chlipala's avatar
Adam Chlipala committed
638 639 640
(** [[
= false : typeDenote Bool
]] *)
641
Eval simpl in texpDenote (TBinop TLt (TBinop TPlus (TNConst 2) (TNConst 2)) (TNConst 7)).
Adam Chlipala's avatar
Adam Chlipala committed
642 643 644
(** [[
= true : typeDenote Bool
]] *)
645

646

Adam Chlipala's avatar
Adam Chlipala committed
647
(** ** Target Language *)
648

Adam Chlipala's avatar
Adam Chlipala committed
649 650 651 652 653 654
(** Now we want to define a suitable stack machine target for compilation.  In the example of the untyped language, stack machine programs could encounter stack underflows and "get stuck."  This was unfortunate, since we had to deal with this complication even though we proved that our compiler never produced underflowing programs.  We could have used dependent types to force all stack machine programs to be underflow-free.

For our new languages, besides underflow, we also have the problem of stack slots with naturals instead of bools or vice versa.  This time, we will use indexed typed families to avoid the need to reason about potential failures.

We start by defining stack types, which classify sets of possible stacks. *)

655 656
Definition tstack := list type.

Adam Chlipala's avatar
Adam Chlipala committed
657 658 659 660
(** Any stack classified by a [tstack] must have exactly as many elements, and each stack element must have the type found in the same position of the stack type.

We can define instructions in terms of stack types, where every instruction's type tells us what initial stack type it expects and what final stack type it will produce. *)

661 662 663 664 665 666 667
Inductive tinstr : tstack -> tstack -> Set :=
| TINConst : forall s, nat -> tinstr s (Nat :: s)
| TIBConst : forall s, bool -> tinstr s (Bool :: s)
| TIBinop : forall arg1 arg2 res s,
  tbinop arg1 arg2 res
  -> tinstr (arg1 :: arg2 :: s) (res :: s).

Adam Chlipala's avatar
Adam Chlipala committed
668 669
(** Stack machine programs must be a similar inductive family, since, if we again used the [list] type family, we would not be able to guarantee that intermediate stack types match within a program. *)

670 671 672 673 674 675 676
Inductive tprog : tstack -> tstack -> Set :=
| TNil : forall s, tprog s s
| TCons : forall s1 s2 s3,
  tinstr s1 s2
  -> tprog s2 s3
  -> tprog s1 s3.

Adam Chlipala's avatar
Adam Chlipala committed
677 678
(** Now, to define the semantics of our new target language, we need a representation for stacks at runtime.  We will again take advantage of type information to define types of value stacks that, by construction, contain the right number and types of elements. *)

679 680 681 682 683 684
Fixpoint vstack (ts : tstack) : Set :=
  match ts with
    | nil => unit
    | t :: ts' => typeDenote t * vstack ts'
  end%type.

Adam Chlipala's avatar
Adam Chlipala committed
685 686 687 688
(** This is another [Set]-valued function.  This time it is recursive, which is perfectly valid, since [Set] is not treated specially in determining which functions may be written.  We say that the value stack of an empty stack type is any value of type [unit], which has just a single value, [tt].  A nonempty stack type leads to a value stack that is a pair, whose first element has the proper type and whose second element follows the representation for the remainder of the stack type.

This idea of programming with types can take a while to internalize, but it enables a very simple definition of instruction denotation.  We have the same kind of type annotations for the dependent [match], but everything else is like what you might expect from a Lisp-like version of ML that ignored type information.  Nonetheless, the fact that [tinstrDenote] passes the type-checker guarantees that our stack machine programs can never go wrong. *)

689 690 691 692 693 694 695 696 697 698
Definition tinstrDenote ts ts' (i : tinstr ts ts') : vstack ts -> vstack ts' :=
  match i in (tinstr ts ts') return (vstack ts -> vstack ts') with
    | TINConst _ n => fun s => (n, s)
    | TIBConst _ b => fun s => (b, s)
    | TIBinop _ _ _ _ b => fun s =>
      match s with
        (arg1, (arg2, s')) => ((tbinopDenote b) arg1 arg2, s')
      end
  end.

Adam Chlipala's avatar
Adam Chlipala committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
(** Why do we choose to use an anonymous function to bind the initial stack in every case of the [match]?  Consider this well-intentioned but invalid alternative version:

[[
Definition tinstrDenote ts ts' (i : tinstr ts ts') (s : vstack ts) : vstack ts' :=
  match i in (tinstr ts ts') return (vstack ts') with
    | TINConst _ n => (n, s)
    | TIBConst _ b => (b, s)
    | TIBinop _ _ _ _ b =>
      match s with
        (arg1, (arg2, s')) => ((tbinopDenote b) arg1 arg2, s')
      end
  end.

The Coq type-checker complains that:

[[
The term "(n, s)" has type "(nat * vstack ts)%type"
 while it is expected to have type "vstack (Nat :: t)"
]]

Recall from our earlier discussion of [match] annotations that we write the annotations to express to the type-checker the relationship between the type indices of the case object and the result type of the [match].  Coq chooses to assign to the wildcard [_] after [TINConst] the name [t], and the type error is telling us that the type checker cannot prove that [t] is the same as [ts].  By moving [s] out of the [match], we lose the ability to express, with [in] and [return] clauses, the relationship between the shared index [ts] of [s] and [i].

There %\textit{%#<i>#are#</i>#%}% reasonably general ways of getting around this problem without pushing binders inside [match]es.  However, the alternatives are significantly more involved, and the technique we use here is almost certainly the best choice, whenever it applies.

*)

(** We finish the semantics with a straightforward definition of program denotation. *)

727 728 729 730 731 732 733 734 735
Fixpoint tprogDenote ts ts' (p : tprog ts ts') {struct p} : vstack ts -> vstack ts' :=
  match p in (tprog ts ts') return (vstack ts -> vstack ts') with
    | TNil _ => fun s => s
    | TCons _ _ _ i p' => fun s => tprogDenote p' (tinstrDenote i s)
  end.


(** ** Translation *)

Adam Chlipala's avatar
Adam Chlipala committed
736 737
(** To define our compilation, it is useful to have an auxiliary function for concatenating two stack machine programs. *)

738 739 740 741 742 743
Fixpoint tconcat ts ts' ts'' (p : tprog ts ts') {struct p} : tprog ts' ts'' -> tprog ts ts'' :=
  match p in (tprog ts ts') return (tprog ts' ts'' -> tprog ts ts'') with
    | TNil _ => fun p' => p'
    | TCons _ _ _ i p1 => fun p' => TCons i (tconcat p1 p')
  end.

Adam Chlipala's avatar
Adam Chlipala committed
744 745
(** With that function in place, the compilation is defined very similarly to how it was before, modulo the use of dependent typing. *)

746 747 748 749 750 751 752 753
Fixpoint tcompile t (e : texp t) (ts : tstack) {struct e} : tprog ts (t :: ts) :=
  match e in (texp t) return (tprog ts (t :: ts)) with
    | TNConst n => TCons (TINConst _ n) (TNil _)
    | TBConst b => TCons (TIBConst _ b) (TNil _)
    | TBinop _ _ _ b e1 e2 => tconcat (tcompile e2 _)
      (tconcat (tcompile e1 _) (TCons (TIBinop _ b) (TNil _)))
  end.

Adam Chlipala's avatar
Adam Chlipala committed
754
(** One interesting feature of the definition is the underscores appearing to the right of [=>] arrows.  Haskell and ML programmers are quite familiar with compilers that infer type parameters to polymorphic values.  In Coq, it is possible to go even further and ask the system to infer arbitrary terms, by writing underscores in place of specific values.  You may have noticed that we have been calling functions without specifying all of their arguments.  For instance, the recursive calls here to [tcompile] omit the [t] argument.  Coq's %\textit{%#<i>#implicit argument#</i>#%}% mechanism automatically inserts underscores for arguments that it will probably be able to infer.  Inference of such values is far from complete, though; generally, it only works in cases similar to those encountered with polymorphic type instantiation in Haskell and ML.
Adam Chlipala's avatar
Adam Chlipala committed
755 756 757

The underscores here are being filled in with stack types.  That is, the Coq type inferencer is, in a sense, inferring something about the flow of control in the translated programs.  We can take a look at exactly which values are filled in: *)

758 759
Print tcompile.

Adam Chlipala's avatar
Adam Chlipala committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
(** [[

tcompile = 
fix tcompile (t : type) (e : texp t) (ts : tstack) {struct e} :
  tprog ts (t :: ts) :=
  match e in (texp t0) return (tprog ts (t0 :: ts)) with
  | TNConst n => TCons (TINConst ts n) (TNil (Nat :: ts))
  | TBConst b => TCons (TIBConst ts b) (TNil (Bool :: ts))
  | TBinop arg1 arg2 res b e1 e2 =>
      tconcat (tcompile arg2 e2 ts)
        (tconcat (tcompile arg1 e1 (arg2 :: ts))
           (TCons (TIBinop ts b) (TNil (res :: ts))))
  end
     : forall t : type, texp t -> forall ts : tstack, tprog ts (t :: ts)
]] *)


(** We can check that the compiler generates programs that behave appropriately on our sample programs from above: *)

Eval simpl in tprogDenote (tcompile (TNConst 42) nil) tt.
(** [[
= (42, tt) : vstack (Nat :: nil)
]] *)
Eval simpl in tprogDenote (tcompile (TBConst true) nil) tt.
(** [[
= (true, tt) : vstack (Bool :: nil)
]] *)
Eval simpl in tprogDenote (tcompile (TBinop TTimes (TBinop TPlus (TNConst 2) (TNConst 2)) (TNConst 7)) nil) tt.
(** [[
= (28, tt) : vstack (Nat :: nil)
]] *)
Eval simpl in tprogDenote (tcompile (TBinop (TEq Nat) (TBinop TPlus (TNConst 2) (TNConst 2)) (TNConst 7)) nil) tt.
(** [[
= (false, tt) : vstack (Bool :: nil)
]] *)
Eval simpl in tprogDenote (tcompile (TBinop TLt (TBinop TPlus (TNConst 2) (TNConst 2)) (TNConst 7)) nil) tt.
(** [[
= (true, tt) : vstack (Bool :: nil)
]] *)

800

Adam Chlipala's avatar
Adam Chlipala committed
801 802 803 804
(** ** Translation Correctness *)

(** We can state a correctness theorem similar to the last one. *)

Adam Chlipala's avatar
Adam Chlipala committed
805
Theorem tcompile_correct : forall t (e : texp t), tprogDenote (tcompile e nil) tt = (texpDenote e, tt).
Adam Chlipala's avatar
Adam Chlipala committed
806 807 808
(* begin hide *)
Abort.
(* end hide *)
Adam Chlipala's avatar
Adam Chlipala committed
809
(* begin thide *)
Adam Chlipala's avatar
Adam Chlipala committed
810 811

(** Again, we need to strengthen the theorem statement so that the induction will go through.  This time, I will develop an alternative approach to this kind of proof, stating the key lemma as: *)
812

Adam Chlipala's avatar
Adam Chlipala committed
813
Lemma tcompile_correct' : forall t (e : texp t)
814 815 816
  ts (s : vstack ts),
  tprogDenote (tcompile e ts) s
  = (texpDenote e, s).
Adam Chlipala's avatar
Adam Chlipala committed
817

Adam Chlipala's avatar
Adam Chlipala committed
818
(** While lemma [compile_correct'] quantified over a program that is the "continuation" for the expression we are considering, here we avoid drawing in any extra syntactic elements.  In addition to the source expression and its type, we also quantify over an initial stack type and a stack compatible with it.  Running the compilation of the program starting from that stack, we should arrive at a stack that differs only in having the program's denotation pushed onto it.
Adam Chlipala's avatar
Adam Chlipala committed
819 820 821

   Let us try to prove this theorem in the same way that we settled on in the last section. *)

822
  induction e; crush.
Adam Chlipala's avatar
Adam Chlipala committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836

(** We are left with this unproved conclusion:

[[

tprogDenote
     (tconcat (tcompile e2 ts)
        (tconcat (tcompile e1 (arg2 :: ts))
           (TCons (TIBinop ts t) (TNil (res :: ts))))) s =
   (tbinopDenote t (texpDenote e1) (texpDenote e2), s)
]]

We need an analogue to the [app_ass] theorem that we used to rewrite the goal in the last section.  We can abort this proof and prove such a lemma about [tconcat].
*)
837 838
Abort.

Adam Chlipala's avatar
Adam Chlipala committed
839
Lemma tconcat_correct : forall ts ts' ts'' (p : tprog ts ts') (p' : tprog ts' ts'')
840 841 842 843 844 845
  (s : vstack ts),
  tprogDenote (tconcat p p') s
  = tprogDenote p' (tprogDenote p s).
  induction p; crush.
Qed.

Adam Chlipala's avatar
Adam Chlipala committed
846 847
(** This one goes through completely automatically.

Adam Chlipala's avatar
Adam Chlipala committed
848
Some code behind the scenes registers [app_ass] for use by [crush].  We must register [tconcat_correct] similarly to get the same effect: *)
Adam Chlipala's avatar
Adam Chlipala committed
849

Adam Chlipala's avatar
Adam Chlipala committed
850
Hint Rewrite tconcat_correct : cpdt.
851

Adam Chlipala's avatar
Adam Chlipala committed
852
(** We ask that the lemma be used for left-to-right rewriting, and we ask for the hint to be added to the hint database called [cpdt], which is the database used by [crush].  Now we are ready to return to [tcompile_correct'], proving it automatically this time. *)
Adam Chlipala's avatar
Adam Chlipala committed
853

Adam Chlipala's avatar
Adam Chlipala committed
854
Lemma tcompile_correct' : forall t (e : texp t)
855 856 857 858 859 860
  ts (s : vstack ts),
  tprogDenote (tcompile e ts) s
  = (texpDenote e, s).
  induction e; crush.
Qed.

Adam Chlipala's avatar
Adam Chlipala committed
861 862
(** We can register this main lemma as another hint, allowing us to prove the final theorem trivially. *)

Adam Chlipala's avatar
Adam Chlipala committed
863
Hint Rewrite tcompile_correct' : cpdt.
864

Adam Chlipala's avatar
Adam Chlipala committed
865
Theorem tcompile_correct : forall t (e : texp t), tprogDenote (tcompile e nil) tt = (texpDenote e, tt).
866 867
  crush.
Qed.
Adam Chlipala's avatar
Adam Chlipala committed
868
(* end thide *)