-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basics.thy
1982 lines (1645 loc) · 66.3 KB
/
Basics.thy
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
theory Basics
imports Main begin
(*
(** * Basics: Functional programming and reasoning about programs *)
(* Version of 4/7/2010 *)
(* ###################################################################### *)
(** * Enumerated Types *)
(** In Coq's programming language, almost nothing is built
in -- not even booleans or numbers! Instead, it provides powerful
tools for defining new types of data and functions that process
and transform them. *)
(* ###################################################################### *)
(** ** Days of the week *)
(** Let's start with a very simple example. The following
definition tells Coq that we are defining a new set of data
values -- a "type." The type is called [day] and its members are
[monday], [tuesday], etc. The lines of the definition can be read
"[monday] is a [day], [tuesday] is a [day], etc." *)
Inductive day : Type :=
| monday : day
| tuesday : day
| wednesday : day
| thursday : day
| friday : day
| saturday : day
| sunday : day.
*)
datatype day = Monday | Tuesday | Wednesday
| Thursday | Friday | Saturday | Sunday
(*
(** Having defined this type, we can write functions that operate
on its members. *)
Definition next_weekday (d:day) : day :=
match d with
| monday => tuesday
| tuesday => wednesday
| wednesday => thursday
| thursday => friday
| friday => monday
| saturday => monday
| sunday => monday
end.
*)
(* CH: One could define things this way, but it's easier to do
definition next_weekday :: "day \<Rightarrow> day" where
"next_weekday d \<equiv> (case d of
Monday \<Rightarrow> Tuesday |
Tuesday \<Rightarrow> Wednesday |
Wednesday \<Rightarrow> Thursday |
Thursday \<Rightarrow> Friday |
Friday \<Rightarrow> Monday |
Saturday \<Rightarrow> Monday |
Sunday \<Rightarrow> Monday)"
*)
primrec next_weekday :: "day \<Rightarrow> day" where
"next_weekday Monday = Tuesday"|
"next_weekday Tuesday = Wednesday" |
"next_weekday Wednesday = Thursday" |
"next_weekday Thursday = Friday" |
"next_weekday Friday = Monday" |
"next_weekday Saturday = Monday" |
"next_weekday Sunday = Monday"
(*
(** One thing to note is that the argument and return types of
this function are explicitly declared. Like most functional
programming languages, Coq can often work out these types even if
they are not given explicitly -- i.e., it performs some _type
inference_ -- but we'll always include them to make reading
easier. *)
(** Having defined a function, we should check that it works on
some examples. There are actually three different ways to do this
in Coq. First, we can use the command [Eval simpl] to evaluate a
compound expression involving [next_weekday]. Uncomment the
following and see what they do. *)
(* Eval simpl in (next_weekday friday). *)
(* Eval simpl in (next_weekday (next_weekday saturday)). *)
(** If you have a computer handy, now would be an excellent
moment to fire up the Coq interpreter under your favorite IDE --
either CoqIde or Proof General -- and try this for yourself. Load
this file ([Basics.v]) from the book's accompanying Coq sources,
find the above example, send it to Coq, and observe the
result. *)
(** The keyword [simpl] (for "simplify") tells Coq precisely how
to evaluate the expression we give it. For the moment, [simpl] is
the only one we'll need; later on we'll see some alternatives that
are sometimes useful. *)
(** Second, we can record what we _expect_ the result to be in
the form of a Coq [Example]: *)
Example test_next_weekday:
(next_weekday (next_weekday saturday)) = tuesday.
*)
lemma "next_weekday (next_weekday Saturday) = Tuesday"
by simp
(* CH: we could also prove this with Isar syntax via *)
lemma "next_weekday (next_weekday Saturday) = Tuesday"
proof (simp)
qed
lemma "next_weekday (next_weekday Saturday) = Tuesday"
proof -
show ?thesis by simp
qed
(* CH: simp and simpl aren't exactly the same thing, but where you'd use
simpl in Coq you probably want simp in Isabelle. Essentially,
simp does a lot more than simpl and can be given sets of rules to
use when rewriting. We demonstrate this here by using the
'add' clause and passing it the 'next_weekday_def'.
One thing that's a little different between Coq & Isabelle is that
definitions are never unfolded unless the rules of the definition
are declared simp rules *)
(*
(** This declaration does two things: it makes an
assertion (that the second weekday after [saturday] is [tuesday]),
and it gives the assertion a name that can be used to refer to it
later. *)
(** Having made the assertion, we can also ask Coq to verify it,
like this: *)
Proof. simpl. reflexivity. Qed.
(** The details are not important for now (we'll come back to
them in a bit), but essentially this can be read as "The assertion
we've just made can be proved by observing that both sides of the
equality are the same after simplification." *)
(** Third, we can ask Coq to "extract," from a [Definition], a
program in some other, more conventional, programming
language (OCaml, Scheme, or Haskell) with a high-performance
compiler. This facility is very interesting, since it gives us a
way to construct _fully certified_ programs in mainstream
languages. Indeed, this is one of the main uses for which Coq was
developed. We won't have space to dig further into this topic,
but more information can be found in the Coq'Art book by Bertot
and Castéran, as well as the Coq reference manual. *)
(* ###################################################################### *)
(** ** Booleans *)
(** In a similar way, we can define the type [bool] of booleans,
with constants [true] and [false]. *)
Inductive bool : Type :=
| true : bool
| false : bool.
*)
(* CH: In this conversion, I won't actually be using homegrown definitions of
bool and nat. Maybe I should, but I don't know if it really helps clarify
things much. *)
(*
(** Although we are rolling our own booleans here for the sake
of building up everything from scratch, Coq does, of course,
provide a default implementation of the booleans in its standard
library, together with a multitude of useful functions and
lemmas. (Take a look at [Coq.Init.Datatypes] in the Coq library
documentation if you're interested.) Whenever possible, we'll
name our own definitions and theorems so that they exactly
coincide with the ones in the standard library. *)
(** Functions over booleans can be defined in the same way as
above: *)
Definition negb (b:bool) : bool :=
match b with
| true => false
| false => true
end.
*)
(*
definition negb :: "bool \<Rightarrow> bool" where
"negb b \<equiv> (case b of
True \<Rightarrow> False |
False \<Rightarrow> True)"
*)
primrec negb :: "bool \<Rightarrow> bool" where
"negb True = False" |
"negb False = True"
(*
Definition andb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => b2
| false => false
end.
*)
(*
definition andb :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
"andb b b' \<equiv> (case b of
True \<Rightarrow> b' |
False \<Rightarrow> False)" *)
primrec andb :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
"andb True b' = b'" |
"andb False b' = False"
(*
Definition orb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => true
| false => b2
end.
*)
(*
definition orb :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
"orb b b' \<equiv> (case b of
True \<Rightarrow> True |
False \<Rightarrow> b')" *)
primrec orb :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
"orb True b' = True" |
"orb False b' = b'"
(* CH: All of the above is probably pretty straight forward, but
what about these wacky 'declare' things below?
Well, as I've stated above Isabelle is more conservative about
unrolling definitions. So in order to not have to worry about providing
explicit definitions to simp, we can set these definitions to be
part of the global simp set *)
(*
(** The last two illustrate the syntax for multi-argument
function definitions. *)
(** The following four "unit tests" constitute a complete
specification -- a truth table -- for the [orb] function: *)
Example test_orb1: (orb true false) = true.
Proof. simpl. reflexivity. Qed.
Example test_orb2: (orb false false) = false.
Proof. simpl. reflexivity. Qed.
Example test_orb3: (orb false true ) = true.
Proof. simpl. reflexivity. Qed.
Example test_orb4: (orb true true ) = true.
Proof. simpl. reflexivity. Qed.
*)
lemma "orb True False = True"
by simp
lemma "orb False False = False"
by simp
(* CH: examples of the above *)
(*
(** _A note on notation_: We will often use square brackets
to delimit fragments of Coq code in comments in .v files;
this convention, which is also used by the coqdoc
documentation tool, keeps them visually separate from the
surrounding text. In the html version of the files, these
pieces of text appear in a different font, like [this]. *)
(** The following line of magic defines an [admit] value
that can fill a hole in an incomplete definition or proof.
We'll use it in the definition of [nandb] in the following
exercise. In general, your job in the exercises is to
replace [admit] or [Admitted] with real definitions or proofs. *)
Definition admit {T: Type} : T. Admitted.
(** **** Exercise: 1 star *)
(** Complete the definitions of the following functions, then make
sure that the [Example] assertions below each can be verified by
Coq. *)
(** This function should return [true] if either or both of
its inputs are [false]. *)
Definition nandb (b1:bool) (b2:bool) : bool :=
(* SOLUTION: *)
match b1 with
| true => negb b2
| false => true
end.
*)
definition nandb :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
"nandb b b' \<equiv> negb (andb b b')"
declare nandb_def [simp]
(*
(** Remove "[Admitted.]" and fill in each proof with
"[Proof. simpl. reflexivity. Qed.]" *)
Example test_nandb1: (nandb true false) = true.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_nandb2: (nandb false false) = true.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_nandb3: (nandb false true) = true.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_nandb4: (nandb true true) = false.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
(** [] *)
*)
lemma "nandb False False = True"
by simp
(*
(** **** Exercise: 1 star *)
Definition andb3 (b1:bool) (b2:bool) (b3:bool) : bool :=
(* SOLUTION: *)
andb b1 (andb b2 b3).
*)
definition andb3 :: "bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool" where
"andb3 b1 b2 b3 \<equiv> andb b1 (andb b2 b3)"
declare andb3_def [simp]
(*
Example test_andb31: (andb3 true true true) = true.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_andb32: (andb3 false true true) = false.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_andb33: (andb3 true false true) = false.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_andb34: (andb3 true true false) = false.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
(** [] *)
*)
lemma "andb3 True True True = True"
by simp
lemma "andb3 True True False = False"
by simp
(*
(* ###################################################################### *)
(** ** Function Types *)
(** The [Check] command causes Coq to print the type of an
expression. For example, the type of [negb true] is [bool].
(Remove the comments to try it yourself.) *)
Check (negb true).
(** Functions like [negb] itself are also data values, just like
[true] and [false]. Their types are called function types, and
they are written with arrows. *)
Check negb.
*)
(* CH: There's two separate things that take the place of check, because
theorems and terms aren't on the same playing field. *)
thm refl
term negb
(*
(** The type of [negb], written [bool->bool] and pronounced
"[bool] arrow [bool]," can be read, "Given an input of type
[bool], this function produces an output of type [bool]."
Similarly, the type of [andb], written [bool->bool->bool], can be
read, "Given two inputs, both of type [bool], this function
produces an output of type [bool]." *)
(* ###################################################################### *)
(** ** Numbers *)
(** _Technical digression_: Coq provides a fairly fancy module system,
to aid in organizing large developments. In this course, we won't
need most of its features, but one of them is useful: if we
enclose a collection of declarations between [Module X] and [End
X] markers, then, in the remainder of the file after the [End],
all these definitions will be referred to by names like [X.foo]
instead of just [foo]. This means that the new definition will
not clash with the unqualified name [foo] later, which would
otherwise be an error (a name can only be defined once in a given
scope).
Here, we use this feature to introduce the definition of the type
[nat] in an inner module so that it does not shadow the one from
the standard library. *)
Module Playground1.
(** The types we have defined so far are examples of "enumerated
types": their definitions explicitly enumerate a finite collection
of elements. A more interesting way of defining a type is to give
a collection of "inductive rules" describing its elements. For
example, we can define the natural numbers as follows: *)
Inductive nat : Type :=
| O : nat
| S : nat -> nat.
(** The clauses of this definition can be read:
- [O] is a natural number (note that this is the letter "[O]," not
the numeral "[0]").
- [S] is a "constructor" that takes a natural number and yields
another one -- that is, if [n] is a natural number, then [S n]
is too. *)
(** We can write simple functions that pattern match on natural
numbers just as we did above -- for example, predecessor: *)
Definition pred (n : nat) : nat :=
match n with
| O => O
| S n' => n'
end.
(** The [n'] in the second branch of the match is different from
the [n] received as input to [pred]. When that branch of the
match is taken, we have [n = S n']. *)
End Playground1.
Definition minustwo (n : nat) : nat :=
match n with
| O => O
| S O => O
| S (S n') => n'
end.
(** Because natural numbers are such a pervasive form of data,
Coq provides a tiny bit of special built-in magic for parsing and
printing them: ordinary arabic numerals can be used as an
alternative to the "unary" notation defined by the constructors
[S] and [O]. Coq prints numbers in arabic form by default: *)
Check (S (S (S (S O)))).
Eval simpl in (minustwo 4).
(** The constructor [S] has the type [nat->nat], just like the
functions [minustwo] and [pred]: *)
Check S.
Check pred.
Check minustwo.
(** These are all things that can be applied to a number to yield a
number. However, there is a fundamental difference: functions
like [pred] and [minustwo] come with _computation rules_
-- e.g., the definition of [pred] says that [pred n] can be
simplified to [match n with | O => O | S m' => m' end] -- while
[S] has no such behavior attached. Although it is a function in
the sense that it can be applied to an argument, it does not _do_
anything at all! *)
(** Every inductively defined set ([weekday], [nat], [bool], etc.) is
actually a set of "expressions". The definition of [nat] says how
expressions in the set [nat] can be constructed:
- the expression [O] belongs to the set [nat];
- if [n] is an expression belonging to the set [nat], then [S n]
is also an expression belonging to the set [nat]; and
- expressions formed in these two ways are the only ones
belonging to the set [nat].
These three conditions are the precise force of the [Inductive]
declaration. They imply that
the expression [O],
the expression [S O],
the expression [S (S O)],
the expression [S (S (S O))],
and so on
all belong to the set [nat], while other expressions like [true] and
[S (S false)] do not. *)
(** For most function definitions over numbers, pure pattern
matching is not enough: we also need recursion. For example, to
check that a number [n] is even, we may need to recursively check
whether [n-2] is even. To write such functions, we use the
keyword [Fixpoint]. *)
Fixpoint evenb (n:nat) : bool :=
match n with
| O => true
| S O => false
| S (S n') => evenb n'
end.
*)
(* CH: you might think that the following would work!
it doesn't because primrec is more restrictive than Fixpoint
in Coq, however we can do it with fun
primrec myeven :: "nat \<Rightarrow> bool" where
"myeven 0 = True" |
"myeven (Suc 0) = False" |
"myeven (Suc (Suc n)) = myeven n"
*)
fun myeven :: "nat \<Rightarrow> bool" where
"myeven 0 = True" |
"myeven (Suc 0) = False" |
"myeven (Suc (Suc n)) = myeven n"
(* CH: OR!! *)
primrec myeven' :: "nat \<Rightarrow> bool" where
"myeven' 0 = True" |
"myeven' (Suc n) = negb (myeven n)"
(*
(** When Coq checks this definition, it notes that [evenb] is
"decreasing on 1st argument." What this means is that we are
performing a "structural recursion" over the argument [n] -- i.e.,
that we make recursive calls only on strictly smaller values of
[n]. This implies that all calls to [evenb] will eventually
terminate. Coq demands that some argument of _every_ [Fixpoint]
definition is decreasing. *)
(** We can define [oddb] by a similar [Fixpoint] declaration, but here
is a simpler definition that will be easier to work with later: *)
Definition oddb (n:nat) : bool := negb (evenb n).
Example test_oddb1: (oddb (S O)) = true.
Proof. simpl. reflexivity. Qed.
Example test_oddb2: (oddb (S (S (S (S O))))) = false.
Proof. simpl. reflexivity. Qed.
(** Naturally, we can also define multi-argument functions by
recursion. *)
(* Once again, a module to avoid polluting the namespace. *)
Module Playground2.
Fixpoint plus (n : nat) (m : nat) : nat :=
match n with
| O => m
| S n' => S (plus n' m)
end.
*)
primrec myplus :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
"myplus 0 m = m" |
"myplus (Suc n) m = Suc (myplus n m)"
(*
(** Adding three to two now gives us five, as we'd expect. *)
(* Eval simpl in (plus (S (S (S O))) (S (S O))). *)
*)
(* CH: rar! *)
lemma "myplus (Suc (Suc (Suc 0))) (Suc (Suc 0)) = (Suc (Suc (Suc (Suc (Suc 0)))))"
by simp
lemma "myplus 3 2 = 5"
by (simp add: eval_nat_numeral)
(* CH: nat_number is a convenient tool for converting back and forth between unary and decimal
representations of numbers. We can go ahead and add nat_number to the global simpset.
As a warning, though, we may have to *remove it* at certain points to prevent infinite
loops in the rewriter. It's more powerful and more aggressive than the one in Coq.
In particular, it uses hypotheses for rewrite while Coq does not. This tends to make Isabelle proofs
more succinct, but occasionally requires a little care.
*)
(*
(** The simplification that Coq performs to reach this conclusion can
be visualized as follows: *)
(* [plus (S (S (S O))) (S (S O))]
--> [S (plus (S (S O)) (S (S O)))] by the second clause of the [match]
--> [S (S (plus (S O) (S (S O))))] by the second clause of the [match]
--> [S (S (S (plus O (S (S O)))))] by the second clause of the [match]
--> [S (S (S (S (S O))))] by the first clause of the [match] *)
(** As a notational convenience, if two or more arguments have
the same type, they can be written together. In the following
definition, [(n m : nat)] means just the same as if we had written
[(n : nat) (m : nat)]. *)
Fixpoint mult (n m : nat) : nat :=
match n with
| O => O
| S n' => plus m (mult n' m)
end.
(** You can match two expressions at once: *)
Fixpoint minus (n m:nat) : nat :=
match n, m with
| O, _ => n
| S n', O => S n'
| S n', S m' => minus n' m'
end.
*)
fun myminus :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
"myminus 0 _ = 0" |
"myminus (Suc n) 0 = Suc n" |
"myminus (Suc n) (Suc m) = myminus n m"
(* CH: We have to use fun instead of primrec because
primrec is more restrictive than fixpoint!
We still have wildcards though. *)
(*
(** (The _ in the first line is a _wildcard pattern_. Writing _ in a
pattern is the same as writing some variable that doesn't get used
on the right-hand side. The _ avoids the need to make up a bogus
name in this case. *)
End Playground2.
Fixpoint exp (base power : nat) : nat :=
match power with
| O => S O
| S p => mult base (exp base p)
end.
Example test_mult1: (mult 3 3) = 9.
Proof. simpl. reflexivity. Qed.
(** **** Exercise: 1 star *)
(** Recall the standard factorial function:
<<
factorial(0) = 1
factorial(n) = n * factorial(n-1) (if n>0)
>>
Translate this into Coq. *)
Fixpoint factorial (n:nat) : nat :=
(* SOLUTION: *)
match n with
| O => 1
| S n' => mult n (factorial n')
end.
Example test_factorial1: (factorial 3) = 6.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_factorial2: (factorial 5) = (mult 10 12).
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
(** [] *)
(** We can make numerical expressions a little easier to read and
write by introducing "notations" for addition, multiplication, and
subtraction. *)
Notation "x + y" := (plus x y) (at level 50, left associativity) : nat_scope.
Notation "x - y" := (minus x y) (at level 50, left associativity) : nat_scope.
Notation "x * y" := (mult x y) (at level 40, left associativity) : nat_scope.
Check ((0 + 1) + 1).
(** Note that these do not change the definitions we've already
made: they are simply instructions to the Coq parser to accept [x
+ y] in place of [plus x y] and, conversely, to the Coq
pretty-printer to display [plus x y] as [x + y].
Each notation-symbol in Coq, such as + - *, is active in a
"notation scope". Coq tries to guess what scope you mean, so when
you write [S(O*O)] it guesses [nat_scope], but when you write the
Cartesian-product (tupling) type [bool*bool] it guesses
[type_scope]. Sometimes you have to help it out with
percent-notation by writing [(x*y)%nat], and sometimes in Coq's
feedback to you it will use [%nat] to indicate what scope a
notation is in.
Notation scopes also apply to numeral notation (3,4,5, etc.), so you
may sometimes see [0%nat] which means [O], or [0%Z] which means the
Integer zero.
*)
(** When we say that Coq comes with nothing built-in, we really
mean it: even equality testing for numbers is a user-defined
operation! *)
(** The [beq_nat] function tests [nat]ural numbers for [eq]uality,
yielding a [b]oolean. Note the use of nested [match]es (we could
also have used a simultaneous match, as we did in [minus].) *)
*)
(* CH: So, I'm not going to try defining an equality for naturals in Isabelle.
All types have an inherent equality in Isabelle, and I feel that it's
far more idiomatic to use that *)
(*
Fixpoint beq_nat (n m : nat) : bool :=
match n with
| O => match m with
| O => true
| S m' => false
end
| S n' => match m with
| O => false
| S m' => beq_nat n' m'
end
end.
(** Similarly, the [ble_nat] function tests [nat]ural numbers for
[l]ess-or-[e]qual, yielding a [b]oolean. *)
Fixpoint ble_nat (n m : nat) : bool :=
match n with
| O => true
| S n' =>
match m with
| O => false
| S m' => ble_nat n' m'
end
end.
Example test_ble_nat1: (ble_nat 2 2) = true.
Proof. simpl. reflexivity. Qed.
Example test_ble_nat2: (ble_nat 2 4) = true.
Proof. simpl. reflexivity. Qed.
Example test_ble_nat3: (ble_nat 4 2) = false.
Proof. simpl. reflexivity. Qed.
(** **** Exercise: 1 star *)
(** The [blt_nat] function tests [nat]ural numbers for [l]ess-[t]han,
yielding a [b]oolean. Instead of making up a new [Fixpoint] for
this one, define it in terms of a previously defined function. *)
Definition blt_nat (n m : nat) : bool :=
(* SOLUTION: *)
(ble_nat (S n) m).
Example test_blt_nat1: (blt_nat 2 2) = false.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_blt_nat2: (blt_nat 2 4) = true.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
Example test_blt_nat3: (blt_nat 4 2) = false.
(* SOLUTION: *) Proof. simpl. reflexivity. Qed.
(** [] *)
(* ###################################################################### *)
(** * Proof By Simplification *)
(** Now that we've defined a few datatypes and functions, let's
turn to the question of how to state and prove properties of their
behavior. Actually, in a sense, we've already started doing this:
each [Example] in the previous sections makes a precise claim
about the behavior of some function on some particular inputs.
The proofs of these claims were always the same: use the
function's definition to simplify the expressions on both sides of
the [=] and notice that they become identical.
The same sort of "proof by simplification" can be used to prove
more interesting properties as well. For example, the fact that
[0] is a "neutral element" for [plus] on the left can be proved
just by observing that [plus 0 n] reduces to [n] no matter what
[n] is, since the definition of [plus] is recursive in its first
argument. *)
Theorem plus_O_n : forall n:nat, plus 0 n = n.
Proof.
simpl. reflexivity. Qed.
*)
(* CH: So let's get into a few differences between Isabelle and Coq when
it comes to real proofs. First off, there's a difference between
the quantifiers of the object logic (in this case HOL) and the
meta logic of Isabelle.
For example, in the object logic of HOL the above theorem would
look like *)
theorem "\<forall> n. myplus 0 n = n"
by (intro allI, simp)
theorem "\<forall> n. myplus 0 n = n"
by auto
(* CH: We could, instead, use the forall of the metalogic *)
theorem "\<And>n. myplus 0 n = n"
by simp
(* CH: In some ways, though, it's more idiomatic to use schematic variables*)
theorem "myplus 0 n = n"
by simp
theorem "\<forall> n. myplus 0 n = n"
proof (rule allI)
fix n
show "myplus 0 n = n" by simp
qed
(* A number of the subtleties related to the difference between schematic
and metalogic bound variables are beyond me, I'll be honest. I've noticed
though that most theorems are described in terms of schematic variables
as much as possible, unless it causes an induction principle to become
too weak *)
(*
(** The [reflexivity] command implicitly simplifies both sides of the
equality before testing to see if they are the same, so we can
shorten the proof a little. *)
Theorem plus_O_n' : forall n:nat, plus 0 n = n.
Proof.
reflexivity. Qed.
*)
(* CH: The equivalent of this is the rule refl, but it doesn't simplifify
first so you have to be more cautious in its use. refl is called by
simp, though, making Isabelle a slight oppositite of Coq. *)
theorem "n=n"
by (rule refl)
(* CH: Now there's a cute trick in Isabelle/Proof General - type C-c C-f and you can search for theorems
by the terms that appear in them. Further, if you type C-c C-f intro you get a list of the introduction
rules you could apply with a "rule" command. This is pretty convenient for when you can't
quite remember what the name of a theorem is.*)
(*
(*
(** The form of this theorem and proof are almost exactly the
same as the examples above: the only differences are that we've
added the quantifier [forall n:nat] and that we've used the
keyword [Theorem] instead of [Example]. Indeed, the latter
difference is purely a matter of style; the keywords [Example] and
[Theorem] (and a few others, including [Lemma], [Fact], and
[Remark]) mean exactly the same thing to Coq.
The keywords [simpl] and [reflexivity] are examples of "tactics".
A tactic is a command that is used between [Proof] and [Qed] to
tell Coq how it should check the correctness of some claim we are
making. We will see several more tactics in the rest of this
lecture, and yet more in future lectures. *)
(** **** Exercise: 1 star, optional *)
(** What will Coq print in response to this query? *)
(* Eval simpl in (forall n:nat, plus n 0 = n). *)
(** What about this one? *)
(* Eval simpl in (forall n:nat, plus 0 n = n). *)
(** Explain the difference. [] *)
(* ###################################################################### *)
(** * The [intros] tactic *)
(** Aside from unit tests, which apply functions to particular
arguments, most of the properties we will be interested in proving
about programs will begin with some quantifiers (e.g., "for all
numbers [n], ...") and/or hypothesis ("assuming [m=n], ..."). In
such situations, we will need to be able to reason by _assuming
the hypothesis_ -- i.e., we start by saying "OK, suppose [n] is
some arbitrary number," or "OK, suppose [m=n]."
The [intros] tactic permits us to do this by moving one or more
quantifiers or hypotheses from the goal to a "context" of current
assumptions.
For example, here is a slightly different proof of the same theorem. *)
*)
(* CH:
There isn't anything quite like the intros tactic in Isabelle; however,
the introduction rule of the object-level forall is allI which can be
applied as many times as needed by the command (intro allI)
this is probably the closest thing to 'intros' there is in Isabelle.
*)
(*
Theorem plus_O_n'' : forall n:nat, plus 0 n = n.
Proof.
intros n. reflexivity. Qed.
*)
*)
lemma "\<forall> n. myplus 0 n = n"
apply (rule allI)
apply simp
done
lemma "\<forall> n. myplus 0 n = n"
by (intro allI, simp)
lemma "\<forall> n. myplus 0 n = n"
by (auto)
(* auto can handle some pretty simple intro rules *)
(*
(*
(** Step through this proof in Coq and notice how the goal and
context change. *)
Theorem plus_1_l : forall n:nat, plus 1 n = S n.
Proof.
intros n. reflexivity. Qed.
Theorem mult_0_l : forall n:nat, mult 0 n = 0.
Proof.
intros n. reflexivity. Qed.
(** (The [_l] suffix in the names of these theorems is
pronounced "on the left.") *)
(* ###################################################################### *)
(** * Proof by rewriting *)
(** Here is a slightly more interesting theorem: *)
Theorem plus_id_example : forall n m:nat,
n = m ->
plus n n = plus m m.
(** Instead of making a completely universal claim about all numbers
[n] and [m], this theorem talks about a more specialized property
that only holds when [n = m]. The arrow symbol is pronounced
"implies".
Since [n] and [m] are arbitrary numbers, we can't just use
simplification to prove this theorem. Instead, we prove it by
observing that, if we are assuming [n = m], then we can replace
[n] with [m] in the goal statement and obtain an equality with the
same expression on both sides. The tactic that tells Coq to
perform this replacement is called [rewrite]. *)
Proof.
intros n m. (* move both quantifiers into the context *)
intros H. (* move the hypothesis into the context *)
rewrite -> H. (* Rewrite the goal using the hypothesis *)
reflexivity. Qed.
*)
*)
(* CH: There's two points I'd like to make here. First, that in general
we'd do this with simp and be down with it! As so *)
lemma "n=m \<Longrightarrow> myplus n n = myplus m m"
by simp
lemma assumes eq:"n=m"
shows "myplus n n = myplus m m"
proof -
from eq show ?thesis by simp
qed
(* CH: However, we could do this more explicitly via subst*)
lemma "n=m \<Longrightarrow> myplus n n = myplus m m"
by (erule ssubst, rule refl)
(*
(** The first line of the proof moves the universally quantified
variables [n] and [m] into the context. The second moves the
hypothesis [n = m] into the context and gives it the name [H].
The third tells Coq to rewrite the current goal ([plus n n = plus
m m]) by replacing the left side of the equality hypothesis [H]
with the right side.
(The arrow symbol in the [rewrite] has nothing to do with
implication: it tells Coq to apply the rewrite from left to right.
To rewrite from right to left, you can use [rewrite <-]. Try
making this change in the above proof and see what difference it
makes in Coq's behavior.) *)
(** **** Exercise: 1 star *)
(** Remove [Admitted.] and fill in the proof. *)
Theorem plus_id_exercise : forall n m o : nat,
n = m -> m = o -> plus n m = plus m o.
Proof.
(* SOLUTION: *)
intros m n o.
intros EQmn.
intros EQno.
rewrite -> EQmn.
rewrite -> EQno.
reflexivity. Qed.
(** [] *)
(** The [Admitted] command tells Coq that we want to give up
trying to prove this theorem and just accept it as a given. This
can be useful for developing longer proofs, since we can state
subsidiary facts that we believe will be useful for making some
larger argument, use [Admitted] to accept them on faith for the