@@ -20,141 +20,312 @@ <h1>Chapter 1</h1><div><textarea id='coq-code'>
2020Set Implicit Arguments.
2121Unset 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+
2530About f.
2631Print f.
2732Locate "_ ^~ _".
2833Check f 3.
2934Fail Check f (fun x : nat => x).
3035Eval 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+
3258Definition repeat_twice (g : nat -> nat) : nat -> nat :=
3359 fun x => g (g x).
3460Eval compute in repeat_twice f 2.
3561Check (repeat_twice f).
3662Fail 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+
3979Check 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
4593Check 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+
4699Locate ".+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+
50106Definition 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+
56113Fail Definition wrong (n : nat) :=
57114 match n with 0 => true end.
115+
58116Definition same_bool b1 b2 :=
59117 match b1, b2 with
60118 | true, true => true
119+ | false, false => true
61120 | _, _ => false
62121 end.
122+
63123Definition 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+
70132Fixpoint addn n m :=
71133 if n is p.+1 then (addn p m).+1 else m.
134+
72135Fixpoint addn2 n m :=
73136 match n with
74137 | 0 => m
75138 | p.+1 => (addn2 p m).+1
76139 end.
140+
77141Fail Fixpoint loop n :=
78142 if n is 0 then loop n else 0.
143+
79144Fixpoint subn m n : nat :=
80145 match m, n with
81146 | p.+1, q.+1 => subn p q
82147 | _ , _ => m
83148 end.
149+
84150Fixpoint 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+
90157Notation "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+
93188About cons.
94189Check cons 2 nil.
95190Check 1 :: 2 :: 3 :: nil.
96191Check 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
100198Fixpoint size A (s : seq A) :=
101199 if s is _ :: tl then (size tl).+1 else 0.
200+
102201Fixpoint 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+
104206Eval 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
108211Definition only_odd (n : nat) : option nat : =
109212 if odd n then Some n else None.
110213
111214Definition 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+
115239Section 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+
127254End iterators.
128- About iter.
255+
129256About 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 *)
140294Fixpoint iota m n := if n is u.+1 then m :: iota m.+1 u else [::].
141295Notation "\sum_ ( m < = i < n ) F " :=
142296 (foldr (fun i a => F + a) 0 (iota m (n-m))).
297+
143298Eval compute in \sum_( 1 < = i < 5 ) (i * 2 - 1).
144299Eval 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
159330Eval compute in fst (4,5).
160331
@@ -188,16 +359,8 @@ <h1>Chapter 1</h1><div><textarea id='coq-code'>
188359
189360Eval 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-
200362End exercises.
363+
201364</ textarea > </ div >
202365< script type ='text/javascript '>
203366var coqdoc_ids = [ 'coq' ] ;
0 commit comments