| 1 |
(* The tactic language *) |
| 2 |
|
| 3 |
(* Submitted by Pierre Crégut *) |
| 4 |
(* Checks substitution of x *) |
| 5 |
Ltac f x := unfold x; idtac. |
| 6 |
|
| 7 |
Lemma lem1 : 0 + 0 = 0. |
| 8 |
f plus. |
| 9 |
reflexivity. |
| 10 |
Qed. |
| 11 |
|
| 12 |
(* Submitted by Pierre Crégut *) |
| 13 |
(* Check syntactic correctness *) |
| 14 |
Ltac F x := idtac; G x |
| 15 |
with G y := idtac; F y. |
| 16 |
|
| 17 |
(* Check that Match Context keeps a closure *) |
| 18 |
Ltac U := let a := constr:(I) in |
| 19 |
match goal with |
| 20 |
| |- _ => apply a |
| 21 |
end. |
| 22 |
|
| 23 |
Lemma lem2 : True. |
| 24 |
U. |
| 25 |
Qed. |
| 26 |
|
| 27 |
(* Check that Match giving non-tactic arguments are evaluated at Let-time *) |
| 28 |
|
| 29 |
Ltac B := let y := (match goal with |
| 30 |
| z:_ |- _ => z |
| 31 |
end) in |
| 32 |
(intro H1; exact y). |
| 33 |
|
| 34 |
Lemma lem3 : True -> False -> True -> False. |
| 35 |
intros H H0. |
| 36 |
B. (* y is H0 if at let-time, H1 otherwise *) |
| 37 |
Qed. |
| 38 |
|
| 39 |
(* Checks the matching order of hypotheses *) |
| 40 |
Ltac Y := match goal with |
| 41 |
| x:_,y:_ |- _ => apply x |
| 42 |
end. |
| 43 |
Ltac Z := match goal with |
| 44 |
| y:_,x:_ |- _ => apply x |
| 45 |
end. |
| 46 |
|
| 47 |
Lemma lem4 : (True -> False) -> (False -> False) -> False. |
| 48 |
intros H H0. |
| 49 |
Z. (* Apply H0 *) |
| 50 |
Y. (* Apply H *) |
| 51 |
exact I. |
| 52 |
Qed. |
| 53 |
|
| 54 |
(* Check backtracking *) |
| 55 |
Lemma back1 : 0 = 1 -> 0 = 0 -> 1 = 1 -> 0 = 0. |
| 56 |
intros; |
| 57 |
match goal with |
| 58 |
| _:(0 = ?X1),_:(1 = 1) |- _ => exact (refl_equal X1) |
| 59 |
end. |
| 60 |
Qed. |
| 61 |
|
| 62 |
Lemma back2 : 0 = 0 -> 0 = 1 -> 1 = 1 -> 0 = 0. |
| 63 |
intros; |
| 64 |
match goal with |
| 65 |
| _:(0 = ?X1),_:(1 = 1) |- _ => exact (refl_equal X1) |
| 66 |
end. |
| 67 |
Qed. |
| 68 |
|
| 69 |
Lemma back3 : 0 = 0 -> 1 = 1 -> 0 = 1 -> 0 = 0. |
| 70 |
intros; |
| 71 |
match goal with |
| 72 |
| _:(0 = ?X1),_:(1 = 1) |- _ => exact (refl_equal X1) |
| 73 |
end. |
| 74 |
Qed. |
| 75 |
|
| 76 |
(* Check context binding *) |
| 77 |
Ltac sym t := |
| 78 |
match constr:(t) with |
| 79 |
| context C[(?X1 = ?X2)] => context C [X1 = X2] |
| 80 |
end. |
| 81 |
|
| 82 |
Lemma sym : 0 <> 1 -> 1 <> 0. |
| 83 |
intro H. |
| 84 |
let t := sym type of H in |
| 85 |
assert t. |
| 86 |
exact H. |
| 87 |
intro H1. |
| 88 |
apply H. |
| 89 |
symmetry . |
| 90 |
assumption. |
| 91 |
Qed. |
| 92 |
|
| 93 |
(* Check context binding in match goal *) |
| 94 |
(* This wasn't working in V8.0pl1, as the list of matched hyps wasn't empty *) |
| 95 |
Ltac sym' := |
| 96 |
match goal with |
| 97 |
| _:True |- context C[(?X1 = ?X2)] => |
| 98 |
let t := context C [X2 = X1] in |
| 99 |
assert t |
| 100 |
end. |
| 101 |
|
| 102 |
Lemma sym' : True -> 0 <> 1 -> 1 <> 0. |
| 103 |
intros Ht H. |
| 104 |
sym'. |
| 105 |
exact H. |
| 106 |
intro H1. |
| 107 |
apply H. |
| 108 |
symmetry . |
| 109 |
assumption. |
| 110 |
Qed. |
| 111 |
|
| 112 |
(* Check that fails abort the current match context *) |
| 113 |
Lemma decide : True \/ False. |
| 114 |
match goal with |
| 115 |
| _ => fail 1 |
| 116 |
| _ => right |
| 117 |
end || left. |
| 118 |
exact I. |
| 119 |
Qed. |
| 120 |
|
| 121 |
(* Check that "match c with" backtracks on subterms *) |
| 122 |
Lemma refl : 1 = 1. |
| 123 |
let t := |
| 124 |
(match constr:(1 = 2) with |
| 125 |
| context [(S ?X1)] => constr:(refl_equal X1:1 = 1) |
| 126 |
end) in |
| 127 |
assert (H := t). |
| 128 |
assumption. |
| 129 |
Qed. |
| 130 |
|
| 131 |
(* Note that backtracking in "match c with" is only on type-checking not on |
| 132 |
evaluation of tactics. E.g., this does not work |
| 133 |
|
| 134 |
Lemma refl : (1)=(1). |
| 135 |
Match (1)=(2) With |
| 136 |
[[(S ?1)]] -> Apply (refl_equal nat ?1). |
| 137 |
Qed. |
| 138 |
*) |
| 139 |
|
| 140 |
|
| 141 |
(* Check the precedences of rel context, ltac context and vars context *) |
| 142 |
(* (was wrong in V8.0) *) |
| 143 |
|
| 144 |
Ltac check_binding y := cut ((fun y => y) = S). |
| 145 |
Goal True. |
| 146 |
check_binding ipattern:(H). |
| 147 |
Abort. |
| 148 |
|
| 149 |
(* Check that variables explicitly parsed as ltac variables are not |
| 150 |
seen as intro pattern or constr (BZ#984) *) |
| 151 |
|
| 152 |
Ltac afi tac := intros; tac. |
| 153 |
Goal 1 = 2. |
| 154 |
afi ltac:(auto). |
| 155 |
Abort. |
| 156 |
|
| 157 |
(* Tactic Notation avec listes *) |
| 158 |
|
| 159 |
Tactic Notation "pat" hyp(id) "occs" integer_list(l) := pattern id at l. |
| 160 |
|
| 161 |
Goal forall x, x=0 -> x=x. |
| 162 |
intro x. |
| 163 |
pat x occs 1 3. |
| 164 |
Abort. |
| 165 |
|
| 166 |
Tactic Notation "revert" ne_hyp_list(l) := generalize l; clear l. |
| 167 |
|
| 168 |
Goal forall a b c, a=0 -> b=c+a. |
| 169 |
intros. |
| 170 |
revert a b c H. |
| 171 |
Abort. |
| 172 |
|
| 173 |
(* Used to fail until revision 9280 because of a parasitic App node with |
| 174 |
empty args *) |
| 175 |
|
| 176 |
Goal True. |
| 177 |
match constr:(@None) with @None => exact I end. |
| 178 |
Abort. |
| 179 |
|
| 180 |
(* Check second-order pattern unification *) |
| 181 |
|
| 182 |
Ltac to_exist := |
| 183 |
match goal with |
| 184 |
|- forall x y, @?P x y => |
| 185 |
let Q := eval lazy beta in (exists x, forall y, P x y) in |
| 186 |
assert (Q->Q) |
| 187 |
end. |
| 188 |
|
| 189 |
Goal forall x y : nat, x = y. |
| 190 |
to_exist. exact (fun H => H). |
| 191 |
Abort. |
| 192 |
|
| 193 |
(* Used to fail in V8.1 *) |
| 194 |
|
| 195 |
Tactic Notation "test" constr(t) integer(n) := |
| 196 |
set (k := t) at n. |
| 197 |
|
| 198 |
Goal forall x : nat, x = 1 -> x + x + x = 3. |
| 199 |
intros x H. |
| 200 |
test x 2. |
| 201 |
Abort. |
| 202 |
|
| 203 |
(* Utilisation de let rec sans arguments *) |
| 204 |
|
| 205 |
Ltac is := |
| 206 |
let rec i := match goal with |- ?A -> ?B => intro; i | _ => idtac end in |
| 207 |
i. |
| 208 |
|
| 209 |
Goal True -> True -> True. |
| 210 |
is. |
| 211 |
exact I. |
| 212 |
Abort. |
| 213 |
|
| 214 |
(* Interférence entre espaces des noms *) |
| 215 |
|
| 216 |
Ltac O := intro. |
| 217 |
Ltac Z1 t := set (x:=t). |
| 218 |
Ltac Z2 t := t. |
| 219 |
Goal True -> True. |
| 220 |
Z1 O. |
| 221 |
Z2 ltac:(O). |
| 222 |
exact I. |
| 223 |
Qed. |
| 224 |
|
| 225 |
(* Illegal application used to make Ltac loop. *) |
| 226 |
|
| 227 |
Section LtacLoopTest. |
| 228 |
Ltac g x := idtac. |
| 229 |
Goal True. |
| 230 |
Timeout 1 try g()(). |
| 231 |
Abort. |
| 232 |
End LtacLoopTest. |
| 233 |
|
| 234 |
(* Test binding of open terms *) |
| 235 |
|
| 236 |
Ltac test_open_match z := |
| 237 |
match z with |
| 238 |
(forall y x, ?h = 0) => assert (forall x y, h = x + y) |
| 239 |
end. |
| 240 |
|
| 241 |
Goal True. |
| 242 |
test_open_match (forall z y, y + z = 0). |
| 243 |
reflexivity. |
| 244 |
apply I. |
| 245 |
Qed. |
| 246 |
|
| 247 |
(* Test binding of open terms with non linear matching *) |
| 248 |
|
| 249 |
Ltac f_non_linear t := |
| 250 |
match t with |
| 251 |
(forall x y, ?u = 0) -> (forall y x, ?u = 0) => |
| 252 |
assert (forall x y:nat, u = u) |
| 253 |
end. |
| 254 |
|
| 255 |
Goal True. |
| 256 |
f_non_linear ((forall x y, x+y = 0) -> (forall x y, y+x = 0)). |
| 257 |
reflexivity. |
| 258 |
f_non_linear ((forall a b, a+b = 0) -> (forall a b, b+a = 0)). |
| 259 |
reflexivity. |
| 260 |
f_non_linear ((forall a b, a+b = 0) -> (forall x y, y+x = 0)). |
| 261 |
reflexivity. |
| 262 |
f_non_linear ((forall x y, x+y = 0) -> (forall a b, b+a = 0)). |
| 263 |
reflexivity. |
| 264 |
f_non_linear ((forall x y, x+y = 0) -> (forall y x, x+y = 0)). |
| 265 |
reflexivity. |
| 266 |
f_non_linear ((forall x y, x+y = 0) -> (forall y x, y+x = 0)) (* should fail *) |
| 267 |
|| exact I. |
| 268 |
Qed. |
| 269 |
|
| 270 |
(* Test regular failure when clear/intro breaks soundness of the |
| 271 |
interpretation of terms in current environment *) |
| 272 |
|
| 273 |
Ltac g y := clear y; assert (y=y). |
| 274 |
Goal forall x:nat, True. |
| 275 |
intro x. |
| 276 |
Fail g x. |
| 277 |
Abort. |
| 278 |
|
| 279 |
Ltac h y := assert (y=y). |
| 280 |
Goal forall x:nat, True. |
| 281 |
intro x. |
| 282 |
Fail clear x; f x. |
| 283 |
Abort. |
| 284 |
|
| 285 |
(* Do not consider evars as unification holes in Ltac matching (and at |
| 286 |
least not as holes unrelated to the original evars) |
| 287 |
[Example adapted from Ynot code] |
| 288 |
*) |
| 289 |
|
| 290 |
Ltac not_eq e1 e2 := |
| 291 |
match e1 with |
| 292 |
| e2 => fail 1 |
| 293 |
| _ => idtac |
| 294 |
end. |
| 295 |
|
| 296 |
Goal True. |
| 297 |
evar(foo:nat). |
| 298 |
let evval := eval compute in foo in not_eq evval 1. |
| 299 |
let evval := eval compute in foo in not_eq 1 evval. |
| 300 |
Abort. |
| 301 |
|
| 302 |
(* Check instantiation of binders using ltac names *) |
| 303 |
|
| 304 |
Goal True. |
| 305 |
let x := ipattern:(y) in assert (forall x y, x = y + 0). |
| 306 |
intro. |
| 307 |
destruct y. (* Check that the name is y here *) |
| 308 |
Abort. |
| 309 |
|
| 310 |
(* An example suggested by Jason (see #4317) showing the intended semantics *) |
| 311 |
(* Order of binders is reverted because y is just told to depend on x *) |
| 312 |
|
| 313 |
Goal 1=1. |
| 314 |
let T := constr:(fun a b : nat => a) in |
| 315 |
lazymatch T with |
| 316 |
| (fun x z => ?y) => pose ((fun x x => y) 2 1) |
| 317 |
end. |
| 318 |
exact (eq_refl n). |
| 319 |
Qed. |
| 320 |
|
| 321 |
(* A variant of #2602 which was wrongly succeeding because "a", bound to |
| 322 |
"?m", was then internally turned into a "_" in the second matching *) |
| 323 |
|
| 324 |
Goal exists m, S m > 0. |
| 325 |
eexists. |
| 326 |
Fail match goal with |
| 327 |
| |- context [ S ?a ] => |
| 328 |
match goal with |
| 329 |
| |- S a > a => idtac |
| 330 |
end |
| 331 |
end. |
| 332 |
Abort. |
| 333 |
|
| 334 |
(* Test evar syntax *) |
| 335 |
|
| 336 |
Goal True. |
| 337 |
evar (0=0). |
| 338 |
Abort. |
| 339 |
|
| 340 |
(* Test location of hypothesis in "symmetry in H". This was broken in |
| 341 |
8.6 where H, when the oldest hyp, was moved at the place of most |
| 342 |
recent hypothesis *) |
| 343 |
|
| 344 |
Goal 0=1 -> True -> True. |
| 345 |
intros H H0. |
| 346 |
symmetry in H. |
| 347 |
(* H should be the first hypothesis *) |
| 348 |
match goal with h:_ |- _ => assert (h=h) end. (* h should be H0 *) |
| 349 |
exact (eq_refl H0). |
| 350 |
Abort. |
| 351 |
|
| 352 |
(* Check that internal names used in "match" compilation to push "term |
| 353 |
to match" on the environment are not interpreted as ltac variables *) |
| 354 |
|
| 355 |
Module ToMatchNames. |
| 356 |
Ltac g c := let r := constr:(match c return _ with a => 1 end) in idtac. |
| 357 |
Goal True. |
| 358 |
g 1. |
| 359 |
Abort. |
| 360 |
End ToMatchNames. |
| 361 |
|
| 362 |
(* An example where internal names used to build the return predicate |
| 363 |
(here "n" because "a" is bound to "nil" and "n" is the first letter |
| 364 |
of "nil") by small inversion should be taken distinct from Ltac names. *) |
| 365 |
|
| 366 |
Module LtacNames. |
| 367 |
Inductive t (A : Type) : nat -> Type := |
| 368 |
nil : t A 0 | cons : A -> forall n : nat, t A n -> t A (S n). |
| 369 |
|
| 370 |
Ltac f a n := |
| 371 |
let x := constr:(match a with nil _ => true | cons _ _ _ _ => I end) in |
| 372 |
assert (x=x/\n=n). |
| 373 |
|
| 374 |
Goal forall (y:t nat 0), True. |
| 375 |
intros. |
| 376 |
f y true. |
| 377 |
Abort. |
| 378 |
|
| 379 |
End LtacNames. |
| 380 |
|
| 381 |
(* Test binding of the name of existential variables in Ltac *) |
| 382 |
|
| 383 |
Module EvarNames. |
| 384 |
|
| 385 |
Ltac pick x := eexists ?[x]. |
| 386 |
Goal exists y, y = 0. |
| 387 |
pick foo. |
| 388 |
[foo]:exact 0. |
| 389 |
auto. |
| 390 |
Qed. |
| 391 |
|
| 392 |
Ltac goal x := refine ?[x]. |
| 393 |
|
| 394 |
Goal forall n, n + 0 = n. |
| 395 |
Proof. |
| 396 |
induction n; [ goal Base | goal Rec ]. |
| 397 |
[Base]: { |
| 398 |
easy. |
| 399 |
} |
| 400 |
[Rec]: { |
| 401 |
simpl. |
| 402 |
now f_equal. |
| 403 |
} |
| 404 |
Qed. |
| 405 |
|
| 406 |
End EvarNames. |
| 407 |
|
| 408 |
Module LocalRedef. |
| 409 |
Ltac thetac := idtac. |
| 410 |
Ltac thetac' := idtac. |
| 411 |
|
| 412 |
Module Inner. |
| 413 |
Ltac thetac ::= fail. |
| 414 |
Local Ltac thetac' ::= fail. |
| 415 |
Goal False. |
| 416 |
Fail thetac. |
| 417 |
Fail thetac'. |
| 418 |
Abort. |
| 419 |
End Inner. |
| 420 |
Goal False. |
| 421 |
Fail thetac. |
| 422 |
thetac'. |
| 423 |
Abort. |
| 424 |
|
| 425 |
Section S. |
| 426 |
Variable f : False. |
| 427 |
Fail Global Ltac thetac' ::= exact f. |
| 428 |
Ltac thetac' ::= exact f. |
| 429 |
Goal False. |
| 430 |
Proof. thetac'. Qed. |
| 431 |
End S. |
| 432 |
Goal False. |
| 433 |
Proof. |
| 434 |
thetac'. |
| 435 |
Fail Qed. |
| 436 |
Abort. |
| 437 |
End LocalRedef. |
| 438 |
|
| 439 |
Module MatchCastInPattern. |
| 440 |
|
| 441 |
Goal let x := True in True. |
| 442 |
Proof. |
| 443 |
intro x. |
| 444 |
lazymatch goal with |
| 445 |
| [ H := ?v : ?T |- _ ] => constr_eq T Prop |
| 446 |
end. |
| 447 |
|
| 448 |
Fail lazymatch goal with |
| 449 |
| [ H := ?v <: ?T |- _ ] => constr_eq T Prop |
| 450 |
end. (* Warning: Casts are ignored in patterns [cast-in-pattern,automation] *) |
| 451 |
|
| 452 |
Set Warnings "+cast-in-pattern". |
| 453 |
Fail lazymatch goal with |
| 454 |
| [ H := ?v <: _ |- _ ] => idtac |
| 455 |
end. |
| 456 |
Fail lazymatch goal with |
| 457 |
| [ H := [ ?v : _ ] : _ |- _ ] => idtac |
| 458 |
end. |
| 459 |
Abort. |
| 460 |
|
| 461 |
End MatchCastInPattern. |
| 462 |
|
| 463 |
Module StrictModeConfusion. |
| 464 |
|
| 465 |
Goal True. |
| 466 |
Fail let x := constr:(match _ with x x => _ end) in idtac. |
| 467 |
(* for_grammar does not reset the ref when an exception is raised *) |
| 468 |
Abort. |
| 469 |
|
| 470 |
Fail Ltac bad := exact x. (* was wrongly accepted *) |
| 471 |
|
| 472 |
End StrictModeConfusion. |