aboutsummaryrefslogtreecommitdiff
path: root/src/Algebra/SubsetoidRing.v
blob: 6b7c7963b607df8e15df5b6c8f210f0ec2bef800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Require Coq.setoid_ring.Ncring.
Require Coq.setoid_ring.Cring.
Require Import Coq.Classes.Morphisms.
Require Import Crypto.Util.Tactics.BreakMatch.
Require Import Crypto.Util.Tactics.OnSubterms.
Require Import Crypto.Util.Tactics.Revert.
Require Import Crypto.Util.Tactics.RewriteHyp.
Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.Group Crypto.Algebra.Monoid.
Require Import Crypto.Algebra.Ring.
Require Import Crypto.Util.Tactics.DestructHead.
Require Coq.ZArith.ZArith Coq.PArith.PArith.


Section Ring.
  Context {T} {ok : T -> Prop} {eq : T -> T -> Prop}
          {zero one : T}
          {opp : T -> T}
          {add sub mul : T -> T -> T}.

  Record subsetoid_ring :=
    {
      zero_ok : ok zero;
      one_ok : ok one;
      opp_ok : forall x, ok x -> ok (opp x);
      add_ok : forall x y, ok x -> ok y -> ok (add x y);
      sub_ok : forall x y, ok x -> ok y -> ok (sub x y);
      mul_ok : forall x y, ok x -> ok y -> ok (mul x y);
      T_sig := { v : T | ok v };
      eq_sig := fun x y : T_sig => eq (proj1_sig x) (proj1_sig y);
      zero_sig : T_sig := exist _ _ zero_ok;
      one_sig : T_sig := exist _ _ one_ok;
      opp_sig : T_sig -> T_sig
      := fun x => exist _ _ (opp_ok _ (proj2_sig x));
      add_sig : T_sig -> T_sig -> T_sig
      := fun x y => exist _ _ (add_ok _ _ (proj2_sig x) (proj2_sig y));
      sub_sig : T_sig -> T_sig -> T_sig
      := fun x y => exist _ _ (sub_ok _ _ (proj2_sig x) (proj2_sig y));
      mul_sig : T_sig -> T_sig -> T_sig
      := fun x y => exist _ _ (mul_ok _ _ (proj2_sig x) (proj2_sig y));
      subsetoid_ring_is_ring : @ring T_sig eq_sig zero_sig one_sig opp_sig add_sig sub_sig mul_sig
    }.
  Existing Class subsetoid_ring.
  Global Existing Instance subsetoid_ring_is_ring.

  Section trivial_ok.
    Context {R:@ring T eq zero one opp add sub mul}
            (always_ok : forall x, ok x).
    Lemma subsetoid_ring_by_always_ok
      : subsetoid_ring.
    Proof.
      unshelve econstructor; intros; try apply always_ok; [].
      destruct R; clear R.
      repeat match goal with H : _ |- _ => destruct H; [] end.
      hnf in *.
      repeat unshelve econstructor; repeat intro; cbn in *;
        match goal with
        | [ H : _ |- _ ] => eapply H; eassumption
        end.
    Qed.
  End trivial_ok.
End Ring.

Section Homomorphism.
  Context {R OK EQ ZERO ONE OPP ADD SUB MUL} `{RR : @subsetoid_ring R OK EQ ZERO ONE OPP ADD SUB MUL}.
  Context {S ok eq zero one opp add sub mul} `{SS : @subsetoid_ring S ok eq zero one opp add sub mul}.
  Context {phi:R->S}.

  Record is_subsetoid_homomorphism :=
    {
      phi_ok : forall x, OK x -> ok (phi x);
      is_subsetoid_homomorphism_phi_proper
      : forall x y, OK x -> OK y -> EQ x y -> eq (phi x) (phi y);
      subsetoid_homomorphism_add
      : forall a b : R, OK a -> OK b -> eq (phi (ADD a b)) (add (phi a) (phi b));
      subsetoid_homomorphism_mul
      : forall x y : R, OK x -> OK y -> eq (phi (MUL x y)) (mul (phi x) (phi y));
      subsetoid_homomorphism_one
      : eq (phi ONE) one
    }.
  Existing Class is_subsetoid_homomorphism.
End Homomorphism.

Section Isomorphism.
  Context {F OK EQ ZERO ONE OPP ADD SUB MUL} {ringF:@subsetoid_ring F OK EQ ZERO ONE OPP ADD SUB MUL}.
  Context {H} {ok : H -> Prop} {eq : H -> H -> Prop} {zero one : H} {opp : H -> H} {add sub mul : H -> H -> H}.
  Context {phi:F->H} {phi':H->F}.
  Local Infix "=" := EQ. Local Infix "=" := EQ : type_scope.
  Context (ok_zero : ok zero)
          (ok_one : ok one)
          (ok_opp : forall x, ok x -> ok (opp x))
          (ok_add : forall x y, ok x -> ok y -> ok (add x y))
          (ok_sub : forall x y, ok x -> ok y -> ok (sub x y))
          (ok_mul : forall x y, ok x -> ok y -> ok (mul x y))
          (phi_ok : forall x, OK x -> ok (phi x))
          (phi'_ok : forall x, ok x -> OK (phi' x))
          (phi'_phi_id : forall A, OK A -> phi' (phi A) = A)
          (phi'_eq : forall a b, EQ (phi' a) (phi' b) <-> eq a b)
          {phi'_zero : phi' zero = ZERO}
          {phi'_one : phi' one = ONE}
          {phi'_opp : forall a, ok a -> phi' (opp a) = OPP (phi' a)}
          (phi'_add : forall a b, ok a -> ok b -> phi' (add a b) = ADD (phi' a) (phi' b))
          (phi'_sub : forall a b, ok a -> ok b -> phi' (sub a b) = SUB (phi' a) (phi' b))
          (phi'_mul : forall a b, ok a -> ok b -> phi' (mul a b) = MUL (phi' a) (phi' b)).

  Lemma subsetoid_ring_by_isomorphism
    : @subsetoid_ring H ok eq zero one opp add sub mul
      /\ @is_subsetoid_homomorphism F OK EQ ONE ADD MUL H ok eq one add mul phi
      /\ @is_subsetoid_homomorphism H ok eq one add mul F OK EQ ONE ADD MUL phi'.
  Proof.
    let lem := open_constr:(@ring_by_isomorphism
                              _ _ _ _ _ _ _ _ (ringF.(subsetoid_ring_is_ring))
                              { v : H | ok v}
                              _ _ _ _ _ _ _
                              (fun x => exist _ _ (phi_ok _ (proj2_sig x)))
                              (fun x => exist _ _ (phi'_ok _ (proj2_sig x)))) in
    edestruct lem as [Hring [H0 H1] ]; cbv [eq_sig T_sig];
      [ ..
      | repeat apply conj;
        [ unshelve econstructor;
          [ .. | exact Hring ]
        | | ] ];
      cbn; intros; destruct_head'_sig; cbn;
        auto with nocore; [ | ].
    1:clear H1.
    2:clear H0.
    all:destruct_head' @is_homomorphism; cbn in *.
    all:destruct_head' @Monoid.is_homomorphism; cbn in *.
    all:unshelve econstructor;
      cbv [Proper respectful eq_sig T_sig] in *; cbn in *.
    all:repeat match goal with
               | [ H : forall a b : sig _, _ |- _ ]
                 => specialize (fun a b apf bpf => H (exist _ a apf) (exist _ b bpf))
               end;
      cbn in *.
    all:eauto with nocore.
  Qed.
End Isomorphism.

Section IsomorphismToTrivial.
  Context {F EQ ZERO ONE OPP ADD SUB MUL} {ringF:@ring F EQ ZERO ONE OPP ADD SUB MUL}.
  Context {H} {ok : H -> Prop} {eq : H -> H -> Prop} {zero one : H} {opp : H -> H} {add sub mul : H -> H -> H}.
  Context {phi:F->H} {phi':H->F}.
  Local Infix "=" := EQ. Local Infix "=" := EQ : type_scope.
  Context (ok_zero : ok zero)
          (ok_one : ok one)
          (ok_opp : forall x, ok x -> ok (opp x))
          (ok_add : forall x y, ok x -> ok y -> ok (add x y))
          (ok_sub : forall x y, ok x -> ok y -> ok (sub x y))
          (ok_mul : forall x y, ok x -> ok y -> ok (mul x y))
          (phi_ok : forall x, ok (phi x))
          (phi'_phi_id : forall A, phi' (phi A) = A)
          (phi'_eq : forall a b, EQ (phi' a) (phi' b) <-> eq a b)
          {phi'_zero : phi' zero = ZERO}
          {phi'_one : phi' one = ONE}
          {phi'_opp : forall a, ok a -> phi' (opp a) = OPP (phi' a)}
          (phi'_add : forall a b, ok a -> ok b -> phi' (add a b) = ADD (phi' a) (phi' b))
          (phi'_sub : forall a b, ok a -> ok b -> phi' (sub a b) = SUB (phi' a) (phi' b))
          (phi'_mul : forall a b, ok a -> ok b -> phi' (mul a b) = MUL (phi' a) (phi' b)).

  Lemma subsetoid_ring_by_ring_isomorphism
        (OK := fun _ => True)
    : @subsetoid_ring H ok eq zero one opp add sub mul
      /\ @is_subsetoid_homomorphism F OK EQ ONE ADD MUL H ok eq one add mul phi
      /\ @is_subsetoid_homomorphism H ok eq one add mul F OK EQ ONE ADD MUL phi'.
  Proof.
    eapply @subsetoid_ring_by_isomorphism;
      [ eapply @subsetoid_ring_by_always_ok; [ eassumption | constructor ]
      | auto with nocore; try solve [ constructor ].. ].
  Qed.
End IsomorphismToTrivial.