(* The record of operations *)
Structure fparams: Type := {
K:> Set;               (* the scalar type *)
v0: K;                (* 0 *)
v1: K;                (* 1 *)
eqK: K -> K -> bool;  (* = as a boolean function *)
oppK: K -> K;         (* - unary *)
addK : K -> K -> K;   (* + *)
multK : K -> K -> K;  (* * *)
invK : K -> K         (* 1/x *)
}.
(* Recover the usual mathematical notations *)
Delimit Scope field_scope with f.
Notation "x ?= y" := (eqK _ x y) (at level 70) : field_scope.
Notation "x + y" := (addK _ x y) : field_scope.
Notation "- x" := (oppK _ x) : field_scope.
Notation "x ^-1" := (invK _ x) (at level 25) : field_scope.
Notation "x * y" := (multK _ x y) : field_scope.
Notation "0" := (v0 _) : field_scope.
Notation "1" := (v1 _) : field_scope.
Section ParamsProp.
Variable p: fparams.
Open Scope field_scope.
(* Properties associated with a params *)
Structure fparamsProp: Type := {
eqK_dec: forall x y: p, if x ?= y then x = y else x <> y;
(* Boolean equality *)
addK_assoc: forall x y z: p, (x + y) + z = x + (y + z);
(* Associativity for + *)
addK_com: forall x y: p, x + y = y + x;
(* Commutativity for + *)
addK0l: forall x: p, 0 + x = x;
(* Left neutral  for + *)
oppKr: forall x: p, x + - x = 0;
(* Right opposite  for + *)
multK_assoc: forall x y z: p, (x * y) * z = x * (y * z);
(* Associativity for * *)
multK_com: forall x y: p, x * y = y * x;
(* Commutativity for * *)
multK1l: forall x: p, 1 * x = x;
(* Left neutral element for * *)
add_multKl: forall x y z: p, (x + y) * z = x * z + y * z;
(* Left distributivity of + over * *)
one_diff_zero: 1 <> 0 :> p;
(* Inverse *)
invKl: forall x: p, x <> 0 -> x * x^-1 = 1
}.
Fixpoint expK  (k: p) (n:nat) {struct n}: p :=
match n with O => 1 | (S n1) => k * expK k n1 end.
Notation "x ^ k" := (expK x k) : field_scope.
(* Derived properties  *)
Variable Hp: fparamsProp.
Implicit Types x y z: p.

(* Specifications: *)
Lemma eqKI: forall x, x ?= x = true.
Lemma eqK_spec x y: eq_Spec x y (x ?= y).
(* Left oppositive for + *)
Lemma oppKl:  forall x, - x + x = 0.
(* Cancelation rule for + *)
Lemma addK_cancel x y z: x + y = x + z -> y = z.
Lemma addK_eq_opp: forall x y, x + (-y) = 0 -> x = y.
(* Right neutral for + *)
Lemma addK0r x: x + 0 = x.
(* Oppositive involutive *)
Lemma opp_oppK x: - (- x) = x.
(* Right neutral for * *)
Lemma multK1r x: x * 1 = x.
(* Right distributivity of + over * *)
Lemma add_multKr x y z: z * (x + y) = z * x + z * y.
(* 0 is its own opposite *)
Lemma oppK0: - 0 = 0 :> p.
(* Distributivity of - over + *)
Lemma opp_addK x y: - (x + y) = - x + - y.
(* Right absorbent for * *)
Lemma multK0l x: 0 * x = 0.
(* Left absorbent for * *)
Lemma multK0r x: x * 0 = 0.
(* Left distributivity of - over * *)
Lemma opp_multKl x y: - (x * y) = - x * y.
(* Right distributivity of - over * *)
Lemma opp_multKr x y: - (x * y) = x * - y.
Lemma opp_multK1l x: - x  = (-(1)) * x.
Lemma opp_multK1r x: - x  = x * (-(1)).
Lemma multK_m1_m1: -(1) * -(1) = 1 :>p.
Lemma expKS n: (-(1)) ^n.+1 = -(-(1)) ^n.
Lemma expK_add n1 n2 a: a ^ (n1 + n2) = a ^n1 * a ^ n2.
Lemma expKm1_even n: even n -> (-(1))^ n = 1
with expKm1_odd n: odd n -> (-(1))^ n = -(1).
Lemma expKm1_sub m n: m <= n -> (-(1))^ (n - m) = (-(1))^ (n + m).
Lemma expKm1_2 n: (-(1))^ (2 * n) = 1.
(* Cancelation rule for * *)
Lemma multK_cancel x y z: x <> 0 -> x * y = x * z -> y = z.
Lemma multK_swap x y z: x * (y * z) = y * (x * z).
(* a field is integral *)
Lemma multK_integral x y : x * y = 0 -> x = 0 \/ y = 0.
Lemma expK_integral x n : x^n = 0 -> x = 0.
Lemma expKm1_n0 n : (-(1))^n <> 0.
Fixpoint n_to_K n :=
match n with O => 0: p | S n => (1 + n_to_K n) end.
Lemma n_to_K0: n_to_K 0 = 0.
Lemma n_to_K_minus m n : n <= m -> n_to_K (m - n) = n_to_K m + -n_to_K n.
Lemma n_to_K_mult m n : n_to_K (m * n) = n_to_K m * n_to_K n.
Definition Z_to_K (z: Z) :=
match z with
| Z0 => 0
| Zpos _ => n_to_K (Z.abs_nat z)
| Zneg _ => - (n_to_K (Z.abs_nat z))
end.
Lemma Z_to_K_opp (z: Z): Z_to_K (-z)%Z = - Z_to_K z.
Lemma Z_to_K_pos (z: Z): (0 <= z)%Z -> Z_to_K z = n_to_K (Z.abs_nat z).
Lemma Z_to_K_add (z1 z2: Z):
Z_to_K (z1 + z2)%Z = Z_to_K z1 + Z_to_K z2.
Lemma Z_to_K_minus (z1 z2: Z):
Z_to_K (z1 - z2)%Z = Z_to_K z1 + - Z_to_K z2.
Lemma Z_to_K_mult (z1 z2: Z):
Z_to_K (z1 * z2)%Z = Z_to_K z1 * Z_to_K z2.