| 1 |
Require Export Crypto.Util.FixCoqMistakes. |
| 2 |
Require Export Crypto.Util.Decidable. |
| 3 |
|
| 4 |
From Coq Require BinPos. |
| 5 |
From Coq Require Import Morphisms. |
| 6 |
|
| 7 |
From Coq Require List. |
| 8 |
|
| 9 |
Local Close Scope nat_scope. Local Close Scope type_scope. Local Close Scope core_scope. |
| 10 |
|
| 11 |
Section Algebra. |
| 12 |
Context {T:Type} {eq:T->T->Prop}. |
| 13 |
Local Infix "=" := eq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 14 |
|
| 15 |
Section SingleOperation. |
| 16 |
Context {op:T->T->T}. |
| 17 |
|
| 18 |
Class is_associative := { associative : forall x y z, op x (op y z) = op (op x y) z }. |
| 19 |
|
| 20 |
Context {id:T}. |
| 21 |
|
| 22 |
Class is_left_identity := { left_identity : forall x, op id x = x }. |
| 23 |
Class is_right_identity := { right_identity : forall x, op x id = x }. |
| 24 |
|
| 25 |
Class monoid := |
| 26 |
{ |
| 27 |
monoid_is_associative : is_associative; |
| 28 |
monoid_is_left_identity : is_left_identity; |
| 29 |
monoid_is_right_identity : is_right_identity; |
| 30 |
|
| 31 |
monoid_op_Proper: Proper (respectful eq (respectful eq eq)) op; |
| 32 |
monoid_Equivalence : Equivalence eq |
| 33 |
}. |
| 34 |
Global Existing Instance monoid_is_associative. |
| 35 |
Global Existing Instance monoid_is_left_identity. |
| 36 |
Global Existing Instance monoid_is_right_identity. |
| 37 |
Global Existing Instance monoid_Equivalence. |
| 38 |
Global Existing Instance monoid_op_Proper. |
| 39 |
|
| 40 |
Context {inv:T->T}. |
| 41 |
Class is_left_inverse := { left_inverse : forall x, op (inv x) x = id }. |
| 42 |
Class is_right_inverse := { right_inverse : forall x, op x (inv x) = id }. |
| 43 |
|
| 44 |
Class group := |
| 45 |
{ |
| 46 |
group_monoid : monoid; |
| 47 |
group_is_left_inverse : is_left_inverse; |
| 48 |
group_is_right_inverse : is_right_inverse; |
| 49 |
|
| 50 |
group_inv_Proper: Proper (respectful eq eq) inv |
| 51 |
}. |
| 52 |
Global Existing Instance group_monoid. |
| 53 |
Global Existing Instance group_is_left_inverse. |
| 54 |
Global Existing Instance group_is_right_inverse. |
| 55 |
Global Existing Instance group_inv_Proper. |
| 56 |
|
| 57 |
Class is_commutative := { commutative : forall x y, op x y = op y x }. |
| 58 |
|
| 59 |
Record commutative_group := |
| 60 |
{ |
| 61 |
commutative_group_group : group; |
| 62 |
commutative_group_is_commutative : is_commutative |
| 63 |
}. |
| 64 |
Existing Class commutative_group. |
| 65 |
Global Existing Instance commutative_group_group. |
| 66 |
Global Existing Instance commutative_group_is_commutative. |
| 67 |
End SingleOperation. |
| 68 |
|
| 69 |
Section AddMul. |
| 70 |
Context {zero one:T}. Local Notation "0" := zero. Local Notation "1" := one. |
| 71 |
Context {opp:T->T}. Local Notation "- x" := (opp x). |
| 72 |
Context {add:T->T->T} {sub:T->T->T} {mul:T->T->T}. |
| 73 |
Local Infix "+" := add. Local Infix "-" := sub. Local Infix "*" := mul. |
| 74 |
|
| 75 |
Class is_left_distributive := { left_distributive : forall a b c, a * (b + c) = a * b + a * c }. |
| 76 |
Class is_right_distributive := { right_distributive : forall a b c, (b + c) * a = b * a + c * a }. |
| 77 |
|
| 78 |
|
| 79 |
Class ring := |
| 80 |
{ |
| 81 |
ring_commutative_group_add : commutative_group (op:=add) (id:=zero) (inv:=opp); |
| 82 |
ring_monoid_mul : monoid (op:=mul) (id:=one); |
| 83 |
ring_is_left_distributive : is_left_distributive; |
| 84 |
ring_is_right_distributive : is_right_distributive; |
| 85 |
|
| 86 |
ring_sub_definition : forall x y, x - y = x + opp y; |
| 87 |
|
| 88 |
ring_mul_Proper : Proper (respectful eq (respectful eq eq)) mul; |
| 89 |
ring_sub_Proper : Proper(respectful eq (respectful eq eq)) sub |
| 90 |
}. |
| 91 |
Global Existing Instance ring_commutative_group_add. |
| 92 |
Global Existing Instance ring_monoid_mul. |
| 93 |
Global Existing Instance ring_is_left_distributive. |
| 94 |
Global Existing Instance ring_is_right_distributive. |
| 95 |
Global Existing Instance ring_mul_Proper. |
| 96 |
Global Existing Instance ring_sub_Proper. |
| 97 |
|
| 98 |
Class commutative_ring := |
| 99 |
{ |
| 100 |
commutative_ring_ring : ring; |
| 101 |
commutative_ring_is_commutative : is_commutative (op:=mul) |
| 102 |
}. |
| 103 |
Global Existing Instance commutative_ring_ring. |
| 104 |
Global Existing Instance commutative_ring_is_commutative. |
| 105 |
|
| 106 |
Class is_zero_product_zero_factor := |
| 107 |
{ zero_product_zero_factor : forall x y, x*y = 0 -> x = 0 \/ y = 0 }. |
| 108 |
|
| 109 |
Class is_zero_neq_one := { zero_neq_one : zero <> one }. |
| 110 |
|
| 111 |
Class integral_domain := |
| 112 |
{ |
| 113 |
integral_domain_commutative_ring : commutative_ring; |
| 114 |
integral_domain_is_zero_product_zero_factor : is_zero_product_zero_factor; |
| 115 |
integral_domain_is_zero_neq_one : is_zero_neq_one |
| 116 |
}. |
| 117 |
Global Existing Instance integral_domain_commutative_ring. |
| 118 |
Global Existing Instance integral_domain_is_zero_product_zero_factor. |
| 119 |
Global Existing Instance integral_domain_is_zero_neq_one. |
| 120 |
|
| 121 |
Context {inv:T->T} {div:T->T->T}. |
| 122 |
Class is_left_multiplicative_inverse := { left_multiplicative_inverse : forall x, x<>0 -> (inv x) * x = 1 }. |
| 123 |
|
| 124 |
Class field := |
| 125 |
{ |
| 126 |
field_commutative_ring : commutative_ring; |
| 127 |
field_is_left_multiplicative_inverse : is_left_multiplicative_inverse; |
| 128 |
field_is_zero_neq_one : is_zero_neq_one; |
| 129 |
|
| 130 |
field_div_definition : forall x y , div x y = x * inv y; |
| 131 |
|
| 132 |
field_inv_Proper : Proper (respectful eq eq) inv; |
| 133 |
field_div_Proper : Proper (respectful eq (respectful eq eq)) div |
| 134 |
}. |
| 135 |
Global Existing Instance field_commutative_ring. |
| 136 |
Global Existing Instance field_is_left_multiplicative_inverse. |
| 137 |
Global Existing Instance field_is_zero_neq_one. |
| 138 |
Global Existing Instance field_inv_Proper. |
| 139 |
Global Existing Instance field_div_Proper. |
| 140 |
End AddMul. |
| 141 |
|
| 142 |
Definition char_ge (zero:T) (inj:BinPos.positive->T) C := forall p, BinPos.Pos.lt p C -> not (eq (inj p) zero). |
| 143 |
Existing Class char_ge. |
| 144 |
Lemma char_ge_weaken id of_pos C |
| 145 |
(HC:@char_ge id of_pos C) c (Hc:BinPos.Pos.le c C) |
| 146 |
: @char_ge id of_pos c. |
| 147 |
Proof. intros ??; eauto using BinPos.Pos.lt_le_trans. Qed. |
| 148 |
(* TODO: make a typeclass instance that applies this lemma |
| 149 |
automatically using [vm_decide] for the inequality if both |
| 150 |
characteristics are literal constants. *) |
| 151 |
End Algebra. |
| 152 |
|
| 153 |
Section ZeroNeqOne. |
| 154 |
Context {T eq zero one} `{@is_zero_neq_one T eq zero one} `{Equivalence T eq}. |
| 155 |
|
| 156 |
Lemma one_neq_zero : not (eq one zero). |
| 157 |
Proof using Type*. |
| 158 |
intro HH; symmetry in HH. epose zero_neq_one; auto. |
| 159 |
Qed. |
| 160 |
End ZeroNeqOne. |