-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAltAuto.v
4329 lines (3486 loc) · 140 KB
/
AltAuto.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** * AltAuto: A Streamlined Treatment of Automation *)
(** So far, we've been doing all our proofs using just a small
handful of Coq's tactics and completely ignoring its powerful
facilities for constructing parts of proofs automatically. Getting
used to them will take some work -- Coq's automation is a power
tool -- but it will allow us to scale up our efforts to more
complex definitions and more interesting properties without
becoming overwhelmed by boring, repetitive, low-level details.
In this chapter, we'll learn about
- _tacticals_, which allow tactics to be combined;
- new tactics that make dealing with hypothesis names less fussy
and more maintainable;
- _automatic solvers_ that can prove limited classes of theorems
without any human assistance;
- _proof search_ with the [auto] tactic; and
- the _Ltac_ language for writing tactics.
These features enable startlingly short proofs. Used properly,
they can also make proofs more maintainable and robust to changes
in underlying definitions.
This chapter is an alternative to the combination of [Imp]
and [Auto], which cover roughly the same material about
automation, but in the context of programming language metatheory.
A deeper treatment of [auto] can be found in the [UseAuto]
chapter in _Programming Language Foundations_. *)
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality,-deprecated-syntactic-definition,-deprecated]".
From Coq Require Import Arith List.
From LF Require Import IndProp.
(** As a simple illustration of the benefits of automation,
let's consider another problem on regular expressions, which we
formalized in [IndProp]. A given set of strings can be
denoted by many different regular expressions. For example, [App
EmptyString re] matches exactly the same strings as [re]. We can
write a function that "optimizes" any regular expression into a
potentially simpler one by applying this fact throughout the r.e.
(Note that, for simplicity, the function does not optimize
expressions that arise as the result of other optimizations.) *)
Fixpoint re_opt_e {T:Type} (re: reg_exp T) : reg_exp T :=
match re with
| App EmptyStr re2 => re_opt_e re2
| App re1 re2 => App (re_opt_e re1) (re_opt_e re2)
| Union re1 re2 => Union (re_opt_e re1) (re_opt_e re2)
| Star re => Star (re_opt_e re)
| _ => re
end.
(** We would like to show the equivalence of re's with their
"optimized" form. One direction of this equivalence looks like
this (the other is similar). *)
Check reg_exp_ind.
Lemma re_opt_e_match : forall T (re: reg_exp T) s,
s =~ re -> s =~ re_opt_e re.
Proof.
intros T re s M.
induction M
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2].
- (* MEmpty *) simpl. apply MEmpty.
- (* MChar *) simpl. apply MChar.
- (* MApp *) simpl.
destruct re1.
+ apply MApp.
* apply IH1.
* apply IH2.
+ inversion Hmatch1. simpl. apply IH2.
+ apply MApp.
* apply IH1.
* apply IH2.
+ apply MApp.
* apply IH1.
* apply IH2.
+ apply MApp.
* apply IH1.
* apply IH2.
+ apply MApp.
* apply IH1.
* apply IH2.
- (* MUnionL *) simpl. apply MUnionL. apply IH.
- (* MUnionR *) simpl. apply MUnionR. apply IH.
- (* MStar0 *) simpl. apply MStar0.
- (* MStarApp *) simpl. apply MStarApp.
* apply IH1.
* apply IH2.
Qed.
(** The amount of repetition in that proof is annoying. And if
we wanted to extend the optimization function to handle other,
similar, rewriting opportunities, it would start to be a real
problem. We can streamline the proof with _tacticals_, which we
turn to, next. *)
(* ################################################################# *)
(** * Tacticals *)
(** _Tacticals_ are tactics that take other tactics as arguments --
"higher-order tactics," if you will. *)
(* ================================================================= *)
(** ** The [try] Tactical *)
(** If [T] is a tactic, then [try T] is a tactic that is just like [T]
except that, if [T] fails, [try T] _successfully_ does nothing at
all instead of failing. *)
Theorem silly1 : forall n, 1 + n = S n.
Proof. try reflexivity. (* this just does [reflexivity] *) Qed.
Theorem silly2 : forall (P : Prop), P -> P.
Proof.
intros P HP.
Fail reflexivity.
try reflexivity. (* proof state is unchanged *)
apply HP.
Qed.
(** There is no real reason to use [try] in completely manual
proofs like these, but it is very useful for doing automated
proofs in conjunction with the [;] tactical, which we show
next. *)
(* ================================================================= *)
(** ** The Sequence Tactical [;] (Simple Form) *)
(** In its most common form, the sequence tactical, written with
semicolon [;], takes two tactics as arguments. The compound
tactic [T; T'] first performs [T] and then performs [T'] on _each
subgoal_ generated by [T]. *)
(** For example, consider the following trivial lemma: *)
Lemma simple_semi : forall n, (n + 1 =? 0) = false.
Proof.
intros n.
destruct n eqn:E.
(* Leaves two subgoals, which are discharged identically... *)
- (* n=0 *) simpl. reflexivity.
- (* n=Sn' *) simpl. reflexivity.
Qed.
(** We can simplify this proof using the [;] tactical: *)
Lemma simple_semi' : forall n, (n + 1 =? 0) = false.
Proof.
intros n.
(* [destruct] the current goal *)
destruct n;
(* then [simpl] each resulting subgoal *)
simpl;
(* and do [reflexivity] on each resulting subgoal *)
reflexivity.
Qed.
(** Or even more tersely, [destruct] can do the [intro], and [simpl]
can be omitted: *)
Lemma simple_semi'' : forall n, (n + 1 =? 0) = false.
Proof.
destruct n; reflexivity.
Qed.
(** **** Exercise: 3 stars, standard (try_sequence) *)
(** Prove the following theorems using [try] and [;]. Like
[simple_semi''] above, each proof script should be a sequence [t1;
...; tn.] of tactics, and there should be only one period in
between [Proof.] and [Qed.]. Let's call that a "one shot"
proof. *)
Theorem andb_eq_orb :
forall (b c : bool),
(andb b c = orb b c) ->
b = c.
Proof.
intros.
destruct b; destruct c; try (discriminate H); try (reflexivity).
Qed.
Theorem add_assoc : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros;
induction n;
try (simpl; reflexivity);
try(simpl; rewrite -> IHn; reflexivity).
Qed.
Fixpoint nonzeros (lst : list nat) :=
match lst with
| [] => []
| 0 :: t => nonzeros t
| h :: t => h :: nonzeros t
end.
Lemma nonzeros_app : forall lst1 lst2 : list nat,
nonzeros (lst1 ++ lst2) = (nonzeros lst1) ++ (nonzeros lst2).
Proof.
intros; generalize dependent lst2; induction lst1 as [| h' t' IH'].
- intros lst2; simpl; reflexivity.
- intros lst2; destruct h' ;simpl; rewrite -> IH'; reflexivity.
Qed.
(** [] *)
(** Using [try] and [;] together, we can improve the proof about
regular expression optimization. *)
Lemma re_opt_e_match' : forall T (re: reg_exp T) s,
s =~ re -> s =~ re_opt_e re.
Proof.
intros T re s M.
induction M
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2];
(* Do the [simpl] for every case here: *)
simpl.
- (* MEmpty *) apply MEmpty.
- (* MChar *) apply MChar.
- (* MApp *)
destruct re1;
(* Most cases follow by the same formula. Notice that [apply
MApp] gives two subgoals: [try apply IH1] is run on _both_ of
them and succeeds on the first but not the second; [apply IH2]
is then run on this remaining goal. *)
try (apply MApp; try apply IH1; apply IH2).
(* The interesting case, on which [try...] does nothing, is when
[re1 = EmptyStr]. In this case, we have to appeal to the fact
that [re1] matches only the empty string: *)
inversion Hmatch1. simpl. apply IH2.
- (* MUnionL *) apply MUnionL. apply IH.
- (* MUnionR *) apply MUnionR. apply IH.
- (* MStar0 *) apply MStar0.
- (* MStarApp *) apply MStarApp. apply IH1. apply IH2.
Qed.
(* ================================================================= *)
(** ** The Sequence Tactical [;] (Local Form) *)
(** The sequence tactical [;] also has a more general form than the
simple [T; T'] we saw above. If [T], [T1], ..., [Tn] are tactics,
then
[[ T; [T1 | T2 | ... | Tn] ]]
is a tactic that first performs [T] and then locally performs [T1]
on the first subgoal generated by [T], locally performs [T2] on
the second subgoal, etc.
So [T; T'] is just special notation for the case when all of the
[Ti]'s are the same tactic; i.e., [T; T'] is shorthand for:
T; [T' | T' | ... | T']
For example, the following proof makes it clear which tactics are
used to solve the base case vs. the inductive case.
*)
Theorem app_length : forall (X : Type) (lst1 lst2 : list X),
length (lst1 ++ lst2) = (length lst1) + (length lst2).
Proof.
intros; induction lst1;
[reflexivity | simpl; rewrite IHlst1; reflexivity].
Qed.
(** The identity tactic [idtac] always succeeds without changing the
proof state. We can use it to factor out [reflexivity] in the
previous proof. *)
Theorem app_length' : forall (X : Type) (lst1 lst2 : list X),
length (lst1 ++ lst2) = (length lst1) + (length lst2).
Proof.
intros; induction lst1;
[idtac | simpl; rewrite IHlst1];
reflexivity.
Qed.
(** **** Exercise: 1 star, standard (notry_sequence) *)
(** Prove the following theorem with a one-shot proof, but this
time, do not use [try]. *)
Theorem add_assoc' : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros;
induction n; [simpl; reflexivity | simpl; rewrite -> IHn; reflexivity].
Qed.
(** [] *)
(** We can use the local form of the sequence tactical to give a
slightly neater version of our optimization proof. Two lines
change, as shown below with [<===]. *)
Lemma re_opt_e_match'' : forall T (re: reg_exp T) s,
s =~ re -> s =~ re_opt_e re.
Proof.
intros T re s M.
induction M
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2];
(* Do the [simpl] for every case here: *)
simpl.
- (* MEmpty *) apply MEmpty.
- (* MChar *) apply MChar.
- (* MApp *)
destruct re1;
try (apply MApp; [apply IH1 | apply IH2]). (* <=== *)
inversion Hmatch1. simpl. apply IH2.
- (* MUnionL *) apply MUnionL. apply IH.
- (* MUnionR *) apply MUnionR. apply IH.
- (* MStar0 *) apply MStar0.
- (* MStarApp *) apply MStarApp; [apply IH1 | apply IH2]. (* <=== *)
Qed.
(* ================================================================= *)
(** ** The [repeat] Tactical *)
(** The [repeat] tactical takes another tactic and keeps
applying this tactic until it fails or stops making progress. Here
is an example showing that [10] is in a long list: *)
Theorem In10 : In 10 [1;2;3;4;5;6;7;8;9;10].
Proof.
repeat (try (left; reflexivity); right).
Qed.
(** The tactic [repeat T] never fails: if the tactic [T] doesn't apply
to the original goal, then [repeat] still succeeds without
changing the original goal (i.e., it repeats zero times). *)
Theorem In10' : In 10 [1;2;3;4;5;6;7;8;9;10].
Proof.
repeat (left; reflexivity).
repeat (right; try (left; reflexivity)).
Qed.
(** The tactic [repeat T] also does not have any upper bound on the
number of times it applies [T]. If [T] is a tactic that always
succeeds, then repeat [T] will loop forever (e.g., [repeat simpl]
loops, since [simpl] always succeeds). Evaluation in Coq's term
language, Gallina, is guaranteed to terminate, but tactic
evaluation is not. This does not affect Coq's logical consistency,
however, since the job of [repeat] and other tactics is to guide
Coq in constructing proofs. If the construction process diverges,
it simply means that we have failed to construct a proof, not that
we have constructed an incorrect proof. *)
(** **** Exercise: 1 star, standard (ev100)
Prove that 100 is even. Your proof script should be quite short. *)
Theorem ev100: ev 100.
Proof.
repeat (apply ev_SS).
apply ev_0.
Qed.
(** [] *)
(* ================================================================= *)
(** ** An Optimization Exercise *)
(** **** Exercise: 4 stars, standard (re_opt) *)
(** Consider this more powerful version of the regular expression
optimizer. *)
Fixpoint re_opt {T:Type} (re: reg_exp T) : reg_exp T :=
match re with
| App _ EmptySet => EmptySet
| App EmptyStr re2 => re_opt re2
| App re1 EmptyStr => re_opt re1
| App re1 re2 => App (re_opt re1) (re_opt re2)
| Union EmptySet re2 => re_opt re2
| Union re1 EmptySet => re_opt re1
| Union re1 re2 => Union (re_opt re1) (re_opt re2)
| Star EmptySet => EmptyStr
| Star EmptyStr => EmptyStr
| Star re => Star (re_opt re)
| EmptySet => EmptySet
| EmptyStr => EmptyStr
| Char x => Char x
end.
Lemma re_opt_match : forall T (re: reg_exp T) s,
s =~ re -> s =~ re_opt re.
Proof.
Proof.
intros T re s M.
induction M
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2].
- (* MEmpty *) simpl; apply MEmpty.
- (* MChar *) simpl; apply MChar.
- (* MApp *)
simpl;
destruct re1;
[inversion IH1 | inversion IH1; simpl; destruct re2; apply IH2 | | | |];
(destruct re2;
[inversion IH2 | inversion IH2; rewrite app_nil_r; apply IH1 | | | | ];
(apply MApp; [apply IH1 | apply IH2])).
- (* MUnionL *)
simpl;
destruct re1;
[inversion IH | | | | |];
destruct re2; try apply MUnionL; apply IH.
- (* MUnionR *)
simpl;
destruct re1;
[apply IH | | | | |];
(destruct re2; [inversion IH | | | | |]; apply MUnionR; apply IH).
- (* MStar0 *)
simpl;
destruct re; try apply MEmpty; try apply MStar0.
- (* MStarApp *)
simpl;
destruct re;
[inversion IH1 | inversion IH1; inversion IH2; apply MEmpty | | | |];
(apply star_app; [apply MStar1; apply IH1 | apply IH2]).
Qed.
(* Do not modify the following line: *)
Definition manual_grade_for_re_opt : option (nat*string) := None.
(** [] *)
(* ################################################################# *)
(** * Tactics that Make Mentioning Names Unnecessary *)
(** So far we have been dependent on knowing the names of
hypotheses. For example, to prove the following simple theorem,
we hardcode the name [HP]: *)
Theorem hyp_name : forall (P : Prop), P -> P.
Proof.
intros P HP. apply HP.
Qed.
(** We took the trouble to invent a name for [HP], then we had
to remember that name. If we later change the name in one place,
we have to change it everywhere. Likewise, if we were to add new
arguments to the theorem, we would have to adjust the [intros]
list. That makes it challenging to maintain large proofs. So, Coq
provides several tactics that make it possible to write proof
scripts that do not hardcode names. *)
(* ================================================================= *)
(** ** The [assumption] tactic *)
(** The [assumption] tactic is useful to streamline the proof
above. It looks through the hypotheses and, if it finds the goal
as one them, it uses that to finish the proof. *)
Theorem no_hyp_name : forall (P : Prop), P -> P.
Proof.
intros. assumption.
Qed.
(** Some might argue to the contrary that hypothesis names
improve self-documention of proof scripts. Maybe they do,
sometimes. But in the case of the two proofs above, the first
mentions unnecessary detail, whereas the second could be
paraphrased simply as "the conclusion follows from the
assumptions."
Anyway, unlike informal (good) mathematical proofs, Coq proof
scripts are generally not that illuminating to readers. Worries
about rich, self-documenting names for hypotheses might be
misplaced. *)
(* ================================================================= *)
(** ** The [contradiction] tactic *)
(** The [contradiction] tactic handles some ad hoc situations where a
hypothesis contains [False], or two hypotheses derive [False]. *)
Theorem false_assumed : False -> 0 = 1.
Proof.
intros H. destruct H.
Qed.
Theorem false_assumed' : False -> 0 = 1.
Proof.
intros. contradiction.
Qed.
Theorem contras : forall (P : Prop), P -> ~P -> 0 = 1.
Proof.
intros P HP HNP. exfalso. apply HNP. apply HP.
Qed.
Theorem contras' : forall (P : Prop), P -> ~P -> 0 = 1.
Proof.
intros. contradiction.
Qed.
(* ================================================================= *)
(** ** The [subst] tactic *)
(** The [subst] tactic substitutes away an identifier, replacing
it everywhere and eliminating it from the context. That helps
us to avoid naming hypotheses in [rewrite]s. *)
Theorem many_eq : forall (n m o p : nat),
n = m ->
o = p ->
[n; o] = [m; p].
Proof.
intros n m o p Hnm Hop. rewrite Hnm. rewrite Hop. reflexivity.
Qed.
Theorem many_eq' : forall (n m o p : nat),
n = m ->
o = p ->
[n; o] = [m; p].
Proof.
intros. subst. reflexivity.
Qed.
(** Actually there are two forms of this tactic.
- [subst x] finds an assumption [x = e] or [e = x] in the
context, replaces [x] with [e] throughout the context and
current goal, and removes the assumption from the context.
- [subst] substitutes away _all_ assumptions of the form [x = e]
or [e = x]. *)
(* ================================================================= *)
(** ** The [constructor] tactic *)
(** The [constructor] tactic tries to find a constructor [c] (from the
appropriate [Inductive] definition in the current environment)
that can be applied to solve the current goal. *)
Check ev_0 : ev 0.
Check ev_SS : forall n : nat, ev n -> ev (S (S n)).
Example constructor_example: forall (n:nat),
ev (n + n).
Proof.
induction n; simpl.
- constructor. (* applies ev_0 *)
- rewrite add_comm. simpl. constructor. (* applies ev_SS *)
assumption.
Qed.
(** Warning: if more than one constructor can apply,
[constructor] picks the first one, in the order in which they were
defined in the [Inductive] definition. That might not be the one
you want. *)
(* ################################################################# *)
(** * Automatic Solvers *)
(** Coq has several special-purpose tactics that can solve
certain kinds of goals in a completely automated way. These
tactics are based on sophisticated algorithms developed for
verification in specific mathematical or logical domains.
Some automatic solvers are _decision procedures_, which are
algorithms that always terminate, and always give a correct
answer. Here, that means that they always find a correct proof, or
correctly determine that the goal is invalid. Other automatic
solvers are _incomplete_: they might fail to find a proof of a
valid goal. *)
(* ================================================================= *)
(** ** Linear Integer Arithmetic: The [lia] Tactic *)
(** The [lia] tactic implements a decision procedure for integer
linear arithmetic, a subset of propositional logic and arithmetic.
As input it accepts goals constructed as follows:
- variables and constants of type [nat], [Z], and other integer
types;
- arithmetic operators [+], [-], [*], and [^];
- equality [=] and ordering [<], [>], [<=], [>=]; and
- the logical connectives [/\], [\/], [~], [->], and [<->]; and
constants [True] and [False].
_Linear_ goals involve (in)equalities over expressions of the form
[c1 * x1 + ... + cn * xn], where [ci] are constants and [xi] are
variables.
- For linear goals, [lia] will either solve the goal or fail,
meaning that the goal is actually invalid.
- For non-linear goals, [lia] will also either solve the goal or
fail. But in this case, the failure does not necessarily mean
that the goal is invalid -- it might just be beyond [lia]'s
reach to prove because of non-linearity.
Also, [lia] will do [intros] as necessary. *)
From Coq Require Import Lia.
Theorem lia_succeed1 : forall (n : nat),
n > 0 -> n * 2 > n.
Proof. lia. Qed.
Theorem lia_succeed2 : forall (n m : nat),
n * m = m * n.
Proof.
lia. (* solvable though non-linear *)
Qed.
Theorem lia_fail1 : 0 = 1.
Proof.
Fail lia. (* goal is invalid *)
Abort.
Theorem lia_fail2 : forall (n : nat),
n >= 1 -> 2 ^ n = 2 * 2 ^ (n - 1).
Proof.
Fail lia. (*goal is non-linear, valid, but unsolvable by lia *)
Abort.
(** There are other tactics that can solve arithmetic goals. The
[ring] and [field] tactics, for example, can solve equations over
the algebraic structures of _rings_ and _fields_, from which the
tactics get their names. These tactics do not do [intros]. *)
Require Import Ring.
Theorem mult_comm : forall (n m : nat),
n * m = m * n.
Proof.
intros n m. ring.
Qed.
(* ================================================================= *)
(** ** Equalities: The [congruence] Tactic *)
(** The [lia] tactic makes use of facts about addition and
multiplication to prove equalities. A more basic way of treating
such formulas is to regard every function appearing in them as
a black box: nothing is known about the function's behavior.
Based on the properties of equality itself, it is still possible
to prove some formulas. For example, [y = f x -> g y = g (f x)],
even if we know nothing about [f] or [g]:
*)
Theorem eq_example1 :
forall (A B C : Type) (f : A -> B) (g : B -> C) (x : A) (y : B),
y = f x -> g y = g (f x).
Proof.
intros. rewrite H. reflexivity.
Qed.
(** The essential properties of equality are that it is:
- reflexive
- symmetric
- transitive
- a _congruence_: it respects function and predicate
application. *)
(** It is that congruence property that we're using when we
[rewrite] in the proof above: if [a = b] then [f a = f b]. (The
[ProofObjects] chapter explores this idea further under the
name "Leibniz equality".) *)
(** The [congruence] tactic is a decision procedure for equality with
uninterpreted functions and other symbols. *)
Theorem eq_example1' :
forall (A B C : Type) (f : A -> B) (g : B -> C) (x : A) (y : B),
y = f x -> g y = g (f x).
Proof.
congruence.
Qed.
(** The [congruence] tactic is able to work with constructors,
even taking advantage of their injectivity and distinctness. *)
Theorem eq_example2 : forall (n m o p : nat),
n = m ->
o = p ->
(n, o) = (m, p).
Proof.
congruence.
Qed.
Theorem eq_example3 : forall (X : Type) (h : X) (t : list X),
[] <> h :: t.
Proof.
congruence.
Qed.
(* ================================================================= *)
(** ** Propositions: The [intuition] Tactic *)
(** A _tautology_ is a logical formula that is always
provable. A formula is _propositional_ if it does not use
quantifiers -- or at least, if quantifiers do not have to be
instantiated to carry out the proof. The [intuition] tactic
implements a decision procedure for propositional tautologies in
Coq's constructive (that is, intuitionistic) logic. Even if a goal
is not a propositional tautology, [intuition] will still attempt
to reduce it to simpler subgoals. *)
Theorem intuition_succeed1 : forall (P : Prop),
P -> P.
Proof. intuition. Qed.
Theorem intuition_succeed2 : forall (P Q : Prop),
~ (P \/ Q) -> ~P /\ ~Q.
Proof. intuition. Qed.
Theorem intuition_simplify1 : forall (P : Prop),
~~P -> P.
Proof.
intuition. (* not a constructively valid formula *)
Abort.
Theorem intuition_simplify2 : forall (x y : nat) (P Q : nat -> Prop),
x = y /\ (P x -> Q x) /\ P x -> Q y.
Proof.
Fail congruence. (* the propositions stump it *)
intuition. (* the [=] stumps it, but it simplifies the propositions *)
congruence.
Qed.
(** In the previous example, neither [congruence] nor
[intuition] alone can solve the goal. But after [intuition]
simplifies the propositions involved in the goal, [congruence] can
succeed. For situations like this, [intuition] takes an optional
argument, which is a tactic to apply to all the unsolved goals
that [intuition] generated. Using that we can offer a shorter
proof: *)
Theorem intuition_simplify2' : forall (x y : nat) (P Q : nat -> Prop),
x = y /\ (P x -> Q x) /\ P x -> Q y.
Proof.
intuition congruence.
Qed.
(* ================================================================= *)
(** ** Exercises with Automatic Solvers *)
(** **** Exercise: 2 stars, standard (automatic_solvers)
The exercises below are gleaned from previous chapters, where they
were proved with (relatively) long proof scripts. Each should now
be provable with just a single invocation of an automatic
solver. *)
Theorem plus_id_exercise_from_basics : forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof.
intuition.
Qed.
Theorem add_assoc_from_induction : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
lia.
Qed.
Theorem S_injective_from_tactics : forall (n m : nat),
S n = S m ->
n = m.
Proof.
intuition.
Qed.
Theorem or_distributes_over_and_from_logic : forall P Q R : Prop,
P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).
Proof.
intuition.
Qed.
(** [] *)
(* ################################################################# *)
(** * Search Tactics *)
(** The automated solvers we just discussed are capable of finding
proofs in specific domains. Some of them might pay attention to
local hypotheses, but overall they don't make use of any custom
lemmas we've proved, or that are provided by libraries that we
load.
Another kind of automation that Coq provides does just that: the
[auto] tactic and its variants search for proofs that can be
assembled out of hypotheses and lemmas. *)
(* ================================================================= *)
(** ** The [auto] Tactic *)
(** Until this chapter, our proof scripts mostly applied relevant
hypotheses or lemmas by name, and one at a time. *)
Example auto_example_1 : forall (P Q R: Prop),
(P -> Q) -> (Q -> R) -> P -> R.
Proof.
intros P Q R H1 H2 H3.
apply H2. apply H1. apply H3.
Qed.
(** The [auto] tactic frees us from this drudgery by _searching_ for a
sequence of applications that will prove the goal: *)
Example auto_example_1' : forall (P Q R: Prop),
(P -> Q) -> (Q -> R) -> P -> R.
Proof.
auto.
Qed.
(** The [auto] tactic solves goals that are solvable by any combination of
- [intros] and
- [apply] (of hypotheses from the local context, by default). *)
(** Using [auto] is always "safe" in the sense that it will
never fail and will never change the proof state: either it
completely solves the current goal, or it does nothing. *)
(** Here is a more interesting example showing [auto]'s power: *)
Example auto_example_2 : forall P Q R S T U : Prop,
(P -> Q) ->
(P -> R) ->
(T -> R) ->
(S -> T -> U) ->
((P -> Q) -> (P -> S)) ->
T ->
P ->
U.
Proof. auto. Qed.
(** Proof search could, in principle, take an arbitrarily long time,
so there are limits to how far [auto] will search by default. *)
Example auto_example_3 : forall (P Q R S T U: Prop),
(P -> Q) ->
(Q -> R) ->
(R -> S) ->
(S -> T) ->
(T -> U) ->
P ->
U.
Proof.
(* When it cannot solve the goal, [auto] does nothing *)
auto.
(* Optional argument says how deep to search (default is 5) *)
auto 6.
Qed.
(** The [auto] tactic considers the hypotheses in the current context
together with a _hint database_ of other lemmas and constructors.
Some common facts about equality and logical operators are
installed in the hint database by default. *)
Example auto_example_4 : forall P Q R : Prop,
Q ->
(Q -> R) ->
P \/ (Q /\ R).
Proof. auto. Qed.
(** If we want to see which facts [auto] is using, we can use
[info_auto] instead. *)
Example auto_example_5 : 2 = 2.
Proof.
(* [auto] subsumes [reflexivity] because [eq_refl] is in the hint
database. *)
info_auto.
Qed.
(** We can extend the hint database with theorem [t] just for the
purposes of one application of [auto] by writing [auto using
t]. *)
Lemma le_antisym : forall n m: nat, (n <= m /\ m <= n) -> n = m.
Proof. intros. lia. Qed.
Example auto_example_6 : forall n m p : nat,
(n <= p -> (n <= m /\ m <= n)) ->
n <= p ->
n = m.
Proof.
auto using le_antisym.
Qed.
(** Of course, in any given development there will probably be
some specific constructors and lemmas that are used very often in
proofs. We can add these to a hint database named [db] by writing
Create HintDb db.
to create the database, then
Hint Resolve T : db.
to add [T] to the database, where [T] is a top-level theorem or a
constructor of an inductively defined proposition (i.e., anything
whose type is an implication). We tell [auto] to use that database
by writing [auto with db]. Technically creation of the database
is optional; Coq will create it automatically the first time
we use [Hint]. *)
Create HintDb le_db.
Hint Resolve le_antisym : le_db.
Example auto_example_6' : forall n m p : nat,
(n <= p -> (n <= m /\ m <= n)) ->
n <= p ->
n = m.
Proof.
auto with le_db.
Qed.
(** As a shorthand, we can write
Hint Constructors c : db.
to tell Coq to do a [Hint Resolve] for _all_ of the constructors
from the inductive definition of [c].
It is also sometimes necessary to add
Hint Unfold d : db.
where [d] is a defined symbol, so that [auto] knows to expand uses
of [d], thus enabling further possibilities for applying lemmas that
it knows about. *)
Definition is_fortytwo x := (x = 42).
Example auto_example_7: forall x,
(x <= 42 /\ 42 <= x) -> is_fortytwo x.
Proof.
auto. (* does nothing *)
Abort.
Hint Unfold is_fortytwo : le_db.
Example auto_example_7' : forall x,
(x <= 42 /\ 42 <= x) -> is_fortytwo x.
Proof. info_auto with le_db. Qed.
(** The "global" database that [auto] always uses is named [core].
You can add your own hints to it, but the Coq manual discourages
that, preferring instead to have specialized databases for
specific developments. Many of the important libraries have their
own hint databases that you can tag in: [arith], [bool], [datatypes]
(including lists), etc. *)
Example auto_example_8 : forall (n m : nat),
n + m = m + n.
Proof.
auto. (* no progress *)
info_auto with arith. (* uses [Nat.add_comm] *)
Qed.
(** **** Exercise: 3 stars, standard (re_opt_match_auto) *)
(** Use [auto] to shorten your proof of [re_opt_match] even
more. Eliminate all uses of [apply], thus removing the need to
name specific constructors and lemmas about regular expressions.
The number of lines of proof script won't decrease that much,
because [auto] won't be able to find [induction], [destruct], or
[inversion] opportunities by itself.
Hint: again, use a bottom-up approach. Always keep the proof
compiling. You might find it easier to return to the original,
very long proof, and shorten it, rather than starting with
[re_opt_match']; but, either way can work. *)
Lemma re_opt_match'' : forall T (re: reg_exp T) s,
s =~ re -> s =~ re_opt re.
Proof.
intros T re s M.
induction M
as [| x'