comparison src/Equality.v @ 364:2fbb47fb02bd

Pass through old content of Equality
author Adam Chlipala <adam@chlipala.net>
date Sun, 06 Nov 2011 16:25:45 -0500
parents d5787b70cf48
children 990151eac6af
comparison
equal deleted inserted replaced
363:7e57c909f0f2 364:2fbb47fb02bd
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, 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. *) 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, 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. *)
22 22
23 23
24 (** * The Definitional Equality *) 24 (** * The Definitional Equality *)
25 25
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 %\textit{%#<i>#definitional equality#</i>#%}%. 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. 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}\textit{%#<i>#definitional equality#</i>#%}%. 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.
27 27
28 The [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]. *) 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]. *)
29 29
30 Definition pred' (x : nat) := 30 Definition pred' (x : nat) :=
31 match x with 31 match x with
32 | O => O 32 | O => O
33 | S n' => let y := n' in y 33 | S n' => let y := n' in y
34 end. 34 end.
35 35
36 Theorem reduce_me : pred' 1 = 0. 36 Theorem reduce_me : pred' 1 = 0.
37 37
38 (* begin thide *) 38 (* begin thide *)
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 that encodes terms canonically. 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.
40 40
41 The 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 [lazy] for call-by-need reduction. *) 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. *)
42 42
43 cbv delta. 43 cbv delta.
44 (** [[ 44 (** %\vspace{-.15in}%[[
45 ============================ 45 ============================
46 (fun x : nat => match x with 46 (fun x : nat => match x with
47 | 0 => 0 47 | 0 => 0
48 | S n' => let y := n' in y 48 | S n' => let y := n' in y
49 end) 1 = 0 49 end) 1 = 0
50
51 ]] 50 ]]
52 51
53 At this point, we want to apply the famous beta reduction of lambda calculus, to simplify the application of a known function abstraction. *) 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. *)
54 53
55 cbv beta. 54 cbv beta.
56 (** [[ 55 (** %\vspace{-.15in}%[[
57 ============================ 56 ============================
58 match 1 with 57 match 1 with
59 | 0 => 0 58 | 0 => 0
60 | S n' => let y := n' in y 59 | S n' => let y := n' in y
61 end = 0 60 end = 0
62
63 ]] 61 ]]
64 62
65 Next on the list is the iota reduction, which simplifies a single [match] term by determining which pattern matches. *) 63 Next on the list is the %\index{iota reduction}%iota reduction, which simplifies a single [match] term by determining which pattern matches. *)
66 64
67 cbv iota. 65 cbv iota.
68 (** [[ 66 (** %\vspace{-.15in}%[[
69 ============================ 67 ============================
70 (fun n' : nat => let y := n' in y) 0 = 0 68 (fun n' : nat => let y := n' in y) 0 = 0
71
72 ]] 69 ]]
73 70
74 Now we need another beta reduction. *) 71 Now we need another beta reduction. *)
75 72
76 cbv beta. 73 cbv beta.
77 (** [[ 74 (** %\vspace{-.15in}%[[
78 ============================ 75 ============================
79 (let y := 0 in y) = 0 76 (let y := 0 in y) = 0
80
81 ]] 77 ]]
82 78
83 The final reduction rule is zeta, which replaces a [let] expression by its body with the appropriate term substituted. *) 79 The final reduction rule is %\index{zeta reduction}%zeta, which replaces a [let] expression by its body with the appropriate term substituted. *)
84 80
85 cbv zeta. 81 cbv zeta.
86 (** [[ 82 (** %\vspace{-.15in}%[[
87 ============================ 83 ============================
88 0 = 0 84 0 = 0
89
90 ]] 85 ]]
91 *) 86 *)
92 87
93 reflexivity. 88 reflexivity.
94 Qed. 89 Qed.
95 (* end thide *) 90 (* end thide *)
96 91
97 (** The standard [eq] relation is critically dependent on the definitional equality. [eq] is often called a %\textit{%#<i>#propositional equality#</i>#%}%, 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. 92 (** The standard [eq] relation is critically dependent on the definitional equality. The relation [eq] is often called a %\index{propositional equality}\textit{%#<i>#propositional equality#</i>#%}%, 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.
98 93
99 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.#"#%''% *) 94 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.#"#%''% *)
100 95
101 96
102 (** * Heterogeneous Lists Revisited *) 97 (** * Heterogeneous Lists Revisited *)
158 Theorem get_imap : forall ls (mem : fmember elm ls) (hls : fhlist B ls), 153 Theorem get_imap : forall ls (mem : fmember elm ls) (hls : fhlist B ls),
159 fhget (fhmap hls) mem = f (fhget hls mem). 154 fhget (fhmap hls) mem = f (fhget hls mem).
160 (* begin hide *) 155 (* begin hide *)
161 induction ls; crush; case a0; reflexivity. 156 induction ls; crush; case a0; reflexivity.
162 (* end hide *) 157 (* end hide *)
163 (** [[ 158 (** %\vspace{-.2in}%[[
164 induction ls; crush. 159 induction ls; crush.
165
166 ]] 160 ]]
167 161
168 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. 162 %\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.
169 163
170 Part of our single remaining subgoal is: 164 Part of our single remaining subgoal is:
171
172 [[ 165 [[
173 a0 : a = elm 166 a0 : a = elm
174 ============================ 167 ============================
175 match a0 in (_ = a2) return (C a2) with 168 match a0 in (_ = a2) return (C a2) with
176 | refl_equal => f a1 169 | refl_equal => f a1
177 end = f match a0 in (_ = a2) return (B a2) with 170 end = f match a0 in (_ = a2) return (B a2) with
178 | refl_equal => a1 171 | refl_equal => a1
179 end 172 end
180
181 ]] 173 ]]
182 174
183 This seems like a trivial enough obligation. The equality proof [a0] must be [refl_equal], since that is the only constructor of [eq]. Therefore, both the [match]es reduce to the point where the conclusion follows by reflexivity. 175 This seems like a trivial enough obligation. The equality proof [a0] must be [refl_equal], since that is the only constructor of [eq]. Therefore, both the [match]es reduce to the point where the conclusion follows by reflexivity.
184
185 [[ 176 [[
186 destruct a0. 177 destruct a0.
187 178 ]]
179
180 <<
188 User error: Cannot solve a second-order unification problem 181 User error: Cannot solve a second-order unification problem
189 182 >>
190 ]]
191 183
192 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. 184 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.
193
194 [[ 185 [[
195 assert (a0 = refl_equal _). 186 assert (a0 = refl_equal _).
196 187 ]]
188
189 <<
197 The term "refl_equal ?98" has type "?98 = ?98" 190 The term "refl_equal ?98" has type "?98 = ?98"
198 while it is expected to have type "a = elm" 191 while it is expected to have type "a = elm"
199 192 >>
200 ]]
201 193
202 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! 194 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!
203 195
204 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. 196 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.
205 197
206 For this particular example, the solution is surprisingly straightforward. [destruct] has a simpler sibling [case] which should behave identically for any inductive type with one constructor of no arguments. 198 For this particular example, the solution is surprisingly straightforward. [destruct] has a simpler sibling [case] which should behave identically for any inductive type with one constructor of no arguments.
207
208 [[ 199 [[
209 case a0. 200 case a0.
210 201
211 ============================ 202 ============================
212 f a1 = f a1 203 f a1 = f a1
213
214 ]] 204 ]]
215 205
216 It seems that [destruct] was trying to be too smart for its own good. 206 It seems that [destruct] was trying to be too smart for its own good.
217
218 [[ 207 [[
219 reflexivity. 208 reflexivity.
220
221 ]] 209 ]]
222 *) 210 %\vspace{-.2in}% *)
223
224 Qed. 211 Qed.
225 (* end thide *) 212 (* end thide *)
226 213
227 (** It will be helpful to examine the proof terms generated by this sort of strategy. A simpler example illustrates what is going on. *) 214 (** It will be helpful to examine the proof terms generated by this sort of strategy. A simpler example illustrates what is going on. *)
228 215
230 (* begin thide *) 217 (* begin thide *)
231 simple destruct pf; reflexivity. 218 simple destruct pf; reflexivity.
232 Qed. 219 Qed.
233 (* end thide *) 220 (* end thide *)
234 221
235 (** [simple destruct pf] is a convenient form for applying [case]. It runs [intro] to bring into scope all quantified variables up to its argument. *) 222 (** 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. *)
236 223
237 Print lemma1. 224 Print lemma1.
238 (** %\vspace{-.15in}% [[ 225 (** %\vspace{-.15in}% [[
239 lemma1 = 226 lemma1 =
240 fun (x : A) (pf : x = elm) => 227 fun (x : A) (pf : x = elm) =>
263 250
264 (** Surprisingly, what seems at first like a %\textit{%#<i>#simpler#</i>#%}% lemma is harder to prove. *) 251 (** Surprisingly, what seems at first like a %\textit{%#<i>#simpler#</i>#%}% lemma is harder to prove. *)
265 252
266 Lemma lemma2 : forall (x : A) (pf : x = x), O = match pf with refl_equal => O end. 253 Lemma lemma2 : forall (x : A) (pf : x = x), O = match pf with refl_equal => O end.
267 (* begin thide *) 254 (* begin thide *)
268 (** [[ 255 (** %\vspace{-.25in}%[[
269 simple destruct pf. 256 simple destruct pf.
270 257 ]]
258
259 <<
271 User error: Cannot solve a second-order unification problem 260 User error: Cannot solve a second-order unification problem
272 261 >>
273 ]]
274 *) 262 *)
275 Abort. 263 Abort.
276 264
277 (** Nonetheless, we can adapt the last manual proof to handle this theorem. *) 265 (** Nonetheless, we can adapt the last manual proof to handle this theorem. *)
278 266
288 276
289 (** We can try to prove a lemma that would simplify proofs of many facts like [lemma2]: *) 277 (** We can try to prove a lemma that would simplify proofs of many facts like [lemma2]: *)
290 278
291 Lemma lemma3 : forall (x : A) (pf : x = x), pf = refl_equal x. 279 Lemma lemma3 : forall (x : A) (pf : x = x), pf = refl_equal x.
292 (* begin thide *) 280 (* begin thide *)
293 (** [[ 281 (** %\vspace{-.25in}%[[
294 simple destruct pf. 282 simple destruct pf.
295 283 ]]
284
285 <<
296 User error: Cannot solve a second-order unification problem 286 User error: Cannot solve a second-order unification problem
297 ]] 287 >>
298 *) 288 %\vspace{-.15in}%*)
299 289
300 Abort. 290 Abort.
301 291
302 (** This time, even our manual attempt fails. 292 (** This time, even our manual attempt fails.
303
304 [[ 293 [[
305 Definition lemma3' := 294 Definition lemma3' :=
306 fun (x : A) (pf : x = x) => 295 fun (x : A) (pf : x = x) =>
307 match pf as pf' in (_ = x') return (pf' = refl_equal x') with 296 match pf as pf' in (_ = x') return (pf' = refl_equal x') with
308 | refl_equal => refl_equal _ 297 | refl_equal => refl_equal _
309 end. 298 end.
310 299 ]]
300
301 <<
311 The term "refl_equal x'" has type "x' = x'" while it is expected to have type 302 The term "refl_equal x'" has type "x' = x'" while it is expected to have type
312 "x = x'" 303 "x = x'"
313 304 >>
314 ]]
315 305
316 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 %\textit{%#<i>#must#</i>#%}% 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 %\textit{%#<i>#must#</i>#%}% equate two non-matching terms, which makes it impossible to equate that proof with reflexivity. 306 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 %\textit{%#<i>#must#</i>#%}% 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 %\textit{%#<i>#must#</i>#%}% equate two non-matching terms, which makes it impossible to equate that proof with reflexivity.
317 307
318 Nonetheless, it turns out that, with one catch, we %\textit{%#<i>#can#</i>#%}% prove this lemma. *) 308 Nonetheless, it turns out that, with one catch, we %\textit{%#<i>#can#</i>#%}% prove this lemma. *)
319 309
323 313
324 Check UIP_refl. 314 Check UIP_refl.
325 (** %\vspace{-.15in}% [[ 315 (** %\vspace{-.15in}% [[
326 UIP_refl 316 UIP_refl
327 : forall (U : Type) (x : U) (p : x = x), p = refl_equal x 317 : forall (U : Type) (x : U) (p : x = x), p = refl_equal x
328
329 ]] 318 ]]
330 319
331 [UIP_refl] comes from the [Eqdep] module of the standard library. 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 %\textit{%#<i>#axiom#</i>#%}%. *) 320 The theorem %\index{Gallina terms!UIF\_refl}%[UIP_refl] comes from the [Eqdep] module of the standard library. 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 %\textit{%#<i>#axiom#</i>#%}%. *)
332 321
333 Print eq_rect_eq. 322 Print eq_rect_eq.
334 (** %\vspace{-.15in}% [[ 323 (** %\vspace{-.15in}% [[
335 eq_rect_eq = 324 eq_rect_eq =
336 fun U : Type => Eq_rect_eq.eq_rect_eq U 325 fun U : Type => Eq_rect_eq.eq_rect_eq U
337 : forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p), 326 : forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p),
338 x = eq_rect p Q x p h 327 x = eq_rect p Q x p h
339
340 ]] 328 ]]
341 329
342 [eq_rect_eq] states a %``%#"#fact#"#%''% that seems like common sense, once the notation is deciphered. [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]. [eq_rect_eq] just says that [match]es on proofs of [p = p], for any [p], are superfluous and may be removed. 330 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). *)
343 331
344 Perhaps surprisingly, we cannot prove [eq_rect_eq] from within Coq. This proposition is introduced as an 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 %\textit{%#<i>#inconsistent#</i>#%}% 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. 332 Eval compute in (forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p),
333 x = eq_rect p Q x p h).
334 (** %\vspace{-.15in}%[[
335 = forall (U : Type) (p : U) (Q : U -> Type) (x : Q p) (h : p = p),
336 x = match h in (_ = y) return (Q y) with
337 | eq_refl => x
338 end
339 ]]
340
341 Perhaps surprisingly, we cannot prove [eq_rect_eq] from within Coq. This proposition is introduced as an %\index{axiom}%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 %\textit{%#<i>#inconsistent#</i>#%}% 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.
345 342
346 This axiom is equivalent to another that is more commonly known and mentioned in type theory circles. *) 343 This axiom is equivalent to another that is more commonly known and mentioned in type theory circles. *)
347 344
348 Print Streicher_K. 345 Print Streicher_K.
349 (* end thide *) 346 (* end thide *)
350 (** %\vspace{-.15in}% [[ 347 (** %\vspace{-.15in}% [[
351 Streicher_K = 348 Streicher_K =
352 fun U : Type => UIP_refl__Streicher_K U (UIP_refl U) 349 fun U : Type => UIP_refl__Streicher_K U (UIP_refl U)
353 : forall (U : Type) (x : U) (P : x = x -> Prop), 350 : forall (U : Type) (x : U) (P : x = x -> Prop),
354 P (refl_equal x) -> forall p : x = x, P p 351 P (refl_equal x) -> forall p : x = x, P p
355
356 ]] 352 ]]
357 353
358 This is the unfortunately-named %``%#"#Streicher's axiom K,#"#%''% which says that a predicate on properly-typed equality proofs holds of all such proofs if it holds of reflexivity. *) 354 This is the unfortunately 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. *)
359 355
360 End fhlist_map. 356 End fhlist_map.
357
358 (** 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. *)
361 359
362 360
363 (** * Type-Casts in Theorem Statements *) 361 (** * Type-Casts in Theorem Statements *)
364 362
365 (** 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. *) 363 (** 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. *)
379 377
380 (* EX: Prove that fhapp is associative. *) 378 (* EX: Prove that fhapp is associative. *)
381 (* begin thide *) 379 (* begin thide *)
382 380
383 (** We might like to prove that [fhapp] is associative. 381 (** We might like to prove that [fhapp] is associative.
384
385 [[ 382 [[
386 Theorem fhapp_ass : forall ls1 ls2 ls3 383 Theorem fhapp_ass : forall ls1 ls2 ls3
387 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3), 384 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3),
388 fhapp hls1 (fhapp hls2 hls3) = fhapp (fhapp hls1 hls2) hls3. 385 fhapp hls1 (fhapp hls2 hls3) = fhapp (fhapp hls1 hls2) hls3.
389 386 ]]
387
388 <<
390 The term 389 The term
391 "fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3) (fhapp (ls1:=ls1) (ls2:=ls2) hls1 hls2) 390 "fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3) (fhapp (ls1:=ls1) (ls2:=ls2) hls1 hls2)
392 hls3" has type "fhlist B ((ls1 ++ ls2) ++ ls3)" 391 hls3" has type "fhlist B ((ls1 ++ ls2) ++ ls3)"
393 while it is expected to have type "fhlist B (ls1 ++ ls2 ++ ls3)" 392 while it is expected to have type "fhlist B (ls1 ++ ls2 ++ ls3)"
394 393 >>
395 ]] 394
396 395 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}\textit{%#<i>#intensional#</i>#%}%, 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. *)
397 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 %\textit{%#<i>#intensional#</i>#%}%, 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. *)
398 396
399 Theorem fhapp_ass : forall ls1 ls2 ls3 397 Theorem fhapp_ass : forall ls1 ls2 ls3
400 (pf : (ls1 ++ ls2) ++ ls3 = ls1 ++ (ls2 ++ ls3)) 398 (pf : (ls1 ++ ls2) ++ ls3 = ls1 ++ (ls2 ++ ls3))
401 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3), 399 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3),
402 fhapp hls1 (fhapp hls2 hls3) 400 fhapp hls1 (fhapp hls2 hls3)
404 | refl_equal => fhapp (fhapp hls1 hls2) hls3 402 | refl_equal => fhapp (fhapp hls1 hls2) hls3
405 end. 403 end.
406 induction ls1; crush. 404 induction ls1; crush.
407 405
408 (** The first remaining subgoal looks trivial enough: 406 (** The first remaining subgoal looks trivial enough:
409
410 [[ 407 [[
411 ============================ 408 ============================
412 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 = 409 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 =
413 match pf in (_ = ls) return (fhlist B ls) with 410 match pf in (_ = ls) return (fhlist B ls) with
414 | refl_equal => fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 411 | refl_equal => fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3
415 end 412 end
416
417 ]] 413 ]]
418 414
419 We can try what worked in previous examples. 415 We can try what worked in previous examples.
420
421 [[ 416 [[
422 case pf. 417 case pf.
423 418 ]]
419
420 <<
424 User error: Cannot solve a second-order unification problem 421 User error: Cannot solve a second-order unification problem
425 422 >>
426 ]]
427 423
428 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. *) 424 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. *)
429 425
430 rewrite (UIP_refl _ _ pf). 426 rewrite (UIP_refl _ _ pf).
431 (** [[ 427 (** [[
432 ============================ 428 ============================
433 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 = 429 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 =
434 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3 430 fhapp (ls1:=ls2) (ls2:=ls3) hls2 hls3
435
436 ]] 431 ]]
437 *) 432 *)
438 433
439 reflexivity. 434 reflexivity.
440 435
441 (** Our second subgoal is trickier. 436 (** Our second subgoal is trickier.
442
443 [[ 437 [[
444 pf : a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3 438 pf : a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3
445 ============================ 439 ============================
446 (a0, 440 (a0,
447 fhapp (ls1:=ls1) (ls2:=ls2 ++ ls3) b 441 fhapp (ls1:=ls1) (ls2:=ls2 ++ ls3) b
452 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3) 446 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
453 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3) 447 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3)
454 end 448 end
455 449
456 rewrite (UIP_refl _ _ pf). 450 rewrite (UIP_refl _ _ pf).
457 451 ]]
452
453 <<
458 The term "pf" has type "a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3" 454 The term "pf" has type "a :: (ls1 ++ ls2) ++ ls3 = a :: ls1 ++ ls2 ++ ls3"
459 while it is expected to have type "?556 = ?556" 455 while it is expected to have type "?556 = ?556"
460 456 >>
461 ]]
462 457
463 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. *) 458 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. *)
464 459
465 injection pf; intro pf'. 460 injection pf; intro pf'.
466 (** [[ 461 (** [[
474 | refl_equal => 469 | refl_equal =>
475 (a0, 470 (a0,
476 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3) 471 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
477 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3) 472 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3)
478 end 473 end
479
480 ]] 474 ]]
481 475
482 Now we can rewrite using the inductive hypothesis. *) 476 Now we can rewrite using the inductive hypothesis. *)
483 477
484 rewrite (IHls1 _ _ pf'). 478 rewrite (IHls1 _ _ pf').
494 | refl_equal => 488 | refl_equal =>
495 (a0, 489 (a0,
496 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3) 490 fhapp (ls1:=ls1 ++ ls2) (ls2:=ls3)
497 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3) 491 (fhapp (ls1:=ls1) (ls2:=ls2) b hls2) hls3)
498 end 492 end
499
500 ]] 493 ]]
501 494
502 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 %\textit{%#<i>#does not matter what the result of the call is#</i>#%}%. In other words, the subgoal should remain true if we replace this [fhapp] call with a fresh variable. The [generalize] tactic helps us do exactly that. *) 495 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 %\textit{%#<i>#does not matter what the result of the call is#</i>#%}%. 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. *)
503 496
504 generalize (fhapp (fhapp b hls2) hls3). 497 generalize (fhapp (fhapp b hls2) hls3).
505 (** [[ 498 (** [[
506 forall f : fhlist B ((ls1 ++ ls2) ++ ls3), 499 forall f : fhlist B ((ls1 ++ ls2) ++ ls3),
507 (a0, 500 (a0,
509 | refl_equal => f 502 | refl_equal => f
510 end) = 503 end) =
511 match pf in (_ = ls) return (fhlist B ls) with 504 match pf in (_ = ls) return (fhlist B ls) with
512 | refl_equal => (a0, f) 505 | refl_equal => (a0, f)
513 end 506 end
514
515 ]] 507 ]]
516 508
517 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. 509 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.
518 510
519 In this case, it is helpful to generalize over our two proofs as well. *) 511 In this case, it is helpful to generalize over our two proofs as well. *)
528 | refl_equal => f 520 | refl_equal => f
529 end) = 521 end) =
530 match pf0 in (_ = ls) return (fhlist B ls) with 522 match pf0 in (_ = ls) return (fhlist B ls) with
531 | refl_equal => (a0, f) 523 | refl_equal => (a0, f)
532 end 524 end
533
534 ]] 525 ]]
535 526
536 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. 527 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.
537 528
538 However, now that we have generalized over the [fhapp] call, the type of the term being type-cast appears explicitly in the goal and %\textit{%#<i>#may be rewritten as well#</i>#%}%. In particular, the final masterstroke is rewriting everywhere in our goal using associativity of list append. *) 529 However, now that we have generalized over the [fhapp] call, the type of the term being type-cast appears explicitly in the goal and %\textit{%#<i>#may be rewritten as well#</i>#%}%. In particular, the final masterstroke is rewriting everywhere in our goal using associativity of list append. *)
548 | refl_equal => f 539 | refl_equal => f
549 end) = 540 end) =
550 match pf0 in (_ = ls) return (fhlist B ls) with 541 match pf0 in (_ = ls) return (fhlist B ls) with
551 | refl_equal => (a0, f) 542 | refl_equal => (a0, f)
552 end 543 end
553
554 ]] 544 ]]
555 545
556 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]. *) 546 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]. *)
557 547
558 intros. 548 intros.
563 (* end thide *) 553 (* end thide *)
564 End fhapp. 554 End fhapp.
565 555
566 Implicit Arguments fhapp [A B ls1 ls2]. 556 Implicit Arguments fhapp [A B ls1 ls2].
567 557
558 (** 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. *)
559
568 560
569 (** * Heterogeneous Equality *) 561 (** * Heterogeneous Equality *)
570 562
571 (** There is another equality predicate, defined in the [JMeq] module of the standard library, implementing %\textit{%#<i>#heterogeneous equality#</i>#%}%. *) 563 (** There is another equality predicate, defined in the %\index{Gallina terms!JMeq}%[JMeq] module of the standard library, implementing %\index{heterogeneous equality}\textit{%#<i>#heterogeneous equality#</i>#%}%. *)
572 564
573 Print JMeq. 565 Print JMeq.
574 (** %\vspace{-.15in}% [[ 566 (** %\vspace{-.15in}% [[
575 Inductive JMeq (A : Type) (x : A) : forall B : Type, B -> Prop := 567 Inductive JMeq (A : Type) (x : A) : forall B : Type, B -> Prop :=
576 JMeq_refl : JMeq x x 568 JMeq_refl : JMeq x x
577
578 ]] 569 ]]
579 570
580 [JMeq] stands for %``%#"#John Major equality,#"#%''% a name coined by Conor McBride as a sort of pun about British politics. [JMeq] starts out looking a lot like [eq]. The crucial difference is that we may use [JMeq] %\textit{%#<i>#on arguments of different types#</i>#%}%. 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. *) 571 The identity [JMeq] stands for %\index{John Major equality}``%#"#John Major equality,#"#%''% a name coined by Conor McBride%~\cite{JMeq}% as a sort of pun about British politics. [JMeq] starts out looking a lot like [eq]. The crucial difference is that we may use [JMeq] %\textit{%#<i>#on arguments of different types#</i>#%}%. 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. *)
581 572
582 Infix "==" := JMeq (at level 70, no associativity). 573 Infix "==" := JMeq (at level 70, no associativity).
583 574
584 (* EX: Prove UIP_refl' : forall (A : Type) (x : A) (pf : x = x), pf == refl_equal x *) 575 (* EX: Prove UIP_refl' : forall (A : Type) (x : A) (pf : x = x), pf == refl_equal x *)
585 (* begin thide *) 576 (* begin thide *)
604 595
605 Check JMeq_eq. 596 Check JMeq_eq.
606 (** %\vspace{-.15in}% [[ 597 (** %\vspace{-.15in}% [[
607 JMeq_eq 598 JMeq_eq
608 : forall (A : Type) (x y : A), x == y -> x = y 599 : forall (A : Type) (x y : A), x == y -> x = y
609
610 ]] 600 ]]
611 601
612 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. 602 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.
613 603
614 We can redo our [fhapp] associativity proof based around [JMeq]. *) 604 We can redo our [fhapp] associativity proof based around [JMeq]. *)
620 (** This time, the naive theorem statement type-checks. *) 610 (** This time, the naive theorem statement type-checks. *)
621 611
622 (* EX: Prove [fhapp] associativity using [JMeq]. *) 612 (* EX: Prove [fhapp] associativity using [JMeq]. *)
623 613
624 (* begin thide *) 614 (* begin thide *)
625 Theorem fhapp_ass' : forall ls1 ls2 ls3 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3), 615 Theorem fhapp_ass' : forall ls1 ls2 ls3 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2)
616 (hls3 : fhlist B ls3),
626 fhapp hls1 (fhapp hls2 hls3) == fhapp (fhapp hls1 hls2) hls3. 617 fhapp hls1 (fhapp hls2 hls3) == fhapp (fhapp hls1 hls2) hls3.
627 induction ls1; crush. 618 induction ls1; crush.
628 619
629 (** Even better, [crush] discharges the first subgoal automatically. The second subgoal is: 620 (** Even better, [crush] discharges the first subgoal automatically. The second subgoal is:
630
631 [[ 621 [[
632 ============================ 622 ============================
633 (a0, fhapp b (fhapp hls2 hls3)) == (a0, fhapp (fhapp b hls2) hls3) 623 (a0, fhapp b (fhapp hls2 hls3)) == (a0, fhapp (fhapp b hls2) hls3)
634
635 ]] 624 ]]
636 625
637 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: 626 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:
638
639 [[ 627 [[
640 rewrite IHls1. 628 rewrite IHls1.
641 629 ]]
630
631 <<
642 Error: Impossible to unify "fhlist B ((ls1 ++ ?1572) ++ ?1573)" with 632 Error: Impossible to unify "fhlist B ((ls1 ++ ?1572) ++ ?1573)" with
643 "fhlist B (ls1 ++ ?1572 ++ ?1573)" 633 "fhlist B (ls1 ++ ?1572 ++ ?1573)"
644 634 >>
645 ]]
646 635
647 Coq 8.3 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. 636 Coq 8.3 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.
648 637
649 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. 638 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.
650 639
651 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]. *) 640 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]. *)
652 641
653 generalize (fhapp b (fhapp hls2 hls3)) 642 generalize (fhapp b (fhapp hls2 hls3))
654 (fhapp (fhapp b hls2) hls3) 643 (fhapp (fhapp b hls2) hls3)
655 (IHls1 _ _ b hls2 hls3). 644 (IHls1 _ _ b hls2 hls3).
656 (** [[ 645 (** %\vspace{-.15in}%[[
657 ============================ 646 ============================
658 forall (f : fhlist B (ls1 ++ ls2 ++ ls3)) 647 forall (f : fhlist B (ls1 ++ ls2 ++ ls3))
659 (f0 : fhlist B ((ls1 ++ ls2) ++ ls3)), f == f0 -> (a0, f) == (a0, f0) 648 (f0 : fhlist B ((ls1 ++ ls2) ++ ls3)), f == f0 -> (a0, f) == (a0, f0)
660
661 ]] 649 ]]
662 650
663 Now we can rewrite with append associativity, as before. *) 651 Now we can rewrite with append associativity, as before. *)
664 652
665 rewrite app_ass. 653 rewrite app_ass.
666 (** [[ 654 (** %\vspace{-.15in}%[[
667 ============================ 655 ============================
668 forall f f0 : fhlist B (ls1 ++ ls2 ++ ls3), f == f0 -> (a0, f) == (a0, f0) 656 forall f f0 : fhlist B (ls1 ++ ls2 ++ ls3), f == f0 -> (a0, f) == (a0, f0)
669
670 ]] 657 ]]
671 658
672 From this point, the goal is trivial. *) 659 From this point, the goal is trivial. *)
673 660
674 intros f f0 H; rewrite H; reflexivity. 661 intros f f0 H; rewrite H; reflexivity.
675 Qed. 662 Qed.
676 (* end thide *) 663 (* end thide *)
677 End fhapp'. 664 End fhapp'.
665
666 (** 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. *)
678 667
679 668
680 (** * Equivalence of Equality Axioms *) 669 (** * Equivalence of Equality Axioms *)
681 670
682 (* EX: Show that the approaches based on K and JMeq are equivalent logically. *) 671 (* EX: Show that the approaches based on K and JMeq are equivalent logically. *)
718 x = match pf in (_ = T) return T with 707 x = match pf in (_ = T) return T with
719 | refl_equal => y 708 | refl_equal => y
720 end 709 end
721 ============================ 710 ============================
722 x = y 711 x = y
723
724 ]] 712 ]]
725 *) 713 *)
726 714
727 destruct H. 715 destruct H.
728 (** [[ 716 (** [[
730 H : x = match x0 in (_ = T) return T with 718 H : x = match x0 in (_ = T) return T with
731 | refl_equal => y 719 | refl_equal => y
732 end 720 end
733 ============================ 721 ============================
734 x = y 722 x = y
735
736 ]] 723 ]]
737 *) 724 *)
738 725
739 rewrite H. 726 rewrite H.
740 (** [[ 727 (** [[
741 x0 : A = A 728 x0 : A = A
742 ============================ 729 ============================
743 match x0 in (_ = T) return T with 730 match x0 in (_ = T) return T with
744 | refl_equal => y 731 | refl_equal => y
745 end = y 732 end = y
746
747 ]] 733 ]]
748 *) 734 *)
749 735
750 rewrite (UIP_refl _ _ x0); reflexivity. 736 rewrite (UIP_refl _ _ x0); reflexivity.
751 Qed. 737 Qed.
752 738
753 (** 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. 739 (** 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. *)
754 740
755 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. *)
756 (* end thide *) 741 (* end thide *)
757 742
758 743
759 (** * Equality of Functions *) 744 (** * Equality of Functions *)
760 745
761 (** The following seems like a reasonable theorem to want to hold, and it does hold in set theory. [[ 746 (** The following seems like a reasonable theorem to want to hold, and it does hold in set theory. [[
762 Theorem S_eta : S = (fun n => S n). 747 Theorem S_eta : S = (fun n => S n).
763
764 ]] 748 ]]
765 749
766 Unfortunately, this theorem is not provable in CIC without additional axioms. None of the definitional equality rules force function equality to be %\textit{%#<i>#extensional#</i>#%}%. That is, the fact that two functions return equal results on equal inputs does not imply that the functions are equal. We %\textit{%#<i>#can#</i>#%}% assert function extensionality as an axiom. *) 750 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}\textit{%#<i>#extensional#</i>#%}%. That is, the fact that two functions return equal results on equal inputs does not imply that the functions are equal. We %\textit{%#<i>#can#</i>#%}% assert function extensionality as an axiom. *)
767 751
768 (* begin thide *) 752 (* begin thide *)
769 Axiom ext_eq : forall A B (f g : A -> B), 753 Axiom ext_eq : forall A B (f g : A -> B),
770 (forall x, f x = g x) 754 (forall x, f x = g x)
771 -> f = g. 755 -> f = g.
785 | O => True 769 | O => True
786 | S _ => True 770 | S _ => True
787 end) 771 end)
788 = (forall _ : nat, True). 772 = (forall _ : nat, True).
789 773
790 (** There are no immediate opportunities to apply [ext_eq], but we can use [change] to fix that. *) 774 (** There are no immediate opportunities to apply [ext_eq], but we can use %\index{tactics!change}%[change] to fix that. *)
791 775
792 (* begin thide *) 776 (* begin thide *)
793 change ((forall x : nat, (fun x => match x with 777 change ((forall x : nat, (fun x => match x with
794 | 0 => True 778 | 0 => True
795 | S _ => True 779 | S _ => True
816 800
817 destruct x; constructor. 801 destruct x; constructor.
818 Qed. 802 Qed.
819 (* end thide *) 803 (* end thide *)
820 804
805 (** Unlike in the case of [eq_rect_eq], we have no way of deriving this axiom of %\index{functional extensionality}\emph{%#<i>#functional extensionality#</i>#%}% 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. *)
806
821 807
822 (** * Exercises *) 808 (** * Exercises *)
823 809
824 (** %\begin{enumerate}%#<ol># 810 (** %\begin{enumerate}%#<ol>#
825 811
826 %\item%#<li># Implement and prove correct a substitution function for simply-typed lambda calculus. In particular: 812 %\item%#<li># Implement and prove correct a substitution function for simply typed lambda calculus. In particular:
827 %\begin{enumerate}%#<ol># 813 %\begin{enumerate}%#<ol>#
828 %\item%#<li># Define a datatype [type] of lambda types, including just booleans and function types.#</li># 814 %\item%#<li># Define a datatype [type] of lambda types, including just booleans and function types.#</li>#
829 %\item%#<li># Define a type family [exp : list type -> type -> Type] of lambda expressions, including boolean constants, variables, and function application and abstraction.#</li># 815 %\item%#<li># Define a type family [exp : list type -> type -> Type] of lambda expressions, including boolean constants, variables, and function application and abstraction.#</li>#
830 %\item%#<li># Implement a definitional interpreter for [exp]s, by way of a recursive function over expressions and substitutions for free variables, like in the related example from the last chapter.#</li># 816 %\item%#<li># Implement a definitional interpreter for [exp]s, by way of a recursive function over expressions and substitutions for free variables, like in the related example from the last chapter.#</li>#
831 %\item%#<li># Implement a function [subst : forall t' ts t, exp (t' :: ts) t -> exp ts t' -> exp ts t]. The type of the first expression indicates that its most recently bound free variable has type [t']. The second expression also has type [t'], and the job of [subst] is to substitute the second expression for every occurrence of the %``%#"#first#"#%''% variable of the first expression.#</li># 817 %\item%#<li># Implement a function [subst : forall t' ts t, exp (t' :: ts) t -> exp ts t' -> exp ts t]. The type of the first expression indicates that its most recently bound free variable has type [t']. The second expression also has type [t'], and the job of [subst] is to substitute the second expression for every occurrence of the %``%#"#first#"#%''% variable of the first expression.#</li>#
832 %\item%#<li># Prove that [subst] preserves program meanings. That is, prove 818 %\item%#<li># Prove that [subst] preserves program meanings. That is, prove
833 [[ 819 [[
834 forall t' ts t (e : exp (t' :: ts) t) (e' : exp ts t') (s : hlist typeDenote ts), 820 forall t' ts t (e : exp (t' :: ts) t) (e' : exp ts t') (s : hlist typeDenote ts),
835 expDenote (subst e e') s = expDenote e (expDenote e' s ::: s) 821 expDenote (subst e e') s = expDenote e (expDenote e' s ::: s)
836
837 ]] 822 ]]
838 where [:::] is an infix operator for heterogeneous %``%#"#cons#"#%''% that is defined in the book's [DepList] module.#</li># 823 where [:::] is an infix operator for heterogeneous %``%#"#cons#"#%''% that is defined in the book's [DepList] module.#</li>#
839 #</ol>#%\end{enumerate}% 824 #</ol>#%\end{enumerate}%
840 The material presented up to this point should be sufficient to enable a good solution of this exercise, with enough ingenuity. If you get stuck, it may be helpful to use the following structure. None of these elements need to appear in your solution, but we can at least guarantee that there is a reasonable solution based on them. 825 The material presented up to this point should be sufficient to enable a good solution of this exercise, with enough ingenuity. If you get stuck, it may be helpful to use the following structure. None of these elements need to appear in your solution, but we can at least guarantee that there is a reasonable solution based on them.
841 %\begin{enumerate}%#<ol># 826 %\begin{enumerate}%#<ol>#
846 %\item%#<li># Define a recursive function [substVar : forall ts1 ts2 t t', member t (ts1 ++ t' :: ts2) -> (t' = t) + member t (ts1 ++ ts2)]. This function is the workhorse behind substitution applied to a variable. It returns [inl] to indicate that the variable we pass to it is the variable that we are substituting for, and it returns [inr] to indicate that the variable we are examining is %\textit{%#<i>#not#</i>#%}% the one we are substituting for. In the first case, we get a proof that the necessary typing relationship holds, and, in the second case, we get the original variable modified to reflect the removal of the substitutee from the typing context.#</li># 831 %\item%#<li># Define a recursive function [substVar : forall ts1 ts2 t t', member t (ts1 ++ t' :: ts2) -> (t' = t) + member t (ts1 ++ ts2)]. This function is the workhorse behind substitution applied to a variable. It returns [inl] to indicate that the variable we pass to it is the variable that we are substituting for, and it returns [inr] to indicate that the variable we are examining is %\textit{%#<i>#not#</i>#%}% the one we are substituting for. In the first case, we get a proof that the necessary typing relationship holds, and, in the second case, we get the original variable modified to reflect the removal of the substitutee from the typing context.#</li>#
847 %\item%#<li># Define a recursive function [subst' : forall ts t (e : exp ts t) ts1 t' ts2, ts = ts1 ++ t' :: ts2 -> exp (ts1 ++ ts2) t' -> exp (ts1 ++ ts2) t]. This is the workhorse of substitution in expressions, employing the same proof-passing trick as for [lift']. You will probably want to use [lift] somewhere in the definition of [subst'].#</li># 832 %\item%#<li># Define a recursive function [subst' : forall ts t (e : exp ts t) ts1 t' ts2, ts = ts1 ++ t' :: ts2 -> exp (ts1 ++ ts2) t' -> exp (ts1 ++ ts2) t]. This is the workhorse of substitution in expressions, employing the same proof-passing trick as for [lift']. You will probably want to use [lift] somewhere in the definition of [subst'].#</li>#
848 %\item%#<li># Now [subst] should be a one-liner, defined in terms of [subst'].#</li># 833 %\item%#<li># Now [subst] should be a one-liner, defined in terms of [subst'].#</li>#
849 %\item%#<li># Prove a correctness theorem for each auxiliary function, leading up to the proof of [subst] correctness.#</li># 834 %\item%#<li># Prove a correctness theorem for each auxiliary function, leading up to the proof of [subst] correctness.#</li>#
850 %\item%#<li># All of the reasoning about equality proofs in these theorems follows a regular pattern. If you have an equality proof that you want to replace with [refl_equal] somehow, run [generalize] on that proof variable. Your goal is to get to the point where you can [rewrite] with the original proof to change the type of the generalized version. To avoid type errors (the infamous %``%#"#second-order unification#"#%''% failure messages), it will be helpful to run [generalize] on other pieces of the proof context that mention the equality's lefthand side. You might also want to use [generalize dependent], which generalizes not just one variable but also all variables whose types depend on it. [generalize dependent] has the sometimes-helpful property of removing from the context all variables that it generalizes. Once you do manage the mind-bending trick of using the equality proof to rewrite its own type, you will be able to rewrite with [UIP_refl].#</li># 835 %\item%#<li># All of the reasoning about equality proofs in these theorems follows a regular pattern. If you have an equality proof that you want to replace with [refl_equal] somehow, run [generalize] on that proof variable. Your goal is to get to the point where you can [rewrite] with the original proof to change the type of the generalized version. To avoid type errors (the infamous %``%#"#second-order unification#"#%''% failure messages), it will be helpful to run [generalize] on other pieces of the proof context that mention the equality's lefthand side. You might also want to use [generalize dependent], which generalizes not just one variable but also all variables whose types depend on it. [generalize dependent] has the sometimes-helpful property of removing from the context all variables that it generalizes. Once you do manage the mind-bending trick of using the equality proof to rewrite its own type, you will be able to rewrite with [UIP_refl].#</li>#
851 %\item%#<li># A variant of the [ext_eq] axiom from the end of this chapter is available in the book module [Axioms], and you will probably want to use it in the [lift'] and [subst'] correctness proofs.#</li># 836 %\item%#<li># The [ext_eq] axiom from the end of this chapter is available in the Coq standard library as [functional_extensionality] in module [FunctionalExtensionality], and you will probably want to use it in the [lift'] and [subst'] correctness proofs.#</li>#
852 %\item%#<li># The [change] tactic should come in handy in the proofs about [lift] and [subst], where you want to introduce %``%#"#extraneous#"#%''% list concatenations with [nil] to match the forms of earlier theorems.#</li># 837 %\item%#<li># The [change] tactic should come in handy in the proofs about [lift] and [subst], where you want to introduce %``%#"#extraneous#"#%''% list concatenations with [nil] to match the forms of earlier theorems.#</li>#
853 %\item%#<li># Be careful about [destruct]ing a term %``%#"#too early.#"#%''% You can use [generalize] on proof terms to bring into the proof context any important propositions about the term. Then, when you [destruct] the term, it is updated in the extra propositions, too. The [case_eq] tactic is another alternative to this approach, based on saving an equality between the original term and its new form.#</li># 838 %\item%#<li># Be careful about [destruct]ing a term %``%#"#too early.#"#%''% You can use [generalize] on proof terms to bring into the proof context any important propositions about the term. Then, when you [destruct] the term, it is updated in the extra propositions, too. The [case_eq] tactic is another alternative to this approach, based on saving an equality between the original term and its new form.#</li>#
854 #</ol>#%\end{enumerate}% 839 #</ol>#%\end{enumerate}%
855 #</li># 840 #</li>#
856 841