annotate src/Subset.v @ 493:4a663981b699

Pass through Chapter 3
author Adam Chlipala <adam@chlipala.net>
date Fri, 18 Jan 2013 15:12:03 -0500
parents 1fd4109f7b31
children b7419a10e52e
rev   line source
adam@380 1 (* Copyright (c) 2008-2012, Adam Chlipala
adamc@70 2 *
adamc@70 3 * This work is licensed under a
adamc@70 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@70 5 * Unported License.
adamc@70 6 * The license text is available at:
adamc@70 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@70 8 *)
adamc@70 9
adamc@70 10 (* begin hide *)
adamc@70 11 Require Import List.
adamc@70 12
adam@314 13 Require Import CpdtTactics.
adamc@70 14
adamc@70 15 Set Implicit Arguments.
adamc@70 16 (* end hide *)
adamc@70 17
adam@403 18 (** printing <-- $\longleftarrow$ *)
adam@403 19
adamc@70 20
adamc@74 21 (** %\part{Programming with Dependent Types}
adamc@74 22
adamc@74 23 \chapter{Subset Types and Variations}% *)
adamc@70 24
adam@423 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. *)
adamc@70 26
adamc@70 27
adamc@70 28 (** * Introducing Subset Types *)
adamc@70 29
adamc@70 30 (** Let us consider several ways of implementing the natural number predecessor function. We start by displaying the definition from the standard library: *)
adamc@70 31
adamc@70 32 Print pred.
adamc@212 33 (** %\vspace{-.15in}% [[
adamc@70 34 pred = fun n : nat => match n with
adamc@70 35 | 0 => 0
adamc@70 36 | S u => u
adamc@70 37 end
adamc@70 38 : nat -> nat
adamc@212 39
adamc@212 40 ]]
adamc@70 41
adam@335 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. *)
adamc@70 43
adamc@70 44 Extraction pred.
adamc@70 45
adamc@70 46 (** %\begin{verbatim}
adamc@70 47 (** val pred : nat -> nat **)
adamc@70 48
adamc@70 49 let pred = function
adamc@70 50 | O -> O
adamc@70 51 | S u -> u
adamc@70 52 \end{verbatim}%
adamc@70 53
adamc@70 54 #<pre>
adamc@70 55 (** val pred : nat -> nat **)
adamc@70 56
adamc@70 57 let pred = function
adamc@70 58 | O -> O
adamc@70 59 | S u -> u
adamc@70 60 </pre># *)
adamc@70 61
adamc@70 62 (** 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. *)
adamc@70 63
adamc@70 64 Lemma zgtz : 0 > 0 -> False.
adamc@70 65 crush.
adamc@70 66 Qed.
adamc@70 67
adamc@70 68 Definition pred_strong1 (n : nat) : n > 0 -> nat :=
adamc@212 69 match n with
adamc@70 70 | O => fun pf : 0 > 0 => match zgtz pf with end
adamc@70 71 | S n' => fun _ => n'
adamc@70 72 end.
adamc@70 73
adam@398 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].
adamc@70 75
adam@398 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. *)
adam@282 77
adam@282 78 Theorem two_gt0 : 2 > 0.
adam@282 79 crush.
adam@282 80 Qed.
adam@282 81
adam@282 82 Eval compute in pred_strong1 two_gt0.
adam@282 83 (** %\vspace{-.15in}% [[
adam@282 84 = 1
adam@282 85 : nat
adam@282 86 ]]
adam@282 87
adam@442 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.
adamc@70 89
adam@440 90 %\vspace{-.15in}%[[
adamc@70 91 Definition pred_strong1' (n : nat) (pf : n > 0) : nat :=
adamc@70 92 match n with
adamc@70 93 | O => match zgtz pf with end
adamc@70 94 | S n' => n'
adamc@70 95 end.
adam@335 96 ]]
adamc@70 97
adam@335 98 <<
adamc@70 99 Error: In environment
adamc@70 100 n : nat
adamc@70 101 pf : n > 0
adamc@70 102 The term "pf" has type "n > 0" while it is expected to have type
adamc@70 103 "0 > 0"
adam@335 104 >>
adamc@70 105
adamc@212 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.
adamc@70 107
adam@398 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.
adamc@70 109
adam@471 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@335 111
adam@335 112 Definition pred_strong1' (n : nat) : n > 0 -> nat :=
adam@335 113 match n return n > 0 -> nat with
adam@335 114 | O => fun pf : 0 > 0 => match zgtz pf with end
adam@335 115 | S n' => fun _ => n'
adam@335 116 end.
adam@335 117
adam@403 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.
adamc@70 119
adamc@70 120 Let us now take a look at the OCaml code Coq generates for [pred_strong1]. *)
adamc@70 121
adamc@70 122 Extraction pred_strong1.
adamc@70 123
adamc@70 124 (** %\begin{verbatim}
adamc@70 125 (** val pred_strong1 : nat -> nat **)
adamc@70 126
adamc@70 127 let pred_strong1 = function
adamc@70 128 | O -> assert false (* absurd case *)
adamc@70 129 | S n' -> n'
adamc@70 130 \end{verbatim}%
adamc@70 131
adamc@70 132 #<pre>
adamc@70 133 (** val pred_strong1 : nat -> nat **)
adamc@70 134
adamc@70 135 let pred_strong1 = function
adamc@70 136 | O -> assert false (* absurd case *)
adamc@70 137 | S n' -> n'
adamc@70 138 </pre># *)
adamc@70 139
adam@451 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.
adamc@70 141
adam@471 142 %\medskip%
adam@471 143
adam@403 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]. *)
adamc@70 145
adam@423 146 (* begin hide *)
adam@437 147 (* begin thide *)
adam@437 148 Definition bar := ex.
adam@437 149 (* end thide *)
adam@423 150 (* end hide *)
adam@423 151
adamc@70 152 Print sig.
adamc@212 153 (** %\vspace{-.15in}% [[
adamc@70 154 Inductive sig (A : Type) (P : A -> Prop) : Type :=
adamc@70 155 exist : forall x : A, P x -> sig P
adamc@70 156 ]]
adamc@70 157
adam@442 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.
adamc@70 159
adamc@70 160 We rewrite [pred_strong1], using some syntactic sugar for subset types. *)
adamc@70 161
adamc@70 162 Locate "{ _ : _ | _ }".
adamc@212 163 (** %\vspace{-.15in}% [[
adam@335 164 Notation
adamc@70 165 "{ x : A | P }" := sig (fun x : A => P)
adam@302 166 ]]
adam@302 167 *)
adamc@70 168
adamc@70 169 Definition pred_strong2 (s : {n : nat | n > 0}) : nat :=
adamc@70 170 match s with
adamc@70 171 | exist O pf => match zgtz pf with end
adamc@70 172 | exist (S n') _ => n'
adamc@70 173 end.
adamc@70 174
adam@474 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). *)
adam@282 176
adam@282 177 Eval compute in pred_strong2 (exist _ 2 two_gt0).
adam@282 178 (** %\vspace{-.15in}% [[
adam@282 179 = 1
adam@282 180 : nat
adam@302 181 ]]
adam@302 182 *)
adam@282 183
adamc@70 184 Extraction pred_strong2.
adamc@70 185
adamc@70 186 (** %\begin{verbatim}
adamc@70 187 (** val pred_strong2 : nat -> nat **)
adamc@70 188
adamc@70 189 let pred_strong2 = function
adamc@70 190 | O -> assert false (* absurd case *)
adamc@70 191 | S n' -> n'
adamc@70 192 \end{verbatim}%
adamc@70 193
adamc@70 194 #<pre>
adamc@70 195 (** val pred_strong2 : nat -> nat **)
adamc@70 196
adamc@70 197 let pred_strong2 = function
adamc@70 198 | O -> assert false (* absurd case *)
adamc@70 199 | S n' -> n'
adamc@70 200 </pre>#
adamc@70 201
adamc@70 202 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.
adamc@70 203
adamc@70 204 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. *)
adamc@70 205
adamc@70 206 Definition pred_strong3 (s : {n : nat | n > 0}) : {m : nat | proj1_sig s = S m} :=
adamc@70 207 match s return {m : nat | proj1_sig s = S m} with
adamc@70 208 | exist 0 pf => match zgtz pf with end
adam@426 209 | exist (S n') pf => exist _ n' (eq_refl _)
adamc@70 210 end.
adamc@70 211
adam@282 212 Eval compute in pred_strong3 (exist _ 2 two_gt0).
adam@282 213 (** %\vspace{-.15in}% [[
adam@426 214 = exist (fun m : nat => 2 = S m) 1 (eq_refl 2)
adam@282 215 : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m}
adam@335 216 ]]
adam@302 217 *)
adam@282 218
adam@423 219 (* begin hide *)
adam@437 220 (* begin thide *)
adam@423 221 Definition pred_strong := 0.
adam@437 222 (* end thide *)
adam@423 223 (* end hide *)
adam@423 224
adam@474 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.
adamc@70 226
adamc@70 227 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. *)
adamc@70 228
adamc@70 229 Extraction pred_strong3.
adamc@70 230
adamc@70 231 (** %\begin{verbatim}
adamc@70 232 (** val pred_strong3 : nat -> nat **)
adamc@70 233
adamc@70 234 let pred_strong3 = function
adamc@70 235 | O -> assert false (* absurd case *)
adamc@70 236 | S n' -> n'
adamc@70 237 \end{verbatim}%
adamc@70 238
adamc@70 239 #<pre>
adamc@70 240 (** val pred_strong3 : nat -> nat **)
adamc@70 241
adamc@70 242 let pred_strong3 = function
adamc@70 243 | O -> assert false (* absurd case *)
adamc@70 244 | S n' -> n'
adamc@70 245 </pre>#
adamc@70 246
adam@335 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].) *)
adamc@70 248
adam@297 249 Definition pred_strong4 : forall n : nat, n > 0 -> {m : nat | n = S m}.
adamc@70 250 refine (fun n =>
adamc@212 251 match n with
adamc@70 252 | O => fun _ => False_rec _ _
adamc@70 253 | S n' => fun _ => exist _ n' _
adamc@70 254 end).
adamc@212 255
adamc@77 256 (* begin thide *)
adam@335 257 (** 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.
adamc@70 258
adam@423 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@335 260
adam@335 261 [[
adam@439 262 2 subgoals
adamc@70 263
adamc@70 264 n : nat
adamc@70 265 _ : 0 > 0
adamc@70 266 ============================
adamc@70 267 False
adam@439 268
adam@439 269 subgoal 2 is
adam@439 270
adamc@70 271 S n' = S n'
adamc@70 272 ]]
adamc@70 273
adamc@70 274 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. *)
adamc@70 275
adamc@70 276 Undo.
adamc@70 277 refine (fun n =>
adamc@212 278 match n with
adamc@70 279 | O => fun _ => False_rec _ _
adamc@70 280 | S n' => fun _ => exist _ n' _
adamc@70 281 end); crush.
adamc@77 282 (* end thide *)
adamc@70 283 Defined.
adamc@70 284
adam@423 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. *)
adamc@70 286
adamc@70 287 Print pred_strong4.
adamc@212 288 (** %\vspace{-.15in}% [[
adamc@70 289 pred_strong4 =
adamc@70 290 fun n : nat =>
adamc@70 291 match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with
adamc@70 292 | 0 =>
adamc@70 293 fun _ : 0 > 0 =>
adamc@70 294 False_rec {m : nat | 0 = S m}
adamc@70 295 (Bool.diff_false_true
adamc@70 296 (Bool.absurd_eq_true false
adamc@70 297 (Bool.diff_false_true
adamc@70 298 (Bool.absurd_eq_true false (pred_strong4_subproof n _)))))
adamc@70 299 | S n' =>
adamc@70 300 fun _ : S n' > 0 =>
adam@426 301 exist (fun m : nat => S n' = S m) n' (eq_refl (S n'))
adamc@70 302 end
adamc@70 303 : forall n : nat, n > 0 -> {m : nat | n = S m}
adamc@70 304 ]]
adamc@70 305
adam@442 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. *)
adamc@70 307
adam@282 308 Eval compute in pred_strong4 two_gt0.
adam@282 309 (** %\vspace{-.15in}% [[
adam@426 310 = exist (fun m : nat => 2 = S m) 1 (eq_refl 2)
adam@282 311 : {m : nat | 2 = S m}
adam@282 312 ]]
adam@282 313
adam@442 314 A tactic modifier called %\index{tactics!abstract}%[abstract] can be helpful for producing shorter terms, by automatically abstracting subgoals into named lemmas. *)
adam@335 315
adam@335 316 (* begin thide *)
adam@335 317 Definition pred_strong4' : forall n : nat, n > 0 -> {m : nat | n = S m}.
adam@335 318 refine (fun n =>
adam@335 319 match n with
adam@335 320 | O => fun _ => False_rec _ _
adam@335 321 | S n' => fun _ => exist _ n' _
adam@335 322 end); abstract crush.
adam@335 323 Defined.
adam@335 324
adam@335 325 Print pred_strong4'.
adam@335 326 (* end thide *)
adam@335 327
adam@335 328 (** %\vspace{-.15in}% [[
adam@335 329 pred_strong4' =
adam@335 330 fun n : nat =>
adam@335 331 match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with
adam@335 332 | 0 =>
adam@335 333 fun _H : 0 > 0 =>
adam@335 334 False_rec {m : nat | 0 = S m} (pred_strong4'_subproof n _H)
adam@335 335 | S n' =>
adam@335 336 fun _H : S n' > 0 =>
adam@335 337 exist (fun m : nat => S n' = S m) n' (pred_strong4'_subproof0 n _H)
adam@335 338 end
adam@335 339 : forall n : nat, n > 0 -> {m : nat | n = S m}
adam@335 340 ]]
adam@335 341
adam@338 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. *)
adamc@70 343
adamc@70 344 Notation "!" := (False_rec _ _).
adamc@70 345 Notation "[ e ]" := (exist _ e _).
adamc@70 346
adam@297 347 Definition pred_strong5 : forall n : nat, n > 0 -> {m : nat | n = S m}.
adamc@70 348 refine (fun n =>
adamc@212 349 match n with
adamc@70 350 | O => fun _ => !
adamc@70 351 | S n' => fun _ => [n']
adamc@70 352 end); crush.
adamc@70 353 Defined.
adamc@71 354
adam@282 355 (** By default, notations are also used in pretty-printing terms, including results of evaluation. *)
adam@282 356
adam@282 357 Eval compute in pred_strong5 two_gt0.
adam@282 358 (** %\vspace{-.15in}% [[
adam@282 359 = [1]
adam@282 360 : {m : nat | 2 = S m}
adam@282 361 ]]
adam@282 362
adam@442 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}% *)
adamc@212 364
adamc@212 365 Obligation Tactic := crush.
adamc@212 366
adamc@212 367 Program Definition pred_strong6 (n : nat) (_ : n > 0) : {m : nat | n = S m} :=
adamc@212 368 match n with
adamc@212 369 | O => _
adamc@212 370 | S n' => n'
adamc@212 371 end.
adamc@212 372
adam@335 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. *)
adamc@212 374
adam@282 375 Eval compute in pred_strong6 two_gt0.
adam@282 376 (** %\vspace{-.15in}% [[
adam@282 377 = [1]
adam@282 378 : {m : nat | 2 = S m}
adam@302 379 ]]
adam@335 380
adam@442 381 In this case, we see that the new definition yields the same computational behavior as before. *)
adam@282 382
adamc@71 383
adamc@71 384 (** * Decidable Proposition Types *)
adamc@71 385
adam@335 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}% *)
adamc@71 387
adamc@71 388 Print sumbool.
adamc@212 389 (** %\vspace{-.15in}% [[
adamc@71 390 Inductive sumbool (A : Prop) (B : Prop) : Set :=
adamc@71 391 left : A -> {A} + {B} | right : B -> {A} + {B}
adamc@212 392 ]]
adamc@71 393
adam@471 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. *)
adamc@71 395
adamc@71 396 Notation "'Yes'" := (left _ _).
adamc@71 397 Notation "'No'" := (right _ _).
adamc@71 398 Notation "'Reduce' x" := (if x then Yes else No) (at level 50).
adamc@71 399
adam@436 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.
adamc@71 401
adamc@71 402 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. *)
adamc@71 403
adam@297 404 Definition eq_nat_dec : forall n m : nat, {n = m} + {n <> m}.
adamc@212 405 refine (fix f (n m : nat) : {n = m} + {n <> m} :=
adamc@212 406 match n, m with
adamc@71 407 | O, O => Yes
adamc@71 408 | S n', S m' => Reduce (f n' m')
adamc@71 409 | _, _ => No
adamc@71 410 end); congruence.
adamc@71 411 Defined.
adamc@71 412
adam@282 413 Eval compute in eq_nat_dec 2 2.
adam@282 414 (** %\vspace{-.15in}% [[
adam@282 415 = Yes
adam@282 416 : {2 = 2} + {2 <> 2}
adam@302 417 ]]
adam@302 418 *)
adam@282 419
adam@282 420 Eval compute in eq_nat_dec 2 3.
adam@282 421 (** %\vspace{-.15in}% [[
adam@282 422 = No
adam@341 423 : {2 = 3} + {2 <> 3}
adam@302 424 ]]
adam@282 425
adam@442 426 Note that the %\coqdocnotation{%#<tt>#Yes#</tt>#%}% and %\coqdocnotation{%#<tt>#No#</tt>#%}% notations are hiding proofs establishing the correctness of the outputs.
adam@335 427
adam@335 428 Our definition extracts to reasonable OCaml code. *)
adamc@71 429
adamc@71 430 Extraction eq_nat_dec.
adamc@71 431
adamc@71 432 (** %\begin{verbatim}
adamc@71 433 (** val eq_nat_dec : nat -> nat -> sumbool **)
adamc@71 434
adamc@71 435 let rec eq_nat_dec n m =
adamc@71 436 match n with
adamc@71 437 | O -> (match m with
adamc@71 438 | O -> Left
adamc@71 439 | S n0 -> Right)
adamc@71 440 | S n' -> (match m with
adamc@71 441 | O -> Right
adamc@71 442 | S m' -> eq_nat_dec n' m')
adamc@71 443 \end{verbatim}%
adamc@71 444
adamc@71 445 #<pre>
adamc@71 446 (** val eq_nat_dec : nat -> nat -> sumbool **)
adamc@71 447
adamc@71 448 let rec eq_nat_dec n m =
adamc@71 449 match n with
adamc@71 450 | O -> (match m with
adamc@71 451 | O -> Left
adamc@71 452 | S n0 -> Right)
adamc@71 453 | S n' -> (match m with
adamc@71 454 | O -> Right
adamc@71 455 | S m' -> eq_nat_dec n' m')
adamc@71 456 </pre>#
adamc@71 457
adam@335 458 Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it.%\index{tactics!decide equality}% *)
adamc@71 459
adamc@71 460 Definition eq_nat_dec' (n m : nat) : {n = m} + {n <> m}.
adamc@71 461 decide equality.
adamc@71 462 Defined.
adamc@71 463
adam@448 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}% *)
adamc@71 465
adamc@71 466 Extract Inductive sumbool => "bool" ["true" "false"].
adamc@71 467 Extraction eq_nat_dec'.
adamc@71 468
adamc@71 469 (** %\begin{verbatim}
adamc@71 470 (** val eq_nat_dec' : nat -> nat -> bool **)
adamc@71 471
adamc@71 472 let rec eq_nat_dec' n m0 =
adamc@71 473 match n with
adamc@71 474 | O -> (match m0 with
adamc@71 475 | O -> true
adamc@71 476 | S n0 -> false)
adamc@71 477 | S n0 -> (match m0 with
adamc@71 478 | O -> false
adamc@71 479 | S n1 -> eq_nat_dec' n0 n1)
adamc@71 480 \end{verbatim}%
adamc@71 481
adamc@71 482 #<pre>
adamc@71 483 (** val eq_nat_dec' : nat -> nat -> bool **)
adamc@71 484
adamc@71 485 let rec eq_nat_dec' n m0 =
adamc@71 486 match n with
adamc@71 487 | O -> (match m0 with
adamc@71 488 | O -> true
adamc@71 489 | S n0 -> false)
adamc@71 490 | S n0 -> (match m0 with
adamc@71 491 | O -> false
adamc@71 492 | S n1 -> eq_nat_dec' n0 n1)
adamc@71 493 </pre># *)
adamc@72 494
adamc@72 495 (** %\smallskip%
adamc@72 496
adam@448 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." *)
adamc@72 498
adam@337 499 (* EX: Write a function that decides if an element belongs to a list. *)
adam@337 500
adamc@77 501 (* begin thide *)
adamc@204 502 Notation "x || y" := (if x then Yes else Reduce y).
adamc@72 503
adamc@72 504 (** 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. *)
adamc@72 505
adamc@72 506 Section In_dec.
adamc@72 507 Variable A : Set.
adamc@72 508 Variable A_eq_dec : forall x y : A, {x = y} + {x <> y}.
adamc@72 509
adamc@72 510 (** The final function is easy to write using the techniques we have developed so far. *)
adamc@72 511
adamc@212 512 Definition In_dec : forall (x : A) (ls : list A), {In x ls} + {~ In x ls}.
adamc@212 513 refine (fix f (x : A) (ls : list A) : {In x ls} + {~ In x ls} :=
adamc@212 514 match ls with
adamc@72 515 | nil => No
adamc@72 516 | x' :: ls' => A_eq_dec x x' || f x ls'
adamc@72 517 end); crush.
adam@282 518 Defined.
adamc@72 519 End In_dec.
adamc@72 520
adam@282 521 Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil).
adam@282 522 (** %\vspace{-.15in}% [[
adam@282 523 = Yes
adam@469 524 : {In 2 (1 :: 2 :: nil)} + { ~ In 2 (1 :: 2 :: nil)}
adam@302 525 ]]
adam@302 526 *)
adam@282 527
adam@282 528 Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil).
adam@282 529 (** %\vspace{-.15in}% [[
adam@282 530 = No
adam@469 531 : {In 3 (1 :: 2 :: nil)} + { ~ In 3 (1 :: 2 :: nil)}
adam@302 532 ]]
adam@282 533
adam@469 534 The [In_dec] function has a reasonable extraction to OCaml. *)
adamc@72 535
adamc@72 536 Extraction In_dec.
adamc@77 537 (* end thide *)
adamc@72 538
adamc@72 539 (** %\begin{verbatim}
adamc@72 540 (** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)
adamc@72 541
adamc@72 542 let rec in_dec a_eq_dec x = function
adamc@72 543 | Nil -> false
adamc@72 544 | Cons (x', ls') ->
adamc@72 545 (match a_eq_dec x x' with
adamc@72 546 | true -> true
adamc@72 547 | false -> in_dec a_eq_dec x ls')
adamc@72 548 \end{verbatim}%
adamc@72 549
adamc@72 550 #<pre>
adamc@72 551 (** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)
adamc@72 552
adamc@72 553 let rec in_dec a_eq_dec x = function
adamc@72 554 | Nil -> false
adamc@72 555 | Cons (x', ls') ->
adamc@72 556 (match a_eq_dec x x' with
adamc@72 557 | true -> true
adamc@72 558 | false -> in_dec a_eq_dec x ls')
adam@403 559 </pre>#
adam@403 560
adam@403 561 This is more or the less code for the corresponding function from the OCaml standard library. *)
adamc@72 562
adamc@72 563
adamc@72 564 (** * Partial Subset Types *)
adamc@72 565
adam@335 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. *)
adamc@73 567
adamc@89 568 Inductive maybe (A : Set) (P : A -> Prop) : Set :=
adamc@72 569 | Unknown : maybe P
adamc@72 570 | Found : forall x : A, P x -> maybe P.
adamc@72 571
adamc@73 572 (** We can define some new notations, analogous to those we defined for subset types. *)
adamc@73 573
adamc@72 574 Notation "{{ x | P }}" := (maybe (fun x => P)).
adamc@72 575 Notation "??" := (Unknown _).
adam@335 576 Notation "[| x |]" := (Found _ x _).
adamc@72 577
adamc@73 578 (** Now our next version of [pred] is trivial to write. *)
adamc@73 579
adam@297 580 Definition pred_strong7 : forall n : nat, {{m | n = S m}}.
adamc@73 581 refine (fun n =>
adam@380 582 match n return {{m | n = S m}} with
adamc@73 583 | O => ??
adam@335 584 | S n' => [|n'|]
adamc@73 585 end); trivial.
adamc@73 586 Defined.
adamc@73 587
adam@282 588 Eval compute in pred_strong7 2.
adam@282 589 (** %\vspace{-.15in}% [[
adam@335 590 = [|1|]
adam@282 591 : {{m | 2 = S m}}
adam@335 592 ]]
adam@302 593 *)
adam@282 594
adam@282 595 Eval compute in pred_strong7 0.
adam@282 596 (** %\vspace{-.15in}% [[
adam@282 597 = ??
adam@282 598 : {{m | 0 = S m}}
adam@282 599 ]]
adam@282 600
adam@442 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]. *)
adamc@73 602
adamc@73 603 Print sumor.
adamc@212 604 (** %\vspace{-.15in}% [[
adamc@73 605 Inductive sumor (A : Type) (B : Prop) : Type :=
adamc@73 606 inleft : A -> A + {B} | inright : B -> A + {B}
adam@302 607 ]]
adamc@73 608
adam@442 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. *)
adamc@73 610
adamc@73 611 Notation "!!" := (inright _ _).
adam@335 612 Notation "[|| x ||]" := (inleft _ [x]).
adamc@73 613
adam@335 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. *)
adamc@73 615
adam@297 616 Definition pred_strong8 : forall n : nat, {m : nat | n = S m} + {n = 0}.
adamc@73 617 refine (fun n =>
adamc@212 618 match n with
adamc@73 619 | O => !!
adam@335 620 | S n' => [||n'||]
adamc@73 621 end); trivial.
adamc@73 622 Defined.
adamc@73 623
adam@282 624 Eval compute in pred_strong8 2.
adam@282 625 (** %\vspace{-.15in}% [[
adam@335 626 = [||1||]
adam@282 627 : {m : nat | 2 = S m} + {2 = 0}
adam@302 628 ]]
adam@302 629 *)
adam@282 630
adam@282 631 Eval compute in pred_strong8 0.
adam@282 632 (** %\vspace{-.15in}% [[
adam@282 633 = !!
adam@282 634 : {m : nat | 0 = S m} + {0 = 0}
adam@302 635 ]]
adam@302 636 *)
adam@282 637
adam@335 638 (** As with our other maximally expressive [pred] function, we arrive at quite simple output values, thanks to notations. *)
adam@335 639
adamc@73 640
adamc@73 641 (** * Monadic Notations *)
adamc@73 642
adam@471 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$.% *)
adamc@73 644
adamc@72 645 Notation "x <- e1 ; e2" := (match e1 with
adamc@72 646 | Unknown => ??
adamc@72 647 | Found x _ => e2
adamc@72 648 end)
adamc@72 649 (right associativity, at level 60).
adamc@72 650
adam@398 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].
adamc@73 652
adam@335 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. *)
adamc@73 654
adam@337 655 (* EX: Write a function that tries to compute predecessors of two [nat]s at once. *)
adam@337 656
adam@337 657 (* begin thide *)
adam@297 658 Definition doublePred : forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}.
adamc@73 659 refine (fun n1 n2 =>
adamc@212 660 m1 <- pred_strong7 n1;
adamc@212 661 m2 <- pred_strong7 n2;
adam@335 662 [|(m1, m2)|]); tauto.
adamc@73 663 Defined.
adam@337 664 (* end thide *)
adamc@73 665
adam@471 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$.% *)
adamc@73 667
adamc@73 668 Notation "x <-- e1 ; e2" := (match e1 with
adamc@73 669 | inright _ => !!
adamc@73 670 | inleft (exist x _) => e2
adamc@73 671 end)
adamc@73 672 (right associativity, at level 60).
adamc@73 673
adamc@73 674 (** printing * $\times$ *)
adamc@73 675
adam@337 676 (* EX: Write a more expressively typed version of the last exercise. *)
adam@337 677
adam@337 678 (* begin thide *)
adam@297 679 Definition doublePred' : forall n1 n2 : nat,
adam@297 680 {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)}
adamc@73 681 + {n1 = 0 \/ n2 = 0}.
adamc@73 682 refine (fun n1 n2 =>
adamc@212 683 m1 <-- pred_strong8 n1;
adamc@212 684 m2 <-- pred_strong8 n2;
adam@335 685 [||(m1, m2)||]); tauto.
adamc@73 686 Defined.
adam@337 687 (* end thide *)
adamc@72 688
adam@392 689 (** This example demonstrates how judicious selection of notations can hide complexities in the rich types of programs. *)
adam@392 690
adamc@72 691
adamc@72 692 (** * A Type-Checking Example *)
adamc@72 693
adam@335 694 (** We can apply these specification types to build a certified type checker for a simple expression language. *)
adamc@75 695
adamc@72 696 Inductive exp : Set :=
adamc@72 697 | Nat : nat -> exp
adamc@72 698 | Plus : exp -> exp -> exp
adamc@72 699 | Bool : bool -> exp
adamc@72 700 | And : exp -> exp -> exp.
adamc@72 701
adamc@75 702 (** We define a simple language of types and its typing rules, in the style introduced in Chapter 4. *)
adamc@75 703
adamc@72 704 Inductive type : Set := TNat | TBool.
adamc@72 705
adamc@72 706 Inductive hasType : exp -> type -> Prop :=
adamc@72 707 | HtNat : forall n,
adamc@72 708 hasType (Nat n) TNat
adamc@72 709 | HtPlus : forall e1 e2,
adamc@72 710 hasType e1 TNat
adamc@72 711 -> hasType e2 TNat
adamc@72 712 -> hasType (Plus e1 e2) TNat
adamc@72 713 | HtBool : forall b,
adamc@72 714 hasType (Bool b) TBool
adamc@72 715 | HtAnd : forall e1 e2,
adamc@72 716 hasType e1 TBool
adamc@72 717 -> hasType e2 TBool
adamc@72 718 -> hasType (And e1 e2) TBool.
adamc@72 719
adamc@75 720 (** It will be helpful to have a function for comparing two types. We build one using [decide equality]. *)
adamc@75 721
adamc@77 722 (* begin thide *)
adamc@75 723 Definition eq_type_dec : forall t1 t2 : type, {t1 = t2} + {t1 <> t2}.
adamc@72 724 decide equality.
adamc@72 725 Defined.
adamc@72 726
adam@423 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. *)
adamc@75 728
adamc@73 729 Notation "e1 ;; e2" := (if e1 then e2 else ??)
adamc@73 730 (right associativity, at level 60).
adamc@73 731
adam@335 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. *)
adamc@77 733 (* end thide *)
adamc@75 734
adam@297 735 Definition typeCheck : forall e : exp, {{t | hasType e t}}.
adamc@77 736 (* begin thide *)
adamc@72 737 Hint Constructors hasType.
adamc@72 738
adamc@72 739 refine (fix F (e : exp) : {{t | hasType e t}} :=
adam@380 740 match e return {{t | hasType e t}} with
adam@335 741 | Nat _ => [|TNat|]
adamc@72 742 | Plus e1 e2 =>
adamc@72 743 t1 <- F e1;
adamc@72 744 t2 <- F e2;
adamc@72 745 eq_type_dec t1 TNat;;
adamc@72 746 eq_type_dec t2 TNat;;
adam@335 747 [|TNat|]
adam@335 748 | Bool _ => [|TBool|]
adamc@72 749 | And e1 e2 =>
adamc@72 750 t1 <- F e1;
adamc@72 751 t2 <- F e2;
adamc@72 752 eq_type_dec t1 TBool;;
adamc@72 753 eq_type_dec t2 TBool;;
adam@335 754 [|TBool|]
adamc@72 755 end); crush.
adamc@77 756 (* end thide *)
adamc@72 757 Defined.
adamc@72 758
adamc@75 759 (** Despite manipulating proofs, our type checker is easy to run. *)
adamc@75 760
adamc@72 761 Eval simpl in typeCheck (Nat 0).
adamc@212 762 (** %\vspace{-.15in}% [[
adam@335 763 = [|TNat|]
adamc@75 764 : {{t | hasType (Nat 0) t}}
adam@302 765 ]]
adam@302 766 *)
adamc@75 767
adamc@72 768 Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)).
adamc@212 769 (** %\vspace{-.15in}% [[
adam@335 770 = [|TNat|]
adamc@75 771 : {{t | hasType (Plus (Nat 1) (Nat 2)) t}}
adam@302 772 ]]
adam@302 773 *)
adamc@75 774
adamc@72 775 Eval simpl in typeCheck (Plus (Nat 1) (Bool false)).
adamc@212 776 (** %\vspace{-.15in}% [[
adamc@75 777 = ??
adamc@75 778 : {{t | hasType (Plus (Nat 1) (Bool false)) t}}
adam@302 779 ]]
adamc@75 780
adam@442 781 The type checker also extracts to some reasonable OCaml code. *)
adamc@75 782
adamc@75 783 Extraction typeCheck.
adamc@75 784
adamc@75 785 (** %\begin{verbatim}
adamc@75 786 (** val typeCheck : exp -> type0 maybe **)
adamc@75 787
adamc@75 788 let rec typeCheck = function
adamc@75 789 | Nat n -> Found TNat
adamc@75 790 | Plus (e1, e2) ->
adamc@75 791 (match typeCheck e1 with
adamc@75 792 | Unknown -> Unknown
adamc@75 793 | Found t1 ->
adamc@75 794 (match typeCheck e2 with
adamc@75 795 | Unknown -> Unknown
adamc@75 796 | Found t2 ->
adamc@75 797 (match eq_type_dec t1 TNat with
adamc@75 798 | true ->
adamc@75 799 (match eq_type_dec t2 TNat with
adamc@75 800 | true -> Found TNat
adamc@75 801 | false -> Unknown)
adamc@75 802 | false -> Unknown)))
adamc@75 803 | Bool b -> Found TBool
adamc@75 804 | And (e1, e2) ->
adamc@75 805 (match typeCheck e1 with
adamc@75 806 | Unknown -> Unknown
adamc@75 807 | Found t1 ->
adamc@75 808 (match typeCheck e2 with
adamc@75 809 | Unknown -> Unknown
adamc@75 810 | Found t2 ->
adamc@75 811 (match eq_type_dec t1 TBool with
adamc@75 812 | true ->
adamc@75 813 (match eq_type_dec t2 TBool with
adamc@75 814 | true -> Found TBool
adamc@75 815 | false -> Unknown)
adamc@75 816 | false -> Unknown)))
adamc@75 817 \end{verbatim}%
adamc@75 818
adamc@75 819 #<pre>
adamc@75 820 (** val typeCheck : exp -> type0 maybe **)
adamc@75 821
adamc@75 822 let rec typeCheck = function
adamc@75 823 | Nat n -> Found TNat
adamc@75 824 | Plus (e1, e2) ->
adamc@75 825 (match typeCheck e1 with
adamc@75 826 | Unknown -> Unknown
adamc@75 827 | Found t1 ->
adamc@75 828 (match typeCheck e2 with
adamc@75 829 | Unknown -> Unknown
adamc@75 830 | Found t2 ->
adamc@75 831 (match eq_type_dec t1 TNat with
adamc@75 832 | true ->
adamc@75 833 (match eq_type_dec t2 TNat with
adamc@75 834 | true -> Found TNat
adamc@75 835 | false -> Unknown)
adamc@75 836 | false -> Unknown)))
adamc@75 837 | Bool b -> Found TBool
adamc@75 838 | And (e1, e2) ->
adamc@75 839 (match typeCheck e1 with
adamc@75 840 | Unknown -> Unknown
adamc@75 841 | Found t1 ->
adamc@75 842 (match typeCheck e2 with
adamc@75 843 | Unknown -> Unknown
adamc@75 844 | Found t2 ->
adamc@75 845 (match eq_type_dec t1 TBool with
adamc@75 846 | true ->
adamc@75 847 (match eq_type_dec t2 TBool with
adamc@75 848 | true -> Found TBool
adamc@75 849 | false -> Unknown)
adamc@75 850 | false -> Unknown)))
adamc@75 851 </pre># *)
adamc@75 852
adamc@75 853 (** %\smallskip%
adamc@75 854
adam@423 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. *)
adamc@73 856
adamc@77 857 (* begin thide *)
adamc@73 858 Notation "e1 ;;; e2" := (if e1 then e2 else !!)
adamc@73 859 (right associativity, at level 60).
adamc@73 860
adamc@75 861 (** Next, we prove a helpful lemma, which states that a given expression can have at most one type. *)
adamc@75 862
adamc@75 863 Lemma hasType_det : forall e t1,
adamc@73 864 hasType e t1
adam@335 865 -> forall t2, hasType e t2
adamc@73 866 -> t1 = t2.
adamc@73 867 induction 1; inversion 1; crush.
adamc@73 868 Qed.
adamc@73 869
adamc@75 870 (** Now we can define the type-checker. Its type expresses that it only fails on untypable expressions. *)
adamc@75 871
adamc@77 872 (* end thide *)
adam@297 873 Definition typeCheck' : forall e : exp, {t : type | hasType e t} + {forall t, ~ hasType e t}.
adamc@77 874 (* begin thide *)
adamc@73 875 Hint Constructors hasType.
adam@475 876
adamc@75 877 (** We register all of the typing rules as hints. *)
adamc@75 878
adamc@73 879 Hint Resolve hasType_det.
adam@475 880
adam@335 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. *)
adamc@73 882
adamc@75 883 (** Finally, the implementation of [typeCheck] can be transcribed literally, simply switching notations as needed. *)
adamc@212 884
adamc@212 885 refine (fix F (e : exp) : {t : type | hasType e t} + {forall t, ~ hasType e t} :=
adam@380 886 match e return {t : type | hasType e t} + {forall t, ~ hasType e t} with
adam@335 887 | Nat _ => [||TNat||]
adamc@73 888 | Plus e1 e2 =>
adamc@73 889 t1 <-- F e1;
adamc@73 890 t2 <-- F e2;
adamc@73 891 eq_type_dec t1 TNat;;;
adamc@73 892 eq_type_dec t2 TNat;;;
adam@335 893 [||TNat||]
adam@335 894 | Bool _ => [||TBool||]
adamc@73 895 | And e1 e2 =>
adamc@73 896 t1 <-- F e1;
adamc@73 897 t2 <-- F e2;
adamc@73 898 eq_type_dec t1 TBool;;;
adamc@73 899 eq_type_dec t2 TBool;;;
adam@335 900 [||TBool||]
adamc@73 901 end); clear F; crush' tt hasType; eauto.
adamc@75 902
adam@471 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. *)
adamc@77 904 (* end thide *)
adamc@212 905
adamc@212 906
adamc@73 907 Defined.
adamc@73 908
adamc@75 909 (** 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.
adamc@75 910
adam@335 911 Our new function remains easy to test: *)
adamc@75 912
adamc@73 913 Eval simpl in typeCheck' (Nat 0).
adamc@212 914 (** %\vspace{-.15in}% [[
adam@335 915 = [||TNat||]
adamc@75 916 : {t : type | hasType (Nat 0) t} +
adamc@75 917 {(forall t : type, ~ hasType (Nat 0) t)}
adam@302 918 ]]
adam@302 919 *)
adamc@75 920
adamc@73 921 Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)).
adamc@212 922 (** %\vspace{-.15in}% [[
adam@335 923 = [||TNat||]
adamc@75 924 : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} +
adamc@75 925 {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)}
adam@302 926 ]]
adam@302 927 *)
adamc@75 928
adamc@73 929 Eval simpl in typeCheck' (Plus (Nat 1) (Bool false)).
adamc@212 930 (** %\vspace{-.15in}% [[
adamc@75 931 = !!
adamc@75 932 : {t : type | hasType (Plus (Nat 1) (Bool false)) t} +
adamc@75 933 {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)}
adam@302 934 ]]
adam@335 935
adam@442 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. *)