Subset.v 36.5 KB
Newer Older
1
(* Copyright (c) 2008-2012, Adam Chlipala
Adam Chlipala's avatar
Adam Chlipala committed
2 3 4 5 6 7 8 9 10 11 12
 * 
 * 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/
 *)

(* begin hide *)
Require Import List.

13
Require Import CpdtTactics.
Adam Chlipala's avatar
Adam Chlipala committed
14 15 16 17

Set Implicit Arguments.
(* end hide *)

18 19
(** printing <-- $\longleftarrow$ *)

Adam Chlipala's avatar
Adam Chlipala committed
20

Adam Chlipala's avatar
Adam Chlipala committed
21 22 23
(** %\part{Programming with Dependent Types}

\chapter{Subset Types and Variations}% *)
Adam Chlipala's avatar
Adam Chlipala committed
24

25
(** So far, we have seen many examples of what we might call "classical program verification."  We write programs, write their specifications, and then prove that the programs satisfy their specifications.  The programs that we have written in Coq have been normal functional programs that we could just as well have written in Haskell or ML.  In this chapter, we start investigating uses of%\index{dependent types}% _dependent types_ to integrate programming, specification, and proving into a single phase.  The techniques we will learn make it possible to reduce the cost of program verification dramatically. *)
Adam Chlipala's avatar
Adam Chlipala committed
26 27 28 29 30 31 32


(** * Introducing Subset Types *)

(** Let us consider several ways of implementing the natural number predecessor function.  We start by displaying the definition from the standard library: *)

Print pred.
Adam Chlipala's avatar
Adam Chlipala committed
33
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
34 35 36 37 38
pred = fun n : nat => match n with
                      | 0 => 0
                      | S u => u
                      end
     : nat -> nat
Adam Chlipala's avatar
Adam Chlipala committed
39 40
 
]]
Adam Chlipala's avatar
Adam Chlipala committed
41

Adam Chlipala's avatar
Adam Chlipala committed
42
We can use a new command, %\index{Vernacular commands!Extraction}\index{program extraction}\index{extraction|see{program extraction}}%[Extraction], to produce an %\index{OCaml}%OCaml version of this function. *)
Adam Chlipala's avatar
Adam Chlipala committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

Extraction pred.

(** %\begin{verbatim}
(** val pred : nat -> nat **)

let pred = function
  | O -> O
  | S u -> u
\end{verbatim}%

#<pre>
(** val pred : nat -> nat **)

let pred = function
  | O -> O
  | S u -> u
</pre># *)

(** Returning 0 as the predecessor of 0 can come across as somewhat of a hack.  In some situations, we might like to be sure that we never try to take the predecessor of 0.  We can enforce this by giving [pred] a stronger, dependent type. *)

Lemma zgtz : 0 > 0 -> False.
  crush.
Qed.

Definition pred_strong1 (n : nat) : n > 0 -> nat :=
Adam Chlipala's avatar
Adam Chlipala committed
69
  match n with
Adam Chlipala's avatar
Adam Chlipala committed
70 71 72 73
    | O => fun pf : 0 > 0 => match zgtz pf with end
    | S n' => fun _ => n'
  end.

74
(** We expand the type of [pred] to include a _proof_ that its argument [n] is greater than 0.  When [n] is 0, we use the proof to derive a contradiction, which we can use to build a value of any type via a vacuous pattern match.  When [n] is a successor, we have no need for the proof and just return the answer.  The proof argument can be said to have a _dependent_ type, because its type depends on the _value_ of the argument [n].
Adam Chlipala's avatar
Adam Chlipala committed
75

76
   Coq's [Eval] command can execute particular invocations of [pred_strong1] just as easily as it can execute more traditional functional programs.  Note that Coq has decided that argument [n] of [pred_strong1] can be made _implicit_, since it can be deduced from the type of the second argument, so we need not write [n] in function calls. *)
77 78 79 80 81 82 83 84 85 86 87

Theorem two_gt0 : 2 > 0.
  crush.
Qed.

Eval compute in pred_strong1 two_gt0.
(** %\vspace{-.15in}% [[
     = 1
     : nat
 ]]

88
One aspect in particular of the definition of [pred_strong1] may be surprising.  We took advantage of [Definition]'s syntactic sugar for defining function arguments in the case of [n], but we bound the proofs later with explicit [fun] expressions.  Let us see what happens if we write this function in the way that at first seems most natural.
Adam Chlipala's avatar
Adam Chlipala committed
89

90
%\vspace{-.15in}%[[
Adam Chlipala's avatar
Adam Chlipala committed
91 92 93 94 95
Definition pred_strong1' (n : nat) (pf : n > 0) : nat :=
  match n with
    | O => match zgtz pf with end
    | S n' => n'
  end.
Adam Chlipala's avatar
Adam Chlipala committed
96
]]
Adam Chlipala's avatar
Adam Chlipala committed
97

Adam Chlipala's avatar
Adam Chlipala committed
98
<<
Adam Chlipala's avatar
Adam Chlipala committed
99 100 101 102 103
Error: In environment
n : nat
pf : n > 0
The term "pf" has type "n > 0" while it is expected to have type 
"0 > 0"
Adam Chlipala's avatar
Adam Chlipala committed
104
>>
Adam Chlipala's avatar
Adam Chlipala committed
105

Adam Chlipala's avatar
Adam Chlipala committed
106
The term [zgtz pf] fails to type-check.  Somehow the type checker has failed to take into account information that follows from which [match] branch that term appears in.  The problem is that, by default, [match] does not let us use such implied information.  To get refined typing, we must always rely on [match] annotations, either written explicitly or inferred.
Adam Chlipala's avatar
Adam Chlipala committed
107

108
In this case, we must use a [return] annotation to declare the relationship between the _value_ of the [match] discriminee and the _type_ of the result.  There is no annotation that lets us declare a relationship between the discriminee and the type of a variable that is already in scope; hence, we delay the binding of [pf], so that we can use the [return] annotation to express the needed relationship.
Adam Chlipala's avatar
Adam Chlipala committed
109

110
We are lucky that Coq's heuristics infer the [return] clause (specifically, [return n > 0 -> nat]) for us in the definition of [pred_strong1], leading to the following elaborated code: *)
Adam Chlipala's avatar
Adam Chlipala committed
111 112 113 114 115 116 117

Definition pred_strong1' (n : nat) : n > 0 -> nat :=
  match n return n > 0 -> nat with
    | O => fun pf : 0 > 0 => match zgtz pf with end
    | S n' => fun _ => n'
  end.

118
(** By making explicit the functional relationship between value [n] and the result type of the [match], we guide Coq toward proper type checking.  The clause for this example follows by simple copying of the original annotation on the definition.  In general, however, the [match] annotation inference problem is undecidable.  The known undecidable problem of%\index{higher-order unification}% _higher-order unification_ %\cite{HOU}% reduces to the [match] type inference problem.  Over time, Coq is enhanced with more and more heuristics to get around this problem, but there must always exist [match]es whose types Coq cannot infer without annotations.
Adam Chlipala's avatar
Adam Chlipala committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

Let us now take a look at the OCaml code Coq generates for [pred_strong1]. *)

Extraction pred_strong1.

(** %\begin{verbatim}
(** val pred_strong1 : nat -> nat **)

let pred_strong1 = function
  | O -> assert false (* absurd case *)
  | S n' -> n'
\end{verbatim}%

#<pre>
(** val pred_strong1 : nat -> nat **)

let pred_strong1 = function
  | O -> assert false (* absurd case *)
  | S n' -> n'
</pre># *)

140
(** The proof argument has disappeared!  We get exactly the OCaml code we would have written manually.  This is our first demonstration of the main technically interesting feature of Coq program extraction: proofs are erased systematically.
Adam Chlipala's avatar
Adam Chlipala committed
141

142 143
%\medskip%

144
We can reimplement our dependently typed [pred] based on%\index{subset types}% _subset types_, defined in the standard library with the type family %\index{Gallina terms!sig}%[sig]. *)
Adam Chlipala's avatar
Adam Chlipala committed
145

146
(* begin hide *)
147 148 149
(* begin thide *)
Definition bar := ex.
(* end thide *)
150 151
(* end hide *)

Adam Chlipala's avatar
Adam Chlipala committed
152
Print sig.
Adam Chlipala's avatar
Adam Chlipala committed
153
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
154 155 156 157
Inductive sig (A : Type) (P : A -> Prop) : Type :=
    exist : forall x : A, P x -> sig P
]]

158
The family [sig] is a Curry-Howard twin of [ex], except that [sig] is in [Type], while [ex] is in [Prop].  That means that [sig] values can survive extraction, while [ex] proofs will always be erased.  The actual details of extraction of [sig]s are more subtle, as we will see shortly.
Adam Chlipala's avatar
Adam Chlipala committed
159 160 161 162

We rewrite [pred_strong1], using some syntactic sugar for subset types. *)

Locate "{ _ : _ | _ }".
Adam Chlipala's avatar
Adam Chlipala committed
163
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
164
Notation
Adam Chlipala's avatar
Adam Chlipala committed
165
"{ x : A  |  P }" := sig (fun x : A => P)
166 167
 ]]
 *)
Adam Chlipala's avatar
Adam Chlipala committed
168 169 170 171 172 173 174

Definition pred_strong2 (s : {n : nat | n > 0}) : nat :=
  match s with
    | exist O pf => match zgtz pf with end
    | exist (S n') _ => n'
  end.

175
(** To build a value of a subset type, we use the [exist] constructor, and the details of how to do that follow from the output of our earlier [Print sig] command, where we elided the extra information that parameter [A] is implicit.  We need an extra [_] here and not in the definition of [pred_strong2] because _parameters_ of inductive types (like the predicate [P] for [sig]) are not mentioned in pattern matching, but _are_ mentioned in construction of terms (if they are not marked as implicit arguments). *)
176 177 178 179 180

Eval compute in pred_strong2 (exist _ 2 two_gt0).
(** %\vspace{-.15in}% [[
     = 1
     : nat
181 182
     ]]
     *)
183

Adam Chlipala's avatar
Adam Chlipala committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
Extraction pred_strong2.

(** %\begin{verbatim}
(** val pred_strong2 : nat -> nat **)

let pred_strong2 = function
  | O -> assert false (* absurd case *)
  | S n' -> n'
\end{verbatim}%

#<pre>
(** val pred_strong2 : nat -> nat **)

let pred_strong2 = function
  | O -> assert false (* absurd case *)
  | S n' -> n'
</pre>#

We arrive at the same OCaml code as was extracted from [pred_strong1], which may seem surprising at first.  The reason is that a value of [sig] is a pair of two pieces, a value and a proof about it.  Extraction erases the proof, which reduces the constructor [exist] of [sig] to taking just a single argument.  An optimization eliminates uses of datatypes with single constructors taking single arguments, and we arrive back where we started.

We can continue on in the process of refining [pred]'s type.  Let us change its result type to capture that the output is really the predecessor of the input. *)

Definition pred_strong3 (s : {n : nat | n > 0}) : {m : nat | proj1_sig s = S m} :=
  match s return {m : nat | proj1_sig s = S m} with
    | exist 0 pf => match zgtz pf with end
209
    | exist (S n') pf => exist _ n' (eq_refl _)
Adam Chlipala's avatar
Adam Chlipala committed
210 211
  end.

212 213
Eval compute in pred_strong3 (exist _ 2 two_gt0).
(** %\vspace{-.15in}% [[
214
     = exist (fun m : nat => 2 = S m) 1 (eq_refl 2)
215
     : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m}
Adam Chlipala's avatar
Adam Chlipala committed
216
      ]]
217
     *)
218

219
(* begin hide *)
220
(* begin thide *)
221
Definition pred_strong := 0.
222
(* end thide *)
223 224
(* end hide *)

225
(** A value in a subset type can be thought of as a%\index{dependent pair}% _dependent pair_ (or%\index{sigma type}% _sigma type_) of a base value and a proof about it.  The function %\index{Gallina terms!proj1\_sig}%[proj1_sig] extracts the first component of the pair.  It turns out that we need to include an explicit [return] clause here, since Coq's heuristics are not smart enough to propagate the result type that we wrote earlier.
Adam Chlipala's avatar
Adam Chlipala committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

By now, the reader is probably ready to believe that the new [pred_strong] leads to the same OCaml code as we have seen several times so far, and Coq does not disappoint. *)

Extraction pred_strong3.

(** %\begin{verbatim}
(** val pred_strong3 : nat -> nat **)

let pred_strong3 = function
  | O -> assert false (* absurd case *)
  | S n' -> n'
\end{verbatim}%

#<pre>
(** val pred_strong3 : nat -> nat **)

let pred_strong3 = function
  | O -> assert false (* absurd case *)
  | S n' -> n'
</pre>#

Adam Chlipala's avatar
Adam Chlipala committed
247
We have managed to reach a type that is, in a formal sense, the most expressive possible for [pred].  Any other implementation of the same type must have the same input-output behavior.  However, there is still room for improvement in making this kind of code easier to write.  Here is a version that takes advantage of tactic-based theorem proving.  We switch back to passing a separate proof argument instead of using a subset type for the function's input, because this leads to cleaner code.  (Recall that [False_rec] is the [Set]-level induction principle for [False], which can be used to produce a value in any [Set] given a proof of [False].) *)
Adam Chlipala's avatar
Adam Chlipala committed
248

249
Definition pred_strong4 : forall n : nat, n > 0 -> {m : nat | n = S m}.
Adam Chlipala's avatar
Adam Chlipala committed
250
  refine (fun n =>
Adam Chlipala's avatar
Adam Chlipala committed
251
    match n with
Adam Chlipala's avatar
Adam Chlipala committed
252 253 254
      | O => fun _ => False_rec _ _
      | S n' => fun _ => exist _ n' _
    end).
Adam Chlipala's avatar
Adam Chlipala committed
255

Adam Chlipala's avatar
Adam Chlipala committed
256
(* begin thide *)
Adam Chlipala's avatar
Adam Chlipala committed
257 258
  (** We build [pred_strong4] using tactic-based proving, beginning with a [Definition] command that ends in a period before a definition is given.  Such a command enters the interactive proving mode, with the type given for the new identifier as our proof goal.  It may seem strange to change perspective so implicitly between programming and proving, but recall that programs and proofs are two sides of the same coin in Coq, thanks to the Curry-Howard correspondence.

259
     We do most of the work with the %\index{tactics!refine}%[refine] tactic, to which we pass a partial "proof" of the type we are trying to prove.  There may be some pieces left to fill in, indicated by underscores.  Any underscore that Coq cannot reconstruct with type inference is added as a proof subgoal.  In this case, we have two subgoals:
Adam Chlipala's avatar
Adam Chlipala committed
260

Adam Chlipala's avatar
Adam Chlipala committed
261
[[
262
2 subgoals
Adam Chlipala's avatar
Adam Chlipala committed
263 264 265 266 267
  
  n : nat
  _ : 0 > 0
  ============================
   False
268 269 270

subgoal 2 is

Adam Chlipala's avatar
Adam Chlipala committed
271 272 273 274 275 276 277
 S n' = S n'
 ]]

We can see that the first subgoal comes from the second underscore passed to [False_rec], and the second subgoal comes from the second underscore passed to [exist].  In the first case, we see that, though we bound the proof variable with an underscore, it is still available in our proof context.  It is hard to refer to underscore-named variables in manual proofs, but automation makes short work of them.  Both subgoals are easy to discharge that way, so let us back up and ask to prove all subgoals automatically. *)

  Undo.
  refine (fun n =>
Adam Chlipala's avatar
Adam Chlipala committed
278
    match n with
Adam Chlipala's avatar
Adam Chlipala committed
279 280 281
      | O => fun _ => False_rec _ _
      | S n' => fun _ => exist _ n' _
    end); crush.
Adam Chlipala's avatar
Adam Chlipala committed
282
(* end thide *)
Adam Chlipala's avatar
Adam Chlipala committed
283 284
Defined.

285
(** We end the "proof" with %\index{Vernacular commands!Defined}%[Defined] instead of [Qed], so that the definition we constructed remains visible.  This contrasts to the case of ending a proof with [Qed], where the details of the proof are hidden afterward.  (More formally, [Defined] marks an identifier as%\index{transparent}% _transparent_, allowing it to be unfolded; while [Qed] marks an identifier as%\index{opaque}% _opaque_, preventing unfolding.)  Let us see what our proof script constructed. *)
Adam Chlipala's avatar
Adam Chlipala committed
286 287

Print pred_strong4.
Adam Chlipala's avatar
Adam Chlipala committed
288
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
289 290 291 292 293 294 295 296 297 298 299 300
pred_strong4 = 
fun n : nat =>
match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with
| 0 =>
    fun _ : 0 > 0 =>
    False_rec {m : nat | 0 = S m}
      (Bool.diff_false_true
         (Bool.absurd_eq_true false
            (Bool.diff_false_true
               (Bool.absurd_eq_true false (pred_strong4_subproof n _)))))
| S n' =>
    fun _ : S n' > 0 =>
301
    exist (fun m : nat => S n' = S m) n' (eq_refl (S n'))
Adam Chlipala's avatar
Adam Chlipala committed
302 303 304 305
end
     : forall n : nat, n > 0 -> {m : nat | n = S m}
]]

306
We see the code we entered, with some proofs filled in.  The first proof obligation, the second argument to [False_rec], is filled in with a nasty-looking proof term that we can be glad we did not enter by hand.  The second proof obligation is a simple reflexivity proof. *)
307 308 309

Eval compute in pred_strong4 two_gt0.
(** %\vspace{-.15in}% [[
310
     = exist (fun m : nat => 2 = S m) 1 (eq_refl 2)
311 312
     : {m : nat | 2 = S m}
     ]]
Adam Chlipala's avatar
Adam Chlipala committed
313

314
  A tactic modifier called %\index{tactics!abstract}%[abstract] can be helpful for producing shorter terms, by automatically abstracting subgoals into named lemmas. *)
Adam Chlipala's avatar
Adam Chlipala committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

(* begin thide *)
Definition pred_strong4' : forall n : nat, n > 0 -> {m : nat | n = S m}.
  refine (fun n =>
    match n with
      | O => fun _ => False_rec _ _
      | S n' => fun _ => exist _ n' _
    end); abstract crush.
Defined.

Print pred_strong4'.
(* end thide *)

(** %\vspace{-.15in}% [[
pred_strong4' = 
fun n : nat =>
match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with
| 0 =>
    fun _H : 0 > 0 =>
    False_rec {m : nat | 0 = S m} (pred_strong4'_subproof n _H)
| S n' =>
    fun _H : S n' > 0 =>
    exist (fun m : nat => S n' = S m) n' (pred_strong4'_subproof0 n _H)
end
     : forall n : nat, n > 0 -> {m : nat | n = S m}
]]

Adam Chlipala's avatar
Adam Chlipala committed
342
We are almost done with the ideal implementation of dependent predecessor.  We can use Coq's syntax extension facility to arrive at code with almost no complexity beyond a Haskell or ML program with a complete specification in a comment.  In this book, we will not dwell on the details of syntax extensions; the Coq manual gives a straightforward introduction to them. *)
Adam Chlipala's avatar
Adam Chlipala committed
343 344 345 346

Notation "!" := (False_rec _ _).
Notation "[ e ]" := (exist _ e _).

347
Definition pred_strong5 : forall n : nat, n > 0 -> {m : nat | n = S m}.
Adam Chlipala's avatar
Adam Chlipala committed
348
  refine (fun n =>
Adam Chlipala's avatar
Adam Chlipala committed
349
    match n with
Adam Chlipala's avatar
Adam Chlipala committed
350 351 352 353
      | O => fun _ => !
      | S n' => fun _ => [n']
    end); crush.
Defined.
Adam Chlipala's avatar
Adam Chlipala committed
354

355 356 357 358 359 360 361 362
(** By default, notations are also used in pretty-printing terms, including results of evaluation. *)

Eval compute in pred_strong5 two_gt0.
(** %\vspace{-.15in}% [[
     = [1]
     : {m : nat | 2 = S m}
     ]]

363
  One other alternative is worth demonstrating.  Recent Coq versions include a facility called %\index{Program}%[Program] that streamlines this style of definition.  Here is a complete implementation using [Program].%\index{Vernacular commands!Obligation Tactic}\index{Vernacular commands!Program Definition}% *)
Adam Chlipala's avatar
Adam Chlipala committed
364 365 366 367 368 369 370 371 372

Obligation Tactic := crush.

Program Definition pred_strong6 (n : nat) (_ : n > 0) : {m : nat | n = S m} :=
  match n with
    | O => _
    | S n' => n'
  end.

Adam Chlipala's avatar
Adam Chlipala committed
373
(** Printing the resulting definition of [pred_strong6] yields a term very similar to what we built with [refine].  [Program] can save time in writing programs that use subset types.  Nonetheless, [refine] is often just as effective, and [refine] gives you more control over the form the final term takes, which can be useful when you want to prove additional theorems about your definition.  [Program] will sometimes insert type casts that can complicate theorem proving. *)
Adam Chlipala's avatar
Adam Chlipala committed
374

375 376 377 378
Eval compute in pred_strong6 two_gt0.
(** %\vspace{-.15in}% [[
     = [1]
     : {m : nat | 2 = S m}
379
     ]]
Adam Chlipala's avatar
Adam Chlipala committed
380

381
In this case, we see that the new definition yields the same computational behavior as before. *)
382

Adam Chlipala's avatar
Adam Chlipala committed
383 384 385

(** * Decidable Proposition Types *)

Adam Chlipala's avatar
Adam Chlipala committed
386
(** There is another type in the standard library which captures the idea of program values that indicate which of two propositions is true.%\index{Gallina terms!sumbool}% *)
Adam Chlipala's avatar
Adam Chlipala committed
387 388

Print sumbool.
Adam Chlipala's avatar
Adam Chlipala committed
389
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
390 391
Inductive sumbool (A : Prop) (B : Prop) : Set :=
    left : A -> {A} + {B} | right : B -> {A} + {B}
Adam Chlipala's avatar
Adam Chlipala committed
392
]]
Adam Chlipala's avatar
Adam Chlipala committed
393

394
Here, the constructors of [sumbool] have types written in terms of a registered notation for [sumbool], such that the result type of each constructor desugars to [sumbool A B].  We can define some notations of our own to make working with [sumbool] more convenient. *)
Adam Chlipala's avatar
Adam Chlipala committed
395 396 397 398 399

Notation "'Yes'" := (left _ _).
Notation "'No'" := (right _ _).
Notation "'Reduce' x" := (if x then Yes else No) (at level 50).

400
(** The %\coqdocnotation{%#<tt>#Reduce#</tt>#%}% notation is notable because it demonstrates how [if] is overloaded in Coq.  The [if] form actually works when the test expression has any two-constructor inductive type.  Moreover, in the [then] and [else] branches, the appropriate constructor arguments are bound.  This is important when working with [sumbool]s, when we want to have the proof stored in the test expression available when proving the proof obligations generated in the appropriate branch.
Adam Chlipala's avatar
Adam Chlipala committed
401 402 403

Now we can write [eq_nat_dec], which compares two natural numbers, returning either a proof of their equality or a proof of their inequality. *)

404
Definition eq_nat_dec : forall n m : nat, {n = m} + {n <> m}.
Adam Chlipala's avatar
Adam Chlipala committed
405 406
  refine (fix f (n m : nat) : {n = m} + {n <> m} :=
    match n, m with
Adam Chlipala's avatar
Adam Chlipala committed
407 408 409 410 411 412
      | O, O => Yes
      | S n', S m' => Reduce (f n' m')
      | _, _ => No
    end); congruence.
Defined.

413 414 415 416
Eval compute in eq_nat_dec 2 2.
(** %\vspace{-.15in}% [[
     = Yes
     : {2 = 2} + {2 <> 2}
417 418
     ]]
     *)
419 420 421 422

Eval compute in eq_nat_dec 2 3.
(** %\vspace{-.15in}% [[
     = No
Adam Chlipala's avatar
Adam Chlipala committed
423
     : {2 = 3} + {2 <> 3}
424
     ]]
425

426
Note that the %\coqdocnotation{%#<tt>#Yes#</tt>#%}% and %\coqdocnotation{%#<tt>#No#</tt>#%}% notations are hiding proofs establishing the correctness of the outputs.
Adam Chlipala's avatar
Adam Chlipala committed
427 428

   Our definition extracts to reasonable OCaml code. *)
Adam Chlipala's avatar
Adam Chlipala committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

Extraction eq_nat_dec.

(** %\begin{verbatim}
(** val eq_nat_dec : nat -> nat -> sumbool **)

let rec eq_nat_dec n m =
  match n with
    | O -> (match m with
              | O -> Left
              | S n0 -> Right)
    | S n' -> (match m with
                 | O -> Right
                 | S m' -> eq_nat_dec n' m')
\end{verbatim}%

#<pre>
(** val eq_nat_dec : nat -> nat -> sumbool **)

let rec eq_nat_dec n m =
  match n with
    | O -> (match m with
              | O -> Left
              | S n0 -> Right)
    | S n' -> (match m with
                 | O -> Right
                 | S m' -> eq_nat_dec n' m')
</pre>#

Adam Chlipala's avatar
Adam Chlipala committed
458
Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it.%\index{tactics!decide equality}% *)
Adam Chlipala's avatar
Adam Chlipala committed
459 460 461 462 463

Definition eq_nat_dec' (n m : nat) : {n = m} + {n <> m}.
  decide equality.
Defined.

464
(** Curious readers can verify that the [decide equality] version extracts to the same OCaml code as our more manual version does.  That OCaml code had one undesirable property, which is that it uses <<Left>> and <<Right>> constructors instead of the Boolean values built into OCaml.  We can fix this, by using Coq's facility for mapping Coq inductive types to OCaml variant types.%\index{Vernacular commands!Extract Inductive}% *)
Adam Chlipala's avatar
Adam Chlipala committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

Extract Inductive sumbool => "bool" ["true" "false"].
Extraction eq_nat_dec'.

(** %\begin{verbatim}
(** val eq_nat_dec' : nat -> nat -> bool **)

let rec eq_nat_dec' n m0 =
  match n with
    | O -> (match m0 with
              | O -> true
              | S n0 -> false)
    | S n0 -> (match m0 with
                 | O -> false
                 | S n1 -> eq_nat_dec' n0 n1)
\end{verbatim}%

#<pre>
(** val eq_nat_dec' : nat -> nat -> bool **)

let rec eq_nat_dec' n m0 =
  match n with
    | O -> (match m0 with
              | O -> true
              | S n0 -> false)
    | S n0 -> (match m0 with
                 | O -> false
                 | S n1 -> eq_nat_dec' n0 n1)
</pre># *)
494 495 496

(** %\smallskip%

497
We can build "smart" versions of the usual Boolean operators and put them to good use in certified programming.  For instance, here is a [sumbool] version of Boolean "or." *)
498

499 500
(* EX: Write a function that decides if an element belongs to a list. *)

Adam Chlipala's avatar
Adam Chlipala committed
501
(* begin thide *)
502
Notation "x || y" := (if x then Yes else Reduce y).
503 504 505 506 507 508 509 510 511

(** Let us use it for building a function that decides list membership.  We need to assume the existence of an equality decision procedure for the type of list elements. *)

Section In_dec.
  Variable A : Set.
  Variable A_eq_dec : forall x y : A, {x = y} + {x <> y}.

  (** The final function is easy to write using the techniques we have developed so far. *)

Adam Chlipala's avatar
Adam Chlipala committed
512 513 514
  Definition In_dec : forall (x : A) (ls : list A), {In x ls} + {~ In x ls}.
    refine (fix f (x : A) (ls : list A) : {In x ls} + {~ In x ls} :=
      match ls with
515 516 517
	| nil => No
	| x' :: ls' => A_eq_dec x x' || f x ls'
      end); crush.
518
  Defined.
519 520
End In_dec.

521 522 523
Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil).
(** %\vspace{-.15in}% [[
     = Yes
524
     : {In 2 (1 :: 2 :: nil)} + { ~ In 2 (1 :: 2 :: nil)}
525 526
     ]]
     *)
527 528 529 530

Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil).
(** %\vspace{-.15in}% [[
     = No
531
     : {In 3 (1 :: 2 :: nil)} + { ~ In 3 (1 :: 2 :: nil)}
532
     ]]
533

534
The [In_dec] function has a reasonable extraction to OCaml. *)
535 536

Extraction In_dec.
Adam Chlipala's avatar
Adam Chlipala committed
537
(* end thide *)
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558

(** %\begin{verbatim}
(** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)

let rec in_dec a_eq_dec x = function
  | Nil -> false
  | Cons (x', ls') ->
      (match a_eq_dec x x' with
         | true -> true
         | false -> in_dec a_eq_dec x ls')
\end{verbatim}%

#<pre>
(** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)

let rec in_dec a_eq_dec x = function
  | Nil -> false
  | Cons (x', ls') ->
      (match a_eq_dec x x' with
         | true -> true
         | false -> in_dec a_eq_dec x ls')
559 560 561
</pre>#

This is more or the less code for the corresponding function from the OCaml standard library. *)
562 563 564 565


(** * Partial Subset Types *)

Adam Chlipala's avatar
Adam Chlipala committed
566
(** Our final implementation of dependent predecessor used a very specific argument type to ensure that execution could always complete normally.  Sometimes we want to allow execution to fail, and we want a more principled way of signaling failure than returning a default value, as [pred] does for [0].  One approach is to define this type family %\index{Gallina terms!maybe}%[maybe], which is a version of [sig] that allows obligation-free failure. *)
Adam Chlipala's avatar
Adam Chlipala committed
567

568
Inductive maybe (A : Set) (P : A -> Prop) : Set :=
569 570 571
| Unknown : maybe P
| Found : forall x : A, P x -> maybe P.

Adam Chlipala's avatar
Adam Chlipala committed
572 573
(** We can define some new notations, analogous to those we defined for subset types. *)

574 575
Notation "{{ x | P }}" := (maybe (fun x => P)).
Notation "??" := (Unknown _).
Adam Chlipala's avatar
Adam Chlipala committed
576
Notation "[| x |]" := (Found _ x _).
577

Adam Chlipala's avatar
Adam Chlipala committed
578 579
(** Now our next version of [pred] is trivial to write. *)

580
Definition pred_strong7 : forall n : nat, {{m | n = S m}}.
Adam Chlipala's avatar
Adam Chlipala committed
581
  refine (fun n =>
582
    match n return {{m | n = S m}} with
Adam Chlipala's avatar
Adam Chlipala committed
583
      | O => ??
Adam Chlipala's avatar
Adam Chlipala committed
584
      | S n' => [|n'|]
Adam Chlipala's avatar
Adam Chlipala committed
585 586 587
    end); trivial.
Defined.

588 589
Eval compute in pred_strong7 2.
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
590
     = [|1|]
591
     : {{m | 2 = S m}}
Adam Chlipala's avatar
Adam Chlipala committed
592
      ]]
593
     *)
594 595 596 597 598 599 600

Eval compute in pred_strong7 0.
(** %\vspace{-.15in}% [[
     = ??
     : {{m | 0 = S m}}
     ]]

601
     Because we used [maybe], one valid implementation of the type we gave [pred_strong7] would return [??] in every case.  We can strengthen the type to rule out such vacuous implementations, and the type family %\index{Gallina terms!sumor}%[sumor] from the standard library provides the easiest starting point.  For type [A] and proposition [B], [A + {B}] desugars to [sumor A B], whose values are either values of [A] or proofs of [B]. *)
Adam Chlipala's avatar
Adam Chlipala committed
602 603

Print sumor.
Adam Chlipala's avatar
Adam Chlipala committed
604
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
605 606
Inductive sumor (A : Type) (B : Prop) : Type :=
    inleft : A -> A + {B} | inright : B -> A + {B}
607
]]
Adam Chlipala's avatar
Adam Chlipala committed
608

609
We add notations for easy use of the [sumor] constructors.  The second notation is specialized to [sumor]s whose [A] parameters are instantiated with regular subset types, since this is how we will use [sumor] below. *)
Adam Chlipala's avatar
Adam Chlipala committed
610 611

Notation "!!" := (inright _ _).
Adam Chlipala's avatar
Adam Chlipala committed
612
Notation "[|| x ||]" := (inleft _ [x]).
Adam Chlipala's avatar
Adam Chlipala committed
613

Adam Chlipala's avatar
Adam Chlipala committed
614
(** Now we are ready to give the final version of possibly failing predecessor.  The [sumor]-based type that we use is maximally expressive; any implementation of the type has the same input-output behavior. *)
Adam Chlipala's avatar
Adam Chlipala committed
615

616
Definition pred_strong8 : forall n : nat, {m : nat | n = S m} + {n = 0}.
Adam Chlipala's avatar
Adam Chlipala committed
617
  refine (fun n =>
Adam Chlipala's avatar
Adam Chlipala committed
618
    match n with
Adam Chlipala's avatar
Adam Chlipala committed
619
      | O => !!
Adam Chlipala's avatar
Adam Chlipala committed
620
      | S n' => [||n'||]
Adam Chlipala's avatar
Adam Chlipala committed
621 622 623
    end); trivial.
Defined.

624 625
Eval compute in pred_strong8 2.
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
626
     = [||1||]
627
     : {m : nat | 2 = S m} + {2 = 0}
628 629
     ]]
     *)
630 631 632 633 634

Eval compute in pred_strong8 0.
(** %\vspace{-.15in}% [[
     = !!
     : {m : nat | 0 = S m} + {0 = 0}
635 636
     ]]
     *)
637

Adam Chlipala's avatar
Adam Chlipala committed
638 639
(** As with our other maximally expressive [pred] function, we arrive at quite simple output values, thanks to notations. *)

Adam Chlipala's avatar
Adam Chlipala committed
640 641 642

(** * Monadic Notations *)

643
(** We can treat [maybe] like a monad%~\cite{Monads}\index{monad}\index{failure monad}%, in the same way that the Haskell <<Maybe>> type is interpreted as a failure monad.  Our [maybe] has the wrong type to be a literal monad, but a "bind"-like notation will still be helpful.  %Note that the notation definition uses an ASCII \texttt{<-}, while later code uses (in this rendering) a nicer left arrow $\leftarrow$.% *)
Adam Chlipala's avatar
Adam Chlipala committed
644

645 646 647 648 649 650
Notation "x <- e1 ; e2" := (match e1 with
                             | Unknown => ??
                             | Found x _ => e2
                           end)
(right associativity, at level 60).

651
(** The meaning of [x <- e1; e2] is: First run [e1].  If it fails to find an answer, then announce failure for our derived computation, too.  If [e1] _does_ find an answer, pass that answer on to [e2] to find the final result.  The variable [x] can be considered bound in [e2].
Adam Chlipala's avatar
Adam Chlipala committed
652

Adam Chlipala's avatar
Adam Chlipala committed
653
   This notation is very helpful for composing richly typed procedures.  For instance, here is a very simple implementation of a function to take the predecessors of two naturals at once. *)
Adam Chlipala's avatar
Adam Chlipala committed
654

655 656 657
(* EX: Write a function that tries to compute predecessors of two [nat]s at once. *)

(* begin thide *)
658
Definition doublePred : forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}.
Adam Chlipala's avatar
Adam Chlipala committed
659
  refine (fun n1 n2 =>
Adam Chlipala's avatar
Adam Chlipala committed
660 661
    m1 <- pred_strong7 n1;
    m2 <- pred_strong7 n2;
Adam Chlipala's avatar
Adam Chlipala committed
662
    [|(m1, m2)|]); tauto.
Adam Chlipala's avatar
Adam Chlipala committed
663
Defined.
664
(* end thide *)
Adam Chlipala's avatar
Adam Chlipala committed
665

666
(** We can build a [sumor] version of the "bind" notation and use it to write a similarly straightforward version of this function.  %Again, the notation definition exposes the ASCII syntax with an operator \texttt{<-{}-}, while the later code uses a nicer long left arrow $\longleftarrow$.% *)
Adam Chlipala's avatar
Adam Chlipala committed
667 668 669 670 671 672 673 674 675

Notation "x <-- e1 ; e2" := (match e1 with
                               | inright _ => !!
                               | inleft (exist x _) => e2
                             end)
(right associativity, at level 60).

(** printing * $\times$ *)

676 677 678
(* EX: Write a more expressively typed version of the last exercise. *)

(* begin thide *)
679 680
Definition doublePred' : forall n1 n2 : nat,
  {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)}
Adam Chlipala's avatar
Adam Chlipala committed
681 682
  + {n1 = 0 \/ n2 = 0}.
  refine (fun n1 n2 =>
Adam Chlipala's avatar
Adam Chlipala committed
683 684
    m1 <-- pred_strong8 n1;
    m2 <-- pred_strong8 n2;
Adam Chlipala's avatar
Adam Chlipala committed
685
    [||(m1, m2)||]); tauto.
Adam Chlipala's avatar
Adam Chlipala committed
686
Defined.
687
(* end thide *)
688

Adam Chlipala's avatar
Adam Chlipala committed
689 690
(** This example demonstrates how judicious selection of notations can hide complexities in the rich types of programs. *)

691 692 693

(** * A Type-Checking Example *)

Adam Chlipala's avatar
Adam Chlipala committed
694
(** We can apply these specification types to build a certified type checker for a simple expression language. *)
695

696 697 698 699 700 701
Inductive exp : Set :=
| Nat : nat -> exp
| Plus : exp -> exp -> exp
| Bool : bool -> exp
| And : exp -> exp -> exp.

702 703
(** We define a simple language of types and its typing rules, in the style introduced in Chapter 4. *)

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
Inductive type : Set := TNat | TBool.

Inductive hasType : exp -> type -> Prop :=
| HtNat : forall n,
  hasType (Nat n) TNat
| HtPlus : forall e1 e2,
  hasType e1 TNat
  -> hasType e2 TNat
  -> hasType (Plus e1 e2) TNat
| HtBool : forall b,
  hasType (Bool b) TBool
| HtAnd : forall e1 e2,
  hasType e1 TBool
  -> hasType e2 TBool
  -> hasType (And e1 e2) TBool.

720 721
(** It will be helpful to have a function for comparing two types.  We build one using [decide equality]. *)

Adam Chlipala's avatar
Adam Chlipala committed
722
(* begin thide *)
723
Definition eq_type_dec : forall t1 t2 : type, {t1 = t2} + {t1 <> t2}.
724 725 726
  decide equality.
Defined.

727
(** Another notation complements the monadic notation for [maybe] that we defined earlier.  Sometimes we want to include "assertions" in our procedures.  That is, we want to run a decision procedure and fail if it fails; otherwise, we want to continue, with the proof that it produced made available to us.  This infix notation captures that idea, for a procedure that returns an arbitrary two-constructor type. *)
728

Adam Chlipala's avatar
Adam Chlipala committed
729 730 731
Notation "e1 ;; e2" := (if e1 then e2 else ??)
  (right associativity, at level 60).

Adam Chlipala's avatar
Adam Chlipala committed
732
(** With that notation defined, we can implement a [typeCheck] function, whose code is only more complex than what we would write in ML because it needs to include some extra type annotations.  Every [[|e|]] expression adds a [hasType] proof obligation, and [crush] makes short work of them when we add [hasType]'s constructors as hints. *)
Adam Chlipala's avatar
Adam Chlipala committed
733
(* end thide *)
734

735
Definition typeCheck : forall e : exp, {{t | hasType e t}}.
Adam Chlipala's avatar
Adam Chlipala committed
736
(* begin thide *)
737 738 739
  Hint Constructors hasType.

  refine (fix F (e : exp) : {{t | hasType e t}} :=
740
    match e return {{t | hasType e t}} with
Adam Chlipala's avatar
Adam Chlipala committed
741
      | Nat _ => [|TNat|]
742 743 744 745 746
      | Plus e1 e2 =>
        t1 <- F e1;
        t2 <- F e2;
        eq_type_dec t1 TNat;;
        eq_type_dec t2 TNat;;
Adam Chlipala's avatar
Adam Chlipala committed
747 748
        [|TNat|]
      | Bool _ => [|TBool|]
749 750 751 752 753
      | And e1 e2 =>
        t1 <- F e1;
        t2 <- F e2;
        eq_type_dec t1 TBool;;
        eq_type_dec t2 TBool;;
Adam Chlipala's avatar
Adam Chlipala committed
754
        [|TBool|]
755
    end); crush.
Adam Chlipala's avatar
Adam Chlipala committed
756
(* end thide *)
757 758
Defined.

759 760
(** Despite manipulating proofs, our type checker is easy to run. *)

761
Eval simpl in typeCheck (Nat 0).
Adam Chlipala's avatar
Adam Chlipala committed
762
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
763
     = [|TNat|]
764
     : {{t | hasType (Nat 0) t}}
765 766
     ]]
     *)
767

768
Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)).
Adam Chlipala's avatar
Adam Chlipala committed
769
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
770
     = [|TNat|]
771
     : {{t | hasType (Plus (Nat 1) (Nat 2)) t}}
772 773
     ]]
     *)
774

775
Eval simpl in typeCheck (Plus (Nat 1) (Bool false)).
Adam Chlipala's avatar
Adam Chlipala committed
776
(** %\vspace{-.15in}% [[
777 778
     = ??
     : {{t | hasType (Plus (Nat 1) (Bool false)) t}}
779
     ]]
780

781
The type checker also extracts to some reasonable OCaml code. *)
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

Extraction typeCheck.

(** %\begin{verbatim}
(** val typeCheck : exp -> type0 maybe **)

let rec typeCheck = function
  | Nat n -> Found TNat
  | Plus (e1, e2) ->
      (match typeCheck e1 with
         | Unknown -> Unknown
         | Found t1 ->
             (match typeCheck e2 with
                | Unknown -> Unknown
                | Found t2 ->
                    (match eq_type_dec t1 TNat with
                       | true ->
                           (match eq_type_dec t2 TNat with
                              | true -> Found TNat
                              | false -> Unknown)
                       | false -> Unknown)))
  | Bool b -> Found TBool
  | And (e1, e2) ->
      (match typeCheck e1 with
         | Unknown -> Unknown
         | Found t1 ->
             (match typeCheck e2 with
                | Unknown -> Unknown
                | Found t2 ->
                    (match eq_type_dec t1 TBool with
                       | true ->
                           (match eq_type_dec t2 TBool with
                              | true -> Found TBool
                              | false -> Unknown)
                       | false -> Unknown)))
\end{verbatim}%

#<pre>
(** val typeCheck : exp -> type0 maybe **)

let rec typeCheck = function
  | Nat n -> Found TNat
  | Plus (e1, e2) ->
      (match typeCheck e1 with
         | Unknown -> Unknown
         | Found t1 ->
             (match typeCheck e2 with
                | Unknown -> Unknown
                | Found t2 ->
                    (match eq_type_dec t1 TNat with
                       | true ->
                           (match eq_type_dec t2 TNat with
                              | true -> Found TNat
                              | false -> Unknown)
                       | false -> Unknown)))
  | Bool b -> Found TBool
  | And (e1, e2) ->
      (match typeCheck e1 with
         | Unknown -> Unknown
         | Found t1 ->
             (match typeCheck e2 with
                | Unknown -> Unknown
                | Found t2 ->
                    (match eq_type_dec t1 TBool with
                       | true ->
                           (match eq_type_dec t2 TBool with
                              | true -> Found TBool
                              | false -> Unknown)
                       | false -> Unknown)))
</pre># *)

(** %\smallskip%

855
We can adapt this implementation to use [sumor], so that we know our type-checker only fails on ill-typed inputs.  First, we define an analogue to the "assertion" notation. *)
Adam Chlipala's avatar
Adam Chlipala committed
856

Adam Chlipala's avatar
Adam Chlipala committed
857
(* begin thide *)
Adam Chlipala's avatar
Adam Chlipala committed
858 859 860
Notation "e1 ;;; e2" := (if e1 then e2 else !!)
  (right associativity, at level 60).

861 862 863
(** Next, we prove a helpful lemma, which states that a given expression can have at most one type. *)

Lemma hasType_det : forall e t1,
Adam Chlipala's avatar
Adam Chlipala committed
864
  hasType e t1
Adam Chlipala's avatar
Adam Chlipala committed
865
  -> forall t2, hasType e t2
Adam Chlipala's avatar
Adam Chlipala committed
866 867 868 869
    -> t1 = t2.
  induction 1; inversion 1; crush.
Qed.

870 871
(** Now we can define the type-checker.  Its type expresses that it only fails on untypable expressions. *)

Adam Chlipala's avatar
Adam Chlipala committed
872
(* end thide *)
873
Definition typeCheck' : forall e : exp, {t : type | hasType e t} + {forall t, ~ hasType e t}.
Adam Chlipala's avatar
Adam Chlipala committed
874
(* begin thide *)
Adam Chlipala's avatar
Adam Chlipala committed
875
  Hint Constructors hasType.
876

877 878
  (** We register all of the typing rules as hints. *)

Adam Chlipala's avatar
Adam Chlipala committed
879
  Hint Resolve hasType_det.
880

Adam Chlipala's avatar
Adam Chlipala committed
881
  (** The lemma [hasType_det] will also be useful for proving proof obligations with contradictory contexts.  Since its statement includes [forall]-bound variables that do not appear in its conclusion, only [eauto] will apply this hint. *)
Adam Chlipala's avatar
Adam Chlipala committed
882

883
  (** Finally, the implementation of [typeCheck] can be transcribed literally, simply switching notations as needed. *)
Adam Chlipala's avatar
Adam Chlipala committed
884 885

  refine (fix F (e : exp) : {t : type | hasType e t} + {forall t, ~ hasType e t} :=
886
    match e return {t : type | hasType e t} + {forall t, ~ hasType e t} with
Adam Chlipala's avatar
Adam Chlipala committed
887
      | Nat _ => [||TNat||]
Adam Chlipala's avatar
Adam Chlipala committed
888 889 890 891 892
      | Plus e1 e2 =>
        t1 <-- F e1;
        t2 <-- F e2;
        eq_type_dec t1 TNat;;;
        eq_type_dec t2 TNat;;;
Adam Chlipala's avatar
Adam Chlipala committed
893 894
        [||TNat||]
      | Bool _ => [||TBool||]
Adam Chlipala's avatar
Adam Chlipala committed
895 896 897 898 899
      | And e1 e2 =>
        t1 <-- F e1;
        t2 <-- F e2;
        eq_type_dec t1 TBool;;;
        eq_type_dec t2 TBool;;;
Adam Chlipala's avatar
Adam Chlipala committed
900
        [||TBool||]
Adam Chlipala's avatar
Adam Chlipala committed
901
    end); clear F; crush' tt hasType; eauto.
902

903
  (** We clear [F], the local name for the recursive function, to avoid strange proofs that refer to recursive calls that we never make.  Such a step is usually warranted when defining a recursive function with [refine].  The [crush] variant %\index{tactics!crush'}%[crush'] helps us by performing automatic inversion on instances of the predicates specified in its second argument.  Once we throw in [eauto] to apply [hasType_det] for us, we have discharged all the subgoals. *)
Adam Chlipala's avatar
Adam Chlipala committed
904
(* end thide *)
Adam Chlipala's avatar
Adam Chlipala committed
905 906


Adam Chlipala's avatar
Adam Chlipala committed
907 908
Defined.

909 910
(** The short implementation here hides just how time-saving automation is.  Every use of one of the notations adds a proof obligation, giving us 12 in total.  Most of these obligations require multiple inversions and either uses of [hasType_det] or applications of [hasType] rules.

Adam Chlipala's avatar
Adam Chlipala committed
911
   Our new function remains easy to test: *)
912

Adam Chlipala's avatar
Adam Chlipala committed
913
Eval simpl in typeCheck' (Nat 0).
Adam Chlipala's avatar
Adam Chlipala committed
914
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
915
     = [||TNat||]
916 917
     : {t : type | hasType (Nat 0) t} +
       {(forall t : type, ~ hasType (Nat 0) t)}
918 919
       ]]
       *)
920

Adam Chlipala's avatar
Adam Chlipala committed
921
Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)).
Adam Chlipala's avatar
Adam Chlipala committed
922
(** %\vspace{-.15in}% [[
Adam Chlipala's avatar
Adam Chlipala committed
923
     = [||TNat||]
924 925
     : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} +
       {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)}
926 927
       ]]
       *)
928

Adam Chlipala's avatar
Adam Chlipala committed
929
Eval simpl in typeCheck' (Plus (Nat 1) (Bool false)).
Adam Chlipala's avatar
Adam Chlipala committed
930
(** %\vspace{-.15in}% [[
931 932 933
     = !!
     : {t : type | hasType (Plus (Nat 1) (Bool false)) t} +
       {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)}
934
       ]]
Adam Chlipala's avatar
Adam Chlipala committed
935

936
The results of simplifying calls to [typeCheck'] look deceptively similar to the results for [typeCheck], but now the types of the results provide more information. *)