Skip to content

Commit 73ee3bf

Browse files
committed
Lists: normalize whitespace
1 parent e02172a commit 73ee3bf

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/Lists.lidr

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
> %default total
1010

11+
1112
== Pairs of Numbers
1213

1314
In an inductive type definition, each constructor can take any number of
@@ -94,20 +95,23 @@ match in `fst` and `snd`. We can do this with `case`.
9495
Notice that `case` matches just one pattern here. That's because `NatProd`s can
9596
only be constructed in one way.
9697

98+
9799
=== Exercise: 1 star (snd_fst_is_swap)
98100

99101
> snd_fst_is_swap : (p : NatProd) -> (snd p, fst p) = swap_pair p
100102
> snd_fst_is_swap p = ?snd_fst_is_swap_rhs
101103

102104
$\square$
103105

106+
104107
=== Exercise: 1 star, optional (fst_swap_is_snd)
105108

106109
> fst_swap_is_snd : (p : NatProd) -> fst (swap_pair p) = snd p
107110
> fst_swap_is_snd p = ?fst_swap_is_snd_rhs
108111

109112
$\square$
110113

114+
111115
== Lists of Numbers
112116

113117
Generalizing the definition of pairs, we can describe the type of _lists_ of
@@ -168,6 +172,7 @@ square-bracket notation for lists; the right-hand side of the third one
168172
illustrates Coq's syntax for declaring n-ary notations and translating them to
169173
nested sequences of binary constructors.
170174
175+
171176
=== Repeat
172177
173178
A number of functions are useful for manipulating lists. For example, the
@@ -178,6 +183,7 @@ A number of functions are useful for manipulating lists. For example, the
178183
> repeat n Z = Nil
179184
> repeat n (S k) = n :: repeat n k
180185
186+
181187
=== Length
182188
183189
The `length` function calculates the length of a list.
@@ -186,6 +192,7 @@ The `length` function calculates the length of a list.
186192
> length Nil = Z
187193
> length (h :: t) = S (length t)
188194
195+
189196
=== Append
190197
191198
The `app` function concatenates (appends) two lists.
@@ -208,6 +215,7 @@ convenient to have an infix operator for it.
208215
> test_app3 : ((1::2::3::[]) ++ []) = (1::2::3::[])
209216
> test_app3 = Refl
210217
218+
211219
=== Head (with default) and Tail
212220
213221
Here are two smaller examples of programming with lists. The `hd` function
@@ -232,6 +240,7 @@ first element, so we must pass a default value to be returned in that case.
232240
> test_tl : tl (1::2::3::[]) = (2::3::[])
233241
> test_tl = Refl
234242
243+
235244
=== Exercises
236245
237246
==== Exercise: 2 stars, recommended (list_funs)
@@ -259,6 +268,7 @@ below. Have a look at the tests to understand what these functions should do.
259268
260269
$\square$
261270
271+
262272
==== Exercise: 3 stars, advanced (alternate)
263273
264274
Complete the definition of `alternate`, which "zips up" two lists into one,
@@ -289,6 +299,7 @@ requires defining a new kind of pairs, but this is not the only way.)
289299
290300
$\square$
291301
302+
292303
=== Bags via Lists
293304
294305
A `bag` (or `multiset`) is like a set, except that each element can appear
@@ -298,6 +309,7 @@ represent a bag of numbers as a list.
298309
> Bag : Type
299310
> Bag = NatList
300311
312+
301313
==== Exercise: 3 stars, recommended (bag_functions)
302314
303315
Complete the following definitions for the functions `count`, `sum`, `add`, and
@@ -352,6 +364,7 @@ functions that have already been defined.
352364
353365
$\square$
354366
367+
355368
==== Exercise: 3 stars, optional (bag_more_functions)
356369
357370
Here are some more bag functions for you to practice with.
@@ -386,7 +399,7 @@ return the same bag unchanged.
386399
> test_remove_all3 : count 4 (remove_all 5 (2::1::5::4::1::4::[])) = 2
387400
> test_remove_all3 = ?test_remove_all3_rhs
388401
389-
> test_remove_all4 : count 5
402+
> test_remove_all4 : count 5
390403
> (remove_all 5 (2::1::5::4::5::1::4::5::1::4::[])) = 0
391404
> test_remove_all4 = ?test_remove_all4_rhs
392405
@@ -401,6 +414,7 @@ return the same bag unchanged.
401414
402415
$\square$
403416
417+
404418
==== Exercise: 3 stars, recommended (bag_theorem)
405419
406420
Write down an interesting theorem `bag_theorem` about bags involving the
@@ -413,6 +427,7 @@ ask for help if you get stuck!
413427
414428
$\square$
415429
430+
416431
== Reasoning About Lists
417432
418433
As with numbers, simple facts about list-processing functions can sometimes be
@@ -441,13 +456,15 @@ tail of the list it is constructing).
441456
Usually, though, interesting theorems about lists require induction for their
442457
proofs.
443458
459+
444460
==== Micro-Sermon
445461
446462
Simply reading example proof scripts will not get you very far! It is important
447463
to work through the details of each one, using Idris and thinking about what
448464
each step achieves. Otherwise it is more or less guaranteed that the exercises
449465
will make no sense when you get to them. 'Nuff said.
450466
467+
451468
=== Induction on Lists
452469
453470
Proofs by induction over datatypes like `NatList` are a little less familiar
@@ -512,6 +529,7 @@ _Proof_: By induction on `l1`.
512529
`n :: ((l1' ++ l2) ++ l 3) = n :: (l1' ++ (l2 ++ l3))`,
513530
which is immediate from the induction hypothesis. $\square$
514531
532+
515533
==== Reversing a List
516534
517535
For a slightly more involved example of inductive proof over lists, suppose we
@@ -527,6 +545,7 @@ use `app` to define a list-reversing function `rev`:
527545
> test_rev2 : rev Nil = Nil
528546
> test_rev2 = Refl
529547
548+
530549
==== Properties of rev
531550
532551
Now let's prove some theorems about our newly defined `rev`. For something a bit
@@ -583,7 +602,7 @@ _Proof_: By induction on `l1`.
583602
584603
- First, suppose `l1 = []`. We must show
585604
`length ([] ++ l2) = length [] + length l2`,
586-
which follows directly from the definitions of `length` and `++`.
605+
which follows directly from the definitions of `length` and `++`.
587606
588607
- Next, suppose `l1 = n :: l1'`, with
589608
`length (l1' ++ l2) = length l1' + length l2`.
@@ -650,6 +669,7 @@ the book; it can save you a lot of time!
650669
If you are using ProofGeneral, you can run Search with C-c C-a C-a. Pasting its
651670
response into your buffer can be accomplished with C-c C-;.
652671
672+
653673
=== List Exercises, Part 1
654674
655675
==== Exercise: 3 stars (list_exercises)
@@ -680,6 +700,7 @@ An exercise about your implementation of `nonzeros`:
680700
681701
$\square$
682702
703+
683704
==== Exercise: 2 stars (beq_NatList)
684705
685706
Fill in the definition of `beq_NatList`, which compares lists of numbers for
@@ -702,6 +723,7 @@ equality. Prove that `beq_NatList l l` yields `True` for every list `l`.
702723
703724
$\square$
704725
726+
705727
=== List Exercises, Part 2
706728
707729
==== Exercise: 3 stars, advanced (bag_proofs)
@@ -726,6 +748,7 @@ The following lemma about `leb` might help you in the next proof.
726748
727749
$\square$
728750
751+
729752
==== Exercise: 3 stars, optional (bag_count_sum)
730753
731754
Write down an interesting theorem `bag_count_sum` about bags involving the
@@ -736,6 +759,7 @@ the proof depends on how you defined `count`!)
736759
737760
$\square$
738761
762+
739763
==== Exercise: 4 stars, advanced (rev_injective)
740764
741765
Prove that the `rev` function is injective -- that is,
@@ -747,6 +771,7 @@ Prove that the `rev` function is injective -- that is,
747771
748772
$\square$
749773
774+
750775
== Options
751776
752777
Suppose we want to write a function that returns the `n`th element of some list.
@@ -811,6 +836,7 @@ default in the `None` case.
811836
> option_elim d (Some k) = k
812837
> option_elim d None = d
813838
839+
814840
==== Exercise: 2 stars (hd_error)
815841
816842
Using the same idea, fix the `hd` function from earlier so we don't have to pass
@@ -830,6 +856,7 @@ a default element for the `Nil` case.
830856
831857
$\square$
832858
859+
833860
==== Exercise: 1 star, optional (option_elim_hd)
834861
835862
This exercise relates your new `hd_error` to the old `hd`.
@@ -840,6 +867,7 @@ This exercise relates your new `hd_error` to the old `hd`.
840867
841868
$\square$
842869
870+
843871
== Partial Maps
844872
845873
As a final illustration of how data structures can be defined in Idris, here is
@@ -861,6 +889,7 @@ We'll also need an equality test for `Id`s:
861889
> beq_id : (x1, x2 : Id) -> Bool
862890
> beq_id (MkId n1) (MkId n2) = beq_nat n1 n2
863891
892+
864893
==== Exercise: 1 star (beq_id_refl)
865894
866895
> beq_id_refl : (x : Id) -> True = beq_id x x
@@ -898,6 +927,7 @@ first one it encounters.
898927
> then Some v
899928
> else find x d'
900929
930+
901931
==== Exercise: 1 star (update_eq)
902932
903933
> update_eq : (d : PartialMap) -> (x : Id) -> (v : Nat) ->
@@ -906,6 +936,7 @@ first one it encounters.
906936
907937
$\square$
908938
939+
909940
==== Exercise: 1 star (update_neq)
910941
911942
> update_neq : (d : PartialMap) -> (x, y : Id ) -> (o : Nat) ->
@@ -915,6 +946,7 @@ $\square$
915946
916947
$\square$
917948
949+
918950
==== Exercise: 2 stars (baz_num_elts)
919951
920952
Consider the following inductive definition:
@@ -926,4 +958,4 @@ Consider the following inductive definition:
926958
How _many_ elements does the type `Baz` have? (Answer in English or the natural
927959
language of your choice.)
928960
929-
$\square$
961+
$\square$

0 commit comments

Comments
 (0)