Debian Codegraph @

src/Algebra/SubsetoidRing.v

raw
1 From Coq Require Ncring.
2 From Coq Require Cring.
3 From Coq Require Import Morphisms.
4 Require Import Crypto.Util.Tactics.BreakMatch.
5 Require Import Crypto.Util.Tactics.OnSubterms.
6 Require Import Crypto.Util.Tactics.Revert.
7 Require Import Crypto.Util.Tactics.RewriteHyp.
8 Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.Group Crypto.Algebra.Monoid.
9 Require Import Crypto.Algebra.Ring.
10 Require Import Crypto.Util.Tactics.DestructHead.
11 From Coq Require ZArith PArith.
12
13
14 Section Ring.
15 Context {T} {ok : T -> Prop} {eq : T -> T -> Prop}
16 {zero one : T}
17 {opp : T -> T}
18 {add sub mul : T -> T -> T}.
19
20 Record subsetoid_ring :=
21 {
22 zero_ok : ok zero;
23 one_ok : ok one;
24 opp_ok : forall x, ok x -> ok (opp x);
25 add_ok : forall x y, ok x -> ok y -> ok (add x y);
26 sub_ok : forall x y, ok x -> ok y -> ok (sub x y);
27 mul_ok : forall x y, ok x -> ok y -> ok (mul x y);
28 T_sig := { v : T | ok v };
29 eq_sig := fun x y : T_sig => eq (proj1_sig x) (proj1_sig y);
30 zero_sig : T_sig := exist _ _ zero_ok;
31 one_sig : T_sig := exist _ _ one_ok;
32 opp_sig : T_sig -> T_sig
33 := fun x => exist _ _ (opp_ok _ (proj2_sig x));
34 add_sig : T_sig -> T_sig -> T_sig
35 := fun x y => exist _ _ (add_ok _ _ (proj2_sig x) (proj2_sig y));
36 sub_sig : T_sig -> T_sig -> T_sig
37 := fun x y => exist _ _ (sub_ok _ _ (proj2_sig x) (proj2_sig y));
38 mul_sig : T_sig -> T_sig -> T_sig
39 := fun x y => exist _ _ (mul_ok _ _ (proj2_sig x) (proj2_sig y));
40 subsetoid_ring_is_ring : @ring T_sig eq_sig zero_sig one_sig opp_sig add_sig sub_sig mul_sig
41 }.
42 Existing Class subsetoid_ring.
43 Global Existing Instance subsetoid_ring_is_ring.
44
45 Section trivial_ok.
46 Context {R:@ring T eq zero one opp add sub mul}
47 (always_ok : forall x, ok x).
48 Lemma subsetoid_ring_by_always_ok
49 : subsetoid_ring.
50 Proof.
51 unshelve econstructor; intros; try apply always_ok; [].
52 destruct R; clear R.
53 repeat match goal with H : _ |- _ => destruct H; [] end.
54 hnf in *.
55 repeat unshelve econstructor; repeat intro; cbn in *;
56 match goal with
57 | [ H : _ |- _ ] => eapply H; eassumption
58 end.
59 Qed.
60 End trivial_ok.
61 End Ring.
62
63 Section Homomorphism.
64 Context {R OK EQ ZERO ONE OPP ADD SUB MUL} `{RR : @subsetoid_ring R OK EQ ZERO ONE OPP ADD SUB MUL}.
65 Context {S ok eq zero one opp add sub mul} `{SS : @subsetoid_ring S ok eq zero one opp add sub mul}.
66 Context {phi:R->S}.
67
68 Record is_subsetoid_homomorphism :=
69 {
70 phi_ok : forall x, OK x -> ok (phi x);
71 is_subsetoid_homomorphism_phi_proper
72 : forall x y, OK x -> OK y -> EQ x y -> eq (phi x) (phi y);
73 subsetoid_homomorphism_add
74 : forall a b : R, OK a -> OK b -> eq (phi (ADD a b)) (add (phi a) (phi b));
75 subsetoid_homomorphism_mul
76 : forall x y : R, OK x -> OK y -> eq (phi (MUL x y)) (mul (phi x) (phi y));
77 subsetoid_homomorphism_one
78 : eq (phi ONE) one
79 }.
80 Existing Class is_subsetoid_homomorphism.
81 End Homomorphism.
82
83 Section Isomorphism.
84 Context {F OK EQ ZERO ONE OPP ADD SUB MUL} {ringF:@subsetoid_ring F OK EQ ZERO ONE OPP ADD SUB MUL}.
85 Context {H} {ok : H -> Prop} {eq : H -> H -> Prop} {zero one : H} {opp : H -> H} {add sub mul : H -> H -> H}.
86 Context {phi:F->H} {phi':H->F}.
87 Local Infix "=" := EQ. Local Infix "=" := EQ : type_scope.
88 Context (ok_zero : ok zero)
89 (ok_one : ok one)
90 (ok_opp : forall x, ok x -> ok (opp x))
91 (ok_add : forall x y, ok x -> ok y -> ok (add x y))
92 (ok_sub : forall x y, ok x -> ok y -> ok (sub x y))
93 (ok_mul : forall x y, ok x -> ok y -> ok (mul x y))
94 (phi_ok : forall x, OK x -> ok (phi x))
95 (phi'_ok : forall x, ok x -> OK (phi' x))
96 (phi'_phi_id : forall A, OK A -> phi' (phi A) = A)
97 (phi'_eq : forall a b, EQ (phi' a) (phi' b) <-> eq a b)
98 {phi'_zero : phi' zero = ZERO}
99 {phi'_one : phi' one = ONE}
100 {phi'_opp : forall a, ok a -> phi' (opp a) = OPP (phi' a)}
101 (phi'_add : forall a b, ok a -> ok b -> phi' (add a b) = ADD (phi' a) (phi' b))
102 (phi'_sub : forall a b, ok a -> ok b -> phi' (sub a b) = SUB (phi' a) (phi' b))
103 (phi'_mul : forall a b, ok a -> ok b -> phi' (mul a b) = MUL (phi' a) (phi' b)).
104
105 Lemma subsetoid_ring_by_isomorphism
106 : @subsetoid_ring H ok eq zero one opp add sub mul
107 /\ @is_subsetoid_homomorphism F OK EQ ONE ADD MUL H ok eq one add mul phi
108 /\ @is_subsetoid_homomorphism H ok eq one add mul F OK EQ ONE ADD MUL phi'.
109 Proof.
110 let lem := open_constr:(@ring_by_isomorphism
111 _ _ _ _ _ _ _ _ (ringF.(subsetoid_ring_is_ring))
112 { v : H | ok v}
113 _ _ _ _ _ _ _
114 (fun x => exist _ _ (phi_ok _ (proj2_sig x)))
115 (fun x => exist _ _ (phi'_ok _ (proj2_sig x)))) in
116 edestruct lem as [Hring [H0 H1] ]; cbv [eq_sig T_sig];
117 [ ..
118 | repeat apply conj;
119 [ unshelve econstructor;
120 [ .. | exact Hring ]
121 | | ] ];
122 cbn; intros; destruct_head'_sig; cbn;
123 auto with nocore; [ | ].
124 1:clear H1.
125 2:clear H0.
126 all:destruct_head' @is_homomorphism; cbn in *.
127 all:destruct_head' @Monoid.is_homomorphism; cbn in *.
128 all:unshelve econstructor;
129 cbv [Proper respectful eq_sig T_sig] in *; cbn in *.
130 all:repeat match goal with
131 | [ H : forall a b : sig _, _ |- _ ]
132 => specialize (fun a b apf bpf => H (exist _ a apf) (exist _ b bpf))
133 end;
134 cbn in *.
135 all:eauto with nocore.
136 Qed.
137 End Isomorphism.
138
139 Section IsomorphismToTrivial.
140 Context {F EQ ZERO ONE OPP ADD SUB MUL} {ringF:@ring F EQ ZERO ONE OPP ADD SUB MUL}.
141 Context {H} {ok : H -> Prop} {eq : H -> H -> Prop} {zero one : H} {opp : H -> H} {add sub mul : H -> H -> H}.
142 Context {phi:F->H} {phi':H->F}.
143 Local Infix "=" := EQ. Local Infix "=" := EQ : type_scope.
144 Context (ok_zero : ok zero)
145 (ok_one : ok one)
146 (ok_opp : forall x, ok x -> ok (opp x))
147 (ok_add : forall x y, ok x -> ok y -> ok (add x y))
148 (ok_sub : forall x y, ok x -> ok y -> ok (sub x y))
149 (ok_mul : forall x y, ok x -> ok y -> ok (mul x y))
150 (phi_ok : forall x, ok (phi x))
151 (phi'_phi_id : forall A, phi' (phi A) = A)
152 (phi'_eq : forall a b, EQ (phi' a) (phi' b) <-> eq a b)
153 {phi'_zero : phi' zero = ZERO}
154 {phi'_one : phi' one = ONE}
155 {phi'_opp : forall a, ok a -> phi' (opp a) = OPP (phi' a)}
156 (phi'_add : forall a b, ok a -> ok b -> phi' (add a b) = ADD (phi' a) (phi' b))
157 (phi'_sub : forall a b, ok a -> ok b -> phi' (sub a b) = SUB (phi' a) (phi' b))
158 (phi'_mul : forall a b, ok a -> ok b -> phi' (mul a b) = MUL (phi' a) (phi' b)).
159
160 Lemma subsetoid_ring_by_ring_isomorphism
161 (OK := fun _ => True)
162 : @subsetoid_ring H ok eq zero one opp add sub mul
163 /\ @is_subsetoid_homomorphism F OK EQ ONE ADD MUL H ok eq one add mul phi
164 /\ @is_subsetoid_homomorphism H ok eq one add mul F OK EQ ONE ADD MUL phi'.
165 Proof.
166 eapply @subsetoid_ring_by_isomorphism;
167 [ eapply @subsetoid_ring_by_always_ok; [ eassumption | constructor ]
168 | auto with nocore; try solve [ constructor ].. ].
169 Qed.
170 End IsomorphismToTrivial.