Skip to content

Commit d79c36c

Browse files
authored
Merge pull request #47 from math-comp/N-semiring
`N` forms a semiring
2 parents 40637d5 + 2131f25 commit d79c36c

File tree

4 files changed

+244
-16
lines changed

4 files changed

+244
-16
lines changed

examples/test_algebra.v

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,42 @@ Unset Printing Implicit Defensive.
77

88
Local Delimit Scope Z_scope with Z.
99

10+
Fact test_zero_nat : Z.of_nat 0%R = 0%Z.
11+
Proof. zify_op; reflexivity. Qed.
12+
13+
Fact test_add_nat n m : Z.of_nat (n + m)%R = (Z.of_nat n + Z.of_nat m)%Z.
14+
Proof. zify_op; reflexivity. Qed.
15+
16+
Fact test_one_nat : Z.of_nat 1%R = 1%Z.
17+
Proof. zify_op; reflexivity. Qed.
18+
19+
Fact test_mul_nat n m : Z.of_nat (n * m)%R = (Z.of_nat n * Z.of_nat m)%Z.
20+
Proof. zify_op; reflexivity. Qed.
21+
22+
Fact test_natmul_nat n m : Z.of_nat (n *+ m)%R = (Z.of_nat n * Z.of_nat m)%Z.
23+
Proof. zify_op; reflexivity. Qed.
24+
25+
Fact test_exp_nat n m : Z.of_nat (n ^+ m)%R = (Z.of_nat n ^ Z.of_nat m)%Z.
26+
Proof. zify_op; reflexivity. Qed.
27+
28+
Fact test_zero_N : Z.of_N 0%R = 0%Z.
29+
Proof. zify_op; reflexivity. Qed.
30+
31+
Fact test_add_N n m : Z.of_N (n + m)%R = (Z.of_N n + Z.of_N m)%Z.
32+
Proof. zify_op; reflexivity. Qed.
33+
34+
Fact test_one_N : Z.of_N 1%R = 1%Z.
35+
Proof. zify_op; reflexivity. Qed.
36+
37+
Fact test_mul_N n m : Z.of_N (n * m)%R = (Z.of_N n * Z.of_N m)%Z.
38+
Proof. zify_op; reflexivity. Qed.
39+
40+
Fact test_natmul_N n m : Z.of_N (n *+ m)%R = (Z.of_N n * Z.of_nat m)%Z.
41+
Proof. zify_op; reflexivity. Qed.
42+
43+
Fact test_exp_N n m : Z.of_N (n ^+ m)%R = (Z.of_N n ^ Z.of_nat m)%Z.
44+
Proof. zify_op; reflexivity. Qed.
45+
1046
Fact test_unit_int m :
1147
m \is a GRing.unit = (Z_of_int m =? 1)%Z || (Z_of_int m =? - 1)%Z.
1248
Proof. zify_pre_hook; zify_op; reflexivity. Qed.

examples/test_ssreflect.v

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Proof. zify_op; reflexivity. Qed.
6363
Fact test_dual_max_bool (b1 b2 : bool^d) : Order.dual_max b1 b2 = b1 && b2.
6464
Proof. zify_op; reflexivity. Qed.
6565

66+
(* FIXME: meet and join below are broken but the tests pass because they are *)
67+
(* convertible anyway. *)
6668
Fact test_meet_bool b1 b2 : (b1 `&` b2)%O = b1 && b2.
6769
Proof. zify_op; reflexivity. Qed.
6870

@@ -75,17 +77,17 @@ Proof. zify_op; reflexivity. Qed.
7577
Fact test_dual_join_bool (b1 b2 : bool^d) : (b1 `|^d` b2)%O = b1 && b2.
7678
Proof. zify_op; reflexivity. Qed.
7779

78-
Fact test_bottom_bool : 0%O = false :> bool.
80+
Fact test_bottom_bool : \bot%O = false :> bool.
7981
Proof. zify_op; reflexivity. Qed.
8082

81-
Fact test_top_bool : 1%O = true :> bool.
83+
Fact test_top_bool : \top%O = true :> bool.
8284
Proof. zify_op; reflexivity. Qed.
8385

8486
(* FIXME: Notations 0^d and 1^d are broken. *)
85-
Fact test_dual_bottom_bool : 0%O = true :> bool^d.
87+
Fact test_dual_bottom_bool : \bot%O = true :> bool^d.
8688
Proof. zify_op; reflexivity. Qed.
8789

88-
Fact test_dual_top_bool : 1%O = false :> bool^d.
90+
Fact test_dual_top_bool : \top%O = false :> bool^d.
8991
Proof. zify_op; reflexivity. Qed.
9092

9193
Fact test_sub_bool b1 b2 : (b1 `\` b2)%O = b1 && ~~ b2.
@@ -235,7 +237,7 @@ Fact test_dual_join_nat (n m : nat^d) :
235237
Z.of_nat (n `|^d` m)%O = Z.min (Z.of_nat n) (Z.of_nat m).
236238
Proof. zify_op; reflexivity. Qed.
237239

238-
Fact test_bottom_nat : Z.of_nat 0%O = 0%Z.
240+
Fact test_bottom_nat : Z.of_nat \bot%O = 0%Z.
239241
Proof. zify_op; reflexivity. Qed.
240242

241243
(******************************************************************************)
@@ -315,15 +317,14 @@ Fact test_dual_join_natdvd (n m : natdvd^d) :
315317
Z.of_nat (n `|` m)%O = Z.gcd (Z.of_nat n) (Z.of_nat m).
316318
Proof. zify_op; reflexivity. Qed.
317319

318-
Fact test_bottom_natdvd : Z.of_nat (0%O : natdvd) = 1%Z.
320+
Fact test_bottom_natdvd : Z.of_nat (\bot%O : natdvd) = 1%Z.
319321
Proof. zify_op; reflexivity. Qed.
320322

321-
Fact test_top_natdvd : Z.of_nat (1%O : natdvd) = 0%Z.
323+
Fact test_top_natdvd : Z.of_nat (\top%O : natdvd) = 0%Z.
322324
Proof. zify_op; reflexivity. Qed.
323325

324-
(* FIXME: Notations 0^d and 1^d are broken. *)
325-
Fact test_dual_bottom_natdvd : Z.of_nat (0%O : natdvd^d) = 0%Z.
326+
Fact test_dual_bottom_natdvd : Z.of_nat (\bot^d%O : natdvd^d) = 0%Z.
326327
Proof. zify_op; reflexivity. Qed.
327328

328-
Fact test_dual_top_natdvd : Z.of_nat (1%O : natdvd^d) = 1%Z.
329+
Fact test_dual_top_natdvd : Z.of_nat (\top^d%O : natdvd^d) = 1%Z.
329330
Proof. zify_op; reflexivity. Qed.

theories/ssrZ.v

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,78 @@ case=> [[|n]|n] //=.
3535
rewrite addnC /=; congr Negz; lia.
3636
Qed.
3737

38-
Module ZInstances.
38+
Module Instances.
39+
40+
(* Instances taken from math-comp/math-comp#1031, authored by Pierre Roux *)
41+
(* TODO: remove them when we drop support for MathComp 2.0 *)
42+
#[export]
43+
HB.instance Definition _ := GRing.isNmodule.Build nat addnA addnC add0n.
44+
45+
#[export]
46+
HB.instance Definition _ := GRing.Nmodule_isComSemiRing.Build nat
47+
mulnA mulnC mul1n mulnDl mul0n erefl.
48+
49+
#[export]
50+
HB.instance Definition _ (V : nmodType) (x : V) :=
51+
GRing.isSemiAdditive.Build nat V (GRing.natmul x) (mulr0n x, mulrnDr x).
52+
53+
#[export]
54+
HB.instance Definition _ (R : semiRingType) :=
55+
GRing.isMultiplicative.Build nat R (GRing.natmul 1) (natrM R, mulr1n 1).
56+
57+
Fact Posz_is_semi_additive : semi_additive Posz.
58+
Proof. by []. Qed.
59+
60+
#[export]
61+
HB.instance Definition _ := GRing.isSemiAdditive.Build nat int Posz
62+
Posz_is_semi_additive.
63+
64+
Fact Posz_is_multiplicative : multiplicative Posz.
65+
Proof. by []. Qed.
66+
67+
#[export]
68+
HB.instance Definition _ := GRing.isMultiplicative.Build nat int Posz
69+
Posz_is_multiplicative.
70+
(* end *)
71+
72+
#[export]
73+
HB.instance Definition _ := Countable.copy N (can_type nat_of_binK).
74+
75+
#[export]
76+
HB.instance Definition _ := GRing.isNmodule.Build N
77+
Nplus_assoc Nplus_comm Nplus_0_l.
78+
79+
#[export]
80+
HB.instance Definition _ := GRing.Nmodule_isComSemiRing.Build N
81+
Nmult_assoc Nmult_comm Nmult_1_l Nmult_plus_distr_r N.mul_0_l isT.
82+
83+
Fact bin_of_nat_is_semi_additive : semi_additive bin_of_nat.
84+
Proof. by split=> //= m n; rewrite /GRing.add /=; lia. Qed.
85+
86+
#[export]
87+
HB.instance Definition _ := GRing.isSemiAdditive.Build nat N bin_of_nat
88+
bin_of_nat_is_semi_additive.
89+
90+
Fact nat_of_bin_is_semi_additive : semi_additive nat_of_bin.
91+
Proof. by split=> //= m n; rewrite /GRing.add /=; lia. Qed.
92+
93+
#[export]
94+
HB.instance Definition _ := GRing.isSemiAdditive.Build N nat nat_of_bin
95+
nat_of_bin_is_semi_additive.
96+
97+
Fact bin_of_nat_is_multiplicative : multiplicative bin_of_nat.
98+
Proof. by split => // m n; rewrite /GRing.mul /=; lia. Qed.
99+
100+
#[export]
101+
HB.instance Definition _ := GRing.isMultiplicative.Build nat N bin_of_nat
102+
bin_of_nat_is_multiplicative.
103+
104+
Fact nat_of_bin_is_multiplicative : multiplicative nat_of_bin.
105+
Proof. exact: can2_rmorphism bin_of_natK nat_of_binK. Qed.
106+
107+
#[export]
108+
HB.instance Definition _ := GRing.isMultiplicative.Build N nat nat_of_bin
109+
nat_of_bin_is_multiplicative.
39110

40111
Implicit Types (m n : Z).
41112

@@ -121,7 +192,7 @@ HB.instance Definition _ := GRing.isAdditive.Build Z int int_of_Z
121192
int_of_Z_is_additive.
122193

123194
Fact Z_of_int_is_multiplicative : multiplicative Z_of_int.
124-
Proof. by split => // n m; rewrite !Z_of_intE rmorphM. Qed.
195+
Proof. by split => // m n; rewrite !Z_of_intE rmorphM. Qed.
125196

126197
#[export]
127198
HB.instance Definition _ := GRing.isMultiplicative.Build int Z Z_of_int
@@ -134,8 +205,36 @@ Proof. exact: can2_rmorphism Z_of_intK int_of_ZK. Qed.
134205
HB.instance Definition _ := GRing.isMultiplicative.Build Z int int_of_Z
135206
int_of_Z_is_multiplicative.
136207

208+
Fact Z_of_nat_is_semi_additive : semi_additive Z.of_nat.
209+
Proof. by split=> //= m n; rewrite /GRing.add /=; lia. Qed.
210+
211+
#[export]
212+
HB.instance Definition _ := GRing.isSemiAdditive.Build nat Z Z.of_nat
213+
Z_of_nat_is_semi_additive.
214+
215+
Fact Z_of_nat_is_multiplicative : multiplicative Z.of_nat.
216+
Proof. by split => // m n; rewrite /GRing.mul /=; lia. Qed.
217+
218+
#[export]
219+
HB.instance Definition _ := GRing.isMultiplicative.Build nat Z Z.of_nat
220+
Z_of_nat_is_multiplicative.
221+
222+
Fact Z_of_N_is_semi_additive : semi_additive Z.of_N.
223+
Proof. by split=> //= m n; rewrite /GRing.add /=; lia. Qed.
224+
225+
#[export]
226+
HB.instance Definition _ := GRing.isSemiAdditive.Build N Z Z.of_N
227+
Z_of_N_is_semi_additive.
228+
229+
Fact Z_of_N_is_multiplicative : multiplicative Z.of_N.
230+
Proof. by split => // m n; rewrite /GRing.mul /=; lia. Qed.
231+
232+
#[export]
233+
HB.instance Definition _ := GRing.isMultiplicative.Build N Z Z.of_N
234+
Z_of_N_is_multiplicative.
235+
137236
Module Exports. HB.reexport. End Exports.
138237

139-
End ZInstances.
238+
End Instances.
140239

141-
Export ZInstances.Exports.
240+
Export Instances.Exports.

theories/zify_algebra.v

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,86 @@ Local Delimit Scope Z_scope with Z.
1717

1818
Import Order.Theory GRing.Theory Num.Theory SsreflectZifyInstances.
1919

20+
(* TODO: remove natn below when we drop support for MathComp 2.0 *)
21+
Local Lemma natn n : n%:R%R = n :> nat.
22+
Proof. by elim: n => // n; rewrite mulrS => ->. Qed.
23+
24+
(******************************************************************************)
25+
(* nat *)
26+
(******************************************************************************)
27+
28+
#[global]
29+
Instance Op_nat_0 : CstOp (0%R : nat) := ZifyInst.Op_O.
30+
Add Zify CstOp Op_nat_0.
31+
32+
#[global]
33+
Instance Op_nat_add : BinOp (+%R : nat -> nat -> nat) := Op_addn.
34+
Add Zify BinOp Op_nat_add.
35+
36+
#[global]
37+
Instance Op_nat_1 : CstOp (1%R : nat) := { TCst := 1%Z; TCstInj := erefl }.
38+
Add Zify CstOp Op_nat_1.
39+
40+
#[global]
41+
Instance Op_nat_mul : BinOp ( *%R : nat -> nat -> nat) := Op_muln.
42+
Add Zify BinOp Op_nat_mul.
43+
44+
Fact Op_nat_natmul_subproof (n m : nat) :
45+
Z.of_nat (n *+ m)%R = (Z.of_nat n * Z.of_nat m)%Z.
46+
Proof. by rewrite raddfMn -mulr_natr -[m in RHS]natn rmorph_nat. Qed.
47+
48+
#[global]
49+
Instance Op_nat_natmul : BinOp (@GRing.natmul _ : nat -> nat -> nat) :=
50+
{ TBOp := Z.mul; TBOpInj := Op_nat_natmul_subproof }.
51+
Add Zify BinOp Op_nat_natmul.
52+
53+
Fact Op_nat_exp_subproof (n : nat) (m : nat) :
54+
Z_of_nat (n ^+ m)%R = (Z_of_int n ^ Z.of_nat m)%Z.
55+
Proof. rewrite -Zpower_nat_Z; elim: m => //= m <-; rewrite exprS; lia. Qed.
56+
57+
#[global]
58+
Instance Op_nat_exp : BinOp (@GRing.exp _ : nat -> nat -> nat) :=
59+
{ TBOp := Z.pow; TBOpInj := Op_nat_exp_subproof }.
60+
Add Zify BinOp Op_nat_exp.
61+
62+
(******************************************************************************)
63+
(* N *)
64+
(******************************************************************************)
65+
66+
#[global]
67+
Instance Op_N_0 : CstOp (0%R : N) := ZifyInst.Op_N_N0.
68+
Add Zify CstOp Op_N_0.
69+
70+
#[global]
71+
Instance Op_N_add : BinOp (+%R : N -> N -> N) := ZifyInst.Op_N_add.
72+
Add Zify BinOp Op_N_add.
73+
74+
#[global]
75+
Instance Op_N_1 : CstOp (1%R : N) := { TCst := 1%Z; TCstInj := erefl }.
76+
Add Zify CstOp Op_N_1.
77+
78+
#[global]
79+
Instance Op_N_mul : BinOp ( *%R : N -> N -> N) := ZifyInst.Op_N_mul.
80+
Add Zify BinOp Op_N_mul.
81+
82+
Fact Op_N_natmul_subproof (n : N) (m : nat) :
83+
Z.of_N (n *+ m)%R = (Z.of_N n * Z.of_nat m)%Z.
84+
Proof. by rewrite raddfMn -mulr_natr -[m in RHS]natn rmorph_nat. Qed.
85+
86+
#[global]
87+
Instance Op_N_natmul : BinOp (@GRing.natmul _ : N -> nat -> N) :=
88+
{ TBOp := Z.mul; TBOpInj := Op_N_natmul_subproof }.
89+
Add Zify BinOp Op_N_natmul.
90+
91+
Fact Op_N_exp_subproof (n : N) (m : nat) :
92+
Z_of_N (n ^+ m)%R = (Z_of_N n ^ Z.of_nat m)%Z.
93+
Proof. rewrite -Zpower_nat_Z; elim: m => //= m <-; rewrite exprS; lia. Qed.
94+
95+
#[global]
96+
Instance Op_N_exp : BinOp (@GRing.exp _ : N -> nat -> N) :=
97+
{ TBOp := Z.pow; TBOpInj := Op_N_exp_subproof }.
98+
Add Zify BinOp Op_N_exp.
99+
20100
(******************************************************************************)
21101
(* ssrint *)
22102
(******************************************************************************)
@@ -287,7 +367,7 @@ Instance Op_Z_exp : BinOp (@GRing.exp _ : Z -> nat -> Z) :=
287367
Add Zify BinOp Op_Z_exp.
288368

289369
#[global]
290-
Instance Op_unitZ : UnOp (has_quality 1 ZInstances.unitZ : Z -> bool) :=
370+
Instance Op_unitZ : UnOp (has_quality 1 Instances.unitZ : Z -> bool) :=
291371
{ TUOp x := (x =? 1)%Z || (x =? - 1)%Z; TUOpInj _ := erefl }.
292372
Add Zify UnOp Op_unitZ.
293373

@@ -296,7 +376,7 @@ Instance Op_Z_unit : UnOp (has_quality 1 GRing.unit : Z -> bool) := Op_unitZ.
296376
Add Zify UnOp Op_Z_unit.
297377

298378
#[global]
299-
Instance Op_invZ : UnOp ZInstances.invZ := { TUOp := id; TUOpInj _ := erefl }.
379+
Instance Op_invZ : UnOp Instances.invZ := { TUOp := id; TUOpInj _ := erefl }.
300380
Add Zify UnOp Op_invZ.
301381

302382
#[global]
@@ -427,6 +507,18 @@ Instance Op_coprimez : BinOp coprimez :=
427507
Add Zify BinOp Op_coprimez.
428508

429509
Module Exports.
510+
Add Zify CstOp Op_nat_0.
511+
Add Zify BinOp Op_nat_add.
512+
Add Zify CstOp Op_nat_1.
513+
Add Zify BinOp Op_nat_mul.
514+
Add Zify BinOp Op_nat_natmul.
515+
Add Zify BinOp Op_nat_exp.
516+
Add Zify CstOp Op_N_0.
517+
Add Zify BinOp Op_N_add.
518+
Add Zify CstOp Op_N_1.
519+
Add Zify BinOp Op_N_mul.
520+
Add Zify BinOp Op_N_natmul.
521+
Add Zify BinOp Op_N_exp.
430522
Add Zify InjTyp Inj_int_Z.
431523
Add Zify UnOp Op_Z_of_int.
432524
Add Zify UnOp Op_Posz.

0 commit comments

Comments
 (0)