@@ -406,11 +406,13 @@ return the same bag unchanged.
406
406
407
407
$\square$
408
408
409
- ==== Exercise: 3 stars, recommendedM (bag_theorem) Write down an interesting
410
- theorem `bag_theorem` about bags involving the functions `count` and `add`, and
411
- prove it. Note that, since this problem is somewhat open-ended, it's possible
412
- that you may come up with a theorem which is true, but whose proof requires
413
- techniques you haven't learned yet. Feel free to ask for help if you get stuck!
409
+ ==== Exercise: 3 stars, recommendedM (bag_theorem)
410
+
411
+ Write down an interesting theorem `bag_theorem` about bags involving the
412
+ functions `count` and `add`, and prove it. Note that, since this problem is
413
+ somewhat open-ended, it's possible that you may come up with a theorem which is
414
+ true, but whose proof requires techniques you haven't learned yet. Feel free to
415
+ ask for help if you get stuck!
414
416
415
417
> bag_theorem : ?bag_theorem
416
418
@@ -510,10 +512,10 @@ _Proof_: By induction on `l1`.
510
512
- Next, suppose `l1 = n :: l1'`, with
511
513
`(l1' ++ l2) ++ l3 = l1' ++ (l2 ++ l3)`
512
514
(the induction hypothesis). We must show
513
- `((n :: l1') ++ l2) ++ l3 = (n :: l1') ++ (l2 ++ l3)`.
514
- By the definition of `++`, this follows from
515
- `n :: ((l1' ++ l2) ++ l 3) = n :: (l1' ++ (l2 ++ l3))`,
516
- which is immediate from the induction hypothesis. $\square$
515
+ `((n :: l1') ++ l2) ++ l3 = (n :: l1') ++ (l2 ++ l3)`.
516
+ By the definition of `++`, this follows from
517
+ `n :: ((l1' ++ l2) ++ l 3) = n :: (l1' ++ (l2 ++ l3))`,
518
+ which is immediate from the induction hypothesis. $\square$
517
519
518
520
==== Reversing a List
519
521
@@ -586,33 +588,33 @@ _Proof_: By induction on `l1`.
586
588
587
589
- First, suppose `l1 = []`. We must show
588
590
`length ([] ++ l2) = length [] + length l2`,
589
- which follows directly from the definitions of `length` and `++`.
591
+ which follows directly from the definitions of `length` and `++`.
590
592
591
593
- Next, suppose `l1 = n :: l1'`, with
592
594
`length (l1' ++ l2) = length l1' + length l2`.
593
- We must show
594
- `length ((n :: l1') ++ l2) = length (n :: l1') + length l2)`.
595
+ We must show
596
+ `length ((n :: l1') ++ l2) = length (n :: l1') + length l2)`.
595
597
This follows directly from the definitions of `length` and `++` together
596
- with the induction hypothesis. $\square$
598
+ with the induction hypothesis. $\square$
597
599
598
600
_Theorem_: For all lists `l`, `length (rev l) = length l`.
599
601
600
602
_Proof_: By induction on `l`.
601
603
602
604
- First, suppose `l = []`. We must show
603
605
`length (rev []) = length []`,
604
- which follows directly from the definitions of `length` and `rev`.
606
+ which follows directly from the definitions of `length` and `rev`.
605
607
606
608
- Next, suppose l = n :: l' , with
607
609
`length (rev l') = length l'`.
608
- We must show
609
- `length (rev (n :: l')) = length (n :: l')`.
610
- By the definition of `rev`, this follows from
611
- `length ((rev l') ++ [n]) = S (length l')`
612
- which, by the previous lemma, is the same as
613
- `length (rev l') + length [n] = S (length l')`.
610
+ We must show
611
+ `length (rev (n :: l')) = length (n :: l')`.
612
+ By the definition of `rev`, this follows from
613
+ `length ((rev l') ++ [n]) = S (length l')`
614
+ which, by the previous lemma, is the same as
615
+ `length (rev l') + length [n] = S (length l')`.
614
616
This follows directly from the induction hypothesis and the definition of
615
- `length`. $\square$
617
+ `length`. $\square$
616
618
617
619
The style of these proofs is rather longwinded and pedantic. After the first
618
620
few, we might find it easier to follow proofs that give fewer details (which can
@@ -918,8 +920,9 @@ $\square$
918
920
919
921
$\square$
920
922
921
- ==== Exercise: 2 starsM (baz_num_elts) Consider the following inductive
922
- definition:
923
+ ==== Exercise: 2 starsM (baz_num_elts)
924
+
925
+ Consider the following inductive definition:
923
926
924
927
> data Baz : Type where
925
928
> Baz1 : Baz -> Baz
0 commit comments