annotate src/Equality.v @ 481:c4a22c9ff090

Link to Pierce's Fall 2012 class
author Adam Chlipala <adam@chlipala.net>
date Fri, 30 Nov 2012 12:07:46 -0500
parents f38a3af9dd17
children 5025a401ad9e
rev   line source
adam@398 1 (* Copyright (c) 2008-2012, Adam Chlipala
adamc@118 2 *
adamc@118 3 * This work is licensed under a
adamc@118 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@118 5 * Unported License.
adamc@118 6 * The license text is available at:
adamc@118 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@118 8 *)
adamc@118 9
adamc@118 10 (* begin hide *)
adamc@120 11 Require Import Eqdep JMeq List.
adamc@118 12
adam@314 13 Require Import CpdtTactics.
adamc@118 14
adamc@118 15 Set Implicit Arguments.
adamc@118 16 (* end hide *)
adamc@118 17
adamc@118 18
adamc@118 19 (** %\chapter{Reasoning About Equality Proofs}% *)
adamc@118 20
adam@456 21 (** In traditional mathematics, the concept of equality is usually taken as a given. On the other hand, in type theory, equality is a very contentious subject. There are at least three different notions of equality that are important in Coq, and researchers are actively investigating new definitions of what it means for two terms to be equal. Even once we fix a notion of equality, there are inevitably tricky issues that arise in proving properties of programs that manipulate equality proofs explicitly. In this chapter, I will focus on design patterns for circumventing these tricky issues, and I will introduce the different notions of equality as they are germane. *)
adamc@118 22
adamc@118 23
adamc@122 24 (** * The Definitional Equality *)
adamc@122 25
adam@435 26 (** We have seen many examples so far where proof goals follow "by computation." That is, we apply computational reduction rules to reduce the goal to a normal form, at which point it follows trivially. Exactly when this works and when it does not depends on the details of Coq's%\index{definitional equality}% _definitional equality_. This is an untyped binary relation appearing in the formal metatheory of CIC. CIC contains a typing rule allowing the conclusion [E : T] from the premise [E : T'] and a proof that [T] and [T'] are definitionally equal.
adamc@122 27
adam@364 28 The %\index{tactics!cbv}%[cbv] tactic will help us illustrate the rules of Coq's definitional equality. We redefine the natural number predecessor function in a somewhat convoluted way and construct a manual proof that it returns [0] when applied to [1]. *)
adamc@122 29
adamc@122 30 Definition pred' (x : nat) :=
adamc@122 31 match x with
adamc@122 32 | O => O
adamc@122 33 | S n' => let y := n' in y
adamc@122 34 end.
adamc@122 35
adamc@122 36 Theorem reduce_me : pred' 1 = 0.
adamc@218 37
adamc@124 38 (* begin thide *)
adam@364 39 (** CIC follows the traditions of lambda calculus in associating reduction rules with Greek letters. Coq can certainly be said to support the familiar alpha reduction rule, which allows capture-avoiding renaming of bound variables, but we never need to apply alpha explicitly, since Coq uses a de Bruijn representation%~\cite{DeBruijn}% that encodes terms canonically.
adamc@122 40
adam@364 41 The %\index{delta reduction}%delta rule is for unfolding global definitions. We can use it here to unfold the definition of [pred']. We do this with the [cbv] tactic, which takes a list of reduction rules and makes as many call-by-value reduction steps as possible, using only those rules. There is an analogous tactic %\index{tactics!lazy}%[lazy] for call-by-need reduction. *)
adamc@122 42
adamc@122 43 cbv delta.
adam@364 44 (** %\vspace{-.15in}%[[
adamc@122 45 ============================
adamc@122 46 (fun x : nat => match x with
adamc@122 47 | 0 => 0
adamc@122 48 | S n' => let y := n' in y
adamc@122 49 end) 1 = 0
adamc@122 50 ]]
adamc@122 51
adam@364 52 At this point, we want to apply the famous %\index{beta reduction}%beta reduction of lambda calculus, to simplify the application of a known function abstraction. *)
adamc@122 53
adamc@122 54 cbv beta.
adam@364 55 (** %\vspace{-.15in}%[[
adamc@122 56 ============================
adamc@122 57 match 1 with
adamc@122 58 | 0 => 0
adamc@122 59 | S n' => let y := n' in y
adamc@122 60 end = 0
adamc@122 61 ]]
adamc@122 62
adam@364 63 Next on the list is the %\index{iota reduction}%iota reduction, which simplifies a single [match] term by determining which pattern matches. *)
adamc@122 64
adamc@122 65 cbv iota.
adam@364 66 (** %\vspace{-.15in}%[[
adamc@122 67 ============================
adamc@122 68 (fun n' : nat => let y := n' in y) 0 = 0
adamc@122 69 ]]
adamc@122 70
adamc@122 71 Now we need another beta reduction. *)
adamc@122 72
adamc@122 73 cbv beta.
adam@364 74 (** %\vspace{-.15in}%[[
adamc@122 75 ============================
adamc@122 76 (let y := 0 in y) = 0
adamc@122 77 ]]
adamc@122 78
adam@364 79 The final reduction rule is %\index{zeta reduction}%zeta, which replaces a [let] expression by its body with the appropriate term substituted. *)
adamc@122 80
adamc@122 81 cbv zeta.
adam@364 82 (** %\vspace{-.15in}%[[
adamc@122 83 ============================
adamc@122 84 0 = 0
adam@302 85 ]]
adam@302 86 *)
adamc@122 87
adamc@122 88 reflexivity.
adamc@122 89 Qed.
adamc@124 90 (* end thide *)
adamc@122 91
adam@365 92 (** The [beta] reduction rule applies to recursive functions as well, and its behavior may be surprising in some instances. For instance, we can run some simple tests using the reduction strategy [compute], which applies all applicable rules of the definitional equality. *)
adam@365 93
adam@365 94 Definition id (n : nat) := n.
adam@365 95
adam@365 96 Eval compute in fun x => id x.
adam@365 97 (** %\vspace{-.15in}%[[
adam@365 98 = fun x : nat => x
adam@365 99 ]]
adam@365 100 *)
adam@365 101
adam@365 102 Fixpoint id' (n : nat) := n.
adam@365 103
adam@365 104 Eval compute in fun x => id' x.
adam@365 105 (** %\vspace{-.15in}%[[
adam@365 106 = fun x : nat => (fix id' (n : nat) : nat := n) x
adam@365 107 ]]
adam@365 108
adam@479 109 By running [compute], we ask Coq to run reduction steps until no more apply, so why do we see an application of a known function, where clearly no beta reduction has been performed? The answer has to do with ensuring termination of all Gallina programs. One candidate rule would say that we apply recursive definitions wherever possible. However, this would clearly lead to nonterminating reduction sequences, since the function may appear fully applied within its own definition, and we would %\%naive%{}%ly "simplify" such applications immediately. Instead, Coq only applies the beta rule for a recursive function when _the top-level structure of the recursive argument is known_. For [id'] above, we have only one argument [n], so clearly it is the recursive argument, and the top-level structure of [n] is known when the function is applied to [O] or to some [S e] term. The variable [x] is neither, so reduction is blocked.
adam@365 110
adam@365 111 What are recursive arguments in general? Every recursive function is compiled by Coq to a %\index{Gallina terms!fix}%[fix] expression, for anonymous definition of recursive functions. Further, every [fix] with multiple arguments has one designated as the recursive argument via a [struct] annotation. The recursive argument is the one that must decrease across recursive calls, to appease Coq's termination checker. Coq will generally infer which argument is recursive, though we may also specify it manually, if we want to tweak reduction behavior. For instance, consider this definition of a function to add two lists of [nat]s elementwise: *)
adam@365 112
adam@365 113 Fixpoint addLists (ls1 ls2 : list nat) : list nat :=
adam@365 114 match ls1, ls2 with
adam@365 115 | n1 :: ls1' , n2 :: ls2' => n1 + n2 :: addLists ls1' ls2'
adam@365 116 | _, _ => nil
adam@365 117 end.
adam@365 118
adam@365 119 (** By default, Coq chooses [ls1] as the recursive argument. We can see that [ls2] would have been another valid choice. The choice has a critical effect on reduction behavior, as these two examples illustrate: *)
adam@365 120
adam@365 121 Eval compute in fun ls => addLists nil ls.
adam@365 122 (** %\vspace{-.15in}%[[
adam@365 123 = fun _ : list nat => nil
adam@365 124 ]]
adam@365 125 *)
adam@365 126
adam@365 127 Eval compute in fun ls => addLists ls nil.
adam@365 128 (** %\vspace{-.15in}%[[
adam@365 129 = fun ls : list nat =>
adam@365 130 (fix addLists (ls1 ls2 : list nat) : list nat :=
adam@365 131 match ls1 with
adam@365 132 | nil => nil
adam@365 133 | n1 :: ls1' =>
adam@365 134 match ls2 with
adam@365 135 | nil => nil
adam@365 136 | n2 :: ls2' =>
adam@365 137 (fix plus (n m : nat) : nat :=
adam@365 138 match n with
adam@365 139 | 0 => m
adam@365 140 | S p => S (plus p m)
adam@365 141 end) n1 n2 :: addLists ls1' ls2'
adam@365 142 end
adam@365 143 end) ls nil
adam@365 144 ]]
adam@365 145
adam@427 146 The outer application of the [fix] expression for [addLists] was only simplified in the first case, because in the second case the recursive argument is [ls], whose top-level structure is not known.
adam@365 147
adam@427 148 The opposite behavior pertains to a version of [addLists] with [ls2] marked as recursive. *)
adam@365 149
adam@365 150 Fixpoint addLists' (ls1 ls2 : list nat) {struct ls2} : list nat :=
adam@365 151 match ls1, ls2 with
adam@365 152 | n1 :: ls1' , n2 :: ls2' => n1 + n2 :: addLists' ls1' ls2'
adam@365 153 | _, _ => nil
adam@365 154 end.
adam@365 155
adam@427 156 (* begin hide *)
adam@437 157 (* begin thide *)
adam@437 158 Definition foo := (@eq, plus).
adam@437 159 (* end thide *)
adam@427 160 (* end hide *)
adam@427 161
adam@365 162 Eval compute in fun ls => addLists' ls nil.
adam@365 163 (** %\vspace{-.15in}%[[
adam@365 164 = fun ls : list nat => match ls with
adam@365 165 | nil => nil
adam@365 166 | _ :: _ => nil
adam@365 167 end
adam@365 168 ]]
adam@365 169
adam@365 170 We see that all use of recursive functions has been eliminated, though the term has not quite simplified to [nil]. We could get it to do so by switching the order of the [match] discriminees in the definition of [addLists'].
adam@365 171
adam@365 172 Recall that co-recursive definitions have a dual rule: a co-recursive call only simplifies when it is the discriminee of a [match]. This condition is built into the beta rule for %\index{Gallina terms!cofix}%[cofix], the anonymous form of [CoFixpoint].
adam@365 173
adam@365 174 %\medskip%
adam@365 175
adam@407 176 The standard [eq] relation is critically dependent on the definitional equality. The relation [eq] is often called a%\index{propositional equality}% _propositional equality_, because it reifies definitional equality as a proposition that may or may not hold. Standard axiomatizations of an equality predicate in first-order logic define equality in terms of properties it has, like reflexivity, symmetry, and transitivity. In contrast, for [eq] in Coq, those properties are implicit in the properties of the definitional equality, which are built into CIC's metatheory and the implementation of Gallina. We could add new rules to the definitional equality, and [eq] would keep its definition and methods of use.
adamc@122 177
adam@427 178 This all may make it sound like the choice of [eq]'s definition is unimportant. To the contrary, in this chapter, we will see examples where alternate definitions may simplify proofs. Before that point, I will introduce proof methods for goals that use proofs of the standard propositional equality "as data." *)
adamc@122 179
adamc@122 180
adamc@118 181 (** * Heterogeneous Lists Revisited *)
adamc@118 182
adam@480 183 (** One of our example dependent data structures from the last chapter (code repeated below) was the heterogeneous list and its associated "cursor" type. The recursive version poses some special challenges related to equality proofs, since it uses such proofs in its definition of [fmember] types. *)
adamc@118 184
adamc@118 185 Section fhlist.
adamc@118 186 Variable A : Type.
adamc@118 187 Variable B : A -> Type.
adamc@118 188
adamc@118 189 Fixpoint fhlist (ls : list A) : Type :=
adamc@118 190 match ls with
adamc@118 191 | nil => unit
adamc@118 192 | x :: ls' => B x * fhlist ls'
adamc@118 193 end%type.
adamc@118 194
adamc@118 195 Variable elm : A.
adamc@118 196
adamc@118 197 Fixpoint fmember (ls : list A) : Type :=
adamc@118 198 match ls with
adamc@118 199 | nil => Empty_set
adamc@118 200 | x :: ls' => (x = elm) + fmember ls'
adamc@118 201 end%type.
adamc@118 202
adamc@118 203 Fixpoint fhget (ls : list A) : fhlist ls -> fmember ls -> B elm :=
adamc@118 204 match ls return fhlist ls -> fmember ls -> B elm with
adamc@118 205 | nil => fun _ idx => match idx with end
adamc@118 206 | _ :: ls' => fun mls idx =>
adamc@118 207 match idx with
adamc@118 208 | inl pf => match pf with
adam@426 209 | eq_refl => fst mls
adamc@118 210 end
adamc@118 211 | inr idx' => fhget ls' (snd mls) idx'
adamc@118 212 end
adamc@118 213 end.
adamc@118 214 End fhlist.
adamc@118 215
adamc@118 216 Implicit Arguments fhget [A B elm ls].
adamc@118 217
adam@427 218 (* begin hide *)
adam@437 219 (* begin thide *)
adam@427 220 Definition map := O.
adam@437 221 (* end thide *)
adam@427 222 (* end hide *)
adam@427 223
adamc@118 224 (** We can define a [map]-like function for [fhlist]s. *)
adamc@118 225
adamc@118 226 Section fhlist_map.
adamc@118 227 Variables A : Type.
adamc@118 228 Variables B C : A -> Type.
adamc@118 229 Variable f : forall x, B x -> C x.
adamc@118 230
adamc@118 231 Fixpoint fhmap (ls : list A) : fhlist B ls -> fhlist C ls :=
adamc@118 232 match ls return fhlist B ls -> fhlist C ls with
adamc@118 233 | nil => fun _ => tt
adamc@118 234 | _ :: _ => fun hls => (f (fst hls), fhmap _ (snd hls))
adamc@118 235 end.
adamc@118 236
adamc@118 237 Implicit Arguments fhmap [ls].
adamc@118 238
adam@427 239 (* begin hide *)
adam@437 240 (* begin thide *)
adam@427 241 Definition ilist := O.
adam@427 242 Definition get := O.
adam@427 243 Definition imap := O.
adam@437 244 (* end thide *)
adam@427 245 (* end hide *)
adam@427 246
adamc@118 247 (** For the inductive versions of the [ilist] definitions, we proved a lemma about the interaction of [get] and [imap]. It was a strategic choice not to attempt such a proof for the definitions that we just gave, because that sets us on a collision course with the problems that are the subject of this chapter. *)
adamc@118 248
adamc@118 249 Variable elm : A.
adamc@118 250
adam@479 251 Theorem fhget_fhmap : forall ls (mem : fmember elm ls) (hls : fhlist B ls),
adamc@118 252 fhget (fhmap hls) mem = f (fhget hls mem).
adam@298 253 (* begin hide *)
adam@298 254 induction ls; crush; case a0; reflexivity.
adam@298 255 (* end hide *)
adam@364 256 (** %\vspace{-.2in}%[[
adamc@118 257 induction ls; crush.
adam@298 258 ]]
adamc@118 259
adam@364 260 %\vspace{-.15in}%In Coq 8.2, one subgoal remains at this point. Coq 8.3 has added some tactic improvements that enable [crush] to complete all of both inductive cases. To introduce the basics of reasoning about equality, it will be useful to review what was necessary in Coq 8.2.
adam@297 261
adam@297 262 Part of our single remaining subgoal is:
adamc@118 263 [[
adamc@118 264 a0 : a = elm
adamc@118 265 ============================
adamc@118 266 match a0 in (_ = a2) return (C a2) with
adam@426 267 | eq_refl => f a1
adamc@118 268 end = f match a0 in (_ = a2) return (B a2) with
adam@426 269 | eq_refl => a1
adamc@118 270 end
adamc@118 271 ]]
adamc@118 272
adam@426 273 This seems like a trivial enough obligation. The equality proof [a0] must be [eq_refl], since that is the only constructor of [eq]. Therefore, both the [match]es reduce to the point where the conclusion follows by reflexivity.
adamc@118 274 [[
adamc@118 275 destruct a0.
adamc@118 276 ]]
adamc@118 277
adam@364 278 <<
adam@364 279 User error: Cannot solve a second-order unification problem
adam@364 280 >>
adam@364 281
adamc@118 282 This is one of Coq's standard error messages for informing us that its heuristics for attempting an instance of an undecidable problem about dependent typing have failed. We might try to nudge things in the right direction by stating the lemma that we believe makes the conclusion trivial.
adamc@118 283 [[
adam@426 284 assert (a0 = eq_refl _).
adam@364 285 ]]
adamc@118 286
adam@364 287 <<
adam@426 288 The term "eq_refl ?98" has type "?98 = ?98"
adamc@118 289 while it is expected to have type "a = elm"
adam@364 290 >>
adamc@118 291
adamc@118 292 In retrospect, the problem is not so hard to see. Reflexivity proofs only show [x = x] for particular values of [x], whereas here we are thinking in terms of a proof of [a = elm], where the two sides of the equality are not equal syntactically. Thus, the essential lemma we need does not even type-check!
adamc@118 293
adam@427 294 Is it time to throw in the towel? Luckily, the answer is "no." In this chapter, we will see several useful patterns for proving obligations like this.
adamc@118 295
adam@407 296 For this particular example, the solution is surprisingly straightforward. The [destruct] tactic has a simpler sibling [case] which should behave identically for any inductive type with one constructor of no arguments.
adam@297 297 [[
adamc@118 298 case a0.
adam@297 299
adamc@118 300 ============================
adamc@118 301 f a1 = f a1
adamc@118 302 ]]
adamc@118 303
adam@297 304 It seems that [destruct] was trying to be too smart for its own good.
adam@297 305 [[
adamc@118 306 reflexivity.
adam@302 307 ]]
adam@364 308 %\vspace{-.2in}% *)
adamc@118 309 Qed.
adamc@124 310 (* end thide *)
adamc@118 311
adamc@118 312 (** It will be helpful to examine the proof terms generated by this sort of strategy. A simpler example illustrates what is going on. *)
adamc@118 313
adam@426 314 Lemma lemma1 : forall x (pf : x = elm), O = match pf with eq_refl => O end.
adamc@124 315 (* begin thide *)
adamc@118 316 simple destruct pf; reflexivity.
adamc@118 317 Qed.
adamc@124 318 (* end thide *)
adamc@118 319
adam@364 320 (** The tactic %\index{tactics!simple destruct}%[simple destruct pf] is a convenient form for applying [case]. It runs [intro] to bring into scope all quantified variables up to its argument. *)
adamc@118 321
adamc@118 322 Print lemma1.
adamc@218 323 (** %\vspace{-.15in}% [[
adamc@118 324 lemma1 =
adamc@118 325 fun (x : A) (pf : x = elm) =>
adamc@118 326 match pf as e in (_ = y) return (0 = match e with
adam@426 327 | eq_refl => 0
adamc@118 328 end) with
adam@426 329 | eq_refl => eq_refl 0
adamc@118 330 end
adamc@118 331 : forall (x : A) (pf : x = elm), 0 = match pf with
adam@426 332 | eq_refl => 0
adamc@118 333 end
adamc@218 334
adamc@118 335 ]]
adamc@118 336
adamc@118 337 Using what we know about shorthands for [match] annotations, we can write this proof in shorter form manually. *)
adamc@118 338
adamc@124 339 (* begin thide *)
adam@456 340 Definition lemma1' (x : A) (pf : x = elm) :=
adam@456 341 match pf return (0 = match pf with
adam@456 342 | eq_refl => 0
adam@456 343 end) with
adam@456 344 | eq_refl => eq_refl 0
adam@456 345 end.
adamc@124 346 (* end thide *)
adamc@118 347
adam@398 348 (** Surprisingly, what seems at first like a _simpler_ lemma is harder to prove. *)
adamc@118 349
adam@426 350 Lemma lemma2 : forall (x : A) (pf : x = x), O = match pf with eq_refl => O end.
adamc@124 351 (* begin thide *)
adam@364 352 (** %\vspace{-.25in}%[[
adamc@118 353 simple destruct pf.
adam@364 354 ]]
adamc@205 355
adam@364 356 <<
adamc@118 357 User error: Cannot solve a second-order unification problem
adam@364 358 >>
adam@302 359 *)
adamc@118 360 Abort.
adamc@118 361
adamc@118 362 (** Nonetheless, we can adapt the last manual proof to handle this theorem. *)
adamc@118 363
adamc@124 364 (* begin thide *)
adamc@124 365 Definition lemma2 :=
adamc@118 366 fun (x : A) (pf : x = x) =>
adamc@118 367 match pf return (0 = match pf with
adam@426 368 | eq_refl => 0
adamc@118 369 end) with
adam@426 370 | eq_refl => eq_refl 0
adamc@118 371 end.
adamc@124 372 (* end thide *)
adamc@118 373
adamc@118 374 (** We can try to prove a lemma that would simplify proofs of many facts like [lemma2]: *)
adamc@118 375
adam@427 376 (* begin hide *)
adam@437 377 (* begin thide *)
adam@427 378 Definition lemma3' := O.
adam@437 379 (* end thide *)
adam@427 380 (* end hide *)
adam@427 381
adam@426 382 Lemma lemma3 : forall (x : A) (pf : x = x), pf = eq_refl x.
adamc@124 383 (* begin thide *)
adam@364 384 (** %\vspace{-.25in}%[[
adamc@118 385 simple destruct pf.
adam@364 386 ]]
adamc@205 387
adam@364 388 <<
adamc@118 389 User error: Cannot solve a second-order unification problem
adam@364 390 >>
adam@364 391 %\vspace{-.15in}%*)
adamc@218 392
adamc@118 393 Abort.
adamc@118 394
adamc@118 395 (** This time, even our manual attempt fails.
adamc@118 396 [[
adamc@118 397 Definition lemma3' :=
adamc@118 398 fun (x : A) (pf : x = x) =>
adam@426 399 match pf as pf' in (_ = x') return (pf' = eq_refl x') with
adam@426 400 | eq_refl => eq_refl _
adamc@118 401 end.
adam@364 402 ]]
adamc@118 403
adam@364 404 <<
adam@426 405 The term "eq_refl x'" has type "x' = x'" while it is expected to have type
adamc@118 406 "x = x'"
adam@364 407 >>
adamc@118 408
adam@427 409 The type error comes from our [return] annotation. In that annotation, the [as]-bound variable [pf'] has type [x = x'], referring to the [in]-bound variable [x']. To do a dependent [match], we _must_ choose a fresh name for the second argument of [eq]. We are just as constrained to use the "real" value [x] for the first argument. Thus, within the [return] clause, the proof we are matching on _must_ equate two non-matching terms, which makes it impossible to equate that proof with reflexivity.
adamc@118 410
adam@398 411 Nonetheless, it turns out that, with one catch, we _can_ prove this lemma. *)
adamc@118 412
adam@426 413 Lemma lemma3 : forall (x : A) (pf : x = x), pf = eq_refl x.
adamc@118 414 intros; apply UIP_refl.
adamc@118 415 Qed.
adamc@118 416
adamc@118 417 Check UIP_refl.
adamc@218 418 (** %\vspace{-.15in}% [[
adamc@118 419 UIP_refl
adam@426 420 : forall (U : Type) (x : U) (p : x = x), p = eq_refl x
adamc@118 421 ]]
adamc@118 422
adam@480 423 The theorem %\index{Gallina terms!UIP\_refl}%[UIP_refl] comes from the [Eqdep] module of the standard library. (Its name uses the acronym "UIP" for "unicity of identity proofs.") Do the Coq authors know of some clever trick for building such proofs that we have not seen yet? If they do, they did not use it for this proof. Rather, the proof is based on an _axiom_, the term [eq_rect_eq] below. *)
adam@427 424
adam@436 425 (* begin hide *)
adam@436 426 Import Eq_rect_eq.
adam@436 427 (* end hide *)
adamc@118 428 Print eq_rect_eq.
adamc@218 429 (** %\vspace{-.15in}% [[
adam@436 430 *** [ eq_rect_eq :
adam@436 431 forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p),
adam@436 432 x = eq_rect p Q x p h ]
adamc@118 433 ]]
adamc@118 434
adam@427 435 The axiom %\index{Gallina terms!eq\_rect\_eq}%[eq_rect_eq] states a "fact" that seems like common sense, once the notation is deciphered. The term [eq_rect] is the automatically generated recursion principle for [eq]. Calling [eq_rect] is another way of [match]ing on an equality proof. The proof we match on is the argument [h], and [x] is the body of the [match]. The statement of [eq_rect_eq] just says that [match]es on proofs of [p = p], for any [p], are superfluous and may be removed. We can see this intuition better in code by asking Coq to simplify the theorem statement with the [compute] reduction strategy (which, by the way, applies all applicable rules of the definitional equality presented in this chapter's first section). *)
adam@427 436
adam@427 437 (* begin hide *)
adam@437 438 (* begin thide *)
adam@427 439 Definition False' := False.
adam@437 440 (* end thide *)
adam@427 441 (* end hide *)
adamc@118 442
adam@364 443 Eval compute in (forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p),
adam@364 444 x = eq_rect p Q x p h).
adam@364 445 (** %\vspace{-.15in}%[[
adam@364 446 = forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p),
adam@364 447 x = match h in (_ = y) return (Q y) with
adam@364 448 | eq_refl => x
adam@364 449 end
adam@364 450 ]]
adam@364 451
adam@427 452 Perhaps surprisingly, we cannot prove [eq_rect_eq] from within Coq. This proposition is introduced as an %\index{axioms}%axiom; that is, a proposition asserted as true without proof. We cannot assert just any statement without proof. Adding [False] as an axiom would allow us to prove any proposition, for instance, defeating the point of using a proof assistant. In general, we need to be sure that we never assert _inconsistent_ sets of axioms. A set of axioms is inconsistent if its conjunction implies [False]. For the case of [eq_rect_eq], consistency has been verified outside of Coq via "informal" metatheory%~\cite{AxiomK}%, in a study that also established unprovability of the axiom in CIC.
adamc@118 453
adamc@118 454 This axiom is equivalent to another that is more commonly known and mentioned in type theory circles. *)
adamc@118 455
adam@427 456 (* begin hide *)
adam@437 457 (* begin thide *)
adam@437 458 Definition Streicher_K' := UIP_refl__Streicher_K.
adam@437 459 (* end thide *)
adam@427 460 (* end hide *)
adam@427 461
adam@480 462 Check Streicher_K.
adamc@124 463 (* end thide *)
adamc@218 464 (** %\vspace{-.15in}% [[
adam@480 465 Streicher_K
adamc@118 466 : forall (U : Type) (x : U) (P : x = x -> Prop),
adam@480 467 P eq_refl -> forall p : x = x, P p
adamc@118 468 ]]
adamc@118 469
adam@480 470 This is the opaquely named %\index{axiom K}%"Streicher's axiom K," which says that a predicate on properly typed equality proofs holds of all such proofs if it holds of reflexivity. *)
adamc@118 471
adamc@118 472 End fhlist_map.
adamc@118 473
adam@436 474 (* begin hide *)
adam@437 475 (* begin thide *)
adam@436 476 Require Eqdep_dec.
adam@437 477 (* end thide *)
adam@436 478 (* end hide *)
adam@436 479
adam@364 480 (** It is worth remarking that it is possible to avoid axioms altogether for equalities on types with decidable equality. The [Eqdep_dec] module of the standard library contains a parametric proof of [UIP_refl] for such cases. To simplify presentation, we will stick with the axiom version in the rest of this chapter. *)
adam@364 481
adamc@119 482
adamc@119 483 (** * Type-Casts in Theorem Statements *)
adamc@119 484
adamc@119 485 (** Sometimes we need to use tricks with equality just to state the theorems that we care about. To illustrate, we start by defining a concatenation function for [fhlist]s. *)
adamc@119 486
adamc@119 487 Section fhapp.
adamc@119 488 Variable A : Type.
adamc@119 489 Variable B : A -> Type.
adamc@119 490
adamc@218 491 Fixpoint fhapp (ls1 ls2 : list A)
adamc@119 492 : fhlist B ls1 -> fhlist B ls2 -> fhlist B (ls1 ++ ls2) :=
adamc@218 493 match ls1 with
adamc@119 494 | nil => fun _ hls2 => hls2
adamc@119 495 | _ :: _ => fun hls1 hls2 => (fst hls1, fhapp _ _ (snd hls1) hls2)
adamc@119 496 end.
adamc@119 497
adamc@119 498 Implicit Arguments fhapp [ls1 ls2].
adamc@119 499
adamc@124 500 (* EX: Prove that fhapp is associative. *)
adamc@124 501 (* begin thide *)
adamc@124 502
adamc@119 503 (** We might like to prove that [fhapp] is associative.
adamc@119 504 [[
adam@479 505 Theorem fhapp_assoc : forall ls1 ls2 ls3
adamc@119 506 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3),
adamc@119 507 fhapp hls1 (fhapp hls2 hls3) = fhapp (fhapp hls1 hls2) hls3.
adam@364 508 ]]
adamc@119 509
adam@364 510 <<
adamc@119 511 The term
adamc@119 512 "fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3) (fhapp (ls1:=ls1) (ls2:=ls2) hls1 hls2)
adamc@119 513 hls3" has type "fhlist B ((ls1 ++ ls2) ++ ls3)"
adamc@119 514 while it is expected to have type "fhlist B (ls1 ++ ls2 ++ ls3)"
adam@364 515 >>
adamc@119 516
adam@407 517 This first cut at the theorem statement does not even type-check. We know that the two [fhlist] types appearing in the error message are always equal, by associativity of normal list append, but this fact is not apparent to the type checker. This stems from the fact that Coq's equality is%\index{intensional type theory}% _intensional_, in the sense that type equality theorems can never be applied after the fact to get a term to type-check. Instead, we need to make use of equality explicitly in the theorem statement. *)
adamc@119 518
adam@479 519 Theorem fhapp_assoc : forall ls1 ls2 ls3
adamc@119 520 (pf : (ls1 ++ ls2) ++ ls3 = ls1 ++ (ls2 ++ ls3))
adamc@119 521 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3),
adamc@119 522 fhapp hls1 (fhapp hls2 hls3)
adamc@119 523 = match pf in (_ = ls) return fhlist _ ls with
adam@426 524 | eq_refl => fhapp (fhapp hls1 hls2) hls3
adamc@119 525 end.
adamc@119 526 induction ls1; crush.
adamc@119 527
adamc@119 528 (** The first remaining subgoal looks trivial enough:
adamc@119 529 [[
adamc@119 530 ============================
adamc@119 531 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 =
adamc@119 532 match pf in (_ = ls) return (fhlist B ls) with
adam@426 533 | eq_refl => fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3
adamc@119 534 end
adamc@119 535 ]]
adamc@119 536
adamc@119 537 We can try what worked in previous examples.
adamc@119 538 [[
adamc@119 539 case pf.
adam@364 540 ]]
adamc@119 541
adam@364 542 <<
adamc@119 543 User error: Cannot solve a second-order unification problem
adam@364 544 >>
adamc@119 545
adamc@119 546 It seems we have reached another case where it is unclear how to use a dependent [match] to implement case analysis on our proof. The [UIP_refl] theorem can come to our rescue again. *)
adamc@119 547
adamc@119 548 rewrite (UIP_refl _ _ pf).
adamc@119 549 (** [[
adamc@119 550 ============================
adamc@119 551 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 =
adamc@119 552 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3
adam@302 553 ]]
adam@302 554 *)
adamc@119 555
adamc@119 556 reflexivity.
adamc@119 557
adamc@119 558 (** Our second subgoal is trickier.
adamc@119 559 [[
adamc@119 560 pf : a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3
adamc@119 561 ============================
adamc@119 562 (a0,
adamc@119 563 fhapp (ls1:=ls1) (ls2:=ls2 ++ ls3) b
adamc@119 564 (fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3)) =
adamc@119 565 match pf in (_ = ls) return (fhlist B ls) with
adam@426 566 | eq_refl =>
adamc@119 567 (a0,
adamc@119 568 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
adamc@119 569 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3)
adamc@119 570 end
adamc@119 571
adamc@119 572 rewrite (UIP_refl _ _ pf).
adam@364 573 ]]
adamc@119 574
adam@364 575 <<
adamc@119 576 The term "pf" has type "a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3"
adamc@119 577 while it is expected to have type "?556 = ?556"
adam@364 578 >>
adamc@119 579
adamc@119 580 We can only apply [UIP_refl] on proofs of equality with syntactically equal operands, which is not the case of [pf] here. We will need to manipulate the form of this subgoal to get us to a point where we may use [UIP_refl]. A first step is obtaining a proof suitable to use in applying the induction hypothesis. Inversion on the structure of [pf] is sufficient for that. *)
adamc@119 581
adamc@119 582 injection pf; intro pf'.
adamc@119 583 (** [[
adamc@119 584 pf : a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3
adamc@119 585 pf' : (ls1 ++ ls2) ++ ls3 = ls1 ++ ls2 ++ ls3
adamc@119 586 ============================
adamc@119 587 (a0,
adamc@119 588 fhapp (ls1:=ls1) (ls2:=ls2 ++ ls3) b
adamc@119 589 (fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3)) =
adamc@119 590 match pf in (_ = ls) return (fhlist B ls) with
adam@426 591 | eq_refl =>
adamc@119 592 (a0,
adamc@119 593 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
adamc@119 594 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3)
adamc@119 595 end
adamc@119 596 ]]
adamc@119 597
adamc@119 598 Now we can rewrite using the inductive hypothesis. *)
adamc@119 599
adamc@119 600 rewrite (IHls1 _ _ pf').
adamc@119 601 (** [[
adamc@119 602 ============================
adamc@119 603 (a0,
adamc@119 604 match pf' in (_ = ls) return (fhlist B ls) with
adam@426 605 | eq_refl =>
adamc@119 606 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
adamc@119 607 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3
adamc@119 608 end) =
adamc@119 609 match pf in (_ = ls) return (fhlist B ls) with
adam@426 610 | eq_refl =>
adamc@119 611 (a0,
adamc@119 612 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
adamc@119 613 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3)
adamc@119 614 end
adamc@119 615 ]]
adamc@119 616
adam@398 617 We have made an important bit of progress, as now only a single call to [fhapp] appears in the conclusion, repeated twice. Trying case analysis on our proofs still will not work, but there is a move we can make to enable it. Not only does just one call to [fhapp] matter to us now, but it also _does not matter what the result of the call is_. In other words, the subgoal should remain true if we replace this [fhapp] call with a fresh variable. The %\index{tactics!generalize}%[generalize] tactic helps us do exactly that. *)
adamc@119 618
adamc@119 619 generalize (fhapp (fhapp b hls2) hls3).
adamc@119 620 (** [[
adamc@119 621 forall f : fhlist B ((ls1 ++ ls2) ++ ls3),
adamc@119 622 (a0,
adamc@119 623 match pf' in (_ = ls) return (fhlist B ls) with
adam@426 624 | eq_refl => f
adamc@119 625 end) =
adamc@119 626 match pf in (_ = ls) return (fhlist B ls) with
adam@426 627 | eq_refl => (a0, f)
adamc@119 628 end
adamc@119 629 ]]
adamc@119 630
adamc@119 631 The conclusion has gotten markedly simpler. It seems counterintuitive that we can have an easier time of proving a more general theorem, but that is exactly the case here and for many other proofs that use dependent types heavily. Speaking informally, the reason why this kind of activity helps is that [match] annotations only support variables in certain positions. By reducing more elements of a goal to variables, built-in tactics can have more success building [match] terms under the hood.
adamc@119 632
adamc@119 633 In this case, it is helpful to generalize over our two proofs as well. *)
adamc@119 634
adamc@119 635 generalize pf pf'.
adamc@119 636 (** [[
adamc@119 637 forall (pf0 : a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3)
adamc@119 638 (pf'0 : (ls1 ++ ls2) ++ ls3 = ls1 ++ ls2 ++ ls3)
adamc@119 639 (f : fhlist B ((ls1 ++ ls2) ++ ls3)),
adamc@119 640 (a0,
adamc@119 641 match pf'0 in (_ = ls) return (fhlist B ls) with
adam@426 642 | eq_refl => f
adamc@119 643 end) =
adamc@119 644 match pf0 in (_ = ls) return (fhlist B ls) with
adam@426 645 | eq_refl => (a0, f)
adamc@119 646 end
adamc@119 647 ]]
adamc@119 648
adamc@119 649 To an experienced dependent types hacker, the appearance of this goal term calls for a celebration. The formula has a critical property that indicates that our problems are over. To get our proofs into the right form to apply [UIP_refl], we need to use associativity of list append to rewrite their types. We could not do that before because other parts of the goal require the proofs to retain their original types. In particular, the call to [fhapp] that we generalized must have type [(ls1 ++ ls2) ++ ls3], for some values of the list variables. If we rewrite the type of the proof used to type-cast this value to something like [ls1 ++ ls2 ++ ls3 = ls1 ++ ls2 ++ ls3], then the lefthand side of the equality would no longer match the type of the term we are trying to cast.
adamc@119 650
adam@398 651 However, now that we have generalized over the [fhapp] call, the type of the term being type-cast appears explicitly in the goal and _may be rewritten as well_. In particular, the final masterstroke is rewriting everywhere in our goal using associativity of list append. *)
adamc@119 652
adam@479 653 rewrite app_assoc.
adamc@119 654 (** [[
adamc@119 655 ============================
adamc@119 656 forall (pf0 : a :: ls1 ++ ls2 ++ ls3 = a :: ls1 ++ ls2 ++ ls3)
adamc@119 657 (pf'0 : ls1 ++ ls2 ++ ls3 = ls1 ++ ls2 ++ ls3)
adamc@119 658 (f : fhlist B (ls1 ++ ls2 ++ ls3)),
adamc@119 659 (a0,
adamc@119 660 match pf'0 in (_ = ls) return (fhlist B ls) with
adam@426 661 | eq_refl => f
adamc@119 662 end) =
adamc@119 663 match pf0 in (_ = ls) return (fhlist B ls) with
adam@426 664 | eq_refl => (a0, f)
adamc@119 665 end
adamc@119 666 ]]
adamc@119 667
adamc@119 668 We can see that we have achieved the crucial property: the type of each generalized equality proof has syntactically equal operands. This makes it easy to finish the proof with [UIP_refl]. *)
adamc@119 669
adamc@119 670 intros.
adamc@119 671 rewrite (UIP_refl _ _ pf0).
adamc@119 672 rewrite (UIP_refl _ _ pf'0).
adamc@119 673 reflexivity.
adamc@119 674 Qed.
adamc@124 675 (* end thide *)
adamc@119 676 End fhapp.
adamc@120 677
adamc@120 678 Implicit Arguments fhapp [A B ls1 ls2].
adamc@120 679
adam@364 680 (** This proof strategy was cumbersome and unorthodox, from the perspective of mainstream mathematics. The next section explores an alternative that leads to simpler developments in some cases. *)
adam@364 681
adamc@120 682
adamc@120 683 (** * Heterogeneous Equality *)
adamc@120 684
adam@407 685 (** There is another equality predicate, defined in the %\index{Gallina terms!JMeq}%[JMeq] module of the standard library, implementing%\index{heterogeneous equality}% _heterogeneous equality_. *)
adamc@120 686
adamc@120 687 Print JMeq.
adamc@218 688 (** %\vspace{-.15in}% [[
adamc@120 689 Inductive JMeq (A : Type) (x : A) : forall B : Type, B -> Prop :=
adamc@120 690 JMeq_refl : JMeq x x
adamc@120 691 ]]
adamc@120 692
adam@480 693 The identifier [JMeq] stands for %\index{John Major equality}%"John Major equality," a name coined by Conor McBride%~\cite{JMeq}% as an inside joke about British politics. The definition [JMeq] starts out looking a lot like the definition of [eq]. The crucial difference is that we may use [JMeq] _on arguments of different types_. For instance, a lemma that we failed to establish before is trivial with [JMeq]. It makes for prettier theorem statements to define some syntactic shorthand first. *)
adamc@120 694
adamc@120 695 Infix "==" := JMeq (at level 70, no associativity).
adamc@120 696
adam@426 697 (* EX: Prove UIP_refl' : forall (A : Type) (x : A) (pf : x = x), pf == eq_refl x *)
adamc@124 698 (* begin thide *)
adam@426 699 Definition UIP_refl' (A : Type) (x : A) (pf : x = x) : pf == eq_refl x :=
adam@426 700 match pf return (pf == eq_refl _) with
adam@426 701 | eq_refl => JMeq_refl _
adamc@120 702 end.
adamc@124 703 (* end thide *)
adamc@120 704
adamc@120 705 (** There is no quick way to write such a proof by tactics, but the underlying proof term that we want is trivial.
adamc@120 706
adamc@271 707 Suppose that we want to use [UIP_refl'] to establish another lemma of the kind we have run into several times so far. *)
adamc@120 708
adamc@120 709 Lemma lemma4 : forall (A : Type) (x : A) (pf : x = x),
adam@426 710 O = match pf with eq_refl => O end.
adamc@124 711 (* begin thide *)
adamc@121 712 intros; rewrite (UIP_refl' pf); reflexivity.
adamc@120 713 Qed.
adamc@124 714 (* end thide *)
adamc@120 715
adamc@120 716 (** All in all, refreshingly straightforward, but there really is no such thing as a free lunch. The use of [rewrite] is implemented in terms of an axiom: *)
adamc@120 717
adamc@120 718 Check JMeq_eq.
adamc@218 719 (** %\vspace{-.15in}% [[
adamc@120 720 JMeq_eq
adamc@120 721 : forall (A : Type) (x y : A), x == y -> x = y
adamc@218 722 ]]
adamc@120 723
adam@480 724 It may be surprising that we cannot prove that heterogeneous equality implies normal equality. The difficulties are the same kind we have seen so far, based on limitations of [match] annotations. The [JMeq_eq] axiom has been proved on paper to be consistent, but asserting it may still be considered to complicate the logic we work in, so there is some motivation for avoiding it.
adamc@120 725
adamc@120 726 We can redo our [fhapp] associativity proof based around [JMeq]. *)
adamc@120 727
adamc@120 728 Section fhapp'.
adamc@120 729 Variable A : Type.
adamc@120 730 Variable B : A -> Type.
adamc@120 731
adam@479 732 (** This time, the %\%naive theorem statement type-checks. *)
adamc@120 733
adamc@124 734 (* EX: Prove [fhapp] associativity using [JMeq]. *)
adamc@124 735
adamc@124 736 (* begin thide *)
adam@479 737 Theorem fhapp_assoc' : forall ls1 ls2 ls3 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2)
adam@364 738 (hls3 : fhlist B ls3),
adamc@120 739 fhapp hls1 (fhapp hls2 hls3) == fhapp (fhapp hls1 hls2) hls3.
adamc@120 740 induction ls1; crush.
adamc@120 741
adamc@120 742 (** Even better, [crush] discharges the first subgoal automatically. The second subgoal is:
adamc@120 743 [[
adamc@120 744 ============================
adam@297 745 (a0, fhapp b (fhapp hls2 hls3)) == (a0, fhapp (fhapp b hls2) hls3)
adamc@120 746 ]]
adamc@120 747
adam@297 748 It looks like one rewrite with the inductive hypothesis should be enough to make the goal trivial. Here is what happens when we try that in Coq 8.2:
adamc@120 749 [[
adamc@120 750 rewrite IHls1.
adam@364 751 ]]
adamc@120 752
adam@364 753 <<
adamc@120 754 Error: Impossible to unify "fhlist B ((ls1 ++ ?1572) ++ ?1573)" with
adamc@120 755 "fhlist B (ls1 ++ ?1572 ++ ?1573)"
adam@364 756 >>
adamc@120 757
adam@407 758 Coq 8.4 currently gives an error message about an uncaught exception. Perhaps that will be fixed soon. In any case, it is educational to consider a more explicit approach.
adam@297 759
adamc@120 760 We see that [JMeq] is not a silver bullet. We can use it to simplify the statements of equality facts, but the Coq type-checker uses non-trivial heterogeneous equality facts no more readily than it uses standard equality facts. Here, the problem is that the form [(e1, e2)] is syntactic sugar for an explicit application of a constructor of an inductive type. That application mentions the type of each tuple element explicitly, and our [rewrite] tries to change one of those elements without updating the corresponding type argument.
adamc@120 761
adamc@120 762 We can get around this problem by another multiple use of [generalize]. We want to bring into the goal the proper instance of the inductive hypothesis, and we also want to generalize the two relevant uses of [fhapp]. *)
adamc@120 763
adamc@120 764 generalize (fhapp b (fhapp hls2 hls3))
adamc@120 765 (fhapp (fhapp b hls2) hls3)
adamc@120 766 (IHls1 _ _ b hls2 hls3).
adam@364 767 (** %\vspace{-.15in}%[[
adamc@120 768 ============================
adamc@120 769 forall (f : fhlist B (ls1 ++ ls2 ++ ls3))
adamc@120 770 (f0 : fhlist B ((ls1 ++ ls2) ++ ls3)), f == f0 -> (a0, f) == (a0, f0)
adamc@120 771 ]]
adamc@120 772
adamc@120 773 Now we can rewrite with append associativity, as before. *)
adamc@120 774
adam@479 775 rewrite app_assoc.
adam@364 776 (** %\vspace{-.15in}%[[
adamc@120 777 ============================
adamc@120 778 forall f f0 : fhlist B (ls1 ++ ls2 ++ ls3), f == f0 -> (a0, f) == (a0, f0)
adamc@120 779 ]]
adamc@120 780
adamc@120 781 From this point, the goal is trivial. *)
adamc@120 782
adamc@120 783 intros f f0 H; rewrite H; reflexivity.
adamc@120 784 Qed.
adamc@124 785 (* end thide *)
adamc@121 786
adam@385 787 End fhapp'.
adam@385 788
adam@385 789 (** This example illustrates a general pattern: heterogeneous equality often simplifies theorem statements, but we still need to do some work to line up some dependent pattern matches that tactics will generate for us.
adam@385 790
adam@385 791 The proof we have found relies on the [JMeq_eq] axiom, which we can verify with a command%\index{Vernacular commands!Print Assumptions}% that we will discuss more in two chapters. *)
adam@385 792
adam@479 793 Print Assumptions fhapp_assoc'.
adam@385 794 (** %\vspace{-.15in}%[[
adam@385 795 Axioms:
adam@385 796 JMeq_eq : forall (A : Type) (x y : A), x == y -> x = y
adam@385 797 ]]
adam@385 798
adam@385 799 It was the [rewrite H] tactic that implicitly appealed to the axiom. By restructuring the proof, we can avoid axiom dependence. A general lemma about pairs provides the key element. (Our use of [generalize] above can be thought of as reducing the proof to another, more complex and specialized lemma.) *)
adam@385 800
adam@385 801 Lemma pair_cong : forall A1 A2 B1 B2 (x1 : A1) (x2 : A2) (y1 : B1) (y2 : B2),
adam@385 802 x1 == x2
adam@385 803 -> y1 == y2
adam@385 804 -> (x1, y1) == (x2, y2).
adam@385 805 intros until y2; intros Hx Hy; rewrite Hx; rewrite Hy; reflexivity.
adam@385 806 Qed.
adam@385 807
adam@385 808 Hint Resolve pair_cong.
adam@385 809
adam@385 810 Section fhapp''.
adam@385 811 Variable A : Type.
adam@385 812 Variable B : A -> Type.
adam@385 813
adam@479 814 Theorem fhapp_assoc'' : forall ls1 ls2 ls3 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2)
adam@385 815 (hls3 : fhlist B ls3),
adam@385 816 fhapp hls1 (fhapp hls2 hls3) == fhapp (fhapp hls1 hls2) hls3.
adam@385 817 induction ls1; crush.
adam@385 818 Qed.
adam@385 819 End fhapp''.
adam@385 820
adam@479 821 Print Assumptions fhapp_assoc''.
adam@385 822 (** <<
adam@385 823 Closed under the global context
adam@385 824 >>
adam@385 825
adam@479 826 One might wonder exactly which elements of a proof involving [JMeq] imply that [JMeq_eq] must be used. For instance, above we noticed that [rewrite] had brought [JMeq_eq] into the proof of [fhapp_assoc'], yet here we have also used [rewrite] with [JMeq] hypotheses while avoiding axioms! One illuminating exercise is comparing the types of the lemmas that [rewrite] uses under the hood to implement the rewrites. Here is the normal lemma for [eq] rewriting:%\index{Gallina terms!eq\_ind\_r}% *)
adam@385 827
adam@385 828 Check eq_ind_r.
adam@385 829 (** %\vspace{-.15in}%[[
adam@385 830 eq_ind_r
adam@385 831 : forall (A : Type) (x : A) (P : A -> Prop),
adam@385 832 P x -> forall y : A, y = x -> P y
adam@385 833 ]]
adam@385 834
adam@398 835 The corresponding lemma used for [JMeq] in the proof of [pair_cong] is %\index{Gallina terms!internal\_JMeq\_rew\_r}%[internal_JMeq_rew_r], which, confusingly, is defined by [rewrite] as needed, so it is not available for checking until after we apply it. *)
adam@385 836
adam@398 837 Check internal_JMeq_rew_r.
adam@385 838 (** %\vspace{-.15in}%[[
adam@398 839 internal_JMeq_rew_r
adam@385 840 : forall (A : Type) (x : A) (B : Type) (b : B)
adam@385 841 (P : forall B0 : Type, B0 -> Type), P B b -> x == b -> P A x
adam@385 842 ]]
adam@385 843
adam@479 844 The key difference is that, where the [eq] lemma is parameterized on a predicate of type [A -> Prop], the [JMeq] lemma is parameterized on a predicate of type more like [forall A : Type, A -> Prop]. To apply [eq_ind_r] with a proof of [x = y], it is only necessary to rearrange the goal into an application of a [fun] abstraction to [y]. In contrast, to apply [internal_JMeq_rew_r], it is necessary to rearrange the goal to an application of a [fun] abstraction to both [y] and _its type_. In other words, the predicate must be _polymorphic_ in [y]'s type; any type must make sense, from a type-checking standpoint. There may be cases where the former rearrangement is easy to do in a type-correct way, but the second rearrangement done %\%naive%{}%ly leads to a type error.
adam@385 845
adam@398 846 When [rewrite] cannot figure out how to apply [internal_JMeq_rew_r] for [x == y] where [x] and [y] have the same type, the tactic can instead use an alternate theorem, which is easy to prove as a composition of [eq_ind_r] and [JMeq_eq]. *)
adam@385 847
adam@385 848 Check JMeq_ind_r.
adam@385 849 (** %\vspace{-.15in}%[[
adam@385 850 JMeq_ind_r
adam@385 851 : forall (A : Type) (x : A) (P : A -> Prop),
adam@385 852 P x -> forall y : A, y == x -> P y
adam@385 853 ]]
adam@385 854
adam@479 855 Ironically, where in the proof of [fhapp_assoc'] we used [rewrite app_assoc] to make it clear that a use of [JMeq] was actually homogeneously typed, we created a situation where [rewrite] applied the axiom-based [JMeq_ind_r] instead of the axiom-free [internal_JMeq_rew_r]!
adam@385 856
adam@385 857 For another simple example, consider this theorem that applies a heterogeneous equality to prove a congruence fact. *)
adam@385 858
adam@385 859 Theorem out_of_luck : forall n m : nat,
adam@385 860 n == m
adam@385 861 -> S n == S m.
adam@385 862 intros n m H.
adam@385 863
adam@480 864 (** Applying [JMeq_ind_r] is easy, as the %\index{tactics!pattern}%[pattern] tactic will transform the goal into an application of an appropriate [fun] to a term that we want to abstract. (In general, [pattern] abstracts over a term by introducing a new anonymous function taking that term as argument.) *)
adam@385 865
adam@385 866 pattern n.
adam@385 867 (** %\vspace{-.15in}%[[
adam@385 868 n : nat
adam@385 869 m : nat
adam@385 870 H : n == m
adam@385 871 ============================
adam@385 872 (fun n0 : nat => S n0 == S m) n
adam@385 873 ]]
adam@385 874 *)
adam@385 875 apply JMeq_ind_r with (x := m); auto.
adam@385 876
adam@398 877 (** However, we run into trouble trying to get the goal into a form compatible with [internal_JMeq_rew_r.] *)
adam@427 878
adam@385 879 Undo 2.
adam@385 880 (** %\vspace{-.15in}%[[
adam@385 881 pattern nat, n.
adam@385 882 ]]
adam@385 883 <<
adam@385 884 Error: The abstracted term "fun (P : Set) (n0 : P) => S n0 == S m"
adam@385 885 is not well typed.
adam@385 886 Illegal application (Type Error):
adam@385 887 The term "S" of type "nat -> nat"
adam@385 888 cannot be applied to the term
adam@385 889 "n0" : "P"
adam@385 890 This term has type "P" which should be coercible to
adam@385 891 "nat".
adam@385 892 >>
adam@385 893
adam@385 894 In other words, the successor function [S] is insufficiently polymorphic. If we try to generalize over the type of [n], we find that [S] is no longer legal to apply to [n]. *)
adam@385 895
adam@385 896 Abort.
adam@385 897
adam@479 898 (** Why did we not run into this problem in our proof of [fhapp_assoc'']? The reason is that the pair constructor is polymorphic in the types of the pair components, while functions like [S] are not polymorphic at all. Use of such non-polymorphic functions with [JMeq] tends to push toward use of axioms. The example with [nat] here is a bit unrealistic; more likely cases would involve functions that have _some_ polymorphism, but not enough to allow abstractions of the sort we attempted above with [pattern]. For instance, we might have an equality between two lists, where the goal only type-checks when the terms involved really are lists, though everything is polymorphic in the types of list data elements. The {{http://www.mpi-sws.org/~gil/Heq/}Heq} library builds up a slightly different foundation to help avoid such problems. *)
adam@364 899
adamc@121 900
adamc@121 901 (** * Equivalence of Equality Axioms *)
adamc@121 902
adamc@124 903 (* EX: Show that the approaches based on K and JMeq are equivalent logically. *)
adamc@124 904
adamc@124 905 (* begin thide *)
adamc@272 906 (** Assuming axioms (like axiom K and [JMeq_eq]) is a hazardous business. The due diligence associated with it is necessarily global in scope, since two axioms may be consistent alone but inconsistent together. It turns out that all of the major axioms proposed for reasoning about equality in Coq are logically equivalent, so that we only need to pick one to assert without proof. In this section, we demonstrate this by showing how each of the previous two sections' approaches reduces to the other logically.
adamc@121 907
adamc@121 908 To show that [JMeq] and its axiom let us prove [UIP_refl], we start from the lemma [UIP_refl'] from the previous section. The rest of the proof is trivial. *)
adamc@121 909
adam@426 910 Lemma UIP_refl'' : forall (A : Type) (x : A) (pf : x = x), pf = eq_refl x.
adamc@121 911 intros; rewrite (UIP_refl' pf); reflexivity.
adamc@121 912 Qed.
adamc@121 913
adamc@121 914 (** The other direction is perhaps more interesting. Assume that we only have the axiom of the [Eqdep] module available. We can define [JMeq] in a way that satisfies the same interface as the combination of the [JMeq] module's inductive definition and axiom. *)
adamc@121 915
adamc@121 916 Definition JMeq' (A : Type) (x : A) (B : Type) (y : B) : Prop :=
adam@426 917 exists pf : B = A, x = match pf with eq_refl => y end.
adamc@121 918
adamc@121 919 Infix "===" := JMeq' (at level 70, no associativity).
adamc@121 920
adam@427 921 (** remove printing exists *)
adam@427 922
adamc@121 923 (** We say that, by definition, [x] and [y] are equal if and only if there exists a proof [pf] that their types are equal, such that [x] equals the result of casting [y] with [pf]. This statement can look strange from the standpoint of classical math, where we almost never mention proofs explicitly with quantifiers in formulas, but it is perfectly legal Coq code.
adamc@121 924
adamc@121 925 We can easily prove a theorem with the same type as that of the [JMeq_refl] constructor of [JMeq]. *)
adamc@121 926
adamc@121 927 Theorem JMeq_refl' : forall (A : Type) (x : A), x === x.
adam@426 928 intros; unfold JMeq'; exists (eq_refl A); reflexivity.
adamc@121 929 Qed.
adamc@121 930
adamc@121 931 (** printing exists $\exists$ *)
adamc@121 932
adamc@121 933 (** The proof of an analogue to [JMeq_eq] is a little more interesting, but most of the action is in appealing to [UIP_refl]. *)
adamc@121 934
adamc@121 935 Theorem JMeq_eq' : forall (A : Type) (x y : A),
adamc@121 936 x === y -> x = y.
adamc@121 937 unfold JMeq'; intros.
adamc@121 938 (** [[
adamc@121 939 H : exists pf : A = A,
adamc@121 940 x = match pf in (_ = T) return T with
adam@426 941 | eq_refl => y
adamc@121 942 end
adamc@121 943 ============================
adamc@121 944 x = y
adam@302 945 ]]
adam@302 946 *)
adamc@121 947
adamc@121 948 destruct H.
adamc@121 949 (** [[
adamc@121 950 x0 : A = A
adamc@121 951 H : x = match x0 in (_ = T) return T with
adam@426 952 | eq_refl => y
adamc@121 953 end
adamc@121 954 ============================
adamc@121 955 x = y
adam@302 956 ]]
adam@302 957 *)
adamc@121 958
adamc@121 959 rewrite H.
adamc@121 960 (** [[
adamc@121 961 x0 : A = A
adamc@121 962 ============================
adamc@121 963 match x0 in (_ = T) return T with
adam@426 964 | eq_refl => y
adamc@121 965 end = y
adam@302 966 ]]
adam@302 967 *)
adamc@121 968
adamc@121 969 rewrite (UIP_refl _ _ x0); reflexivity.
adamc@121 970 Qed.
adamc@121 971
adam@427 972 (** We see that, in a very formal sense, we are free to switch back and forth between the two styles of proofs about equality proofs. One style may be more convenient than the other for some proofs, but we can always interconvert between our results. The style that does not use heterogeneous equality may be preferable in cases where many results do not require the tricks of this chapter, since then the use of axioms is avoided altogether for the simple cases, and a wider audience will be able to follow those "simple" proofs. On the other hand, heterogeneous equality often makes for shorter and more readable theorem statements. *)
adamc@123 973
adamc@124 974 (* end thide *)
adamc@123 975
adamc@123 976
adamc@123 977 (** * Equality of Functions *)
adamc@123 978
adam@444 979 (** The following seems like a reasonable theorem to want to hold, and it does hold in set theory.
adam@444 980 %\vspace{-.15in}%[[
adam@456 981 Theorem two_funs : (fun n => n) = (fun n => n + 0).
adamc@205 982 ]]
adam@444 983 %\vspace{-.15in}%Unfortunately, this theorem is not provable in CIC without additional axioms. None of the definitional equality rules force function equality to be%\index{extensionality of function equality}% _extensional_. That is, the fact that two functions return equal results on equal inputs does not imply that the functions are equal. We _can_ assert function extensionality as an axiom, and indeed the standard library already contains that axiom. *)
adamc@123 984
adam@407 985 Require Import FunctionalExtensionality.
adam@407 986 About functional_extensionality.
adam@407 987 (** %\vspace{-.15in}%[[
adam@407 988 functional_extensionality :
adam@407 989 forall (A B : Type) (f g : A -> B), (forall x : A, f x = g x) -> f = g
adam@407 990 ]]
adam@407 991 *)
adamc@123 992
adam@456 993 (** This axiom has been verified metatheoretically to be consistent with CIC and the two equality axioms we considered previously. With it, the proof of [two_funs] is trivial. *)
adamc@123 994
adam@456 995 Theorem two_funs : (fun n => n) = (fun n => n + 0).
adamc@124 996 (* begin thide *)
adam@407 997 apply functional_extensionality; crush.
adamc@123 998 Qed.
adamc@124 999 (* end thide *)
adamc@123 1000
adam@427 1001 (** The same axiom can help us prove equality of types, where we need to "reason under quantifiers." *)
adamc@123 1002
adamc@123 1003 Theorem forall_eq : (forall x : nat, match x with
adamc@123 1004 | O => True
adamc@123 1005 | S _ => True
adamc@123 1006 end)
adamc@123 1007 = (forall _ : nat, True).
adamc@123 1008
adam@456 1009 (** There are no immediate opportunities to apply [functional_extensionality], but we can use %\index{tactics!change}%[change] to fix that problem. *)
adamc@123 1010
adamc@124 1011 (* begin thide *)
adamc@123 1012 change ((forall x : nat, (fun x => match x with
adamc@123 1013 | 0 => True
adamc@123 1014 | S _ => True
adamc@123 1015 end) x) = (nat -> True)).
adam@407 1016 rewrite (functional_extensionality (fun x => match x with
adam@407 1017 | 0 => True
adam@407 1018 | S _ => True
adam@407 1019 end) (fun _ => True)).
adamc@123 1020 (** [[
adamc@123 1021 2 subgoals
adamc@123 1022
adamc@123 1023 ============================
adamc@123 1024 (nat -> True) = (nat -> True)
adamc@123 1025
adamc@123 1026 subgoal 2 is:
adamc@123 1027 forall x : nat, match x with
adamc@123 1028 | 0 => True
adamc@123 1029 | S _ => True
adamc@123 1030 end = True
adam@302 1031 ]]
adam@302 1032 *)
adamc@123 1033
adamc@123 1034 reflexivity.
adamc@123 1035
adamc@123 1036 destruct x; constructor.
adamc@123 1037 Qed.
adamc@124 1038 (* end thide *)
adamc@127 1039
adam@407 1040 (** Unlike in the case of [eq_rect_eq], we have no way of deriving this axiom of%\index{functional extensionality}% _functional extensionality_ for types with decidable equality. To allow equality reasoning without axioms, it may be worth rewriting a development to replace functions with alternate representations, such as finite map types for which extensionality is derivable in CIC. *)