Skip to content

Commit 1a19186

Browse files
committed
snippets updated
1 parent 09c5c17 commit 1a19186

File tree

8 files changed

+1021
-216
lines changed

8 files changed

+1021
-216
lines changed

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ <h3>
6464
<p>
6565
Play Coq snippets in the browser:
6666
<!-- -*- CHAPTER_LIST -*- -->
67-
<a href="snippets/ch0.html">Ch0</a>, <a href="snippets/ch1.html">Ch1</a>, <a href="snippets/ch2.html">Ch2</a>, <a href="snippets/ch3.html">Ch3</a>, <a href="snippets/ch4.html">Ch4</a>, <a href="snippets/ch6.html">Ch6</a>, <a href="snippets/ch7_1.html">Ch7_1</a>, <a href="snippets/ch7_2.html">Ch7_2</a>, <a href="snippets/ch7_3.html">Ch7_3</a>, <a href="snippets/ch7_4.html">Ch7_4</a>, <a href="snippets/ch7_5.html">Ch7_5</a>, <a href="snippets/ch8.html">Ch8</a>.<br/>The corresponding .v files are available on github in the
67+
<a href="snippets/ch0.html">Ch0</a>, <a href="snippets/ch1.html">Ch1</a>, <a href="snippets/ch2.html">Ch2</a>, <a href="snippets/ch3.html">Ch3</a>, <a href="snippets/ch4.html">Ch4</a>, <a href="snippets/ch5.html">Ch5</a>, <a href="snippets/ch6.html">Ch6</a>, <a href="snippets/ch7_1.html">Ch7_1</a>, <a href="snippets/ch7_2.html">Ch7_2</a>, <a href="snippets/ch7_3.html">Ch7_3</a>, <a href="snippets/ch7_4.html">Ch7_4</a>, <a href="snippets/ch7_5.html">Ch7_5</a>, <a href="snippets/ch8.html">Ch8</a>.<br/>The corresponding .v files are available on github in the
6868
<a href="https://github.com/math-comp/mcb/tree/master/coq">coq folder</a>.
6969
</p>
7070

docs/snippets/ch0.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ <h1>Chapter 0</h1><div><textarea id='coq-code'>
2525
by rewrite mulnS muln1 -addnA -mulSn -mulnS.
2626
Qed.
2727

28+
(* Recommended headers *)
29+
(* From mathcomp Require Import all_ssreflect. *)
30+
(* Set Implicit Arguments. *)
31+
(* Unset Strict Implicit. *)
32+
(* Unset Printing Implicit Defensive. *)
2833

2934
</textarea></div>
3035
<script type='text/javascript'>

docs/snippets/ch1.html

Lines changed: 217 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,141 +20,312 @@ <h1>Chapter 1</h1><div><textarea id='coq-code'>
2020
Set Implicit Arguments.
2121
Unset Strict Implicit.
2222

23-
(* functions *)
24-
Definition f := fun n => n + 1.
23+
(* 1.1 Functions *)
24+
(* 1.1.1 Defining functions, performing computation *)
25+
26+
Definition f1 := fun n => n + 1.
27+
Definition f2 n := n + 1.
28+
Definition f (n : nat) : nat := n + 1.
29+
2530
About f.
2631
Print f.
2732
Locate "_ ^~ _".
2833
Check f 3.
2934
Fail Check f (fun x : nat => x).
3035
Eval compute in f 3.
3136

37+
38+
(* 1.1.2 Functions with several arguments *)
39+
40+
Definition g' (n : nat) (m : nat) : nat := n + m * 2.
41+
Definition g (n m : nat ) : nat := n + m * 2.
42+
43+
About g.
44+
45+
Definition h (n : nat) : nat -> nat := fun m => n + m * 2.
46+
47+
About h.
48+
49+
Print g.
50+
Print h.
51+
52+
Check g 3.
53+
Compute g 2 3.
54+
55+
56+
(* 1.1.3 Higher-order functions *)
57+
3258
Definition repeat_twice (g : nat -> nat) : nat -> nat :=
3359
fun x => g (g x).
3460
Eval compute in repeat_twice f 2.
3561
Check (repeat_twice f).
3662
Fail Check (repeat_twice f f).
3763

38-
(* data *)
64+
65+
(* 1.1.4 Local definitions *)
66+
67+
Compute
68+
let n := 33 : nat in
69+
let e := n + n + n in
70+
e + e + e.
71+
72+
73+
(* 1.2 Data types, first examples *)
74+
(* 1.2.1 Boolean values *)
75+
76+
(* This definition is already imported *)
77+
(* Inductive bool := true | false. *)
78+
3979
Check true.
40-
Definition f23 b := if b then 2 else 3.
41-
Eval compute in f23 true.
42-
Eval compute in f23 false.
43-
Definition andb b1 b2 := if b1 then b2 else false.
80+
81+
Definition twoVthree (b : bool) := if b then 2 else 3.
82+
83+
Eval compute in twoVthree true.
84+
Eval compute in twoVthree false.
85+
86+
Definition andb (b1 b2 : bool) := if b1 then b2 else false.
87+
Definition orb (b1 b2 : bool) := if b1 then true else b2.
88+
89+
90+
(* 1.2.2 Natural Numbers *)
91+
(* Inductive nat := O | S (n : nat). *)
4492

4593
Check fun x => f x.+1.
94+
95+
Definition non_zero n := if n is p.+1 then true else false.
96+
97+
Compute non_zero 5.
98+
4699
Locate ".+4".
47-
Definition pred n := if n is u.+1 then u else n.
48-
Definition pred5 n :=
49-
if n is u.+1.+1.+1.+1.+1 then u else 0.
100+
101+
Definition pred n := if n is u.+1 then u else O.
102+
103+
Definition larger_then_4 n :=
104+
if n is u.+1.+1.+1.+1.+1 then true else false.
105+
50106
Definition three_patterns n :=
51107
match n with
52108
u.+1.+1.+1.+1.+1 => u
53109
| v.+1 => v
54110
| 0 => n
55111
end.
112+
56113
Fail Definition wrong (n : nat) :=
57114
match n with 0 => true end.
115+
58116
Definition same_bool b1 b2 :=
59117
match b1, b2 with
60118
| true, true => true
119+
| false, false => true
61120
| _, _ => false
62121
end.
122+
63123
Definition same_bool2 b1 b2 :=
64124
match b1 with
65125
| true => match b2 with true => true | _ => false end
66-
| _ => false
126+
| false => match b2 with true => false | _ => true end
67127
end.
68128

69-
(* recursion *)
129+
130+
(* 1.2.3 Recursion on natural numbers *)
131+
70132
Fixpoint addn n m :=
71133
if n is p.+1 then (addn p m).+1 else m.
134+
72135
Fixpoint addn2 n m :=
73136
match n with
74137
| 0 => m
75138
| p.+1 => (addn2 p m).+1
76139
end.
140+
77141
Fail Fixpoint loop n :=
78142
if n is 0 then loop n else 0.
143+
79144
Fixpoint subn m n : nat :=
80145
match m, n with
81146
| p.+1, q.+1 => subn p q
82147
| _ , _ => m
83148
end.
149+
84150
Fixpoint eqn m n :=
85151
match m, n with
86152
| 0, 0 => true
87153
| p.+1, q.+1 => eqn p q
88154
| _, _ => false
89155
end.
156+
90157
Notation "x == y" := (eqn x y).
91158

92-
(* containers *)
159+
Compute O == O.
160+
Compute 1 == S O.
161+
Compute 1 == O.+1.
162+
Compute 2 == S O.
163+
Compute 2 == 1.+1.
164+
Compute 2 == addn 1 O.+1.
165+
166+
Definition leq m n := m - n == 0.
167+
Notation "m <= n" := (leq m n).
168+
169+
170+
(* 1.3 Containers *)
171+
172+
Inductive listn : Set := niln | consn (hd : nat) (tl : listn).
173+
174+
Check consn 1 (consn 2 niln).
175+
176+
Inductive testbool : Set := ttrue | tfalse.
177+
178+
Fail Check consn ttrue (consn tfalse niln).
179+
180+
Inductive listb := nilb | consb (hd : testbool) (tl : listb).
181+
182+
183+
(* 1.3.1 The polymorphic sequence data type *)
184+
185+
(* This definition is already imported. *)
186+
(* Inductive seq (A : Type) := nil | cons (hd : A) (tl : seq A). *)
187+
93188
About cons.
94189
Check cons 2 nil.
95190
Check 1 :: 2 :: 3 :: nil.
96191
Check fun l => 1 :: 2 :: 3 :: l.
97-
Definition first_element_or_0 (s : seq nat) :=
98-
if s is a :: _ then a else 0.
192+
193+
Definition head T (x0 : T) (s : seq T) := if s is x :: _ then x else x0.
194+
195+
196+
(* 1.3.2 Recursion for sequences *)
99197

100198
Fixpoint size A (s : seq A) :=
101199
if s is _ :: tl then (size tl).+1 else 0.
200+
102201
Fixpoint map A B (f : A -> B) s :=
103202
if s is e :: tl then f e :: map f tl else nil.
203+
204+
Notation "[ 'seq' E | i <- s ]" := (map (fun i => E) s).
205+
104206
Eval compute in [seq i.+1 | i <- [:: 2; 3]].
105-
Check (3, false).
106-
Eval compute in (true, false).1.
207+
208+
(* This definition is already imported *)
209+
(* Inductive option A := None | Some (a : A). *)
107210

108211
Definition only_odd (n : nat) : option nat :=
109212
if odd n then Some n else None.
110213

111214
Definition ohead (A : Type) (s : seq A) :=
112215
if s is x :: _ then Some x else None.
113216

114-
(* section *)
217+
Inductive pair (A B : Type) : Type := mk_pair (a : A) (b : B).
218+
219+
(* These definitions are already imported *)
220+
(* Notation "( a , b )" := (mk_pair a b). *)
221+
(* Notation "A * B" := (pair A B). *)
222+
(* Definition fst A B (p : pair A B) := *)
223+
(* match p with mk_pair x _ => x end. *)
224+
225+
Check (3, false).
226+
Compute (true, false).1.
227+
228+
229+
(* 1.1.4 Aggregating data in record types *)
230+
231+
Record point : Type := Point { x : nat; y : nat; z : nat }.
232+
233+
Compute x (Point 3 0 2).
234+
Compute y (Point 3 0 2).
235+
236+
237+
(* 1.4 The Section mechanism *)
238+
115239
Section iterators.
116-
Variables (T : Type) (A : Type).
117-
Variables (f : T -> A -> A).
118-
Implicit Type x : T.
119-
Fixpoint iter n op x :=
120-
if n is p.+1 then op (iter p op x) else x.
121-
Fixpoint foldr a s :=
122-
if s is x :: xs then f x (foldr a xs) else a.
123-
About foldr.
124-
Variable init : A.
125-
Variables x1 x2 x3 : T.
126-
Eval compute in foldr init [:: x1; x2; x3].
240+
241+
Variables (T : Type) (A : Type).
242+
Variables (f : T -> A -> A).
243+
244+
Implicit Type x : T.
245+
246+
Fixpoint iter n op x :=
247+
if n is p.+1 then op (iter p op x) else x.
248+
249+
Fixpoint foldr a s :=
250+
if s is x :: xs then f x (foldr a xs) else a.
251+
252+
About foldr.
253+
127254
End iterators.
128-
About iter.
255+
129256
About foldr.
130-
Eval compute in iter 5 pred 7.
131-
Eval compute in foldr addn 0 [:: 1; 2; 3].
132-
Fixpoint addn_alt m n := if m is u.+1 then addn_alt u n.+1 else n.
133-
Section symbolic.
257+
258+
Eval compute in iter 5 predn 7.
259+
Eval compute in foldr addn 0 [:: 1; 2 ].
260+
261+
262+
(* 1.5 Symbolic computation *)
263+
264+
Section iterators_symbolic.
265+
266+
Variables (T : Type) (A : Type).
267+
Variables (f : T -> A -> A).
268+
269+
Fixpoint foldr' a s :=
270+
if s is x :: xs then f x (foldr' a xs) else a.
271+
272+
About foldr'.
273+
274+
Variable init : A.
275+
Variables x1 x2 : T.
276+
Compute foldr' init [:: x1; x2].
277+
278+
Compute addn 1 (addn 2 0).
279+
280+
(* already defined *)
281+
(* Fixpoint addn n m := if n is p.+1 then (addn p m).+1 else m. *)
282+
283+
Fixpoint add n m := if n is p.+1 then add p m.+1 else m.
284+
134285
Variable n : nat.
135-
Eval simpl in pred (addn_alt n.+1 7).
136-
Eval simpl in pred (addn n.+1 7).
137-
End symbolic.
286+
Eval simpl in predn (add n.+1 7).
287+
Eval simpl in predn (addn n.+1 7).
288+
289+
End iterators_symbolic.
290+
291+
292+
(* 1.6 Iterators and mathematical notations *)
138293

139-
(* iterated ops *)
140294
Fixpoint iota m n := if n is u.+1 then m :: iota m.+1 u else [::].
141295
Notation "\sum_ ( m <= i < n ) F" :=
142296
(foldr (fun i a => F + a) 0 (iota m (n-m))).
297+
143298
Eval compute in \sum_( 1 <= i < 5 ) (i * 2 - 1).
144299
Eval compute in \sum_( 1 <= i < 5 ) i.
145300

146-
(* aggregated data *)
147-
Record point := Point { x : nat; y : nat; z : nat }.
148-
Eval compute in x (Point 3 0 2).
149-
Eval compute in y (Point 3 0 2).
150301

151-
(** Exercises *************************************** *)
302+
(* 1.7 Notations, abbreviations *)
303+
Notation "m + n" := (addn m n) (at level 50, left associativity).
304+
Notation "m <= n" := (leq m n) (at level 70, no associativity).
305+
Notation "m < n" := (m.+1 <= n).
306+
Notation "n > m" := (m.+1 <= n) (only parsing).
307+
Notation succn := S.
308+
Notation "n .+1" := (succn n) (at level 2, left associativity): nat_scope.
152309

153-
Module exercises.
154-
(* pair *)
310+
Locate "<=".
155311

312+
(* all_words, we will face later in the book *)
313+
314+
Definition all_words n T (alphabet : seq T) :=
315+
let prepend x wl := [seq x :: w | w <- wl] in
316+
let extend wl := flatten [seq prepend x wl | x <- alphabet] in
317+
iter n extend [::[::]].
318+
319+
Eval compute in all_words 2 [:: 1; 2; 3].
156320

157321

322+
(* Code below is not anymore in the book and given here for *)
323+
(* additional examples. *)
324+
325+
(** Exercises *************************************** *)
326+
327+
Module exercises.
328+
(* pair *)
158329

159330
Eval compute in fst (4,5).
160331

@@ -188,16 +359,8 @@ <h1>Chapter 1</h1><div><textarea id='coq-code'>
188359

189360
Eval compute in
190361
flatten [:: [:: 1; 2; 3]; [:: 4; 5] ].
191-
192-
(* all_words *)
193-
Definition all_words n T (alphabet : seq T) :=
194-
let prepend x wl := [seq x :: w | w <- wl] in
195-
let extend wl := flatten [seq prepend x wl | x <- alphabet] in
196-
iter n extend [::[::]].
197-
198-
Eval compute in all_words 2 [:: 1; 2; 3].
199-
200362
End exercises.
363+
201364
</textarea></div>
202365
<script type='text/javascript'>
203366
var coqdoc_ids = ['coq'];

0 commit comments

Comments
 (0)