(* Multiplication on Z, (Z, +, *, 0, 1) is a unitary commutative ring *)

(*Recursive Definition multZ : Z -> Z -> Z := 
        OZ      y  => OZ
 | (pos O)      y  => y
 | (pos (S n1)) y  => (addZ (multZ (pos n1) y) y)
 | (neg O)      y  => (oppZ y)
 | (neg (S n1)) y  => (addZ (multZ (neg n1) y) (oppZ y)).
*)


Fixpoint multpos (x2 : Z) (n : nat) {struct n} : Z :=
  match n with
  | O => x2
  | S n0 => addZ (multpos x2 n0) x2
  end.Fixpoint multneg (x2 : Z) (n : nat) {struct n} : Z :=
  match n with
  | O => oppZ x2
  | S n0 => addZ (multneg x2 n0) (oppZ x2)
  end.Definition multZ (x1 x2 : Z) :=
  match x1 with
  | OZ => OZ
  | pos n => multpos x2 n
  | neg n => multneg x2 n
  end.

(* Specifications: *)
Lemma multZ_eq1 : forall n : Z, multZ OZ n = OZ.
Lemma multZ_eq2 : forall n : Z, multZ (pos 0) n = n.
Lemma multZ_eq3 : forall (n1 : nat) (n : Z), multZ (pos (S n1)) n = addZ (multZ (pos n1) n) n.
Lemma multZ_eq4 : forall n : Z, multZ (neg 0) n = oppZ n.
Lemma multZ_eq5 : forall (n1 : nat) (n : Z), multZ (neg (S n1)) n = addZ (multZ (neg n1) n) (oppZ n).
Lemma tech_mult_posZ : forall (x : nat) (y : Z), multZ (pos (S x)) y = addZ (multZ (pos x) y) y.
Lemma tech_mult_negZ : forall (x : nat) (y : Z), multZ (neg (S x)) y = addZ (multZ (neg x) y) (oppZ y).
Lemma mult_succZ_l : forall x y : Z, multZ (succZ x) y = addZ (multZ x y) y.
Lemma mult_predZ_l : forall x y : Z, multZ (predZ x) y = addZ (multZ x y) (oppZ y).
Lemma mult_succZ_r : forall x y : Z, multZ x (succZ y) = addZ (multZ x y) x.
Lemma mult_predZ_r : forall x y : Z, multZ x (predZ y) = addZ (multZ x y) (oppZ x).
Lemma mult_OZ : forall x : Z, multZ x OZ = OZ.
Lemma mult_IZ : forall x : Z, multZ x IZ = x.
Lemma mult_mIZ : forall x : Z, multZ x (neg 0) = oppZ x.
Theorem multZ_commutativity : commutativity Z multZ.
Theorem multZ_neutral : neutral Z IdZ multZ IZ.
Theorem mult_add_distributivity : distributivity Z addZ multZ.
Lemma mult_oppZ_r : forall x y : Z, multZ x (oppZ y) = oppZ (multZ x y).
Lemma mult_oppZ_l : forall x y : Z, multZ (oppZ x) y = oppZ (multZ x y).
Lemma tech_multZ_negO : forall x : Z, multZ (neg 0) x = oppZ x.
Lemma tech_mult_pos_posZ : forall n m : nat, multZ (pos n) (pos m) = pos (n * m + (n + m)).
Lemma tech_mult_neg_negZ : forall n m : nat, multZ (neg n) (neg m) = pos (n * m + (n + m)).
Lemma tech_mult_pos_negZ : forall n m : nat, multZ (pos n) (neg m) = neg (n * m + (n + m)).
Lemma tech_mult_neg_posZ : forall n m : nat, multZ (neg n) (pos m) = neg (n * m + (n + m)).
Theorem multZ_associativity : associativity Z multZ.
Theorem Z_ring : is_ring Z IdZ addZ multZ OZ oppZ.
Theorem Z_unitary_commutative_ring : is_unitary_commutative_ring Z IdZ addZ multZ OZ IZ oppZ.
Lemma tech_integ_posZ : forall (n : nat) (x : Z), multZ (pos n) x = OZ -> x = OZ.
Lemma tech_integ_negZ : forall (n : nat) (x : Z), multZ (neg n) x = OZ -> x = OZ.
Theorem integrityZ : integrity Z multZ OZ.
Lemma tech_mult_pos_succZ : forall n m : nat, posOZ (S n * S m) = multZ (pos n) (pos m).
Lemma tech_mult_pos_succZ2 : forall n m : nat, multZ (pos n) (pos m) = pos (S n * m + n).
Lemma tech_div1 : forall n0 n q r : nat, S n0 = q * S n + r -> pos n0 = addZ (multZ (pos n) (posOZ q)) (posOZ r).
Lemma tech_div2 : forall n0 n q : nat, S n0 = q * S n -> neg n0 = multZ (pos n) (negOZ q).
Lemma tech_div31 : forall n q : nat, addZ (oppZ (multZ (pos n) (pos q))) (pos n) = oppZ (multZ (pos n) (posOZ q)).
Lemma tech_div32 : forall n q r : nat, S n > r -> pos (n - r) = addZ (pos n) (oppZ (posOZ r)).
Lemma tech_div3 : forall n0 n q r : nat, S n0 = q * S n + r -> S n > r -> neg n0 = addZ (multZ (pos n) (neg q)) (pos (n - r)).
Lemma tech_div4 : forall n0 n q r : nat, S n0 = q * S n + r -> pos n0 = addZ (multZ (neg n) (negOZ q)) (posOZ r).
Lemma tech_div5 : forall n0 n q : nat, S n0 = q * S n -> neg n0 = multZ (neg n) (posOZ q).
Lemma tech_div6 : forall n0 n q r : nat, S n0 = q * S n + r -> S n > r -> neg n0 = addZ (multZ (neg n) (pos q)) (pos (n - r)).
Lemma inversibleZ : forall x : Z, inversible Z multZ IZ x -> x = IZ \/ x = oppZ IZ.
Lemma sgn_abs : forall x : Z, multZ x (sgnZ x) = absZ x.