Debian Codegraph @

test-suite/prerequisite/ssr_mini_mathcomp.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Some code from mathcomp needed in order to run ssr_* tests *)
12
13 Require Import ssreflect ssrfun ssrbool.
14
15 Global Set SsrOldRewriteGoalsOrder.
16 Global Set Asymmetric Patterns.
17 Global Set Bullet Behavior "None".
18
19 Set Implicit Arguments.
20 Unset Strict Implicit.
21 Unset Printing Implicit Defensive.
22
23 (* eqtype ---------------------------------------------------------- *)
24
25 Module Equality.
26
27 Definition axiom T (e : rel T) := forall x y, reflect (x = y) (e x y).
28
29 Structure mixin_of T := Mixin {op : rel T; _ : axiom op}.
30 Notation class_of := mixin_of (only parsing).
31
32 Section ClassDef.
33
34 Structure type := Pack {sort; _ : class_of sort; _ : Type}.
35 Local Coercion sort : type >-> Sortclass.
36 Variables (T : Type) (cT : type).
37
38 Definition class := let: Pack _ c _ := cT return class_of cT in c.
39
40 Definition pack c := @Pack T c T.
41 Definition clone := fun c & cT -> T & phant_id (pack c) cT => pack c.
42
43 End ClassDef.
44
45 Module Exports.
46 Coercion sort : type >-> Sortclass.
47 Notation eqType := type.
48 Notation EqMixin := Mixin.
49 Notation EqType T m := (@pack T m).
50 Notation "[ 'eqMixin' 'of' T ]" := (class _ : mixin_of T)
51 (at level 0, format "[ 'eqMixin' 'of' T ]") : form_scope.
52 Notation "[ 'eqType' 'of' T 'for' C ]" := (@clone T C _ idfun id)
53 (at level 0, format "[ 'eqType' 'of' T 'for' C ]") : form_scope.
54 Notation "[ 'eqType' 'of' T ]" := (@clone T _ _ id id)
55 (at level 0, format "[ 'eqType' 'of' T ]") : form_scope.
56 End Exports.
57
58 End Equality.
59 Export Equality.Exports.
60
61 Definition eq_op T := Equality.op (Equality.class T).
62
63 Lemma eqE T x : eq_op x = Equality.op (Equality.class T) x.
64 Proof. by []. Qed.
65
66 Lemma eqP T : Equality.axiom (@eq_op T).
67 Proof. by case: T => ? []. Qed.
68 Arguments eqP {T x y}.
69
70 Declare Scope eq_scope.
71 Delimit Scope eq_scope with EQ.
72 Open Scope eq_scope.
73
74 Notation "x == y" := (eq_op x y)
75 (at level 70, no associativity) : bool_scope.
76 Notation "x == y :> T" := ((x : T) == (y : T))
77 (at level 70, y at next level) : bool_scope.
78 Notation "x != y" := (~~ (x == y))
79 (at level 70, no associativity) : bool_scope.
80 Notation "x != y :> T" := (~~ (x == y :> T))
81 (at level 70, y at next level) : bool_scope.
82 Notation "x =P y" := (eqP : reflect (x = y) (x == y))
83 (at level 70, no associativity) : eq_scope.
84 Notation "x =P y :> T" := (eqP : reflect (x = y :> T) (x == y :> T))
85 (at level 70, y at next level, no associativity) : eq_scope.
86
87 Prenex Implicits eq_op eqP.
88
89 Lemma eq_refl (T : eqType) (x : T) : x == x. Proof. exact/eqP. Qed.
90 Notation eqxx := eq_refl.
91
92 Lemma eq_sym (T : eqType) (x y : T) : (x == y) = (y == x).
93 Proof. exact/eqP/eqP. Qed.
94
95 #[global] Hint Resolve eq_refl eq_sym : core.
96
97
98 Definition eqb b := addb (~~ b).
99
100 Lemma eqbP : Equality.axiom eqb.
101 Proof. by do 2!case; constructor. Qed.
102
103 Canonical bool_eqMixin := EqMixin eqbP.
104 Canonical bool_eqType := Eval hnf in EqType bool bool_eqMixin.
105
106 Section ProdEqType.
107
108 Variable T1 T2 : eqType.
109
110 Definition pair_eq := [rel u v : T1 * T2 | (u.1 == v.1) && (u.2 == v.2)].
111
112 Lemma pair_eqP : Equality.axiom pair_eq.
113 Proof.
114 move=> [x1 x2] [y1 y2] /=; apply: (iffP andP) => [[]|[<- <-]] //=.
115 by do 2!move/eqP->.
116 Qed.
117
118 Definition prod_eqMixin := EqMixin pair_eqP.
119 Canonical prod_eqType := Eval hnf in EqType (T1 * T2) prod_eqMixin.
120
121 End ProdEqType.
122
123 Section OptionEqType.
124
125 Variable T : eqType.
126
127 Definition opt_eq (u v : option T) : bool :=
128 oapp (fun x => oapp (eq_op x) false v) (~~ v) u.
129
130 Lemma opt_eqP : Equality.axiom opt_eq.
131 Proof.
132 case=> [x|] [y|] /=; by [constructor | apply: (iffP eqP) => [|[]] ->].
133 Qed.
134
135 Canonical option_eqMixin := EqMixin opt_eqP.
136 Canonical option_eqType := Eval hnf in EqType (option T) option_eqMixin.
137
138 End OptionEqType.
139
140 Notation xpred1 := (fun a1 x => x == a1).
141 Notation xpredU1 := (fun a1 (p : pred _) x => (x == a1) || p x).
142
143 Section EqPred.
144
145 Variable T : eqType.
146
147 Definition pred1 (a1 : T) := SimplPred (xpred1 a1).
148 Definition predU1 (a1 : T) p := SimplPred (xpredU1 a1 p).
149
150 End EqPred.
151
152 Section TransferEqType.
153
154 Variables (T : Type) (eT : eqType) (f : T -> eT).
155
156 Lemma inj_eqAxiom : injective f -> Equality.axiom (fun x y => f x == f y).
157 Proof. by move=> f_inj x y; apply: (iffP eqP) => [|-> //]; apply: f_inj. Qed.
158
159 Definition InjEqMixin f_inj := EqMixin (inj_eqAxiom f_inj).
160
161 Definition PcanEqMixin g (fK : pcancel f g) := InjEqMixin (pcan_inj fK).
162
163 Definition CanEqMixin g (fK : cancel f g) := InjEqMixin (can_inj fK).
164
165 End TransferEqType.
166
167 (* We use the module system to circumvent a silly limitation that *)
168 (* forbids using the same constant to coerce to different targets. *)
169 Module Type EqTypePredSig.
170 Parameter sort : eqType -> predArgType.
171 End EqTypePredSig.
172 Module MakeEqTypePred (eqmod : EqTypePredSig).
173 Coercion eqmod.sort : eqType >-> predArgType.
174 End MakeEqTypePred.
175 Module Export EqTypePred := MakeEqTypePred Equality.
176
177
178 Section SubType.
179
180 Variables (T : Type) (P : pred T).
181
182 Structure subType : Type := SubType {
183 sub_sort :> Type;
184 val : sub_sort -> T;
185 Sub : forall x, P x -> sub_sort;
186 _ : forall K (_ : forall x Px, K (@Sub x Px)) u, K u;
187 _ : forall x Px, val (@Sub x Px) = x
188 }.
189
190 Arguments Sub [s].
191 Lemma vrefl : forall x, P x -> x = x. Proof. by []. Qed.
192 Definition vrefl_rect := vrefl.
193
194 Definition clone_subType U v :=
195 fun sT & sub_sort sT -> U =>
196 fun c Urec cK (sT' := @SubType U v c Urec cK) & phant_id sT' sT => sT'.
197
198 Variable sT : subType.
199
200 Variant Sub_spec : sT -> Type := SubSpec x Px : Sub_spec (Sub x Px).
201
202 Lemma SubP u : Sub_spec u.
203 Proof. by case: sT Sub_spec SubSpec u => T' _ C rec /= _. Qed.
204
205 Lemma SubK x Px : @val sT (Sub x Px) = x.
206 Proof. by case: sT. Qed.
207
208 Definition insub x :=
209 if @idP (P x) is ReflectT Px then @Some sT (Sub x Px) else None.
210
211 Definition insubd u0 x := odflt u0 (insub x).
212
213 Variant insub_spec x : option sT -> Type :=
214 | InsubSome u of P x & val u = x : insub_spec x (Some u)
215 | InsubNone of ~~ P x : insub_spec x None.
216
217 Lemma insubP x : insub_spec x (insub x).
218 Proof.
219 by rewrite /insub; case: {-}_ / idP; [left; rewrite ?SubK | right; apply/negP].
220 Qed.
221
222 Lemma insubT x Px : insub x = Some (Sub x Px).
223 Admitted.
224
225 Lemma insubF x : P x = false -> insub x = None.
226 Proof. by move/idP; case: insubP. Qed.
227
228 Lemma insubN x : ~~ P x -> insub x = None.
229 Proof. by move/negPf/insubF. Qed.
230
231 Lemma isSome_insub : ([eta insub] : pred T) =1 P.
232 Proof. by apply: fsym => x; case: insubP => // /negPf. Qed.
233
234 Lemma insubK : ocancel insub (@val _).
235 Proof. by move=> x; case: insubP. Qed.
236
237 Lemma valP (u : sT) : P (val u).
238 Proof. by case/SubP: u => x Px; rewrite SubK. Qed.
239
240 Lemma valK : pcancel (@val _) insub.
241 Proof. by case/SubP=> x Px; rewrite SubK; apply: insubT. Qed.
242
243 Lemma val_inj : injective (@val sT).
244 Proof. exact: pcan_inj valK. Qed.
245
246 Lemma valKd u0 : cancel (@val _) (insubd u0).
247 Proof. by move=> u; rewrite /insubd valK. Qed.
248
249 Lemma val_insubd u0 x : val (insubd u0 x) = if P x then x else val u0.
250 Proof. by rewrite /insubd; case: insubP => [u -> | /negPf->]. Qed.
251
252 Lemma insubdK u0 : {in P, cancel (insubd u0) (@val _)}.
253 Proof. by move=> x Px; rewrite /= val_insubd [P x]Px. Qed.
254
255 Definition insub_eq x :=
256 let Some_sub Px := Some (Sub x Px : sT) in
257 let None_sub _ := None in
258 (if P x as Px return P x = Px -> _ then Some_sub else None_sub) (erefl _).
259
260 Lemma insub_eqE : insub_eq =1 insub.
261 Proof.
262 rewrite /insub_eq /insub => x; case: {2 3}_ / idP (erefl _) => // Px Px'.
263 by congr (Some _); apply: val_inj; rewrite !SubK.
264 Qed.
265
266 End SubType.
267
268 Arguments SubType [T P].
269 Arguments Sub [T P s].
270 Arguments vrefl [T P].
271 Arguments vrefl_rect [T P].
272 Arguments clone_subType [T P] U v [sT] _ [c Urec cK].
273 Arguments insub [T P sT].
274 Arguments insubT [T] P [sT x].
275 Arguments val_inj [T P sT].
276 Prenex Implicits val Sub vrefl vrefl_rect insub insubd val_inj.
277
278 Local Notation inlined_sub_rect :=
279 (fun K K_S u => let (x, Px) as u return K u := u in K_S x Px).
280
281 Local Notation inlined_new_rect :=
282 (fun K K_S u => let (x) as u return K u := u in K_S x).
283
284 Notation "[ 'subType' 'for' v ]" := (SubType _ v _ inlined_sub_rect vrefl_rect)
285 (at level 0, only parsing) : form_scope.
286
287 Notation "[ 'sub' 'Type' 'for' v ]" := (SubType _ v _ _ vrefl_rect)
288 (at level 0, format "[ 'sub' 'Type' 'for' v ]") : form_scope.
289
290 Notation "[ 'subType' 'for' v 'by' rec ]" := (SubType _ v _ rec vrefl)
291 (at level 0, format "[ 'subType' 'for' v 'by' rec ]") : form_scope.
292
293 Notation "[ 'subType' 'of' U 'for' v ]" := (clone_subType U v id idfun)
294 (at level 0, format "[ 'subType' 'of' U 'for' v ]") : form_scope.
295
296 (*
297 Notation "[ 'subType' 'for' v ]" := (clone_subType _ v id idfun)
298 (at level 0, format "[ 'subType' 'for' v ]") : form_scope.
299 *)
300 Notation "[ 'subType' 'of' U ]" := (clone_subType U _ id id)
301 (at level 0, format "[ 'subType' 'of' U ]") : form_scope.
302
303 Definition NewType T U v c Urec :=
304 let Urec' P IH := Urec P (fun x : T => IH x isT : P _) in
305 SubType U v (fun x _ => c x) Urec'.
306 Arguments NewType [T U].
307
308 Notation "[ 'newType' 'for' v ]" := (NewType v _ inlined_new_rect vrefl_rect)
309 (at level 0, only parsing) : form_scope.
310
311 Notation "[ 'new' 'Type' 'for' v ]" := (NewType v _ _ vrefl_rect)
312 (at level 0, format "[ 'new' 'Type' 'for' v ]") : form_scope.
313
314 Notation "[ 'newType' 'for' v 'by' rec ]" := (NewType v _ rec vrefl)
315 (at level 0, format "[ 'newType' 'for' v 'by' rec ]") : form_scope.
316
317 Definition innew T nT x := @Sub T predT nT x (erefl true).
318 Arguments innew [T nT].
319 Prenex Implicits innew.
320
321 Lemma innew_val T nT : cancel val (@innew T nT).
322 Proof. by move=> u; apply: val_inj; apply: SubK. Qed.
323
324 (* Prenex Implicits and renaming. *)
325 Notation sval := (@proj1_sig _ _).
326 Notation "@ 'sval'" := (@proj1_sig) (at level 10, format "@ 'sval'").
327
328 Section SubEqType.
329
330 Variables (T : eqType) (P : pred T) (sT : subType P).
331
332 Local Notation ev_ax := (fun T v => @Equality.axiom T (fun x y => v x == v y)).
333 Lemma val_eqP : ev_ax sT val. Proof. exact: inj_eqAxiom val_inj. Qed.
334
335 Definition sub_eqMixin := EqMixin val_eqP.
336 Canonical sub_eqType := Eval hnf in EqType sT sub_eqMixin.
337
338 Definition SubEqMixin :=
339 (let: SubType _ v _ _ _ as sT' := sT
340 return ev_ax sT' val -> Equality.class_of sT' in
341 fun vP : ev_ax _ v => EqMixin vP
342 ) val_eqP.
343
344 Lemma val_eqE (u v : sT) : (val u == val v) = (u == v).
345 Proof. by []. Qed.
346
347 End SubEqType.
348
349 Arguments val_eqP {T P sT x y}.
350 Prenex Implicits val_eqP.
351
352 Notation "[ 'eqMixin' 'of' T 'by' <: ]" := (SubEqMixin _ : Equality.class_of T)
353 (at level 0, format "[ 'eqMixin' 'of' T 'by' <: ]") : form_scope.
354
355 (* ssrnat ---------------------------------------------------------- *)
356
357 Notation succn := Datatypes.S.
358 Notation predn := Peano.pred.
359
360 Notation "n .+1" := (succn n) (at level 2, left associativity,
361 format "n .+1") : nat_scope.
362 Notation "n .+2" := n.+1.+1 (at level 2, left associativity,
363 format "n .+2") : nat_scope.
364 Notation "n .+3" := n.+2.+1 (at level 2, left associativity,
365 format "n .+3") : nat_scope.
366 Notation "n .+4" := n.+2.+2 (at level 2, left associativity,
367 format "n .+4") : nat_scope.
368
369 Notation "n .-1" := (predn n) (at level 2, left associativity,
370 format "n .-1") : nat_scope.
371 Notation "n .-2" := n.-1.-1 (at level 2, left associativity,
372 format "n .-2") : nat_scope.
373
374 Fixpoint eqn m n {struct m} :=
375 match m, n with
376 | 0, 0 => true
377 | m'.+1, n'.+1 => eqn m' n'
378 | _, _ => false
379 end.
380
381 Lemma eqnP : Equality.axiom eqn.
382 Proof.
383 move=> n m; apply: (iffP idP) => [|<-]; last by elim n.
384 by elim: n m => [|n IHn] [|m] //= /IHn->.
385 Qed.
386
387 Canonical nat_eqMixin := EqMixin eqnP.
388 Canonical nat_eqType := Eval hnf in EqType nat nat_eqMixin.
389
390 Arguments eqnP {x y}.
391 Prenex Implicits eqnP.
392
393 Coercion nat_of_bool (b : bool) := if b then 1 else 0.
394
395 Fixpoint odd n := if n is n'.+1 then ~~ odd n' else false.
396
397 Lemma oddb (b : bool) : odd b = b. Proof. by case: b. Qed.
398
399 Declare Scope nat_rec_scope.
400
401 Set Warnings "-notation-overridden".
402
403 Definition subn_rec := minus.
404 Notation "m - n" := (subn_rec m n) : nat_rec_scope.
405
406 Definition subn := nosimpl subn_rec.
407 Notation "m - n" := (subn m n) : nat_scope.
408
409 Definition leq m n := m - n == 0.
410
411 Notation "m <= n" := (leq m n) : nat_scope.
412 Notation "m < n" := (m.+1 <= n) : nat_scope.
413 Notation "m >= n" := (n <= m) (only parsing) : nat_scope.
414 Notation "m > n" := (n < m) (only parsing) : nat_scope.
415
416
417 Notation "m <= n <= p" := ((m <= n) && (n <= p)) : nat_scope.
418 Notation "m < n <= p" := ((m < n) && (n <= p)) : nat_scope.
419 Notation "m <= n < p" := ((m <= n) && (n < p)) : nat_scope.
420 Notation "m < n < p" := ((m < n) && (n < p)) : nat_scope.
421
422 Open Scope nat_scope.
423
424
425 Lemma ltnS m n : (m < n.+1) = (m <= n). Proof. by []. Qed.
426 Lemma leq0n n : 0 <= n. Proof. by []. Qed.
427 Lemma ltn0Sn n : 0 < n.+1. Proof. by []. Qed.
428 Lemma ltn0 n : (n < 0) = false. Proof. by []. Qed.
429 Lemma leqnn n : n <= n. Proof. by elim: n. Qed.
430 #[global] Hint Resolve leqnn : core.
431 Lemma leqnSn n : n <= n.+1. Proof. by elim: n. Qed.
432
433 Lemma leq_trans n m p : m <= n -> n <= p -> m <= p.
434 Admitted.
435 Lemma leq_ltn_trans n m p : m <= n -> n < p -> m < p.
436 Admitted.
437 Lemma leqW m n : m <= n -> m <= n.+1.
438 Admitted.
439 #[global] Hint Resolve leqnSn : core.
440 Lemma ltnW m n : m < n -> m <= n.
441 Proof. exact: leq_trans. Qed.
442 #[global] Hint Resolve ltnW : core.
443
444 Definition addn_rec := plus.
445 Notation "m + n" := (addn_rec m n) : nat_rec_scope.
446
447 Definition addn := nosimpl addn_rec.
448 Notation "m + n" := (addn m n) : nat_scope.
449
450 Lemma addn0 : right_id 0 addn. Proof. by move=> n; apply/eqP; elim: n. Qed.
451 Lemma add0n : left_id 0 addn. Proof. by []. Qed.
452 Lemma addSn m n : m.+1 + n = (m + n).+1. Proof. by []. Qed.
453 Lemma addnS m n : m + n.+1 = (m + n).+1. Proof. by elim: m. Qed.
454
455 Lemma addnCA : left_commutative addn.
456 Proof. by move=> m n p; elim: m => //= m; rewrite addnS => <-. Qed.
457
458 Lemma addnC : commutative addn.
459 Proof. by move=> m n; rewrite -{1}[n]addn0 addnCA addn0. Qed.
460
461 Lemma addnA : associative addn.
462 Proof. by move=> m n p; rewrite (addnC n) addnCA addnC. Qed.
463
464 Lemma subnK m n : m <= n -> (n - m) + m = n.
465 Admitted.
466
467
468 Definition muln_rec := mult.
469 Notation "m * n" := (muln_rec m n) : nat_rec_scope.
470
471 Definition muln := nosimpl muln_rec.
472 Notation "m * n" := (muln m n) : nat_scope.
473
474 Lemma mul0n : left_zero 0 muln. Proof. by []. Qed.
475 Lemma muln0 : right_zero 0 muln. Proof. by elim. Qed.
476 Lemma mul1n : left_id 1 muln. Proof. exact: addn0. Qed.
477
478 Lemma mulSn m n : m.+1 * n = n + m * n. Proof. by []. Qed.
479 Lemma mulSnr m n : m.+1 * n = m * n + n. Proof. exact: addnC. Qed.
480
481 Lemma mulnS m n : m * n.+1 = m + m * n.
482 Proof. by elim: m => // m; rewrite !mulSn !addSn addnCA => ->. Qed.
483
484 Lemma mulnSr m n : m * n.+1 = m * n + m.
485 Proof. by rewrite addnC mulnS. Qed.
486
487 Lemma muln1 : right_id 1 muln.
488 Proof. by move=> n; rewrite mulnSr muln0. Qed.
489
490 Lemma mulnC : commutative muln.
491 Proof.
492 by move=> m n; elim: m => [|m]; rewrite (muln0, mulnS) // mulSn => ->.
493 Qed.
494
495 Lemma mulnDl : left_distributive muln addn.
496 Proof. by move=> m1 m2 n; elim: m1 => //= m1 IHm; rewrite -addnA -IHm. Qed.
497
498 Lemma mulnDr : right_distributive muln addn.
499 Proof. by move=> m n1 n2; rewrite !(mulnC m) mulnDl. Qed.
500
501 Lemma mulnA : associative muln.
502 Proof. by move=> m n p; elim: m => //= m; rewrite mulSn mulnDl => ->. Qed.
503
504 Lemma mulnCA : left_commutative muln.
505 Proof. by move=> m n1 n2; rewrite !mulnA (mulnC m). Qed.
506
507 Lemma mulnAC : right_commutative muln.
508 Proof. by move=> m n p; rewrite -!mulnA (mulnC n). Qed.
509
510 Lemma mulnACA : interchange muln muln.
511 Proof. by move=> m n p q; rewrite -!mulnA (mulnCA n). Qed.
512
513 (* seq ------------------------------------------------------------- *)
514
515 Declare Scope seq_scope.
516 Delimit Scope seq_scope with SEQ.
517 Open Scope seq_scope.
518
519 (* Inductive seq (T : Type) : Type := Nil | Cons of T & seq T. *)
520 Notation seq := list.
521 Prenex Implicits cons.
522 Notation Cons T := (@cons T) (only parsing).
523 Notation Nil T := (@nil T) (only parsing).
524
525 Bind Scope seq_scope with list.
526 Arguments cons _%_type _ _%_SEQ.
527
528 (* As :: and ++ are (improperly) declared in Init.datatypes, we only rebind *)
529 (* them here. *)
530 Infix "::" := cons : seq_scope.
531
532 (* GG - this triggers a camlp4 warning, as if this Notation had been Reserved *)
533 Notation "[ :: ]" := nil (at level 0, format "[ :: ]") : seq_scope.
534
535 Notation "[ :: x1 ]" := (x1 :: [::])
536 (at level 0, format "[ :: x1 ]") : seq_scope.
537
538 Notation "[ :: x & s ]" := (x :: s) (at level 0, only parsing) : seq_scope.
539
540 Notation "[ :: x1 , x2 , .. , xn & s ]" := (x1 :: x2 :: .. (xn :: s) ..)
541 (at level 0, format
542 "'[hv' [ :: '[' x1 , '/' x2 , '/' .. , '/' xn ']' '/ ' & s ] ']'"
543 ) : seq_scope.
544
545 Notation "[ :: x1 ; x2 ; .. ; xn ]" := (x1 :: x2 :: .. [:: xn] ..)
546 (at level 0, format "[ :: '[' x1 ; '/' x2 ; '/' .. ; '/' xn ']' ]"
547 ) : seq_scope.
548
549 Section Sequences.
550
551 Variable n0 : nat. (* numerical parameter for take, drop et al *)
552 Variable T : Type. (* must come before the implicit Type *)
553 Variable x0 : T. (* default for head/nth *)
554
555 Implicit Types x y z : T.
556 Implicit Types m n : nat.
557 Implicit Type s : seq T.
558
559 Fixpoint size s := if s is _ :: s' then (size s').+1 else 0.
560
561 Fixpoint cat s1 s2 := if s1 is x :: s1' then x :: s1' ++ s2 else s2
562 where "s1 ++ s2" := (cat s1 s2) : seq_scope.
563
564 Lemma cat0s s : [::] ++ s = s. Proof. by []. Qed.
565
566 Lemma cats0 s : s ++ [::] = s.
567 Proof. by elim: s => //= x s ->. Qed.
568
569 Lemma catA s1 s2 s3 : s1 ++ s2 ++ s3 = (s1 ++ s2) ++ s3.
570 Proof. by elim: s1 => //= x s1 ->. Qed.
571
572 Fixpoint nth s n {struct n} :=
573 if s is x :: s' then if n is n'.+1 then @nth s' n' else x else x0.
574
575 Fixpoint rcons s z := if s is x :: s' then x :: rcons s' z else [:: z].
576
577 Variant last_spec : seq T -> Type :=
578 | LastNil : last_spec [::]
579 | LastRcons s x : last_spec (rcons s x).
580
581 Lemma lastP s : last_spec s.
582 Proof using. Admitted.
583
584 Lemma last_ind P :
585 P [::] -> (forall s x, P s -> P (rcons s x)) -> forall s, P s.
586 Proof using. Admitted.
587
588
589 Section Map.
590
591 Variables (T2 : Type) (f : T -> T2).
592
593 Fixpoint map s := if s is x :: s' then f x :: map s' else [::].
594
595 End Map.
596
597 Section SeqFind.
598
599 Variable a : pred T.
600
601 Fixpoint count s := if s is x :: s' then a x + count s' else 0.
602
603 Fixpoint filter s :=
604 if s is x :: s' then if a x then x :: filter s' else filter s' else [::].
605
606 End SeqFind.
607
608 End Sequences.
609
610 Infix "++" := cat : seq_scope.
611
612 Notation count_mem x := (count (pred_of_simpl (pred1 x))).
613
614 Section EqSeq.
615
616 Variables (n0 : nat) (T : eqType) (x0 : T).
617 Local Notation nth := (nth x0).
618 Implicit Type s : seq T.
619 Implicit Types x y z : T.
620
621 Fixpoint eqseq s1 s2 {struct s2} :=
622 match s1, s2 with
623 | [::], [::] => true
624 | x1 :: s1', x2 :: s2' => (x1 == x2) && eqseq s1' s2'
625 | _, _ => false
626 end.
627
628 Lemma eqseqP : Equality.axiom eqseq.
629 Proof.
630 move; elim=> [|x1 s1 IHs] [|x2 s2]; do [by constructor | simpl].
631 case: (x1 =P x2) => [<-|neqx]; last by right; case.
632 by apply: (iffP (IHs s2)) => [<-|[]].
633 Qed.
634
635 Canonical seq_eqMixin := EqMixin eqseqP.
636 Canonical seq_eqType := Eval hnf in EqType (seq T) seq_eqMixin.
637
638 Fixpoint mem_seq (s : seq T) :=
639 if s is y :: s' then xpredU1 y (mem_seq s') else xpred0.
640
641 Definition eqseq_class := seq T.
642 Identity Coercion seq_of_eqseq : eqseq_class >-> seq.
643 Coercion pred_of_eq_seq (s : eqseq_class) : {pred T} := [eta mem_seq s].
644
645 Canonical seq_predType := @PredType T (seq T) pred_of_eq_seq.
646
647 Fixpoint uniq s := if s is x :: s' then (x \notin s') && uniq s' else true.
648
649 End EqSeq.
650
651 Definition bitseq := seq bool.
652 Canonical bitseq_eqType := Eval hnf in [eqType of bitseq].
653 Canonical bitseq_predType := Eval hnf in [predType of bitseq].
654
655 Section Pmap.
656
657 Variables (aT rT : Type) (f : aT -> option rT) (g : rT -> aT).
658
659 Fixpoint pmap s :=
660 if s is x :: s' then let r := pmap s' in oapp (cons^~ r) r (f x) else [::].
661
662 End Pmap.
663
664 Fixpoint iota m n := if n is n'.+1 then m :: iota m.+1 n' else [::].
665
666 Section FoldRight.
667
668 Variables (T : Type) (R : Type) (f : T -> R -> R) (z0 : R).
669
670 Fixpoint foldr s := if s is x :: s' then f x (foldr s') else z0.
671
672 End FoldRight.
673
674 Lemma mem_iota m n i : (i \in iota m n) = (m <= i) && (i < m + n).
675 Admitted.
676
677
678 (* choice ------------------------------------------------------------- *)
679
680 Module Choice.
681
682 Section ClassDef.
683
684 Record mixin_of T := Mixin {
685 find : pred T -> nat -> option T;
686 _ : forall P n x, find P n = Some x -> P x;
687 _ : forall P : pred T, (exists x, P x) -> exists n, find P n;
688 _ : forall P Q : pred T, P =1 Q -> find P =1 find Q
689 }.
690
691 Record class_of T := Class {base : Equality.class_of T; mixin : mixin_of T}.
692 Local Coercion base : class_of >-> Equality.class_of.
693
694 Structure type := Pack {sort; _ : class_of sort; _ : Type}.
695 Local Coercion sort : type >-> Sortclass.
696 Variables (T : Type) (cT : type).
697 Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
698 Definition clone c of phant_id class c := @Pack T c T.
699 Let xT := let: Pack T _ _ := cT in T.
700 Notation xclass := (class : class_of xT).
701
702 Definition pack m :=
703 fun b bT & phant_id (Equality.class bT) b => Pack (@Class T b m) T.
704
705 (* Inheritance *)
706 Definition eqType := @Equality.Pack cT xclass xT.
707
708 End ClassDef.
709
710 Module Import Exports.
711 Coercion base : class_of >-> Equality.class_of.
712 Coercion sort : type >-> Sortclass.
713 Coercion eqType : type >-> Equality.type.
714 Canonical eqType.
715 Notation choiceType := type.
716 Notation choiceMixin := mixin_of.
717 Notation ChoiceType T m := (@pack T m _ _ id).
718 Notation "[ 'choiceType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
719 (at level 0, format "[ 'choiceType' 'of' T 'for' cT ]") : form_scope.
720 Notation "[ 'choiceType' 'of' T ]" := (@clone T _ _ id)
721 (at level 0, format "[ 'choiceType' 'of' T ]") : form_scope.
722
723 End Exports.
724
725 End Choice.
726 Export Choice.Exports.
727
728 Section ChoiceTheory.
729
730 Variable T : choiceType.
731
732 Section CanChoice.
733
734 Variables (sT : Type) (f : sT -> T).
735
736 Lemma PcanChoiceMixin f' : pcancel f f' -> choiceMixin sT.
737 Admitted.
738
739 Definition CanChoiceMixin f' (fK : cancel f f') :=
740 PcanChoiceMixin (can_pcan fK).
741
742 End CanChoice.
743
744 Section SubChoice.
745
746 Variables (P : pred T) (sT : subType P).
747
748 Definition sub_choiceMixin := PcanChoiceMixin (@valK T P sT).
749 Definition sub_choiceClass := @Choice.Class sT (sub_eqMixin sT) sub_choiceMixin.
750 Canonical sub_choiceType := Choice.Pack sub_choiceClass sT.
751
752 End SubChoice.
753
754
755 Fact seq_choiceMixin : choiceMixin (seq T).
756 Admitted.
757 Canonical seq_choiceType := Eval hnf in ChoiceType (seq T) seq_choiceMixin.
758 End ChoiceTheory.
759
760 Fact nat_choiceMixin : choiceMixin nat.
761 Proof.
762 pose f := [fun (P : pred nat) n => if P n then Some n else None].
763 exists f => [P n m | P [n Pn] | P Q eqPQ n] /=; last by rewrite eqPQ.
764 by case: ifP => // Pn [<-].
765 by exists n; rewrite Pn.
766 Qed.
767 Canonical nat_choiceType := Eval hnf in ChoiceType nat nat_choiceMixin.
768
769 Definition bool_choiceMixin := CanChoiceMixin oddb.
770 Canonical bool_choiceType := Eval hnf in ChoiceType bool bool_choiceMixin.
771 Canonical bitseq_choiceType := Eval hnf in [choiceType of bitseq].
772
773
774 Notation "[ 'choiceMixin' 'of' T 'by' <: ]" :=
775 (sub_choiceMixin _ : choiceMixin T)
776 (at level 0, format "[ 'choiceMixin' 'of' T 'by' <: ]") : form_scope.
777
778
779
780
781 Module Countable.
782
783 Record mixin_of (T : Type) : Type := Mixin {
784 pickle : T -> nat;
785 unpickle : nat -> option T;
786 pickleK : pcancel pickle unpickle
787 }.
788
789 Definition EqMixin T m := PcanEqMixin (@pickleK T m).
790 Definition ChoiceMixin T m := PcanChoiceMixin (@pickleK T m).
791
792 Section ClassDef.
793
794 Record class_of T := Class { base : Choice.class_of T; mixin : mixin_of T }.
795 Local Coercion base : class_of >-> Choice.class_of.
796
797 Structure type : Type := Pack {sort : Type; _ : class_of sort; _ : Type}.
798 Local Coercion sort : type >-> Sortclass.
799 Variables (T : Type) (cT : type).
800 Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
801 Definition clone c of phant_id class c := @Pack T c T.
802 Let xT := let: Pack T _ _ := cT in T.
803 Notation xclass := (class : class_of xT).
804
805 Definition pack m :=
806 fun bT b & phant_id (Choice.class bT) b => Pack (@Class T b m) T.
807
808 Definition eqType := @Equality.Pack cT xclass xT.
809 Definition choiceType := @Choice.Pack cT xclass xT.
810
811 End ClassDef.
812
813 Module Exports.
814 Coercion base : class_of >-> Choice.class_of.
815 Coercion mixin : class_of >-> mixin_of.
816 Coercion sort : type >-> Sortclass.
817 Coercion eqType : type >-> Equality.type.
818 Canonical eqType.
819 Coercion choiceType : type >-> Choice.type.
820 Canonical choiceType.
821 Notation countType := type.
822 Notation CountType T m := (@pack T m _ _ id).
823 Notation CountMixin := Mixin.
824 Notation CountChoiceMixin := ChoiceMixin.
825 Notation "[ 'countType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
826 (at level 0, format "[ 'countType' 'of' T 'for' cT ]") : form_scope.
827 Notation "[ 'countType' 'of' T ]" := (@clone T _ _ id)
828 (at level 0, format "[ 'countType' 'of' T ]") : form_scope.
829
830 End Exports.
831
832 End Countable.
833 Export Countable.Exports.
834
835 Definition unpickle T := Countable.unpickle (Countable.class T).
836 Definition pickle T := Countable.pickle (Countable.class T).
837 Arguments unpickle [T].
838 Prenex Implicits pickle unpickle.
839
840 Section CountableTheory.
841
842 Variable T : countType.
843
844 Lemma pickleK : @pcancel nat T pickle unpickle.
845 Proof. exact: Countable.pickleK. Qed.
846
847 Definition pickle_inv n :=
848 obind (fun x : T => if pickle x == n then Some x else None) (unpickle n).
849
850 Lemma pickle_invK : ocancel pickle_inv pickle.
851 Proof.
852 by rewrite /pickle_inv => n; case def_x: (unpickle n) => //= [x]; case: eqP.
853 Qed.
854
855 Lemma pickleK_inv : pcancel pickle pickle_inv.
856 Proof. by rewrite /pickle_inv => x; rewrite pickleK /= eqxx. Qed.
857
858 Lemma pcan_pickleK sT f f' :
859 @pcancel T sT f f' -> pcancel (pickle \o f) (pcomp f' unpickle).
860 Proof. by move=> fK x; rewrite /pcomp pickleK /= fK. Qed.
861
862 Definition PcanCountMixin sT f f' (fK : pcancel f f') :=
863 @CountMixin sT _ _ (pcan_pickleK fK).
864
865 Definition CanCountMixin sT f f' (fK : cancel f f') :=
866 @PcanCountMixin sT _ _ (can_pcan fK).
867
868 Definition sub_countMixin P sT := PcanCountMixin (@valK T P sT).
869
870 End CountableTheory.
871 Notation "[ 'countMixin' 'of' T 'by' <: ]" :=
872 (sub_countMixin _ : Countable.mixin_of T)
873 (at level 0, format "[ 'countMixin' 'of' T 'by' <: ]") : form_scope.
874
875 Section SubCountType.
876
877 Variables (T : choiceType) (P : pred T).
878 Import Countable.
879
880 Structure subCountType : Type :=
881 SubCountType {subCount_sort :> subType P; _ : mixin_of subCount_sort}.
882
883 Coercion sub_countType (sT : subCountType) :=
884 Eval hnf in pack (let: SubCountType _ m := sT return mixin_of sT in m) id.
885 Canonical sub_countType.
886
887 Definition pack_subCountType U :=
888 fun sT cT & sub_sort sT * sort cT -> U * U =>
889 fun b m & phant_id (Class b m) (class cT) => @SubCountType sT m.
890
891 End SubCountType.
892
893 (* This assumes that T has both countType and subType structures. *)
894 Notation "[ 'subCountType' 'of' T ]" :=
895 (@pack_subCountType _ _ T _ _ id _ _ id)
896 (at level 0, format "[ 'subCountType' 'of' T ]") : form_scope.
897
898 Lemma nat_pickleK : pcancel id (@Some nat). Proof. by []. Qed.
899 Definition nat_countMixin := CountMixin nat_pickleK.
900 Canonical nat_countType := Eval hnf in CountType nat nat_countMixin.
901
902 (* fintype --------------------------------------------------------- *)
903
904 Module Finite.
905
906 Section RawMixin.
907
908 Variable T : eqType.
909
910 Definition axiom e := forall x : T, count_mem x e = 1.
911
912 Lemma uniq_enumP e : uniq e -> e =i T -> axiom e.
913 Admitted.
914
915 Record mixin_of := Mixin {
916 mixin_base : Countable.mixin_of T;
917 mixin_enum : seq T;
918 _ : axiom mixin_enum
919 }.
920
921 End RawMixin.
922
923 Section Mixins.
924
925 Variable T : countType.
926
927 Definition EnumMixin :=
928 let: Countable.Pack _ (Countable.Class _ m) _ as cT := T
929 return forall e : seq cT, axiom e -> mixin_of cT in
930 @Mixin (EqType _ _) m.
931
932 Definition UniqMixin e Ue eT := @EnumMixin e (uniq_enumP Ue eT).
933
934 Variable n : nat.
935
936 End Mixins.
937
938 Section ClassDef.
939
940 Record class_of T := Class {
941 base : Choice.class_of T;
942 mixin : mixin_of (Equality.Pack base T)
943 }.
944 Definition base2 T c := Countable.Class (@base T c) (mixin_base (mixin c)).
945 Local Coercion base : class_of >-> Choice.class_of.
946
947 Structure type : Type := Pack {sort; _ : class_of sort; _ : Type}.
948 Local Coercion sort : type >-> Sortclass.
949 Variables (T : Type) (cT : type).
950 Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
951 Definition clone c of phant_id class c := @Pack T c T.
952 Let xT := let: Pack T _ _ := cT in T.
953 Notation xclass := (class : class_of xT).
954
955 Definition pack b0 (m0 : mixin_of (EqType T b0)) :=
956 fun bT b & phant_id (Choice.class bT) b =>
957 fun m & phant_id m0 m => Pack (@Class T b m) T.
958
959 Definition eqType := @Equality.Pack cT xclass xT.
960 Definition choiceType := @Choice.Pack cT xclass xT.
961 Definition countType := @Countable.Pack cT (base2 xclass) xT.
962
963 End ClassDef.
964
965 Module Import Exports.
966 Coercion mixin_base : mixin_of >-> Countable.mixin_of.
967 Coercion base : class_of >-> Choice.class_of.
968 Coercion mixin : class_of >-> mixin_of.
969 Coercion base2 : class_of >-> Countable.class_of.
970 Coercion sort : type >-> Sortclass.
971 Coercion eqType : type >-> Equality.type.
972 Canonical eqType.
973 Coercion choiceType : type >-> Choice.type.
974 Canonical choiceType.
975 Coercion countType : type >-> Countable.type.
976 Canonical countType.
977 Notation finType := type.
978 Notation FinType T m := (@pack T _ m _ _ id _ id).
979 Notation FinMixin := EnumMixin.
980 Notation UniqFinMixin := UniqMixin.
981 Notation "[ 'finType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
982 (at level 0, format "[ 'finType' 'of' T 'for' cT ]") : form_scope.
983 Notation "[ 'finType' 'of' T ]" := (@clone T _ _ id)
984 (at level 0, format "[ 'finType' 'of' T ]") : form_scope.
985 End Exports.
986
987 Module Type EnumSig.
988 Parameter enum : forall cT : type, seq cT.
989 Axiom enumDef : enum = fun cT => mixin_enum (class cT).
990 End EnumSig.
991
992 Module EnumDef : EnumSig.
993 Definition enum cT := mixin_enum (class cT).
994 Definition enumDef := erefl enum.
995 End EnumDef.
996
997 Notation enum := EnumDef.enum.
998
999 End Finite.
1000 Export Finite.Exports.
1001
1002 Section SubFinType.
1003
1004 Variables (T : choiceType) (P : pred T).
1005 Import Finite.
1006
1007 Structure subFinType := SubFinType {
1008 subFin_sort :> subType P;
1009 _ : mixin_of (sub_eqType subFin_sort)
1010 }.
1011
1012 Definition pack_subFinType U :=
1013 fun cT b m & phant_id (class cT) (@Class U b m) =>
1014 fun sT m' & phant_id m' m => @SubFinType sT m'.
1015
1016 Implicit Type sT : subFinType.
1017
1018 Definition subFin_mixin sT :=
1019 let: SubFinType _ m := sT return mixin_of (sub_eqType sT) in m.
1020
1021 Coercion subFinType_subCountType sT := @SubCountType _ _ sT (subFin_mixin sT).
1022 Canonical subFinType_subCountType.
1023
1024 Coercion subFinType_finType sT :=
1025 Pack (@Class sT (sub_choiceClass sT) (subFin_mixin sT)) sT.
1026 Canonical subFinType_finType.
1027
1028 Definition enum_mem T (mA : mem_pred _) := filter mA (Finite.enum T).
1029 Definition image_mem T T' f mA : seq T' := map f (@enum_mem T mA).
1030 Definition codom T T' f := @image_mem T T' f (mem T).
1031
1032 Lemma codom_val sT x : (x \in codom (val : sT -> T)) = P x.
1033 Admitted.
1034
1035 End SubFinType.
1036
1037
1038 (* This assumes that T has both finType and subCountType structures. *)
1039 Notation "[ 'subFinType' 'of' T ]" := (@pack_subFinType _ _ T _ _ _ id _ _ id)
1040 (at level 0, format "[ 'subFinType' 'of' T ]") : form_scope.
1041
1042
1043
1044 Section OrdinalSub.
1045
1046 Variable n : nat.
1047
1048 Inductive ordinal : predArgType := Ordinal m of m < n.
1049
1050 Coercion nat_of_ord i := let: Ordinal m _ := i in m.
1051
1052 Canonical ordinal_subType := [subType for nat_of_ord].
1053 Definition ordinal_eqMixin := Eval hnf in [eqMixin of ordinal by <:].
1054 Canonical ordinal_eqType := Eval hnf in EqType ordinal ordinal_eqMixin.
1055 Definition ordinal_choiceMixin := [choiceMixin of ordinal by <:].
1056 Canonical ordinal_choiceType :=
1057 Eval hnf in ChoiceType ordinal ordinal_choiceMixin.
1058 Definition ordinal_countMixin := [countMixin of ordinal by <:].
1059 Canonical ordinal_countType := Eval hnf in CountType ordinal ordinal_countMixin.
1060 Canonical ordinal_subCountType := [subCountType of ordinal].
1061
1062 Lemma ltn_ord (i : ordinal) : i < n. Proof. exact: valP i. Qed.
1063
1064 Lemma ord_inj : injective nat_of_ord. Proof. exact: val_inj. Qed.
1065
1066 Definition ord_enum : seq ordinal := pmap insub (iota 0 n).
1067
1068 Lemma val_ord_enum : map val ord_enum = iota 0 n.
1069 Admitted.
1070
1071 Lemma ord_enum_uniq : uniq ord_enum.
1072 Admitted.
1073
1074 Lemma mem_ord_enum i : i \in ord_enum.
1075 Admitted.
1076
1077 Definition ordinal_finMixin :=
1078 Eval hnf in UniqFinMixin ord_enum_uniq mem_ord_enum.
1079 Canonical ordinal_finType := Eval hnf in FinType ordinal ordinal_finMixin.
1080 Canonical ordinal_subFinType := Eval hnf in [subFinType of ordinal].
1081
1082 End OrdinalSub.
1083
1084 Notation "''I_' n" := (ordinal n)
1085 (at level 8, n at level 2, format "''I_' n").
1086
1087 (* bigop ----------------------------------------------------------------- *)
1088
1089 Reserved Notation "\big [ op / idx ]_ i F"
1090 (at level 36, F at level 36, op, idx at level 10, i at level 0,
1091 right associativity,
1092 format "'[' \big [ op / idx ]_ i '/ ' F ']'").
1093 Reserved Notation "\big [ op / idx ]_ ( i <- r | P ) F"
1094 (at level 36, F at level 36, op, idx at level 10, i, r at level 50,
1095 format "'[' \big [ op / idx ]_ ( i <- r | P ) '/ ' F ']'").
1096 Reserved Notation "\big [ op / idx ]_ ( i <- r ) F"
1097 (at level 36, F at level 36, op, idx at level 10, i, r at level 50,
1098 format "'[' \big [ op / idx ]_ ( i <- r ) '/ ' F ']'").
1099 Reserved Notation "\big [ op / idx ]_ ( m <= i < n | P ) F"
1100 (at level 36, F at level 36, op, idx at level 10, m, i, n at level 50,
1101 format "'[' \big [ op / idx ]_ ( m <= i < n | P ) F ']'").
1102 Reserved Notation "\big [ op / idx ]_ ( m <= i < n ) F"
1103 (at level 36, F at level 36, op, idx at level 10, i, m, n at level 50,
1104 format "'[' \big [ op / idx ]_ ( m <= i < n ) '/ ' F ']'").
1105 Reserved Notation "\big [ op / idx ]_ ( i | P ) F"
1106 (at level 36, F at level 36, op, idx at level 10, i at level 50,
1107 format "'[' \big [ op / idx ]_ ( i | P ) '/ ' F ']'").
1108 Reserved Notation "\big [ op / idx ]_ ( i : t | P ) F"
1109 (at level 36, F at level 36, op, idx at level 10, i at level 50,
1110 format "'[' \big [ op / idx ]_ ( i : t | P ) '/ ' F ']'").
1111 Reserved Notation "\big [ op / idx ]_ ( i : t ) F"
1112 (at level 36, F at level 36, op, idx at level 10, i at level 50,
1113 format "'[' \big [ op / idx ]_ ( i : t ) '/ ' F ']'").
1114 Reserved Notation "\big [ op / idx ]_ ( i < n | P ) F"
1115 (at level 36, F at level 36, op, idx at level 10, i, n at level 50,
1116 format "'[' \big [ op / idx ]_ ( i < n | P ) '/ ' F ']'").
1117 Reserved Notation "\big [ op / idx ]_ ( i < n ) F"
1118 (at level 36, F at level 36, op, idx at level 10, i, n at level 50,
1119 format "'[' \big [ op / idx ]_ ( i < n ) F ']'").
1120 Reserved Notation "\big [ op / idx ]_ ( i 'in' A | P ) F"
1121 (at level 36, F at level 36, op, idx at level 10, i, A at level 50,
1122 format "'[' \big [ op / idx ]_ ( i 'in' A | P ) '/ ' F ']'").
1123 Reserved Notation "\big [ op / idx ]_ ( i 'in' A ) F"
1124 (at level 36, F at level 36, op, idx at level 10, i, A at level 50,
1125 format "'[' \big [ op / idx ]_ ( i 'in' A ) '/ ' F ']'").
1126
1127 Module Monoid.
1128
1129 Section Definitions.
1130 Variables (T : Type) (idm : T).
1131
1132 Structure law := Law {
1133 operator : T -> T -> T;
1134 _ : associative operator;
1135 _ : left_id idm operator;
1136 _ : right_id idm operator
1137 }.
1138 Local Coercion operator : law >-> Funclass.
1139
1140 Structure com_law := ComLaw {
1141 com_operator : law;
1142 _ : commutative com_operator
1143 }.
1144 Local Coercion com_operator : com_law >-> law.
1145
1146 Structure mul_law := MulLaw {
1147 mul_operator : T -> T -> T;
1148 _ : left_zero idm mul_operator;
1149 _ : right_zero idm mul_operator
1150 }.
1151 Local Coercion mul_operator : mul_law >-> Funclass.
1152
1153 Structure add_law (mul : T -> T -> T) := AddLaw {
1154 add_operator : com_law;
1155 _ : left_distributive mul add_operator;
1156 _ : right_distributive mul add_operator
1157 }.
1158 Local Coercion add_operator : add_law >-> com_law.
1159
1160 Let op_id (op1 op2 : T -> T -> T) := phant_id op1 op2.
1161
1162 Definition clone_law op :=
1163 fun (opL : law) & op_id opL op =>
1164 fun opmA op1m opm1 (opL' := @Law op opmA op1m opm1)
1165 & phant_id opL' opL => opL'.
1166
1167 Definition clone_com_law op :=
1168 fun (opL : law) (opC : com_law) & op_id opL op & op_id opC op =>
1169 fun opmC (opC' := @ComLaw opL opmC) & phant_id opC' opC => opC'.
1170
1171 Definition clone_mul_law op :=
1172 fun (opM : mul_law) & op_id opM op =>
1173 fun op0m opm0 (opM' := @MulLaw op op0m opm0) & phant_id opM' opM => opM'.
1174
1175 Definition clone_add_law mop aop :=
1176 fun (opC : com_law) (opA : add_law mop) & op_id opC aop & op_id opA aop =>
1177 fun mopDm mopmD (opA' := @AddLaw mop opC mopDm mopmD)
1178 & phant_id opA' opA => opA'.
1179
1180 End Definitions.
1181
1182 Module Import Exports.
1183 Coercion operator : law >-> Funclass.
1184 Coercion com_operator : com_law >-> law.
1185 Coercion mul_operator : mul_law >-> Funclass.
1186 Coercion add_operator : add_law >-> com_law.
1187 Notation "[ 'law' 'of' f ]" := (@clone_law _ _ f _ id _ _ _ id)
1188 (at level 0, format"[ 'law' 'of' f ]") : form_scope.
1189 Notation "[ 'com_law' 'of' f ]" := (@clone_com_law _ _ f _ _ id id _ id)
1190 (at level 0, format "[ 'com_law' 'of' f ]") : form_scope.
1191 Notation "[ 'mul_law' 'of' f ]" := (@clone_mul_law _ _ f _ id _ _ id)
1192 (at level 0, format"[ 'mul_law' 'of' f ]") : form_scope.
1193 Notation "[ 'add_law' m 'of' a ]" := (@clone_add_law _ _ m a _ _ id id _ _ id)
1194 (at level 0, format "[ 'add_law' m 'of' a ]") : form_scope.
1195 End Exports.
1196
1197 Section CommutativeAxioms.
1198
1199 Variable (T : Type) (zero one : T) (mul add : T -> T -> T) (inv : T -> T).
1200 Hypothesis mulC : commutative mul.
1201
1202 Lemma mulC_id : left_id one mul -> right_id one mul.
1203 Proof. by move=> mul1x x; rewrite mulC. Qed.
1204
1205 Lemma mulC_zero : left_zero zero mul -> right_zero zero mul.
1206 Proof. by move=> mul0x x; rewrite mulC. Qed.
1207
1208 Lemma mulC_dist : left_distributive mul add -> right_distributive mul add.
1209 Proof. by move=> mul_addl x y z; rewrite !(mulC x). Qed.
1210
1211 End CommutativeAxioms.
1212 Module Theory.
1213
1214 Section Theory.
1215 Variables (T : Type) (idm : T).
1216
1217 Section Plain.
1218 Variable mul : law idm.
1219 Lemma mul1m : left_id idm mul. Proof. by case mul. Qed.
1220 Lemma mulm1 : right_id idm mul. Proof. by case mul. Qed.
1221 Lemma mulmA : associative mul. Proof. by case mul. Qed.
1222 (*Lemma iteropE n x : iterop n mul x idm = iter n (mul x) idm.*)
1223
1224 End Plain.
1225
1226 Section Commutative.
1227 Variable mul : com_law idm.
1228 Lemma mulmC : commutative mul. Proof. by case mul. Qed.
1229 Lemma mulmCA : left_commutative mul.
1230 Proof. by move=> x y z; rewrite !mulmA (mulmC x). Qed.
1231 Lemma mulmAC : right_commutative mul.
1232 Proof. by move=> x y z; rewrite -!mulmA (mulmC y). Qed.
1233 Lemma mulmACA : interchange mul mul.
1234 Proof. by move=> x y z t; rewrite -!mulmA (mulmCA y). Qed.
1235 End Commutative.
1236
1237 Section Mul.
1238 Variable mul : mul_law idm.
1239 Lemma mul0m : left_zero idm mul. Proof. by case mul. Qed.
1240 Lemma mulm0 : right_zero idm mul. Proof. by case mul. Qed.
1241 End Mul.
1242
1243 Section Add.
1244 Variables (mul : T -> T -> T) (add : add_law idm mul).
1245 Lemma addmA : associative add. Proof. exact: mulmA. Qed.
1246 Lemma addmC : commutative add. Proof. exact: mulmC. Qed.
1247 Lemma addmCA : left_commutative add. Proof. exact: mulmCA. Qed.
1248 Lemma addmAC : right_commutative add. Proof. exact: mulmAC. Qed.
1249 Lemma add0m : left_id idm add. Proof. exact: mul1m. Qed.
1250 Lemma addm0 : right_id idm add. Proof. exact: mulm1. Qed.
1251 Lemma mulm_addl : left_distributive mul add. Proof. by case add. Qed.
1252 Lemma mulm_addr : right_distributive mul add. Proof. by case add. Qed.
1253 End Add.
1254
1255 Definition simpm := (mulm1, mulm0, mul1m, mul0m, mulmA).
1256
1257 End Theory.
1258
1259 End Theory.
1260 Include Theory.
1261
1262 End Monoid.
1263 Export Monoid.Exports.
1264
1265 Section PervasiveMonoids.
1266
1267 Import Monoid.
1268
1269 Canonical andb_monoid := Law andbA andTb andbT.
1270 Canonical andb_comoid := ComLaw andbC.
1271
1272 Canonical andb_muloid := MulLaw andFb andbF.
1273 Canonical orb_monoid := Law orbA orFb orbF.
1274 Canonical orb_comoid := ComLaw orbC.
1275 Canonical orb_muloid := MulLaw orTb orbT.
1276 Canonical addb_monoid := Law addbA addFb addbF.
1277 Canonical addb_comoid := ComLaw addbC.
1278 Canonical orb_addoid := AddLaw andb_orl andb_orr.
1279 Canonical andb_addoid := AddLaw orb_andl orb_andr.
1280 Canonical addb_addoid := AddLaw andb_addl andb_addr.
1281
1282 Canonical addn_monoid := Law addnA add0n addn0.
1283 Canonical addn_comoid := ComLaw addnC.
1284 Canonical muln_monoid := Law mulnA mul1n muln1.
1285 Canonical muln_comoid := ComLaw mulnC.
1286 Canonical muln_muloid := MulLaw mul0n muln0.
1287 Canonical addn_addoid := AddLaw mulnDl mulnDr.
1288
1289 Canonical cat_monoid T := Law (@catA T) (@cat0s T) (@cats0 T).
1290
1291 End PervasiveMonoids.
1292 Declare Scope big_scope.
1293 Delimit Scope big_scope with BIG.
1294 Open Scope big_scope.
1295
1296 (* The bigbody wrapper is a workaround for a quirk of the Coq pretty-printer, *)
1297 (* which would fail to redisplay the \big notation when the <general_term> or *)
1298 (* <condition> do not depend on the bound index. The BigBody constructor *)
1299 (* packages both in in a term in which i occurs; it also depends on the *)
1300 (* iterated <op>, as this can give more information on the expected type of *)
1301 (* the <general_term>, thus allowing for the insertion of coercions. *)
1302 Variant bigbody R I := BigBody of I & (R -> R -> R) & bool & R.
1303
1304 Definition applybig {R I} (body : bigbody R I) x :=
1305 let: BigBody _ op b v := body in if b then op v x else x.
1306
1307 Definition reducebig R I idx r (body : I -> bigbody R I) :=
1308 foldr (applybig \o body) idx r.
1309
1310 Module Type BigOpSig.
1311 Parameter bigop : forall R I, R -> seq I -> (I -> bigbody R I) -> R.
1312 Axiom bigopE : bigop = reducebig.
1313 End BigOpSig.
1314
1315 Module BigOp : BigOpSig.
1316 Definition bigop := reducebig.
1317 Lemma bigopE : bigop = reducebig. Proof. by []. Qed.
1318 End BigOp.
1319
1320 Notation bigop := BigOp.bigop (only parsing).
1321 Canonical bigop_unlock := Unlockable BigOp.bigopE.
1322
1323 Definition index_iota m n := iota m (n - m).
1324
1325 Definition index_enum (T : finType) := Finite.enum T.
1326
1327 Lemma mem_index_iota m n i : (i \in index_iota m n) = (m <= i < n).
1328 Admitted.
1329
1330 Lemma mem_index_enum T i : i \in index_enum T.
1331 Admitted.
1332
1333 #[global] Hint Resolve mem_index_enum : core.
1334
1335 (*
1336 Lemma filter_index_enum T P : filter P (index_enum T) = enum P.
1337 Proof. by []. Qed.
1338 *)
1339
1340 Notation "\big [ op / idx ]_ ( i <- r | P ) F" :=
1341 (bigop idx r (fun i => BigBody i op P%B F)) : big_scope.
1342 Notation "\big [ op / idx ]_ ( i <- r ) F" :=
1343 (bigop idx r (fun i => BigBody i op true F)) : big_scope.
1344 Notation "\big [ op / idx ]_ ( m <= i < n | P ) F" :=
1345 (bigop idx (index_iota m n) (fun i : nat => BigBody i op P%B F))
1346 : big_scope.
1347 Notation "\big [ op / idx ]_ ( m <= i < n ) F" :=
1348 (bigop idx (index_iota m n) (fun i : nat => BigBody i op true F))
1349 : big_scope.
1350 Notation "\big [ op / idx ]_ ( i | P ) F" :=
1351 (bigop idx (index_enum _) (fun i => BigBody i op P%B F)) : big_scope.
1352 Notation "\big [ op / idx ]_ i F" :=
1353 (bigop idx (index_enum _) (fun i => BigBody i op true F)) : big_scope.
1354 Notation "\big [ op / idx ]_ ( i : t | P ) F" :=
1355 (bigop idx (index_enum _) (fun i : t => BigBody i op P%B F))
1356 (only parsing) : big_scope.
1357 Notation "\big [ op / idx ]_ ( i : t ) F" :=
1358 (bigop idx (index_enum _) (fun i : t => BigBody i op true F))
1359 (only parsing) : big_scope.
1360 Notation "\big [ op / idx ]_ ( i < n | P ) F" :=
1361 (\big[op/idx]_(i : ordinal n | P%B) F) : big_scope.
1362 Notation "\big [ op / idx ]_ ( i < n ) F" :=
1363 (\big[op/idx]_(i : ordinal n) F) : big_scope.
1364 Notation "\big [ op / idx ]_ ( i 'in' A | P ) F" :=
1365 (\big[op/idx]_(i | (i \in A) && P) F) : big_scope.
1366 Notation "\big [ op / idx ]_ ( i 'in' A ) F" :=
1367 (\big[op/idx]_(i | i \in A) F) : big_scope.
1368
1369 Notation BIG_F := (F in \big[_/_]_(i <- _ | _) F i)%pattern.
1370 Notation BIG_P := (P in \big[_/_]_(i <- _ | P i) _)%pattern.
1371
1372 (* Induction loading *)
1373 Lemma big_load R (K K' : R -> Type) idx op I r (P : pred I) F :
1374 K (\big[op/idx]_(i <- r | P i) F i) * K' (\big[op/idx]_(i <- r | P i) F i)
1375 -> K' (\big[op/idx]_(i <- r | P i) F i).
1376 Proof. by case. Qed.
1377
1378 Arguments big_load [R] K [K'] idx op [I].
1379
1380 Section Elim3.
1381
1382 Variables (R1 R2 R3 : Type) (K : R1 -> R2 -> R3 -> Type).
1383 Variables (id1 : R1) (op1 : R1 -> R1 -> R1).
1384 Variables (id2 : R2) (op2 : R2 -> R2 -> R2).
1385 Variables (id3 : R3) (op3 : R3 -> R3 -> R3).
1386
1387 Hypothesis Kid : K id1 id2 id3.
1388
1389 Lemma big_rec3 I r (P : pred I) F1 F2 F3
1390 (K_F : forall i y1 y2 y3, P i -> K y1 y2 y3 ->
1391 K (op1 (F1 i) y1) (op2 (F2 i) y2) (op3 (F3 i) y3)) :
1392 K (\big[op1/id1]_(i <- r | P i) F1 i)
1393 (\big[op2/id2]_(i <- r | P i) F2 i)
1394 (\big[op3/id3]_(i <- r | P i) F3 i).
1395 Proof. by rewrite unlock; elim: r => //= i r; case: ifP => //; apply: K_F. Qed.
1396
1397 Hypothesis Kop : forall x1 x2 x3 y1 y2 y3,
1398 K x1 x2 x3 -> K y1 y2 y3-> K (op1 x1 y1) (op2 x2 y2) (op3 x3 y3).
1399 Lemma big_ind3 I r (P : pred I) F1 F2 F3
1400 (K_F : forall i, P i -> K (F1 i) (F2 i) (F3 i)) :
1401 K (\big[op1/id1]_(i <- r | P i) F1 i)
1402 (\big[op2/id2]_(i <- r | P i) F2 i)
1403 (\big[op3/id3]_(i <- r | P i) F3 i).
1404 Proof. by apply: big_rec3 => i x1 x2 x3 /K_F; apply: Kop. Qed.
1405
1406 End Elim3.
1407
1408 Arguments big_rec3 [R1 R2 R3] K [id1 op1 id2 op2 id3 op3] _ [I r P F1 F2 F3].
1409 Arguments big_ind3 [R1 R2 R3] K [id1 op1 id2 op2 id3 op3] _ _ [I r P F1 F2 F3].
1410
1411 Section Elim2.
1412
1413 Variables (R1 R2 : Type) (K : R1 -> R2 -> Type) (f : R2 -> R1).
1414 Variables (id1 : R1) (op1 : R1 -> R1 -> R1).
1415 Variables (id2 : R2) (op2 : R2 -> R2 -> R2).
1416
1417 Hypothesis Kid : K id1 id2.
1418
1419 Lemma big_rec2 I r (P : pred I) F1 F2
1420 (K_F : forall i y1 y2, P i -> K y1 y2 ->
1421 K (op1 (F1 i) y1) (op2 (F2 i) y2)) :
1422 K (\big[op1/id1]_(i <- r | P i) F1 i) (\big[op2/id2]_(i <- r | P i) F2 i).
1423 Proof. by rewrite unlock; elim: r => //= i r; case: ifP => //; apply: K_F. Qed.
1424
1425 Hypothesis Kop : forall x1 x2 y1 y2,
1426 K x1 x2 -> K y1 y2 -> K (op1 x1 y1) (op2 x2 y2).
1427 Lemma big_ind2 I r (P : pred I) F1 F2 (K_F : forall i, P i -> K (F1 i) (F2 i)) :
1428 K (\big[op1/id1]_(i <- r | P i) F1 i) (\big[op2/id2]_(i <- r | P i) F2 i).
1429 Proof. by apply: big_rec2 => i x1 x2 /K_F; apply: Kop. Qed.
1430
1431 Hypotheses (f_op : {morph f : x y / op2 x y >-> op1 x y}) (f_id : f id2 = id1).
1432 Lemma big_morph I r (P : pred I) F :
1433 f (\big[op2/id2]_(i <- r | P i) F i) = \big[op1/id1]_(i <- r | P i) f (F i).
1434 Proof. by rewrite unlock; elim: r => //= i r <-; rewrite -f_op -fun_if. Qed.
1435
1436 End Elim2.
1437
1438 Arguments big_rec2 [R1 R2] K [id1 op1 id2 op2] _ [I r P F1 F2].
1439 Arguments big_ind2 [R1 R2] K [id1 op1 id2 op2] _ _ [I r P F1 F2].
1440 Arguments big_morph [R1 R2] f [id1 op1 id2 op2] _ _ [I].
1441
1442 Section Elim1.
1443
1444 Variables (R : Type) (K : R -> Type) (f : R -> R).
1445 Variables (idx : R) (op op' : R -> R -> R).
1446
1447 Hypothesis Kid : K idx.
1448
1449 Lemma big_rec I r (P : pred I) F
1450 (Kop : forall i x, P i -> K x -> K (op (F i) x)) :
1451 K (\big[op/idx]_(i <- r | P i) F i).
1452 Proof. by rewrite unlock; elim: r => //= i r; case: ifP => //; apply: Kop. Qed.
1453
1454 Hypothesis Kop : forall x y, K x -> K y -> K (op x y).
1455 Lemma big_ind I r (P : pred I) F (K_F : forall i, P i -> K (F i)) :
1456 K (\big[op/idx]_(i <- r | P i) F i).
1457 Proof. by apply: big_rec => // i x /K_F /Kop; apply. Qed.
1458
1459 Hypothesis Kop' : forall x y, K x -> K y -> op x y = op' x y.
1460 Lemma eq_big_op I r (P : pred I) F (K_F : forall i, P i -> K (F i)) :
1461 \big[op/idx]_(i <- r | P i) F i = \big[op'/idx]_(i <- r | P i) F i.
1462 Proof.
1463 by elim/(big_load K): _; elim/big_rec2: _ => // i _ y Pi [Ky <-]; auto.
1464 Qed.
1465
1466 Hypotheses (fM : {morph f : x y / op x y}) (f_id : f idx = idx).
1467 Lemma big_endo I r (P : pred I) F :
1468 f (\big[op/idx]_(i <- r | P i) F i) = \big[op/idx]_(i <- r | P i) f (F i).
1469 Proof. exact: big_morph. Qed.
1470
1471 End Elim1.
1472
1473 Arguments big_rec [R] K [idx op] _ [I r P F].
1474 Arguments big_ind [R] K [idx op] _ _ [I r P F].
1475 Arguments eq_big_op [R] K [idx op] op' _ _ _ [I].
1476 Arguments big_endo [R] f [idx op] _ _ [I].
1477
1478 (* zmodp -------------------------------------------------------------------- *)
1479
1480 Lemma ord1 : all_equal_to (@Ordinal 1 0 is_true_true : 'I_1).
1481 Admitted.