annotate src/Match.v @ 331:2eeb96aa0426

Finish adding Match exercises
author Adam Chlipala <adam@chlipala.net>
date Sun, 02 Oct 2011 16:34:15 -0400
parents 49c6aa7d2148
children 386b7ad8849b
rev   line source
adam@297 1 (* Copyright (c) 2008-2011, Adam Chlipala
adamc@132 2 *
adamc@132 3 * This work is licensed under a
adamc@132 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@132 5 * Unported License.
adamc@132 6 * The license text is available at:
adamc@132 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@132 8 *)
adamc@132 9
adamc@132 10 (* begin hide *)
adamc@132 11 Require Import List.
adamc@132 12
adam@314 13 Require Import CpdtTactics.
adamc@132 14
adamc@132 15 Set Implicit Arguments.
adamc@132 16 (* end hide *)
adamc@132 17
adamc@132 18
adam@324 19 (** %\chapter{Proof Search in Ltac}% *)
adamc@132 20
adam@328 21 (** We have seen many examples of proof automation so far, some with tantalizing code snippets from Ltac, Coq's domain-specific language for proof search procedures. This chapter aims to give a bottom-up presentation of the features of Ltac, focusing in particular on the Ltac %\index{tactics!match}%[match] construct, which supports a novel approach to backtracking search. First, though, we will run through some useful automation tactics that are built into Coq. They are described in detail in the manual, so we only outline what is possible. *)
adamc@132 22
adamc@132 23 (** * Some Built-In Automation Tactics *)
adamc@132 24
adam@328 25 (** A number of tactics are called repeatedly by [crush]. The %\index{tactics!intuition}%[intuition] tactic simplifies propositional structure of goals. The %\index{tactics!congruence}%[congruence] tactic applies the rules of equality and congruence closure, plus properties of constructors of inductive types. The %\index{tactics!omega}%[omega] tactic provides a complete decision procedure for a theory that is called %\index{linear arithmetic}%quantifier-free linear arithmetic or %\index{Presburger arithmetic}%Presburger arithmetic, depending on whom you ask. That is, [omega] proves any goal that follows from looking only at parts of that goal that can be interpreted as propositional formulas whose atomic formulas are basic comparison operations on natural numbers or integers.
adamc@132 26
adam@328 27 The %\index{tactics!ring}%[ring] tactic solves goals by appealing to the axioms of rings or semi-rings (as in algebra), depending on the type involved. Coq developments may declare new types to be parts of rings and semi-rings by proving the associated axioms. There is a similar tactic %\index{tactics!field}\coqdockw{%#<tt>#field#</tt>#%}% for simplifying values in fields by conversion to fractions over rings. Both [ring] and %\coqdockw{%#<tt>#field#</tt>#%}% can only solve goals that are equalities. The %\index{tactics!fourier}\coqdockw{%#<tt>#fourier#</tt>#%}% tactic uses Fourier's method to prove inequalities over real numbers, which are axiomatized in the Coq standard library.
adamc@132 28
adam@328 29 The %\index{setoids}\textit{%#<i>#setoid#</i>#%}% facility makes it possible to register new equivalence relations to be understood by tactics like [rewrite]. For instance, [Prop] is registered as a setoid with the equivalence relation %``%#"#if and only if.#"#%''% The ability to register new setoids can be very useful in proofs of a kind common in math, where all reasoning is done after %``%#"#modding out by a relation.#"#%''%
adam@328 30
adam@328 31 There are several other built-in %``%#"#black box#"#%''% automation tactics, which one can learn about by perusing the Coq manual. The real promise of Coq, though, is in the coding of problem-specific tactics with Ltac. *)
adamc@132 32
adamc@132 33
adamc@135 34 (** * Ltac Programming Basics *)
adamc@135 35
adam@328 36 (** We have already seen many examples of Ltac programs. In the rest of this chapter, we attempt to give a thorough introduction to the important features and design patterns.
adamc@135 37
adamc@135 38 One common use for [match] tactics is identification of subjects for case analysis, as we see in this tactic definition. *)
adamc@135 39
adamc@141 40 (* begin thide *)
adamc@135 41 Ltac find_if :=
adamc@135 42 match goal with
adamc@135 43 | [ |- if ?X then _ else _ ] => destruct X
adamc@135 44 end.
adamc@141 45 (* end thide *)
adamc@135 46
adamc@135 47 (** The tactic checks if the conclusion is an [if], [destruct]ing the test expression if so. Certain classes of theorem are trivial to prove automatically with such a tactic. *)
adamc@135 48
adamc@135 49 Theorem hmm : forall (a b c : bool),
adamc@135 50 if a
adamc@135 51 then if b
adamc@135 52 then True
adamc@135 53 else True
adamc@135 54 else if c
adamc@135 55 then True
adamc@135 56 else True.
adamc@141 57 (* begin thide *)
adamc@135 58 intros; repeat find_if; constructor.
adamc@135 59 Qed.
adamc@141 60 (* end thide *)
adamc@135 61
adam@328 62 (** The %\index{tactics!repeat}%[repeat] that we use here is called a %\index{tactical}\textit{%#<i>#tactical#</i>#%}%, or tactic combinator. The behavior of [repeat t] is to loop through running [t], running [t] on all generated subgoals, running [t] on %\textit{%#<i>#their#</i>#%}% generated subgoals, and so on. When [t] fails at any point in this search tree, that particular subgoal is left to be handled by later tactics. Thus, it is important never to use [repeat] with a tactic that always succeeds.
adamc@135 63
adam@328 64 Another very useful Ltac building block is %\index{context patterns}\textit{%#<i>#context patterns#</i>#%}%. *)
adamc@135 65
adamc@141 66 (* begin thide *)
adamc@135 67 Ltac find_if_inside :=
adamc@135 68 match goal with
adamc@135 69 | [ |- context[if ?X then _ else _] ] => destruct X
adamc@135 70 end.
adamc@141 71 (* end thide *)
adamc@135 72
adamc@135 73 (** The behavior of this tactic is to find any subterm of the conclusion that is an [if] and then [destruct] the test expression. This version subsumes [find_if]. *)
adamc@135 74
adamc@135 75 Theorem hmm' : forall (a b c : bool),
adamc@135 76 if a
adamc@135 77 then if b
adamc@135 78 then True
adamc@135 79 else True
adamc@135 80 else if c
adamc@135 81 then True
adamc@135 82 else True.
adamc@141 83 (* begin thide *)
adamc@135 84 intros; repeat find_if_inside; constructor.
adamc@135 85 Qed.
adamc@141 86 (* end thide *)
adamc@135 87
adamc@135 88 (** We can also use [find_if_inside] to prove goals that [find_if] does not simplify sufficiently. *)
adamc@135 89
adamc@141 90 Theorem hmm2 : forall (a b : bool),
adamc@135 91 (if a then 42 else 42) = (if b then 42 else 42).
adamc@141 92 (* begin thide *)
adamc@135 93 intros; repeat find_if_inside; reflexivity.
adamc@135 94 Qed.
adamc@141 95 (* end thide *)
adamc@135 96
adam@288 97 (** Many decision procedures can be coded in Ltac via %``%#"#[repeat match] loops.#"#%''% For instance, we can implement a subset of the functionality of [tauto]. *)
adamc@135 98
adamc@141 99 (* begin thide *)
adamc@135 100 Ltac my_tauto :=
adamc@135 101 repeat match goal with
adamc@135 102 | [ H : ?P |- ?P ] => exact H
adamc@135 103
adamc@135 104 | [ |- True ] => constructor
adamc@135 105 | [ |- _ /\ _ ] => constructor
adamc@135 106 | [ |- _ -> _ ] => intro
adamc@135 107
adamc@135 108 | [ H : False |- _ ] => destruct H
adamc@135 109 | [ H : _ /\ _ |- _ ] => destruct H
adamc@135 110 | [ H : _ \/ _ |- _ ] => destruct H
adamc@135 111
adam@328 112 | [ H1 : ?P -> ?Q, H2 : ?P |- _ ] => specialize (H1 H2)
adamc@135 113 end.
adamc@141 114 (* end thide *)
adamc@135 115
adam@328 116 (** Since [match] patterns can share unification variables between hypothesis and conclusion patterns, it is easy to figure out when the conclusion matches a hypothesis. The %\index{tactics!exact}%[exact] tactic solves a goal completely when given a proof term of the proper type.
adamc@135 117
adam@328 118 It is also trivial to implement the introduction rules (in the sense of %\index{natural deduction}%natural deduction%~\cite{TAPLNatDed}%) for a few of the connectives. Implementing elimination rules is only a little more work, since we must give a name for a hypothesis to [destruct].
adamc@135 119
adam@328 120 The last rule implements modus ponens, using a tactic %\index{tactics!specialize}\coqdockw{%#<tt>#specialize#</tt>#%}% which will replace a hypothesis with a version that is specialized to a provided set of arguments (for quantified variables or local hypotheses from implications). *)
adamc@135 121
adamc@135 122 Section propositional.
adamc@135 123 Variables P Q R : Prop.
adamc@135 124
adamc@138 125 Theorem propositional : (P \/ Q \/ False) /\ (P -> Q) -> True /\ Q.
adamc@141 126 (* begin thide *)
adamc@135 127 my_tauto.
adamc@135 128 Qed.
adamc@141 129 (* end thide *)
adamc@135 130 End propositional.
adamc@135 131
adam@328 132 (** It was relatively easy to implement modus ponens, because we do not lose information by clearing every implication that we use. If we want to implement a similarly complete procedure for quantifier instantiation, we need a way to ensure that a particular proposition is not already included among our hypotheses. To do that effectively, we first need to learn a bit more about the semantics of [match].
adamc@135 133
adamc@135 134 It is tempting to assume that [match] works like it does in ML. In fact, there are a few critical differences in its behavior. One is that we may include arbitrary expressions in patterns, instead of being restricted to variables and constructors. Another is that the same variable may appear multiple times, inducing an implicit equality constraint.
adamc@135 135
adam@328 136 There is a related pair of two other differences that are much more important than the others. The [match] construct has a %\textit{%#<i>#backtracking semantics for failure#</i>#%}%. In ML, pattern matching works by finding the first pattern to match and then executing its body. If the body raises an exception, then the overall match raises the same exception. In Coq, failures in case bodies instead trigger continued search through the list of cases.
adamc@135 137
adamc@135 138 For instance, this (unnecessarily verbose) proof script works: *)
adamc@135 139
adamc@135 140 Theorem m1 : True.
adamc@135 141 match goal with
adamc@135 142 | [ |- _ ] => intro
adamc@135 143 | [ |- True ] => constructor
adamc@135 144 end.
adamc@141 145 (* begin thide *)
adamc@135 146 Qed.
adamc@141 147 (* end thide *)
adamc@135 148
adamc@135 149 (** The first case matches trivially, but its body tactic fails, since the conclusion does not begin with a quantifier or implication. In a similar ML match, that would mean that the whole pattern-match fails. In Coq, we backtrack and try the next pattern, which also matches. Its body tactic succeeds, so the overall tactic succeeds as well.
adamc@135 150
adamc@135 151 The example shows how failure can move to a different pattern within a [match]. Failure can also trigger an attempt to find %\textit{%#<i>#a different way of matching a single pattern#</i>#%}%. Consider another example: *)
adamc@135 152
adamc@135 153 Theorem m2 : forall P Q R : Prop, P -> Q -> R -> Q.
adamc@135 154 intros; match goal with
adamc@220 155 | [ H : _ |- _ ] => idtac H
adamc@135 156 end.
adamc@135 157
adam@328 158 (** Coq prints %``%#"#[H1]#"#%''%. By applying %\index{tactics!idtac}%[idtac] with an argument, a convenient debugging tool for %``%#"#leaking information out of [match]es,#"#%''% we see that this [match] first tries binding [H] to [H1], which cannot be used to prove [Q]. Nonetheless, the following variation on the tactic succeeds at proving the goal: *)
adamc@135 159
adamc@141 160 (* begin thide *)
adamc@135 161 match goal with
adamc@135 162 | [ H : _ |- _ ] => exact H
adamc@135 163 end.
adamc@135 164 Qed.
adamc@141 165 (* end thide *)
adamc@135 166
adamc@135 167 (** The tactic first unifies [H] with [H1], as before, but [exact H] fails in that case, so the tactic engine searches for more possible values of [H]. Eventually, it arrives at the correct value, so that [exact H] and the overall tactic succeed. *)
adamc@135 168
adamc@135 169 (** Now we are equipped to implement a tactic for checking that a proposition is not among our hypotheses: *)
adamc@135 170
adamc@141 171 (* begin thide *)
adamc@135 172 Ltac notHyp P :=
adamc@135 173 match goal with
adamc@135 174 | [ _ : P |- _ ] => fail 1
adamc@135 175 | _ =>
adamc@135 176 match P with
adamc@135 177 | ?P1 /\ ?P2 => first [ notHyp P1 | notHyp P2 | fail 2 ]
adamc@135 178 | _ => idtac
adamc@135 179 end
adamc@135 180 end.
adamc@141 181 (* end thide *)
adamc@135 182
adam@328 183 (** We use the equality checking that is built into pattern-matching to see if there is a hypothesis that matches the proposition exactly. If so, we use the %\index{tactics!fail}%[fail] tactic. Without arguments, [fail] signals normal tactic failure, as you might expect. When [fail] is passed an argument [n], [n] is used to count outwards through the enclosing cases of backtracking search. In this case, [fail 1] says %``%#"#fail not just in this pattern-matching branch, but for the whole [match].#"#%''% The second case will never be tried when the [fail 1] is reached.
adamc@135 184
adam@328 185 This second case, used when [P] matches no hypothesis, checks if [P] is a conjunction. Other simplifications may have split conjunctions into their component formulas, so we need to check that at least one of those components is also not represented. To achieve this, we apply the %\index{tactics!first}%[first] tactical, which takes a list of tactics and continues down the list until one of them does not fail. The [fail 2] at the end says to [fail] both the [first] and the [match] wrapped around it.
adamc@135 186
adam@328 187 The body of the [?P1 /\ ?P2] case guarantees that, if it is reached, we either succeed completely or fail completely. Thus, if we reach the wildcard case, [P] is not a conjunction. We use %\index{tactics!idtac}%[idtac], a tactic that would be silly to apply on its own, since its effect is to succeed at doing nothing. Nonetheless, [idtac] is a useful placeholder for cases like what we see here.
adamc@135 188
adamc@135 189 With the non-presence check implemented, it is easy to build a tactic that takes as input a proof term and adds its conclusion as a new hypothesis, only if that conclusion is not already present, failing otherwise. *)
adamc@135 190
adamc@141 191 (* begin thide *)
adamc@135 192 Ltac extend pf :=
adamc@135 193 let t := type of pf in
adamc@135 194 notHyp t; generalize pf; intro.
adamc@141 195 (* end thide *)
adamc@135 196
adam@328 197 (** We see the useful %\index{tactics!type of}%[type of] operator of Ltac. This operator could not be implemented in Gallina, but it is easy to support in Ltac. We end up with [t] bound to the type of [pf]. We check that [t] is not already present. If so, we use a [generalize]/[intro] combo to add a new hypothesis proved by [pf]. The tactic %\index{tactics!generalize}%[generalize] takes as input a term [t] (for instance, a proof of some proposition) and then changes the conclusion from [G] to [T -> G], where [T] is the type of [t] (for instance, the proposition proved by a proof given as argument).
adamc@135 198
adamc@135 199 With these tactics defined, we can write a tactic [completer] for adding to the context all consequences of a set of simple first-order formulas. *)
adamc@135 200
adamc@141 201 (* begin thide *)
adamc@135 202 Ltac completer :=
adamc@135 203 repeat match goal with
adamc@135 204 | [ |- _ /\ _ ] => constructor
adamc@135 205 | [ H : _ /\ _ |- _ ] => destruct H
adam@328 206 | [ H : ?P -> ?Q, H' : ?P |- _ ] => specialize (H H')
adamc@135 207 | [ |- forall x, _ ] => intro
adamc@135 208
adam@328 209 | [ H : forall x, ?P x -> _, H' : ?P ?X |- _ ] => extend (H X H')
adamc@135 210 end.
adamc@141 211 (* end thide *)
adamc@135 212
adamc@135 213 (** We use the same kind of conjunction and implication handling as previously. Note that, since [->] is the special non-dependent case of [forall], the fourth rule handles [intro] for implications, too.
adamc@135 214
adamc@135 215 In the fifth rule, when we find a [forall] fact [H] with a premise matching one of our hypotheses, we add the appropriate instantiation of [H]'s conclusion, if we have not already added it.
adamc@135 216
adamc@135 217 We can check that [completer] is working properly: *)
adamc@135 218
adamc@135 219 Section firstorder.
adamc@135 220 Variable A : Set.
adamc@135 221 Variables P Q R S : A -> Prop.
adamc@135 222
adamc@135 223 Hypothesis H1 : forall x, P x -> Q x /\ R x.
adamc@135 224 Hypothesis H2 : forall x, R x -> S x.
adamc@135 225
adamc@135 226 Theorem fo : forall x, P x -> S x.
adamc@141 227 (* begin thide *)
adamc@135 228 completer.
adamc@135 229 (** [[
adamc@135 230 x : A
adamc@135 231 H : P x
adamc@135 232 H0 : Q x
adamc@135 233 H3 : R x
adamc@135 234 H4 : S x
adamc@135 235 ============================
adamc@135 236 S x
adam@302 237 ]]
adam@302 238 *)
adamc@135 239
adamc@135 240 assumption.
adamc@135 241 Qed.
adamc@141 242 (* end thide *)
adamc@135 243 End firstorder.
adamc@135 244
adamc@135 245 (** We narrowly avoided a subtle pitfall in our definition of [completer]. Let us try another definition that even seems preferable to the original, to the untrained eye. *)
adamc@135 246
adamc@141 247 (* begin thide *)
adamc@135 248 Ltac completer' :=
adamc@135 249 repeat match goal with
adamc@135 250 | [ |- _ /\ _ ] => constructor
adamc@135 251 | [ H : _ /\ _ |- _ ] => destruct H
adam@328 252 | [ H : ?P -> _, H' : ?P |- _ ] => specialize (H H')
adamc@135 253 | [ |- forall x, _ ] => intro
adamc@135 254
adam@328 255 | [ H : forall x, ?P x -> _, H' : ?P ?X |- _ ] => extend (H X H')
adamc@135 256 end.
adamc@141 257 (* end thide *)
adamc@135 258
adam@328 259 (** The only difference is in the modus ponens rule, where we have replaced an unused unification variable [?][Q] with a wildcard. Let us try our example again with this version: *)
adamc@135 260
adamc@135 261 Section firstorder'.
adamc@135 262 Variable A : Set.
adamc@135 263 Variables P Q R S : A -> Prop.
adamc@135 264
adamc@135 265 Hypothesis H1 : forall x, P x -> Q x /\ R x.
adamc@135 266 Hypothesis H2 : forall x, R x -> S x.
adamc@135 267
adamc@135 268 Theorem fo' : forall x, P x -> S x.
adamc@141 269 (* begin thide *)
adamc@135 270 (** [[
adamc@135 271 completer'.
adamc@220 272
adamc@205 273 ]]
adamc@205 274
adamc@135 275 Coq loops forever at this point. What went wrong? *)
adamc@220 276
adamc@135 277 Abort.
adamc@141 278 (* end thide *)
adamc@135 279 End firstorder'.
adamc@136 280
adamc@136 281 (** A few examples should illustrate the issue. Here we see a [match]-based proof that works fine: *)
adamc@136 282
adamc@136 283 Theorem t1 : forall x : nat, x = x.
adamc@136 284 match goal with
adamc@136 285 | [ |- forall x, _ ] => trivial
adamc@136 286 end.
adamc@141 287 (* begin thide *)
adamc@136 288 Qed.
adamc@141 289 (* end thide *)
adamc@136 290
adamc@136 291 (** This one fails. *)
adamc@136 292
adamc@141 293 (* begin thide *)
adamc@136 294 Theorem t1' : forall x : nat, x = x.
adamc@136 295 (** [[
adamc@136 296 match goal with
adamc@136 297 | [ |- forall x, ?P ] => trivial
adamc@136 298 end.
adam@328 299 ]]
adamc@136 300
adam@328 301 <<
adamc@136 302 User error: No matching clauses for match goal
adam@328 303 >>
adam@328 304 *)
adamc@220 305
adamc@136 306 Abort.
adamc@141 307 (* end thide *)
adamc@136 308
adam@328 309 (** The problem is that unification variables may not contain locally bound variables. In this case, [?][P] would need to be bound to [x = x], which contains the local quantified variable [x]. By using a wildcard in the earlier version, we avoided this restriction. To understand why this applies to the [completer] tactics, recall that, in Coq, implication is shorthand for degenerate universal quantification where the quantified variable is not used. Nonetheless, in an Ltac pattern, Coq is happy to match a wildcard implication against a universal quantification.
adamc@136 310
adam@288 311 The Coq 8.2 release includes a special pattern form for a unification variable with an explicit set of free variables. That unification variable is then bound to a function from the free variables to the %``%#"#real#"#%''% value. In Coq 8.1 and earlier, there is no such workaround.
adamc@136 312
adam@288 313 No matter which version you use, it is important to be aware of this restriction. As we have alluded to, the restriction is the culprit behind the infinite-looping behavior of [completer']. We unintentionally match quantified facts with the modus ponens rule, circumventing the %``%#"#already present#"#%''% check and leading to different behavior, where the same fact may be added to the context repeatedly in an infinite loop. Our earlier [completer] tactic uses a modus ponens rule that matches the implication conclusion with a variable, which blocks matching against non-trivial universal quantifiers. *)
adamc@137 314
adamc@137 315
adamc@137 316 (** * Functional Programming in Ltac *)
adamc@137 317
adamc@141 318 (* EX: Write a list length function in Ltac. *)
adamc@141 319
adamc@137 320 (** Ltac supports quite convenient functional programming, with a Lisp-with-syntax kind of flavor. However, there are a few syntactic conventions involved in getting programs to be accepted. The Ltac syntax is optimized for tactic-writing, so one has to deal with some inconveniences in writing more standard functional programs.
adamc@137 321
adamc@137 322 To illustrate, let us try to write a simple list length function. We start out writing it just like in Gallina, simply replacing [Fixpoint] (and its annotations) with [Ltac].
adamc@137 323
adamc@137 324 [[
adamc@137 325 Ltac length ls :=
adamc@137 326 match ls with
adamc@137 327 | nil => O
adamc@137 328 | _ :: ls' => S (length ls')
adamc@137 329 end.
adam@328 330 ]]
adamc@137 331
adam@328 332 <<
adamc@137 333 Error: The reference ls' was not found in the current environment
adam@328 334 >>
adamc@137 335
adamc@137 336 At this point, we hopefully remember that pattern variable names must be prefixed by question marks in Ltac.
adamc@137 337
adamc@137 338 [[
adamc@137 339 Ltac length ls :=
adamc@137 340 match ls with
adamc@137 341 | nil => O
adamc@137 342 | _ :: ?ls' => S (length ls')
adamc@137 343 end.
adamc@137 344 ]]
adamc@137 345
adam@328 346 <<
adam@328 347 Error: The reference S was not found in the current environment
adam@328 348 >>
adam@328 349
adam@328 350 The problem is that Ltac treats the expression [S (][length ls')] as an invocation of a tactic [S] with argument [length ls']. We need to use a special annotation to %``%#"#escape into#"#%''% the Gallina parsing nonterminal.%\index{tactics!constr}% *)
adamc@137 351
adamc@141 352 (* begin thide *)
adamc@137 353 Ltac length ls :=
adamc@137 354 match ls with
adamc@137 355 | nil => O
adamc@137 356 | _ :: ?ls' => constr:(S (length ls'))
adamc@137 357 end.
adamc@137 358
adamc@137 359 (** This definition is accepted. It can be a little awkward to test Ltac definitions like this. Here is one method. *)
adamc@137 360
adamc@137 361 Goal False.
adamc@137 362 let n := length (1 :: 2 :: 3 :: nil) in
adamc@137 363 pose n.
adamc@137 364 (** [[
adamc@137 365 n := S (length (2 :: 3 :: nil)) : nat
adamc@137 366 ============================
adamc@137 367 False
adamc@220 368
adamc@137 369 ]]
adamc@137 370
adam@328 371 We use the %\index{tactics!pose}%[pose] tactic, which extends the proof context with a new variable that is set equal to a particular term. We could also have used [idtac n] in place of [pose n], which would have printed the result without changing the context.
adamc@220 372
adam@328 373 The value of [n] only has the length calculation unrolled one step. What has happened here is that, by escaping into the [constr] nonterminal, we referred to the [length] function of Gallina, rather than the [length] Ltac function that we are defining. *)
adamc@220 374
adamc@220 375 Abort.
adamc@137 376
adam@328 377 (* begin hide *)
adamc@137 378 Reset length.
adam@328 379 (* end hide *)
adam@328 380 (** %\noindent\coqdockw{%#<tt>#Reset#</tt>#%}% [length.] *)
adamc@137 381
adamc@137 382 (** The thing to remember is that Gallina terms built by tactics must be bound explicitly via [let] or a similar technique, rather than inserting Ltac calls directly in other Gallina terms. *)
adamc@137 383
adamc@137 384 Ltac length ls :=
adamc@137 385 match ls with
adamc@137 386 | nil => O
adamc@137 387 | _ :: ?ls' =>
adamc@137 388 let ls'' := length ls' in
adamc@137 389 constr:(S ls'')
adamc@137 390 end.
adamc@137 391
adamc@137 392 Goal False.
adamc@137 393 let n := length (1 :: 2 :: 3 :: nil) in
adamc@137 394 pose n.
adamc@137 395 (** [[
adamc@137 396 n := 3 : nat
adamc@137 397 ============================
adamc@137 398 False
adam@302 399 ]]
adam@302 400 *)
adamc@220 401
adamc@137 402 Abort.
adamc@141 403 (* end thide *)
adamc@141 404
adamc@141 405 (* EX: Write a list map function in Ltac. *)
adamc@137 406
adamc@137 407 (** We can also use anonymous function expressions and local function definitions in Ltac, as this example of a standard list [map] function shows. *)
adamc@137 408
adamc@141 409 (* begin thide *)
adamc@137 410 Ltac map T f :=
adamc@137 411 let rec map' ls :=
adamc@137 412 match ls with
adam@288 413 | nil => constr:( @nil T)
adamc@137 414 | ?x :: ?ls' =>
adamc@137 415 let x' := f x in
adamc@137 416 let ls'' := map' ls' in
adam@288 417 constr:( x' :: ls'')
adamc@137 418 end in
adamc@137 419 map'.
adamc@137 420
adam@328 421 (** Ltac functions can have no implicit arguments. It may seem surprising that we need to pass [T], the carried type of the output list, explicitly. We cannot just use [type of f], because [f] is an Ltac term, not a Gallina term, and Ltac programs are dynamically typed. The function [f] could use very syntactic methods to decide to return differently typed terms for different inputs. We also could not replace [constr:( @][nil T)] with [constr: nil], because we have no strongly typed context to use to infer the parameter to [nil]. Luckily, we do have sufficient context within [constr:( x' :: ls'')].
adamc@137 422
adam@288 423 Sometimes we need to employ the opposite direction of %``%#"#nonterminal escape,#"#%''% when we want to pass a complicated tactic expression as an argument to another tactic, as we might want to do in invoking [map]. *)
adamc@137 424
adamc@137 425 Goal False.
adam@288 426 let ls := map (nat * nat)%type ltac:(fun x => constr:( x, x)) (1 :: 2 :: 3 :: nil) in
adamc@137 427 pose ls.
adamc@137 428 (** [[
adamc@137 429 l := (1, 1) :: (2, 2) :: (3, 3) :: nil : list (nat * nat)
adamc@137 430 ============================
adamc@137 431 False
adam@302 432 ]]
adam@302 433 *)
adamc@220 434
adamc@137 435 Abort.
adamc@141 436 (* end thide *)
adamc@137 437
adam@328 438 (** One other gotcha shows up when we want to debug our Ltac functional programs. We might expect the following code to work, to give us a version of [length] that prints a debug trace of the arguments it is called with. *)
adam@328 439
adam@328 440 (* begin hide *)
adam@328 441 Reset length.
adam@328 442 (* end hide *)
adam@328 443 (** %\noindent\coqdockw{%#<tt>#Reset#</tt>#%}% [length.] *)
adam@328 444
adam@328 445 Ltac length ls :=
adam@328 446 idtac ls;
adam@328 447 match ls with
adam@328 448 | nil => O
adam@328 449 | _ :: ?ls' =>
adam@328 450 let ls'' := length ls' in
adam@328 451 constr:(S ls'')
adam@328 452 end.
adam@328 453
adam@328 454 (** Coq accepts the tactic definition, but the code is fatally flawed and will always lead to dynamic type errors. *)
adam@328 455
adam@328 456 Goal False.
adam@328 457 (** %\vspace{-.15in}%[[
adam@328 458 let n := length (1 :: 2 :: 3 :: nil) in
adam@328 459 pose n.
adam@328 460 ]]
adam@328 461
adam@328 462 <<
adam@328 463 Error: variable n should be bound to a term.
adam@328 464 >> *)
adam@328 465 Abort.
adam@328 466
adam@328 467 (** What is going wrong here? The answer has to do with the dual status of Ltac as both a purely functional and an imperative programming language. The basic programming language is purely functional, but tactic scripts are one %``%#"#datatype#"#%''% that can be returned by such programs, and Coq will run such a script using an imperative semantics that mutates proof states. Readers familiar with %\index{monads}\index{Haskell}%monadic programming in Haskell%~\cite{monads,IO}% may recognize a similarity. Side-effecting Haskell programs can be thought of as pure programs that return %\emph{%#<i>#the code of programs in an imperative language#</i>#%}%, where some out-of-band mechanism takes responsibility for running these derived programs. In this way, Haskell remains pure, while supporting usual input-output side effects and more. Ltac uses the same basic mechanism, but in a dynamically typed setting. Here the embedded imperative language includes all the tactics we have been applying so far.
adam@328 468
adam@328 469 Even basic [idtac] is an embedded imperative program, so we may not automatically mix it with purely functional code. In fact, a semicolon operator alone marks a span of Ltac code as an embedded tactic script. This makes some amount of sense, since pure functional languages have no need for sequencing: since they lack side effects, there is no reason to run an expression and then just throw away its value and move on to another expression.
adam@328 470
adam@328 471 The solution is like in Haskell: we must %``%#"#monadify#"#%''% our pure program to give it access to side effects. The trouble is that the embedded tactic language has no [return] construct. Proof scripts are about proving theorems, not calculating results. We can apply a somewhat awkward workaround that requires translating our program into %\index{continuation-passing style}\emph{%#<i>#continuation-passing style#</i>#%}%, a program structuring idea popular in functional programming. *)
adam@328 472
adam@328 473 (* begin hide *)
adam@328 474 Reset length.
adam@328 475 (* end hide *)
adam@328 476 (** %\noindent\coqdockw{%#<tt>#Reset#</tt>#%}% [length.] *)
adam@328 477
adam@328 478 Ltac length ls k :=
adam@328 479 idtac ls;
adam@328 480 match ls with
adam@328 481 | nil => k O
adam@328 482 | _ :: ?ls' => length ls' ltac:(fun n => k (S n))
adam@328 483 end.
adam@328 484
adam@328 485 (** The new [length] takes a new input: a %\emph{%#<i>#continuation#</i>#%}% [k], which is a function to be called to continue whatever proving process we were in the middle of when we called [length]. The argument passed to [k] may be thought of as the return value of [length]. *)
adam@328 486
adam@328 487 Goal False.
adam@328 488 length (1 :: 2 :: 3 :: nil) ltac:(fun n => pose n).
adam@328 489 (** [[
adam@328 490 (1 :: 2 :: 3 :: nil)
adam@328 491 (2 :: 3 :: nil)
adam@328 492 (3 :: nil)
adam@328 493 nil
adam@328 494 ]]
adam@328 495 *)
adam@328 496 Abort.
adam@328 497
adam@328 498 (** We see exactly the trace of function arguments that we expected initially, and an examination of the proof state afterward would show that variable [n] has been added with value [3]. *)
adam@328 499
adamc@138 500
adamc@139 501 (** * Recursive Proof Search *)
adamc@139 502
adamc@139 503 (** Deciding how to instantiate quantifiers is one of the hardest parts of automated first-order theorem proving. For a given problem, we can consider all possible bounded-length sequences of quantifier instantiations, applying only propositional reasoning at the end. This is probably a bad idea for almost all goals, but it makes for a nice example of recursive proof search procedures in Ltac.
adamc@139 504
adam@288 505 We can consider the maximum %``%#"#dependency chain#"#%''% length for a first-order proof. We define the chain length for a hypothesis to be 0, and the chain length for an instantiation of a quantified fact to be one greater than the length for that fact. The tactic [inster n] is meant to try all possible proofs with chain length at most [n]. *)
adamc@139 506
adamc@141 507 (* begin thide *)
adamc@139 508 Ltac inster n :=
adamc@139 509 intuition;
adamc@139 510 match n with
adamc@139 511 | S ?n' =>
adamc@139 512 match goal with
adamc@139 513 | [ H : forall x : ?T, _, x : ?T |- _ ] => generalize (H x); inster n'
adamc@139 514 end
adamc@139 515 end.
adamc@141 516 (* end thide *)
adamc@139 517
adam@328 518 (** The tactic begins by applying propositional simplification. Next, it checks if any chain length remains. If so, it tries all possible ways of instantiating quantified hypotheses with properly typed local variables. It is critical to realize that, if the recursive call [inster n'] fails, then the [match goal] just seeks out another way of unifying its pattern against proof state. Thus, this small amount of code provides an elegant demonstration of how backtracking [match] enables exhaustive search.
adamc@139 519
adamc@139 520 We can verify the efficacy of [inster] with two short examples. The built-in [firstorder] tactic (with no extra arguments) is able to prove the first but not the second. *)
adamc@139 521
adamc@139 522 Section test_inster.
adamc@139 523 Variable A : Set.
adamc@139 524 Variables P Q : A -> Prop.
adamc@139 525 Variable f : A -> A.
adamc@139 526 Variable g : A -> A -> A.
adamc@139 527
adamc@139 528 Hypothesis H1 : forall x y, P (g x y) -> Q (f x).
adamc@139 529
adam@328 530 Theorem test_inster : forall x, P (g x x) -> Q (f x).
adamc@220 531 inster 2.
adamc@139 532 Qed.
adamc@139 533
adamc@139 534 Hypothesis H3 : forall u v, P u /\ P v /\ u <> v -> P (g u v).
adamc@139 535 Hypothesis H4 : forall u, Q (f u) -> P u /\ P (f u).
adamc@139 536
adamc@139 537 Theorem test_inster2 : forall x y, x <> y -> P x -> Q (f y) -> Q (f x).
adamc@220 538 inster 3.
adamc@139 539 Qed.
adamc@139 540 End test_inster.
adamc@139 541
adam@328 542 (** The style employed in the definition of [inster] can seem very counterintuitive to functional programmers. Usually, functional programs accumulate state changes in explicit arguments to recursive functions. In Ltac, the state of the current subgoal is always implicit. Nonetheless, in contrast to general imperative programming, it is easy to undo any changes to this state, and indeed such %``%#"#undoing#"#%''% happens automatically at failures within [match]es. In this way, Ltac programming is similar to programming in Haskell with a stateful failure monad that supports a composition operator along the lines of the [first] tactical. The key pieces of state include not only the form of the goal, but also decisions about the values of unification variables. These decisions are rolled back with all the other state after failure.
adamc@140 543
adam@288 544 Functional programming purists may react indignantly to the suggestion of programming this way. Nonetheless, as with other kinds of %``%#"#monadic programming,#"#%''% many problems are much simpler to solve with Ltac than they would be with explicit, pure proof manipulation in ML or Haskell. To demonstrate, we will write a basic simplification procedure for logical implications.
adamc@140 545
adam@328 546 This procedure is inspired by one for separation logic%~\cite{separation}%, where conjuncts in formulas are thought of as %``%#"#resources,#"#%''% such that we lose no completeness by %``%#"#crossing out#"#%''% equal conjuncts on the two sides of an implication. This process is complicated by the fact that, for reasons of modularity, our formulas can have arbitrary nested tree structure (branching at conjunctions) and may include existential quantifiers. It is helpful for the matching process to %``%#"#go under#"#%''% quantifiers and in fact decide how to instantiate existential quantifiers in the conclusion.
adamc@140 547
adam@288 548 To distinguish the implications that our tactic handles from the implications that will show up as %``%#"#plumbing#"#%''% in various lemmas, we define a wrapper definition, a notation, and a tactic. *)
adamc@138 549
adamc@138 550 Definition imp (P1 P2 : Prop) := P1 -> P2.
adamc@140 551 Infix "-->" := imp (no associativity, at level 95).
adamc@140 552 Ltac imp := unfold imp; firstorder.
adamc@138 553
adamc@140 554 (** These lemmas about [imp] will be useful in the tactic that we will write. *)
adamc@138 555
adamc@138 556 Theorem and_True_prem : forall P Q,
adamc@138 557 (P /\ True --> Q)
adamc@138 558 -> (P --> Q).
adamc@138 559 imp.
adamc@138 560 Qed.
adamc@138 561
adamc@138 562 Theorem and_True_conc : forall P Q,
adamc@138 563 (P --> Q /\ True)
adamc@138 564 -> (P --> Q).
adamc@138 565 imp.
adamc@138 566 Qed.
adamc@138 567
adamc@138 568 Theorem assoc_prem1 : forall P Q R S,
adamc@138 569 (P /\ (Q /\ R) --> S)
adamc@138 570 -> ((P /\ Q) /\ R --> S).
adamc@138 571 imp.
adamc@138 572 Qed.
adamc@138 573
adamc@138 574 Theorem assoc_prem2 : forall P Q R S,
adamc@138 575 (Q /\ (P /\ R) --> S)
adamc@138 576 -> ((P /\ Q) /\ R --> S).
adamc@138 577 imp.
adamc@138 578 Qed.
adamc@138 579
adamc@138 580 Theorem comm_prem : forall P Q R,
adamc@138 581 (P /\ Q --> R)
adamc@138 582 -> (Q /\ P --> R).
adamc@138 583 imp.
adamc@138 584 Qed.
adamc@138 585
adamc@138 586 Theorem assoc_conc1 : forall P Q R S,
adamc@138 587 (S --> P /\ (Q /\ R))
adamc@138 588 -> (S --> (P /\ Q) /\ R).
adamc@138 589 imp.
adamc@138 590 Qed.
adamc@138 591
adamc@138 592 Theorem assoc_conc2 : forall P Q R S,
adamc@138 593 (S --> Q /\ (P /\ R))
adamc@138 594 -> (S --> (P /\ Q) /\ R).
adamc@138 595 imp.
adamc@138 596 Qed.
adamc@138 597
adamc@138 598 Theorem comm_conc : forall P Q R,
adamc@138 599 (R --> P /\ Q)
adamc@138 600 -> (R --> Q /\ P).
adamc@138 601 imp.
adamc@138 602 Qed.
adamc@138 603
adam@288 604 (** The first order of business in crafting our [matcher] tactic will be auxiliary support for searching through formula trees. The [search_prem] tactic implements running its tactic argument [tac] on every subformula of an [imp] premise. As it traverses a tree, [search_prem] applies some of the above lemmas to rewrite the goal to bring different subformulas to the head of the goal. That is, for every subformula [P] of the implication premise, we want [P] to %``%#"#have a turn,#"#%''% where the premise is rearranged into the form [P /\ Q] for some [Q]. The tactic [tac] should expect to see a goal in this form and focus its attention on the first conjunct of the premise. *)
adamc@140 605
adamc@138 606 Ltac search_prem tac :=
adamc@138 607 let rec search P :=
adamc@138 608 tac
adamc@138 609 || (apply and_True_prem; tac)
adamc@138 610 || match P with
adamc@138 611 | ?P1 /\ ?P2 =>
adamc@138 612 (apply assoc_prem1; search P1)
adamc@138 613 || (apply assoc_prem2; search P2)
adamc@138 614 end
adamc@138 615 in match goal with
adamc@138 616 | [ |- ?P /\ _ --> _ ] => search P
adamc@138 617 | [ |- _ /\ ?P --> _ ] => apply comm_prem; search P
adamc@138 618 | [ |- _ --> _ ] => progress (tac || (apply and_True_prem; tac))
adamc@138 619 end.
adamc@138 620
adam@328 621 (** To understand how [search_prem] works, we turn first to the final [match]. If the premise begins with a conjunction, we call the [search] procedure on each of the conjuncts, or only the first conjunct, if that already yields a case where [tac] does not fail. The call [search P] expects and maintains the invariant that the premise is of the form [P /\ Q] for some [Q]. We pass [P] explicitly as a kind of decreasing induction measure, to avoid looping forever when [tac] always fails. The second [match] case calls a commutativity lemma to realize this invariant, before passing control to [search]. The final [match] case tries applying [tac] directly and then, if that fails, changes the form of the goal by adding an extraneous [True] conjunct and calls [tac] again.
adamc@140 622
adam@328 623 The [search] function itself tries the same tricks as in the last case of the final [match]. Additionally, if neither works, it checks if [P] is a conjunction. If so, it calls itself recursively on each conjunct, first applying associativity lemmas to maintain the goal-form invariant.
adamc@140 624
adamc@140 625 We will also want a dual function [search_conc], which does tree search through an [imp] conclusion. *)
adamc@140 626
adamc@138 627 Ltac search_conc tac :=
adamc@138 628 let rec search P :=
adamc@138 629 tac
adamc@138 630 || (apply and_True_conc; tac)
adamc@138 631 || match P with
adamc@138 632 | ?P1 /\ ?P2 =>
adamc@138 633 (apply assoc_conc1; search P1)
adamc@138 634 || (apply assoc_conc2; search P2)
adamc@138 635 end
adamc@138 636 in match goal with
adamc@138 637 | [ |- _ --> ?P /\ _ ] => search P
adamc@138 638 | [ |- _ --> _ /\ ?P ] => apply comm_conc; search P
adamc@138 639 | [ |- _ --> _ ] => progress (tac || (apply and_True_conc; tac))
adamc@138 640 end.
adamc@138 641
adamc@140 642 (** Now we can prove a number of lemmas that are suitable for application by our search tactics. A lemma that is meant to handle a premise should have the form [P /\ Q --> R] for some interesting [P], and a lemma that is meant to handle a conclusion should have the form [P --> Q /\ R] for some interesting [Q]. *)
adamc@140 643
adam@328 644 (* begin thide *)
adamc@138 645 Theorem False_prem : forall P Q,
adamc@138 646 False /\ P --> Q.
adamc@138 647 imp.
adamc@138 648 Qed.
adamc@138 649
adamc@138 650 Theorem True_conc : forall P Q : Prop,
adamc@138 651 (P --> Q)
adamc@138 652 -> (P --> True /\ Q).
adamc@138 653 imp.
adamc@138 654 Qed.
adamc@138 655
adamc@138 656 Theorem Match : forall P Q R : Prop,
adamc@138 657 (Q --> R)
adamc@138 658 -> (P /\ Q --> P /\ R).
adamc@138 659 imp.
adamc@138 660 Qed.
adamc@138 661
adamc@138 662 Theorem ex_prem : forall (T : Type) (P : T -> Prop) (Q R : Prop),
adamc@138 663 (forall x, P x /\ Q --> R)
adamc@138 664 -> (ex P /\ Q --> R).
adamc@138 665 imp.
adamc@138 666 Qed.
adamc@138 667
adamc@138 668 Theorem ex_conc : forall (T : Type) (P : T -> Prop) (Q R : Prop) x,
adamc@138 669 (Q --> P x /\ R)
adamc@138 670 -> (Q --> ex P /\ R).
adamc@138 671 imp.
adamc@138 672 Qed.
adamc@138 673
adam@288 674 (** We will also want a %``%#"#base case#"#%''% lemma for finishing proofs where cancelation has removed every constituent of the conclusion. *)
adamc@140 675
adamc@138 676 Theorem imp_True : forall P,
adamc@138 677 P --> True.
adamc@138 678 imp.
adamc@138 679 Qed.
adamc@138 680
adamc@220 681 (** Our final [matcher] tactic is now straightforward. First, we [intros] all variables into scope. Then we attempt simple premise simplifications, finishing the proof upon finding [False] and eliminating any existential quantifiers that we find. After that, we search through the conclusion. We remove [True] conjuncts, remove existential quantifiers by introducing unification variables for their bound variables, and search for matching premises to cancel. Finally, when no more progress is made, we see if the goal has become trivial and can be solved by [imp_True]. In each case, we use the tactic [simple apply] in place of [apply] to use a simpler, less expensive unification algorithm. *)
adamc@140 682
adamc@138 683 Ltac matcher :=
adamc@138 684 intros;
adam@288 685 repeat search_prem ltac:( simple apply False_prem || ( simple apply ex_prem; intro));
adam@288 686 repeat search_conc ltac:( simple apply True_conc || simple eapply ex_conc
adam@288 687 || search_prem ltac:( simple apply Match));
adamc@204 688 try simple apply imp_True.
adamc@141 689 (* end thide *)
adamc@140 690
adamc@140 691 (** Our tactic succeeds at proving a simple example. *)
adamc@138 692
adamc@138 693 Theorem t2 : forall P Q : Prop,
adamc@138 694 Q /\ (P /\ False) /\ P --> P /\ Q.
adamc@138 695 matcher.
adamc@138 696 Qed.
adamc@138 697
adamc@140 698 (** In the generated proof, we find a trace of the workings of the search tactics. *)
adamc@140 699
adamc@140 700 Print t2.
adamc@220 701 (** %\vspace{-.15in}% [[
adamc@140 702 t2 =
adamc@140 703 fun P Q : Prop =>
adamc@140 704 comm_prem (assoc_prem1 (assoc_prem2 (False_prem (P:=P /\ P /\ Q) (P /\ Q))))
adamc@140 705 : forall P Q : Prop, Q /\ (P /\ False) /\ P --> P /\ Q
adamc@220 706
adamc@220 707 ]]
adamc@140 708
adamc@220 709 We can also see that [matcher] is well-suited for cases where some human intervention is needed after the automation finishes. *)
adamc@140 710
adamc@138 711 Theorem t3 : forall P Q R : Prop,
adamc@138 712 P /\ Q --> Q /\ R /\ P.
adamc@138 713 matcher.
adamc@140 714 (** [[
adamc@140 715 ============================
adamc@140 716 True --> R
adamc@220 717
adamc@140 718 ]]
adamc@140 719
adam@328 720 Our tactic canceled those conjuncts that it was able to cancel, leaving a simplified subgoal for us, much as [intuition] does. *)
adamc@220 721
adamc@138 722 Abort.
adamc@138 723
adam@328 724 (** The [matcher] tactic even succeeds at guessing quantifier instantiations. It is the unification that occurs in uses of the [Match] lemma that does the real work here. *)
adamc@140 725
adamc@138 726 Theorem t4 : forall (P : nat -> Prop) Q, (exists x, P x /\ Q) --> Q /\ (exists x, P x).
adamc@138 727 matcher.
adamc@138 728 Qed.
adamc@138 729
adamc@140 730 Print t4.
adamc@220 731 (** %\vspace{-.15in}% [[
adamc@140 732 t4 =
adamc@140 733 fun (P : nat -> Prop) (Q : Prop) =>
adamc@140 734 and_True_prem
adamc@140 735 (ex_prem (P:=fun x : nat => P x /\ Q)
adamc@140 736 (fun x : nat =>
adamc@140 737 assoc_prem2
adamc@140 738 (Match (P:=Q)
adamc@140 739 (and_True_conc
adamc@140 740 (ex_conc (fun x0 : nat => P x0) x
adamc@140 741 (Match (P:=P x) (imp_True (P:=True))))))))
adamc@140 742 : forall (P : nat -> Prop) (Q : Prop),
adamc@140 743 (exists x : nat, P x /\ Q) --> Q /\ (exists x : nat, P x)
adam@302 744 ]]
adam@302 745 *)
adamc@234 746
adamc@234 747
adamc@234 748 (** * Creating Unification Variables *)
adamc@234 749
adamc@234 750 (** A final useful ingredient in tactic crafting is the ability to allocate new unification variables explicitly. Tactics like [eauto] introduce unification variable internally to support flexible proof search. While [eauto] and its relatives do %\textit{%#<i>#backward#</i>#%}% reasoning, we often want to do similar %\textit{%#<i>#forward#</i>#%}% reasoning, where unification variables can be useful for similar reasons.
adamc@234 751
adam@328 752 For example, we can write a tactic that instantiates the quantifiers of a universally quantified hypothesis. The tactic should not need to know what the appropriate instantiantiations are; rather, we want these choices filled with placeholders. We hope that, when we apply the specialized hypothesis later, syntactic unification will determine concrete values.
adamc@234 753
adamc@234 754 Before we are ready to write a tactic, we can try out its ingredients one at a time. *)
adamc@234 755
adamc@234 756 Theorem t5 : (forall x : nat, S x > x) -> 2 > 1.
adamc@234 757 intros.
adamc@234 758
adamc@234 759 (** [[
adamc@234 760 H : forall x : nat, S x > x
adamc@234 761 ============================
adamc@234 762 2 > 1
adamc@234 763
adamc@234 764 ]]
adamc@234 765
adam@328 766 To instantiate [H] generically, we first need to name the value to be used for [x].%\index{tactics!evar}% *)
adamc@234 767
adamc@234 768 evar (y : nat).
adamc@234 769
adamc@234 770 (** [[
adamc@234 771 H : forall x : nat, S x > x
adamc@234 772 y := ?279 : nat
adamc@234 773 ============================
adamc@234 774 2 > 1
adamc@234 775
adamc@234 776 ]]
adamc@234 777
adam@328 778 The proof context is extended with a new variable [y], which has been assigned to be equal to a fresh unification variable [?279]. We want to instantiate [H] with [?279]. To get ahold of the new unification variable, rather than just its alias [y], we perform a trivial unfolding in the expression [y], using the %\index{tactics!eval}%[eval] Ltac construct, which works with the same reduction strategies that we have seen in tactics (e.g., [simpl], [compute], etc.). *)
adamc@234 779
adam@328 780 let y' := eval unfold y in y in
adamc@234 781 clear y; generalize (H y').
adamc@234 782
adamc@234 783 (** [[
adamc@234 784 H : forall x : nat, S x > x
adamc@234 785 ============================
adamc@234 786 S ?279 > ?279 -> 2 > 1
adamc@234 787
adamc@234 788 ]]
adamc@234 789
adamc@234 790 Our instantiation was successful. We can finish by using the refined formula to replace the original. *)
adamc@234 791
adamc@234 792 clear H; intro H.
adamc@234 793
adamc@234 794 (** [[
adamc@234 795 H : S ?281 > ?281
adamc@234 796 ============================
adamc@234 797 2 > 1
adamc@234 798
adamc@234 799 ]]
adamc@234 800
adamc@234 801 We can finish the proof by using [apply]'s unification to figure out the proper value of [?281]. (The original unification variable was replaced by another, as often happens in the internals of the various tactics' implementations.) *)
adamc@234 802
adamc@234 803 apply H.
adamc@234 804 Qed.
adamc@234 805
adamc@234 806 (** Now we can write a tactic that encapsulates the pattern we just employed, instantiating all quantifiers of a particular hypothesis. *)
adamc@234 807
adamc@234 808 Ltac insterU H :=
adamc@234 809 repeat match type of H with
adamc@234 810 | forall x : ?T, _ =>
adamc@234 811 let x := fresh "x" in
adamc@234 812 evar (x : T);
adam@328 813 let x' := eval unfold x in x in
adam@328 814 clear x; specialize (H x')
adamc@234 815 end.
adamc@234 816
adamc@234 817 Theorem t5' : (forall x : nat, S x > x) -> 2 > 1.
adamc@234 818 intro H; insterU H; apply H.
adamc@234 819 Qed.
adamc@234 820
adam@328 821 (** This particular example is somewhat silly, since [apply] by itself would have solved the goal originally. Separate forward reasoning is more useful on hypotheses that end in existential quantifications. Before we go through an example, it is useful to define a variant of [insterU] that does not clear the base hypothesis we pass to it. We use the Ltac construct %\index{tactics!fresh}%[fresh] to generate a hypothesis name that is not already used, based on a string suggesting a good name. *)
adamc@234 822
adamc@234 823 Ltac insterKeep H :=
adamc@234 824 let H' := fresh "H'" in
adamc@234 825 generalize H; intro H'; insterU H'.
adamc@234 826
adamc@234 827 Section t6.
adamc@234 828 Variables A B : Type.
adamc@234 829 Variable P : A -> B -> Prop.
adamc@234 830 Variable f : A -> A -> A.
adamc@234 831 Variable g : B -> B -> B.
adamc@234 832
adamc@234 833 Hypothesis H1 : forall v, exists u, P v u.
adamc@234 834 Hypothesis H2 : forall v1 u1 v2 u2,
adamc@234 835 P v1 u1
adamc@234 836 -> P v2 u2
adamc@234 837 -> P (f v1 v2) (g u1 u2).
adamc@234 838
adamc@234 839 Theorem t6 : forall v1 v2, exists u1, exists u2, P (f v1 v2) (g u1 u2).
adamc@234 840 intros.
adamc@234 841
adam@328 842 (** Neither [eauto] nor [firstorder] is clever enough to prove this goal. We can help out by doing some of the work with quantifiers ourselves, abbreviating the proof with the %\index{tactics!do}%[do] tactical for repetition of a tactic a set number of times. *)
adamc@234 843
adamc@234 844 do 2 insterKeep H1.
adamc@234 845
adamc@234 846 (** Our proof state is extended with two generic instances of [H1].
adamc@234 847
adamc@234 848 [[
adamc@234 849 H' : exists u : B, P ?4289 u
adamc@234 850 H'0 : exists u : B, P ?4288 u
adamc@234 851 ============================
adamc@234 852 exists u1 : B, exists u2 : B, P (f v1 v2) (g u1 u2)
adamc@234 853
adamc@234 854 ]]
adamc@234 855
adam@328 856 [eauto] still cannot prove the goal, so we eliminate the two new existential quantifiers. (Recall that [ex] is the underlying type family to which uses of the [exists] syntax are compiled.) *)
adamc@234 857
adamc@234 858 repeat match goal with
adamc@234 859 | [ H : ex _ |- _ ] => destruct H
adamc@234 860 end.
adamc@234 861
adamc@234 862 (** Now the goal is simple enough to solve by logic programming. *)
adamc@234 863
adamc@234 864 eauto.
adamc@234 865 Qed.
adamc@234 866 End t6.
adamc@234 867
adamc@234 868 (** Our [insterU] tactic does not fare so well with quantified hypotheses that also contain implications. We can see the problem in a slight modification of the last example. We introduce a new unary predicate [Q] and use it to state an additional requirement of our hypothesis [H1]. *)
adamc@234 869
adamc@234 870 Section t7.
adamc@234 871 Variables A B : Type.
adamc@234 872 Variable Q : A -> Prop.
adamc@234 873 Variable P : A -> B -> Prop.
adamc@234 874 Variable f : A -> A -> A.
adamc@234 875 Variable g : B -> B -> B.
adamc@234 876
adamc@234 877 Hypothesis H1 : forall v, Q v -> exists u, P v u.
adamc@234 878 Hypothesis H2 : forall v1 u1 v2 u2,
adamc@234 879 P v1 u1
adamc@234 880 -> P v2 u2
adamc@234 881 -> P (f v1 v2) (g u1 u2).
adamc@234 882
adam@297 883 Theorem t7 : forall v1 v2, Q v1 -> Q v2 -> exists u1, exists u2, P (f v1 v2) (g u1 u2).
adamc@234 884 intros; do 2 insterKeep H1;
adamc@234 885 repeat match goal with
adamc@234 886 | [ H : ex _ |- _ ] => destruct H
adamc@234 887 end; eauto.
adamc@234 888
adamc@234 889 (** This proof script does not hit any errors until the very end, when an error message like this one is displayed.
adamc@234 890
adam@328 891 <<
adamc@234 892 No more subgoals but non-instantiated existential variables :
adamc@234 893 Existential 1 =
adam@328 894 >>
adam@328 895 [[
adamc@234 896 ?4384 : [A : Type
adamc@234 897 B : Type
adamc@234 898 Q : A -> Prop
adamc@234 899 P : A -> B -> Prop
adamc@234 900 f : A -> A -> A
adamc@234 901 g : B -> B -> B
adamc@234 902 H1 : forall v : A, Q v -> exists u : B, P v u
adamc@234 903 H2 : forall (v1 : A) (u1 : B) (v2 : A) (u2 : B),
adamc@234 904 P v1 u1 -> P v2 u2 -> P (f v1 v2) (g u1 u2)
adamc@234 905 v1 : A
adamc@234 906 v2 : A
adamc@234 907 H : Q v1
adamc@234 908 H0 : Q v2
adamc@234 909 H' : Q v2 -> exists u : B, P v2 u |- Q v2]
adamc@234 910
adamc@234 911 ]]
adamc@234 912
adam@288 913 There is another similar line about a different existential variable. Here, %``%#"#existential variable#"#%''% means what we have also called %``%#"#unification variable.#"#%''% In the course of the proof, some unification variable [?4384] was introduced but never unified. Unification variables are just a device to structure proof search; the language of Gallina proof terms does not include them. Thus, we cannot produce a proof term without instantiating the variable.
adamc@234 914
adamc@234 915 The error message shows that [?4384] is meant to be a proof of [Q v2] in a particular proof state, whose variables and hypotheses are displayed. It turns out that [?4384] was created by [insterU], as the value of a proof to pass to [H1]. Recall that, in Gallina, implication is just a degenerate case of [forall] quantification, so the [insterU] code to match against [forall] also matched the implication. Since any proof of [Q v2] is as good as any other in this context, there was never any opportunity to use unification to determine exactly which proof is appropriate. We expect similar problems with any implications in arguments to [insterU]. *)
adamc@234 916
adamc@234 917 Abort.
adamc@234 918 End t7.
adamc@234 919
adam@328 920 (* begin hide *)
adamc@234 921 Reset insterU.
adam@328 922 (* end hide *)
adam@328 923 (** %\noindent\coqdockw{%#<tt>#Reset#</tt>#%}% [insterU.] *)
adamc@234 924
adam@328 925 (** We can redefine [insterU] to treat implications differently. In particular, we pattern-match on the type of the type [T] in [forall x : ?T, ...]. If [T] has type [Prop], then [x]'s instantiation should be thought of as a proof. Thus, instead of picking a new unification variable for it, we instead apply a user-supplied tactic [tac]. It is important that we end this special [Prop] case with [|| fail 1], so that, if [tac] fails to prove [T], we abort the instantiation, rather than continuing on to the default quantifier handling. Also recall that the tactic form %\index{tactics!solve}%[solve [ t ]] fails if [t] does not completely solve the goal. *)
adamc@234 926
adamc@234 927 Ltac insterU tac H :=
adamc@234 928 repeat match type of H with
adamc@234 929 | forall x : ?T, _ =>
adamc@234 930 match type of T with
adamc@234 931 | Prop =>
adamc@234 932 (let H' := fresh "H'" in
adam@328 933 assert (H' : T) by solve [ tac ];
adam@328 934 specialize (H H'); clear H')
adamc@234 935 || fail 1
adamc@234 936 | _ =>
adamc@234 937 let x := fresh "x" in
adamc@234 938 evar (x : T);
adam@328 939 let x' := eval unfold x in x in
adam@328 940 clear x; specialize (H x')
adamc@234 941 end
adamc@234 942 end.
adamc@234 943
adamc@234 944 Ltac insterKeep tac H :=
adamc@234 945 let H' := fresh "H'" in
adamc@234 946 generalize H; intro H'; insterU tac H'.
adamc@234 947
adamc@234 948 Section t7.
adamc@234 949 Variables A B : Type.
adamc@234 950 Variable Q : A -> Prop.
adamc@234 951 Variable P : A -> B -> Prop.
adamc@234 952 Variable f : A -> A -> A.
adamc@234 953 Variable g : B -> B -> B.
adamc@234 954
adamc@234 955 Hypothesis H1 : forall v, Q v -> exists u, P v u.
adamc@234 956 Hypothesis H2 : forall v1 u1 v2 u2,
adamc@234 957 P v1 u1
adamc@234 958 -> P v2 u2
adamc@234 959 -> P (f v1 v2) (g u1 u2).
adamc@234 960
adamc@234 961 Theorem t6 : forall v1 v2, Q v1 -> Q v2 -> exists u1, exists u2, P (f v1 v2) (g u1 u2).
adamc@234 962
adamc@234 963 (** We can prove the goal by calling [insterKeep] with a tactic that tries to find and apply a [Q] hypothesis over a variable about which we do not yet know any [P] facts. We need to begin this tactic code with [idtac; ] to get around a strange limitation in Coq's proof engine, where a first-class tactic argument may not begin with a [match]. *)
adamc@234 964
adamc@234 965 intros; do 2 insterKeep ltac:(idtac; match goal with
adamc@234 966 | [ H : Q ?v |- _ ] =>
adamc@234 967 match goal with
adamc@234 968 | [ _ : context[P v _] |- _ ] => fail 1
adamc@234 969 | _ => apply H
adamc@234 970 end
adamc@234 971 end) H1;
adamc@234 972 repeat match goal with
adamc@234 973 | [ H : ex _ |- _ ] => destruct H
adamc@234 974 end; eauto.
adamc@234 975 Qed.
adamc@234 976 End t7.
adamc@234 977
adamc@234 978 (** It is often useful to instantiate existential variables explicitly. A built-in tactic provides one way of doing so. *)
adamc@234 979
adamc@234 980 Theorem t8 : exists p : nat * nat, fst p = 3.
adamc@234 981 econstructor; instantiate (1 := (3, 2)); reflexivity.
adamc@234 982 Qed.
adamc@234 983
adamc@234 984 (** The [1] above is identifying an existential variable appearing in the current goal, with the last existential appearing assigned number 1, the second last assigned number 2, and so on. The named existential is replaced everywhere by the term to the right of the [:=].
adamc@234 985
adam@328 986 The %\index{tactics!instantiate}%[instantiate] tactic can be convenient for exploratory proving, but it leads to very brittle proof scripts that are unlikely to adapt to changing theorem statements. It is often more helpful to have a tactic that can be used to assign a value to a term that is known to be an existential. By employing a roundabout implementation technique, we can build a tactic that generalizes this functionality. In particular, our tactic [equate] will assert that two terms are equal. If one of the terms happens to be an existential, then it will be replaced everywhere with the other term. *)
adamc@234 987
adamc@234 988 Ltac equate x y :=
adamc@234 989 let H := fresh "H" in
adam@328 990 assert (H : x = y) by reflexivity; clear H.
adamc@234 991
adam@328 992 (** This tactic fails if it is not possible to prove [x = y] by [reflexivity]. We perform the proof only for its unification side effects, clearing the fact [x = y] afterward. With [equate], we can build a less brittle version of the prior example. *)
adamc@234 993
adamc@234 994 Theorem t9 : exists p : nat * nat, fst p = 3.
adamc@234 995 econstructor; match goal with
adamc@234 996 | [ |- fst ?x = 3 ] => equate x (3, 2)
adamc@234 997 end; reflexivity.
adamc@234 998 Qed.
adam@329 999
adam@329 1000
adam@329 1001 (** * Exercises *)
adam@329 1002
adam@329 1003 (** %\begin{enumerate}%#<ol>#
adam@329 1004
adam@329 1005 %\item%#<li># An anonymous Coq fan from the Internet was excited to come up with this tactic definition shortly after getting started learning Ltac: *)
adam@329 1006
adam@329 1007 Ltac deSome :=
adam@329 1008 match goal with
adam@329 1009 | [ H : Some _ = Some _ |- _ ] => injection H; clear H; intros; subst; deSome
adam@329 1010 | _ => reflexivity
adam@329 1011 end.
adam@329 1012
adam@329 1013 (** Without lifting a finger, exciting theorems can be proved: *)
adam@329 1014
adam@329 1015 Theorem test : forall (a b c d e f g : nat),
adam@329 1016 Some a = Some b
adam@329 1017 -> Some b = Some c
adam@329 1018 -> Some e = Some c
adam@329 1019 -> Some f = Some g
adam@329 1020 -> c = a.
adam@329 1021 intros; deSome.
adam@329 1022 Qed.
adam@329 1023
adam@329 1024 (** Unfortunately, this tactic exhibits some degenerate behavior. Consider the following example: *)
adam@329 1025
adam@329 1026 Theorem test2 : forall (a x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6 : nat),
adam@329 1027 Some x1 = Some y1
adam@329 1028 -> Some x2 = Some y2
adam@329 1029 -> Some x3 = Some y3
adam@329 1030 -> Some x4 = Some y4
adam@329 1031 -> Some x5 = Some y5
adam@329 1032 -> Some x6 = Some y6
adam@329 1033 -> Some a = Some a
adam@329 1034 -> x1 = x2.
adam@329 1035 intros.
adam@329 1036 Time try deSome.
adam@329 1037 Abort.
adam@329 1038
adam@329 1039 (* begin hide *)
adam@329 1040 Reset test.
adam@329 1041 (* end hide *)
adam@329 1042
adam@329 1043 (** This (failed) proof already takes about one second on my workstation. I hope a pattern in the theorem statement is clear; this is a representative of a class of theorems, where we may add more matched pairs of [x] and [y] variables, with equality hypotheses between them. The running time of [deSome] is exponential in the number of such hypotheses.
adam@329 1044
adam@329 1045 The task in this exercise is twofold. First, figure out why [deSome] exhibits exponential behavior for this class of examples and record your explanation in a comment. Second, write an improved version of [deSome] that runs in polynomial time.#</li>#
adam@329 1046
adam@331 1047 %\item%#<li># Sometimes it can be convenient to know that a proof attempt is doomed because the theorem is false. For instance, here are three non-theorems about lists: *)
adam@331 1048
adam@331 1049 Theorem test1 : forall A (ls1 ls2 : list A), ls1 ++ ls2 = ls2 ++ ls1.
adam@331 1050 (* begin hide *)
adam@331 1051 Abort.
adam@331 1052 (* end hide *)
adam@331 1053
adam@331 1054 Theorem test2 : forall A (ls1 ls2 : list A), length (ls1 ++ ls2) = length ls1 - length ls2.
adam@331 1055 (* begin hide *)
adam@331 1056 Abort.
adam@331 1057 (* end hide *)
adam@331 1058
adam@331 1059 Theorem test3 : forall A (ls : list A), length (rev ls) - 3 = 0.
adam@331 1060 (* begin hide *)
adam@331 1061 Abort.
adam@331 1062 (* end hide *)
adam@331 1063
adam@331 1064 (** The task in this exercise is to write a tactic that disproves these and many other related %``%#"#theorems#"#%''% about lists. Your tactic should follow a simple brute-force enumeration strategy, considering all [list bool] values with length up to some bound given by the user, as a [nat] argument to the tactic. A successful invocation should add a new hypothesis of the negation of the theorem (guaranteeing that the tactic has made a sound decision about falsehood).
adam@331 1065
adam@331 1066 A few hints: A good starting point is to pattern-match the conclusion formula and use the [assert] tactic on its negation. An [assert] invocation may include a [by] clause to specify a tactic to use to prove the assertion.
adam@331 1067
adam@331 1068 The idea in this exercise is to disprove a quantified formula by finding instantiations for the quantifiers that make it manifestly false. Recall the [specialize] tactic for specializing a hypothesis to particular quantifier instantiations. When you have instantiated quantifiers fully, [discriminate] is a good choice to derive a contradiction. (It at least works for the three examples above and is smart enough for this exercise's purposes.) The [type of] Ltac construct may be useful to analyze the type of a hypothesis to choose how to instantiate its quantifiers.
adam@331 1069
adam@331 1070 To enumerate all boolean lists up to a certain length, it will be helpful to write a recursive tactic in continuation-passing style, where the continuation is meant to be called on each candidate list.
adam@331 1071
adam@331 1072 Remember that arguments to Ltac functions may not be type-checked in contexts large enough to allow usual implicit argument inference, so instead of [nil] it will be useful to write [@][nil bool], which specifies the usually implicit argument explicitly.
adam@331 1073
adam@329 1074 %\item%#<li># Some theorems involving existential quantifiers are easy to prove with [eauto]. *)
adam@329 1075
adam@329 1076 Theorem test1 : exists x, x = 0.
adam@329 1077 eauto.
adam@329 1078 Qed.
adam@329 1079
adam@329 1080 (** Others are harder. The problem with the next theorem is that the existentially quantified variable does not appear in the rest of the theorem, so [eauto] has no way to deduce its value. However, we know that we had might as well instantiate that variable to [tt], the only value of type [unit]. *)
adam@329 1081
adam@329 1082 Theorem test2 : exists x : unit, 0 = 0.
adam@329 1083 (* begin hide *)
adam@329 1084 eauto.
adam@329 1085 Abort.
adam@329 1086 (* end hide *)
adam@329 1087
adam@329 1088 (** We also run into trouble in the next theorem, because [eauto] does not understand the [fst] and [snd] projection functions for pairs. *)
adam@329 1089
adam@329 1090 Theorem test3 : exists x : nat * nat, fst x = 7 /\ snd x = 2 + fst x.
adam@329 1091 (* begin hide *)
adam@329 1092 eauto.
adam@329 1093 Abort.
adam@329 1094 (* end hide *)
adam@329 1095
adam@329 1096 (** Both problems show up in this monster example. *)
adam@329 1097
adam@329 1098 Theorem test4 : exists x : (unit * nat) * (nat * bool),
adam@329 1099 snd (fst x) = 7 /\ fst (snd x) = 2 + snd (fst x) /\ snd (snd x) = true.
adam@329 1100 (* begin hide *)
adam@329 1101 eauto.
adam@329 1102 Abort.
adam@329 1103 (* end hide *)
adam@329 1104
adam@329 1105 (** The task in this problem is to write a tactic that preprocesses such goals so that [eauto] can finish them. Your tactic should serve as a complete proof of each of the above examples, along with the wide class of similar examples. The key smarts that your tactic will bring are: first, it introduces separate unification variables for all the %``%#"#leaf types#"#%''% of compound types built out of pairs; and second, leaf unification variables of type [unit] are simply replaced by [tt].
adam@329 1106
adam@329 1107 A few hints: The following tactic is more convenient than direct use of the built-in tactic [evar], for generation of new unification variables: *)
adam@329 1108
adam@329 1109 Ltac makeEvar T k := let x := fresh in
adam@329 1110 evar (x : T); let y := eval unfold x in x in clear x; k y.
adam@329 1111
adam@329 1112 (** remove printing exists *)
adam@329 1113
adam@329 1114 (** This is a continuation-passing style tactic. For instance, when the goal begins with existential quantification over a type [T], the following tactic invocation will create a new unification variable to use as the quantifier instantiation:
adam@329 1115
adam@329 1116 [makeEvar T ltac:(][fun x => exists x)] *)
adam@329 1117
adam@329 1118 (** printing exists $\exists$ *)
adam@329 1119
adam@329 1120 (** Recall that [exists] formulas are desugared to uses of the [ex] inductive family. In particular, a pattern like the following can be used to extract the domain of an [exists] quantifier into variable [T]:
adam@329 1121
adam@329 1122 [| [ |- ex ( A := ?T) _ ] => ...]
adam@329 1123
adam@329 1124 The [equate] tactic used as an example in this chapter will probably be useful, to unify two terms, for instance if the first is a unification variable whose value you want to set.
adam@329 1125 [[
adam@330 1126 Ltac equate E1 E2 := let H := fresh in
adam@330 1127 assert (H : E1 = E2) by reflexivity; clear H.
adam@329 1128 ]]
adam@329 1129
adam@329 1130 Finally, there are some minor complications surrounding overloading of the [*] operator for both numeric multiplication and Cartesian product for sets (i.e., pair types). To ensure that an Ltac pattern is using the type version, write it like this:
adam@329 1131
adam@330 1132 [| (?T1 * ?T2)%type => ...]#</li>#
adam@330 1133
adam@330 1134 %\item%#<li># An exercise in the last chapter dealt with automating proofs about rings using [eauto], where we must prove some odd-looking theorems to push proof search in a direction where unification does all the work. Algebraic proofs consist mostly of rewriting in equations, so we might hope that the [autorewrite] tactic would yield more natural automated proofs. Indeed, consider this example within the same formulation of ring theory that we dealt with last chapter, where each of the three axioms has been added to the rewrite hint database [cpdt] using [Hint Rewrite]:
adam@330 1135 [[
adam@330 1136 Theorem test1 : forall a b, a * b * i b = a.
adam@330 1137 intros; autorewrite with cpdt; reflexivity.
adam@330 1138 Qed.
adam@330 1139 ]]
adam@330 1140
adam@330 1141 So far so good. However, consider this further example:
adam@330 1142 [[
adam@330 1143 Theorem test2 : forall a, a * e * i a * i e = e.
adam@330 1144 intros; autorewrite with cpdt.
adam@330 1145 ]]
adam@330 1146
adam@330 1147 The goal is merely reduced to [a * (i a * i e) = e], which of course [reflexivity] cannot prove. The essential problem is that [autorewrite] does not do backtracking search. Instead, it follows a %``%#"#greedy#"#%''% approach, at each stage choosing a rewrite to perform and then never allowing that rewrite to be undone. An early mistake can doom the whole process.
adam@330 1148
adam@330 1149 The task in this problem is to use Ltac to implement a backtracking version of [autorewrite] that works much like [eauto], in that its inputs are a database of hint lemmas and a bound on search depth. Here our search trees will have uses of [rewrite] at their nodes, rather than uses of [eapply] as in the case of [eauto], and proofs must be finished by [reflexivity].
adam@330 1150
adam@330 1151 An invocation to the tactic to prove [test2] might look like this:
adam@330 1152 [[
adam@330 1153 rewriter (right_identity, (right_inverse, tt)) 3.
adam@330 1154 ]]
adam@330 1155
adam@330 1156 The first argument gives the set of lemmas to consider, as a kind of list encoded with pair types. Such a format cannot be analyzed directly by Gallina programs, but Ltac allows us much more freedom to deconstruct syntax. For example, to case analyze such a list found in a variable [x], we need only write:
adam@330 1157 [[
adam@330 1158 match x with
adam@330 1159 | (?lemma, ?more) => ...
adam@330 1160 end
adam@330 1161 ]]
adam@330 1162
adam@330 1163 In the body of the case analysis, [lemma] will be bound to the first lemma, and [more] will be bound to the remaining lemmas. There is no need to consider a case for [tt], our stand-in for [nil]. This is because lack of any matching pattern will trigger failure, which is exactly the outcome we would like upon reaching the end of the lemma list without finding one that applies. The tactic will fail, triggering backtracking to some previous [match].
adam@330 1164
adam@330 1165 There are different kinds of backtracking, corresponding to different sorts of decisions to be made. The examples considered above can be handled with backtracking that only reconsiders decisions about the order in which to apply rewriting lemmas. A full-credit solution need only handle that kind of backtracking, considering all rewriting sequences up to the length bound passed to your tactic. A good test of this level of applicability is to prove both [test1] and [test2] above. However, some theorems could only be proved using a smarter tactic that considers not only order of rewriting lemma uses, but also choice of arguments to the lemmas. That is, at some points in a proof, the same lemma may apply at multiple places within the goal formula, and some choices may lead to stuck proof states while others lead to success. For an extra challenge (without any impact on the grade for the problem), you might try beefing up your tactic to do backtracking on argument choice, too.#</li>#
adam@329 1166
adam@329 1167 #</ol>#%\end{enumerate}% *)