forked from plclub/metalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoqIntro.v
2686 lines (2422 loc) · 78.8 KB
/
CoqIntro.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
(**************************************************************)
(** * Coq for POPL Folk *)
(**************************************************************)
(** A streamlined interactive tutorial on fundamentals of Coq,
focusing on a minimal set of features needed for developing
programming language metatheory.
Mostly developed by Aaron Bohannon, with help from Benjamin
Pierce, Dimitrios Vytiniotis, and Steve Zdancewic.
*)
(**************************************************************)
(** * Contents
- Getting Started
- Definitions
- Proofs
-- Working with Implication and Universal Quantification
-- Working with Definitions
-- Working with Conjunction and Disjunction
-- Reasoning by Cases and Induction
-- Working with Existential Quantification
-- Working with Negation
-- Working with Equality
-- Reasoning by Inversion
-- Additional Important Tactics
-- Basic Automation
-- Functions and Conversion
- Solutions to Exercises
*)
(**************************************************************)
(**************************************************************)
(** * Getting Started *)
(**************************************************************)
(** To get started...
- Install Coq (from the Download page of the main Coq web
site)
- Install an IDE: either CoqIDE (from the same page) or
Proof General (use Google to find it).
- Which should you choose? Their command sets are similar,
so basically the trade-offs are simple:
-- Proof General is an extension of Emacs (or XEmacs,
if you prefer), while CoqIDE uses a simpler
point-and-click model of editing.
-- PG is pretty easy to install; CoqIDE is very easy
to install if you can find a pre-built version for
the OS you are running on but can be a little tricky
to build from scratch because it has lots of
dependencies.
- Familiarize yourself with the most important commands
-- If you are using PG, try this:
--- Open this file and check that the mode line says
something like "coq Holes Scripting"
--- Go down a few pages and do C-C C-return. Notice
that the part of the file above the cursor
changes color (and becomes read-only),
indicating that it has been sent to Coq.
--- Come back here and do C-C C-return again
--- You now know all you really need to navigate in
this file and send parts of it to Coq, but there
are some other navigation commands that are
sometimes more convenient. To see a couple
more, do C-C C-n several times and observe the
result; then do C-C C-u a couple of times and
observe the result.
-- If you are using CoqIDE, try this:
--- Open this file.
--- Scroll down a few pages.
--- Hover the mouse over each the buttons at the top
of the window; this will make the "tool tips"
appear so you can see which is which.
--- Press the "Go to cursor" button. Observe what
happens.
--- Press the forward and backward buttons a few
times. Observe.
--- Scroll back to here again and start reading.
- Open the Coq reference manual (from the Documentation
page of the Coq web site) in a web browser. Spend 30
seconds looking over the table of contents to get an idea
what's there. (There is no need to actually read
anything now.)
*)
(**************************************************************)
(** * Definitions *)
(**************************************************************)
(** In this file, we will be working with a very simple language
of expressions over natural numbers and booleans. First we
need to define the datatype of terms.
The [Inductive] keyword defines a new inductive type. We
name our new type [tm] and declare that [tm] lives in the
sort [Set]. Types in [Set] are datatypes, which can be used
just like those in standard programming languages. After
the inductive definition, the global environment contains
the name of the newly defined type, along with the names of
all of the constructors. Coq also automatically defines a
few operators for eliminating values of the new type
([tm_rec], [tm_ind], etc.); we can ignore these for the time
being, since we will not need to use them directly.
*)
Inductive tm : Set :=
| tm_true : tm
| tm_false : tm
| tm_if : tm -> tm -> tm -> tm
| tm_zero : tm
| tm_succ : tm -> tm
| tm_pred : tm -> tm
| tm_iszero : tm -> tm.
(** Next, we want to designate some of our [tm] expressions as
"values" in our object language. Mathematically, the
property of being a value is a unary relation over [tm]s.
The definition of the unary relation [value] will be built
from the definition of two auxiliary relations: [bvalue] for
the set of boolean values and [nvalue] for the set of
numerical values.
We define n-ary relations in Coq much as we do on paper --
by giving a set of inference rules that can be used to
justify the membership of a tuple in the relation. The
definition of such an inference system also uses the keyword
[Inductive]. Although this is exactly the same device we
used to build the set [tm], in this case we want to do
something slightly different: we will be using it to
inductively defining the structures (derivation trees) that
justify the membership of a tuple in a relation, rather than
directly defining a set inductively. These derivation trees
will need to be given a dependent type in order to ensure
that only correct derivation trees can be built. It is
possible to view their type as a datatype such as [tm];
however, it is more natural to interpret it as a
proposition, so it will be declared to live in [Prop]
instead of [Set]. [Prop] is parallel to [Set] in the sort
hierarchy, but types in [Prop] can be thought of as logical
propositions rather than datatypes. The inhabitants of
types in [Prop] can be thought of as proofs rather than
programs.
The unary relations [bvalue] and [nvalue] are defined by
very simple inference systems, each having just two rules.
After defining these inference systems, [bvalue] and
[nvalue] will each be a family of types indexed by elements
of [tm]. We can build inhabitants of some (but not all) of
the types in these families with the constructors from our
inductive definition. Semantically, we consider the
proposition [bvalue t] to be true if it is inhabited and
false if it is not. (It is worth noting at this point that,
by default, Coq's logic is constructive and [P \/ not P] is
provable for some [P] but not for others. However, it is
sound to add the law of the excluded middle as an axiom, if
desired.)
*)
Inductive bvalue : tm -> Prop :=
| b_true : bvalue tm_true
| b_false : bvalue tm_false.
Inductive nvalue : tm -> Prop :=
| n_zero : nvalue tm_zero
| n_succ : forall t,
nvalue t ->
nvalue (tm_succ t).
(** The [Definition] keyword is used for defining non-recursive
functions (including 0-ary functions, i.e., constants).
Here we define the unary predicate [value] using disjunction
on propositions (written [\/]).
Note: It is not actually necessary to provide the types of
the arguments nor the return type when they can easily be
inferred; for example, the annotations [: tm] and [: Prop]
are optional here.
*)
Definition value (t : tm) : Prop :=
bvalue t \/ nvalue t.
(** Having defined [tm]s and [value]s, we can define a
call-by-value operational semantics of our language by
giving a definition of the single-step evaluation of one
term to another. We give this as an inductively defined
binary relation.
*)
Inductive eval : tm -> tm -> Prop :=
| e_iftrue : forall t2 t3,
eval (tm_if tm_true t2 t3) t2
| e_iffalse : forall t2 t3,
eval (tm_if tm_false t2 t3) t3
| e_if : forall t1 t1' t2 t3,
eval t1 t1' ->
eval (tm_if t1 t2 t3) (tm_if t1' t2 t3)
| e_succ : forall t t',
eval t t' ->
eval (tm_succ t) (tm_succ t')
| e_predzero :
eval (tm_pred tm_zero) tm_zero
| e_predsucc : forall t,
nvalue t ->
eval (tm_pred (tm_succ t)) t
| e_pred : forall t t',
eval t t' ->
eval (tm_pred t) (tm_pred t')
| e_iszerozero :
eval (tm_iszero tm_zero) tm_true
| e_iszerosucc : forall t,
nvalue t ->
eval (tm_iszero (tm_succ t)) tm_false
| e_iszero : forall t t',
eval t t' ->
eval (tm_iszero t) (tm_iszero t').
(** We define multi-step evaluation with the relation
[eval_many]. This relation includes all of the pairs of
terms that are connected by sequences of evaluation steps.
*)
Inductive eval_many : tm -> tm -> Prop :=
| m_refl : forall t,
eval_many t t
| m_step : forall t t' u,
eval t t' ->
eval_many t' u ->
eval_many t u.
(** *** Exercise
Multi-step evaluation is often defined as the "reflexive,
transitive closure" of single-step evaluation. Write an
inductively defined relation [eval_rtc] that corresponds to
that verbal description.
In case you get stuck or need a hint, you can find solutions
to all the exercises near the bottom of the file.
*)
(** A term is a [normal_form] if there is no term to which it
can step. Note the concrete syntax for negation and
existential quantification in the definition below.
*)
Definition normal_form (t : tm) : Prop :=
~ exists t', eval t t'.
(** *** Exercise
Sometimes it is more convenient to use a big-step semantics
for a language. Add the remaining constructors to finish
the inductive definition [full_eval] for the big-step
semantics that corresponds to the small-step semantics
defined by [eval]. Build the inference rules so that
[full_eval t v] logically implies both [eval_many t v] and
[value v]. In order to do this, you may need to add the
premise [nvalue v] to the appropriate cases.
Hint: You should end up with a total of 8 cases.
<<
Inductive full_eval : tm -> tm -> Prop :=
| f_value : forall v,
value v ->
full_eval v v
| f_iftrue : forall t1 t2 t3 v,
full_eval t1 tm_true ->
full_eval t2 v ->
full_eval (tm_if t1 t2 t3) v
| f_succ : forall t v,
nvalue v ->
full_eval t v ->
full_eval (tm_succ t) (tm_succ v).
>>
*)
(** *** Tip
If you want to see the type of an identifier [x], you can
use the command [Check x.] If you want to see the
definition of an identifier [x], you can use the command
[Print x.]
*)
Check tm_if.
Check m_step.
Check value.
Print value.
(**************************************************************)
(** * Proofs *)
(**************************************************************)
(** A proposition and its proof are both represented as terms in
the calculus of inductive constructions, which is
syntactically very small.
Proof terms are most easily built interactively, using
tactics to manipulate a proof state. A proof state consists
of a set of goals (propositions or types for which you must
produce an inhabitant), each with a context of hypotheses
(inhabitants of propositions or types you are allowed to
use). A proof state begins initially with one goal (the
statement of the lemma you are tying to prove) and no
hypotheses. A goal can be solved, and thereby eliminated,
when it exactly matches one of hypotheses in the context. A
proof is completed when all goals are solved.
Tactics can be used for forward reasoning (which, roughly
speaking, means modifying the hypotheses of a context while
leaving the goal unchanged) or backward reasoning (replacing
the current goal with one or more new goals in simpler
contexts). Given the level of detail required in a formal
proof, it would be ridiculously impractical to complete a
proof using forward reasoning alone. However it is usually
both possible and practical to complete a proof using
backward reasoning alone. Therefore, we focus almost
exclusively on backward reasoning in this tutorial. Of
course, most people naturally use a significant amount of
forward reasoning in their thinking process, so it may take
you a while to become accustomed to getting by without it.
We use the keyword [Lemma] to state a new proposition we
wish to prove. ([Theorem] and [Fact] are exact synonyms for
[Lemma].) The keyword [Proof], immediately following the
statement of the proposition, indicates the beginning of a
proof script. A proof script is a sequence of tactic
expressions, each concluding with a "[.]". Once all of the
goals are solved, we use the keyword [Qed] to record the
completed proof. If the proof is incomplete, we may tell
Coq to accept the lemma on faith by using [Admitted] instead
of [Qed].
We now proceed to introduce the specific proof tactics.
*)
(**************************************************************)
(** ** Working with Implication and Universal Quantification
- [intros]
- [apply]
- [apply with (x := ...)]
*)
(** *** Example
The tactic [intros x1 ... xn] moves antecedents and
universally quantified variables from the goal into the
context as hypotheses. The tactic [apply] is complementary
to [intros]. If the conclusion (the part following the
rightmost arrow) of a constructor, hypothesis, or lemma [e]
matches our current goal, then [apply e] will replace the
goal with a new goal for each premise/antecedent of [e]. If
[e] has no premises, then the current goal is solved. Using
[apply] allows us to build a proof tree from the bottom up.
In the following example, our proof script will effectively
build the following proof tree:
<<
nvalue t
---------------------------- (e_predsucc)
eval (tm_pred (tm_succ t)) t
------------------------------------------------ (e_succ)
eval (tm_succ (tm_pred (tm_succ t))) (tm_succ t)
>>
Step through the proof to see how this tree is constructed.
For each tactic, we give the corresponding statement in a
written proof. (The uses of [Check] are inessential.)
*)
Lemma e_succ_pred_succ : forall t,
nvalue t ->
eval (tm_succ (tm_pred (tm_succ t))) (tm_succ t).
Proof.
(** Let [t] be a [tm]. *)
intros t.
(** Assume that [t] is an [nvalue] (and let's call that
assumption [Hn] for future reference). *)
intros Hn.
(** By [e_succ], in order to prove our conclusion, it suffices
to prove that [eval (tm_pred (tm_succ t)) t]. *)
Check e_succ.
apply e_succ.
(** That, in turn, can be shown by [e_predsucc], if we are
able to show that [nvalue t]. *)
Check e_predsucc.
apply e_predsucc.
(** But, in fact, we assumed [nvalue t]. *)
apply Hn.
Qed.
(** *** Hint for PG users
If you place the cursor after [Proof.] and do C-c C-return,
you'll notice that the window displaying Coq's responses is
(annoyingly) empty, instead of showing what is to be proved.
The reason for this is that, actually, a different buffer is
being displayed! (To see this, do C-c C-n and C-c C-u a few
times and notice that the buffer name in the mode line
changes.) You can use C-c C-p to switch the display back
from the *response* buffer to the *goals* buffer.
*)
(** *** Example
Now consider, for a moment, the rule [m_step]:
<<
eval t t' eval_many t' u
------------------------- (m_step)
eval_many t u
>>
If we have a goal such as [eval_many e1 e2], we should be
able to use [apply m_step] in order to replace it with the
goals [eval e1 t'] and [eval_many t' e2]. But what exactly
is [t'] here? When and how is it chosen? It stands to
reason the conclusion is justified if we can come up with
any [t'] for which the premises can be justified.
Now we note that, in the Coq syntax for the type of
[m_step], all three variables [t], [t'], and [u] are
universally quantified. The tactic [apply m_step] will use
pattern matching between our goal and the conclusion of
[m_step] to find the only possible instantiation of [t] and
[u]. However, [apply m_step] will raise an error since it
does not know how it should instantiate [t']. In this case,
the [apply] tactic takes a [with] clause that allows us to
provide this instantiation. This is demonstrated in the
proof below. (Note that the use of this feature means that
our proof scripts are not invariant under alpha-equivalence
on the types of our constructors and lemmas. If this is a
concern, there are other means of achieving the same result
in a way that is compatible with alpha-conversion. See the
next example.)
Observe how this works in the proof script below. The proof
tree here gives a visual representation of the proof term we
are going to construct and the proof script has again been
annotated with the steps in English.
<<
Letting s = tm_succ
p = tm_pred
lem = e_succ_pred_succ,
nvalue t
- - - - - - - - - - - - (lem) --------------------- (m_refl)
eval (s (p (s t))) (s t) eval_many (s t) (s t)
--------------------------------------------------- (m_step)
eval_many (s (p (s t))) (s t)
>>
*)
Lemma m_succ_pred_succ : forall t,
nvalue t ->
eval_many (tm_succ (tm_pred (tm_succ t))) (tm_succ t).
Proof.
(** Let [t] be a [tm], and assume [nvalue t]. *)
intros t Hn.
(** By [m_step], to show our conclusion, it suffices to find
some [t'] for which
[eval (tm_succ (tm_pred (tm_succ t))) t']
and
[eval t' (tm_succ t)].
Let us choose [t'] to be [tm_succ t]. *)
Check m_step.
apply m_step with (t' := tm_succ t).
(** By the lemma [e_succ_pred_succ], to show
[eval (tm_succ (tm_pred (tm_succ t))) (tm_succ t)],
it suffices to show [nvalue t]. *)
Check e_succ_pred_succ.
apply e_succ_pred_succ.
(** And, in fact, we assumed [nvalue t]. *)
apply Hn.
(** Moreover, by the rule [m_refl], we also may conclude
[eval (tm_succ t) (tm_succ t)]. *)
Check m_refl.
apply m_refl.
Qed.
(** *** Example
Coq is built around the Curry-Howard correspondence.
Proofs of universally quantified propositions are functions
that take the witness of the quantifier as an argument.
Similarly, proofs of implications are functions that take
one proof as an argument and return another proof. Observe
the types of the following terms.
*)
Check (e_succ_pred_succ).
Check (e_succ_pred_succ tm_zero).
Check (n_zero).
Check (e_succ_pred_succ tm_zero n_zero).
(** *** Example
Any tactic like [apply] that takes the name of a constructor
or lemma as an argument can just as easily be given a more
complicated expression as an argument. Thus, we may use
function application to construct proof objects on the fly
in these cases. Observe how this technique can be used to
rewrite the proof of the previous lemma.
Although, we have eliminated one use of [apply], this is not
necessarily an improvement over the previous proof.
However, there are cases where this technique is quite
valuable.
*)
Lemma m_succ_pred_succ_alt : forall t,
nvalue t ->
eval_many (tm_succ (tm_pred (tm_succ t))) (tm_succ t).
Proof.
intros t Hn.
Check m_step.
apply (m_step
(tm_succ (tm_pred (tm_succ t)))
(tm_succ t)
(tm_succ t)
).
Check e_succ_pred_succ.
apply (e_succ_pred_succ t Hn).
Check m_refl.
apply (m_refl (tm_succ t)).
Qed.
(** *** Hint for PG users
By default, the "." key is "electric" -- it inserts a period
_and_ causes the material up to the cursor to be sent to
Coq. If you find this behavior annoying, it can be toggled
by doing "C-c .".
*)
(*
LAB 1: (10 minutes)
Work on the following three exercises.
*)
(** *** Exercise
Write a proof script to prove the following lemma, based
upon the proof given in English.
Note: The lemma and the next should be useful in later
proofs.
*)
Lemma m_one : forall t1 t2,
eval t1 t2 ->
eval_many t1 t2.
Proof.
(** Let [t1] and [t2] be terms, and assume [eval t1 t2]. We
may conclude [eval_many t1 t2] by [m_step] if we can find
a term [t'] such that [eval t1 t'] and [eval_many t' t2].
We will choose [t'] to be [t2]. Now we can show
[eval t1 t2] by our assumption, and we can show
[eval_many t2 t2] by [m_refl]. *)
(* to finish *)
Admitted.
(** *** Exercise *)
Lemma m_two : forall t1 t2 t3,
eval t1 t2 ->
eval t2 t3 ->
eval_many t1 t3.
Proof.
(** Let [t1], [t2], and [t3] be terms. Assume [eval t1 t2]
and [eval t2 t3]. By [m_step], we may conclude that
[eval_many t1 t3] if we can find a term [t'] such that
[eval t1 t'] and [eval_many t' t3]. Let's choose [t'] to
be [t2]. We know [eval t1 t2] holds by assumption. In
the other case, by the lemma [m_one], to show [eval_many
t2 t3], it suffices to show [eval t2 t3], which is one of
our assumptions. *)
(* to finish *)
Admitted.
(** *** Exercise *)
Lemma m_iftrue_step : forall t t1 t2 u,
eval t tm_true ->
eval_many t1 u ->
eval_many (tm_if t t1 t2) u.
Proof.
(** Let [t], [t1], [t2], and [u] be terms. Assume that
[eval t tm_true] and [eval_many t1 u]. To show
[eval_many (tm_if t t1 t2) u], by [m_step], it suffices to
find a [t'] for which [eval (tm_if t t1 t2) t'] and
[eval_many t' u]. Let us choose [t'] to be
[tm_if tm_true t1 t2]. Now we can use [e_if] to show that
[eval (tm_if t t1 t2) (tm_if tm_true t1 t2)] if we can
show [eval t tm_true], which is actually one of our
assumptions. Moreover, using [m_step] once more, we can
show [eval_many (tm_if tm_true t1 t2) u] where [t'] is
chosen to be [t1]. Doing so leaves us to show
[eval (tm_if tm_true t1 t2) t1] and [eval_many t1 u]. The
former holds by [e_iftrue] and the latter holds by
assumption. *)
(* to finish *)
Admitted.
(**************************************************************)
(** ** Working with Definitions
- [unfold]
*)
(** *** Example
There is a notion of equivalence on Coq terms that arises
from the conversion rules of the underlying calculus of
constructions. It is sometimes useful to be able to replace
one term in a proof with an equivalent one. For instance,
we may want to replace a defined name with its definition.
This sort of replacement can be done the tactic [unfold].
This tactic can be used to manipulate the goal or the
hypotheses.
*)
Definition strongly_diverges t :=
forall u, eval_many t u -> ~ normal_form u.
Lemma unfold_example : forall t t',
strongly_diverges t ->
eval t t' ->
strongly_diverges t'.
Proof.
intros t t' Hd He.
unfold strongly_diverges. intros u Hm.
unfold strongly_diverges in Hd.
apply Hd. apply m_step with (t' := t').
apply He.
apply Hm.
Qed.
(** *** Exercise
In reality, many tactics will perform conversion
automatically as necessary. Try removing the uses of
[unfold] from the above proof to check which ones were
necessary.
*)
(**************************************************************)
(** ** Working with Conjunction and Disjunction
- [split]
- [left]
- [right]
- [destruct] (for conjunction and disjunction)
*)
(** *** Example
If [H] is the name of a conjunctive hypothesis, then
[destruct H as p] will replace the hypothesis [H] with its
components using the names in the pattern [p]. Observe the
pattern in the example below.
*)
Lemma m_two_conj : forall t t' t'',
eval t t' /\ eval t' t'' ->
eval_many t t''.
Proof.
intros t t' t'' H.
destruct H as [ He1 He2 ].
apply m_two with (t2 := t').
apply He1.
apply He2.
Qed.
(** *** Example
Patterns may be nested to break apart nested structures.
Note that infix conjunction is right-associative, which is
significant when trying to write nested patterns. We will
later see how to use [destruct] on many different sorts of
hypotheses.
*)
Lemma m_three_conj : forall t t' t'' t''',
eval t t' /\ eval t' t'' /\ eval t'' t''' ->
eval_many t t'''.
Proof.
intros t t' t'' t''' H.
destruct H as [ He1 [ He2 He3 ] ].
apply m_step with (t' := t').
apply He1.
apply m_two with (t2 := t'').
apply He2.
apply He3.
Qed.
(** *** Example
If your goal is a conjunction, use [split] to break it apart
into two separate subgoals.
*)
Lemma m_three : forall t t' t'' t''',
eval t t' ->
eval t' t'' ->
eval t'' t''' ->
eval_many t t'''.
Proof.
intros t t' t'' t''' He1 He2 He3.
apply m_three_conj with (t' := t') (t'' := t'').
split.
apply He1.
split.
apply He2.
apply He3.
Qed.
(** *** Exercise
Hint: You might find lemma [m_three] useful here.
*)
Lemma m_if_iszero_conj : forall v t2 t2' t3 t3',
nvalue v /\ eval t2 t2' /\ eval t3 t3' ->
eval_many (tm_if (tm_iszero tm_zero) t2 t3) t2' /\
eval_many (tm_if (tm_iszero (tm_succ v)) t2 t3) t3'.
Proof.
(* to finish *)
Admitted.
(** *** Example
If the goal is a disjunction, we can use the [left] or
[right] tactics to solve it by proving the left or right
side of the conclusion.
*)
Lemma true_and_succ_zero_values :
value tm_true /\ value (tm_succ tm_zero).
Proof.
unfold value. split.
left. apply b_true.
right. apply n_succ. apply n_zero.
Qed.
(** *** Example
If we have a disjunction in the context, we can use
[destruct] to reason by cases on the hypothesis. Note the
syntax of the associated pattern.
*)
Lemma e_if_true_or_false : forall t1 t2,
eval t1 tm_true \/ eval t1 tm_false ->
eval_many (tm_if t1 t2 t2) t2.
Proof.
intros t1 t2 H. destruct H as [ He1 | He2 ].
apply m_two with (t2 := tm_if tm_true t2 t2).
apply e_if. apply He1.
apply e_iftrue.
apply m_two with (t2 := tm_if tm_false t2 t2).
apply e_if. apply He2.
apply e_iffalse.
Qed.
(*
LAB 2: (10 minutes)
Work on the following exercise.
*)
(** *** Exercise *)
Lemma two_values : forall t u,
value t /\ value u ->
bvalue t \/
bvalue u \/
(nvalue t /\ nvalue u).
Proof.
(** We know [value t] and [value u], which means either
[bvalue t] or [nvalue t], and either [bvalue u] or
[nvalue u]. Consider the case in which
[bvalue t] holds. Then one of the disjuncts of our
conclusion is proved. Next, consider the case in which
[nvalue t] holds. Now consider the subcase where
[bvalue u] holds. ... *)
(* to finish *)
Admitted.
(** *** Example
[destruct] can be used on propositions with implications.
This will have the effect of performing [destruct] on the
conclusion of the implication, while leaving the hypotheses
of the implication as additional subgoals.
*)
Lemma destruct_example : forall bv t t' t'',
bvalue bv ->
(value bv -> eval t t' /\ eval t' t'') ->
eval_many t t''.
Proof.
intros bv t t' t'' Hbv H. destruct H as [ H1 H2 ].
Show 2.
unfold value. left. apply Hbv.
apply m_two with (t2 := t').
apply H1.
apply H2.
Qed.
(** *** Tip
After applying a tactic that introduces multiple subgoals,
it is sometimes useful to see not only the subgoals
themselves but also their hypotheses. Adding the command
[Show n.] to your proof script to cause Coq to display the
nth subgoal in full.
*)
(**************************************************************)
(** ** Reasoning by Cases and Induction
- [destruct] (for inductively defined propositions)
- [induction]
*)
(** *** Example
Use [destruct] to reason by cases on an inductively defined
datatype or proposition.
Note: It is possible to supply [destruct] with a pattern in
these instances also. However, the patterns become
increasingly complex for bigger inductive definitions; so it
is often more practical to omit the pattern (thereby letting
Coq choose the names of the terms and hypotheses in each
case), in spite of the fact that this adds an element of
fragility to the proof script (since the proof script will
mention names that were system-generated).
*)
Lemma e_iszero_nvalue : forall v,
nvalue v ->
eval (tm_iszero v) tm_true \/
eval (tm_iszero v) tm_false.
Proof.
intros v Hn. destruct Hn.
(* Case [n_zero].
Note how [v] becomes [tm_zero] in the goal. *)
left. apply e_iszerozero.
(* Case [n_succ].
Note how [v] becomes [tm_succ v] in the goal. *)
right. apply e_iszerosucc. apply Hn.
Qed.
(** *** Example
You can use [induction] to reason by induction on an
inductively defined datatype or proposition. This is the
same as [destruct], except that it also introduces an
induction hypothesis in the inductive cases.
*)
Lemma m_iszero : forall t u,
eval_many t u ->
eval_many (tm_iszero t) (tm_iszero u).
Proof.
intros t u Hm. induction Hm.
apply m_refl.
apply m_step with (t' := tm_iszero t').
apply e_iszero. apply H.
apply IHHm.
Qed.
(*
LAB 3: (5 minutes)
Work on the following exercise.
*)
(** *** Exercise *)
Lemma m_trans : forall t t' u,
eval_many t t' ->
eval_many t' u ->
eval_many t u.
Proof.
(** We proceed by induction on the derivation of
[eval_many t t'].
Case [m_refl]: Since [t] and [t'] must be the same, our
conclusion holds by assumption.
Case [m_step]: Now let's rename the [t'] from the lemma
statement to [u0] (as Coq likely will) and observe that
there must be some [t'] (from above the line of the
[m_step] rule) such that [eval t t'] and
[eval_many t' u0]. Our conclusion follows from from
an application of [m_step] with our new [t'] and our
induction hypothesis, which allows us to piece together
[eval_many t' u0] and [eval_many u0 u] to get
[eval_many t' u]. *)
(* to finish *)
Admitted.
(** *** Exercise
It is possible to use [destruct] not just on hypotheses but
on any lemma we have proved. If we have a lemma
<<
lemma1 : P /\ Q
>>
then we can use the tactic
<<
destruct lemma1 as [ H1 H2 ].
>>
to continue our proof with [H1 : P] and [H2 : Q] in our
context. This works even if the lemma has antecedents (they
become new subgoals); however it fail if the lemma has a
universal quantifier, such as this:
<<
lemma2 : forall x, P(x) /\ Q(x)
>>
However, remember that we can build a proof of
[P(e) /\ Q(e)] (which can be destructed) using the Coq
expression [lemma2 e]. So we need to phrase our tactic as
<<
destruct (lemma2 e) as [ H1 H2 ].
>>
An example of this technique is below.
*)
Lemma m_iszero_nvalue : forall t v,
nvalue v ->
eval_many t v ->
eval_many (tm_iszero t) tm_true \/
eval_many (tm_iszero t) tm_false.
Proof.
intros t v Hnv Hm.
destruct (e_iszero_nvalue v) as [ H1 | H2 ].
apply Hnv.
left. apply m_trans with (t' := tm_iszero v).
apply m_iszero. apply Hm.
apply m_one. apply H1.
right. apply m_trans with (t' := tm_iszero v).
apply m_iszero. apply Hm.
apply m_one. apply H2.
Qed.
(** *** Exercise
Prove the following lemma.
Hint: You may be interested in some previously proved
lemmas, such as [m_one] and [m_trans].
Note: Even though this lemma is in a comment, its solution
is also at the bottom. (Coq will give an error if we leave
it uncommented since it mentions the [eval_rtc] relation,
which was the solution to another exercise.)
<<
Lemma eval_rtc_many : forall t u,
eval_rtc t u ->
eval_many t u.
>>
*)
(** *** Exercise
Prove the following lemma.
<<
Lemma eval_many_rtc : forall t u,
eval_many t u ->
eval_rtc t u.
>>
*)
(** *** Exercise
Prove the following lemma.
<<
Lemma full_eval_to_value : forall t v,
full_eval t v ->
value v.
>>
*)
(**************************************************************)
(** ** Working with Existential Quantification
- [exists]
- [destruct] (for existential propositions)
*)
(** *** Example
Use [exists] to give the witness for an existential
quantifier in your goal.
*)
Lemma if_bvalue : forall t1 t2 t3,
bvalue t1 ->
exists u, eval (tm_if t1 t2 t3) u.
Proof.
intros t1 t2 t3 Hb. destruct Hb.
exists t2. apply e_iftrue.
exists t3. apply e_iffalse.
Qed.
(** *** Example
You may use [destruct] to break open an existential
hypothesis.
*)
Lemma m_two_exists : forall t u,
(exists w, eval t w /\ eval w u) ->
eval_many t u.
Proof.
intros t u H.
destruct H as [ w He ].
destruct He as [ He1 He2 ].
apply m_two with (t2 := w).
apply He1.
apply He2.
Qed.
(** *** Example