Definition spec_q (a b c d e f g h : Z) (q1 q2 : Q) : Q :=
  Qmult
    (Qplus (Qplus (Qplus (Qmult (Qmult a q1) q2) (Qmult b q1)) (Qmult c q2))
       d)
    (Qinv
       (Qplus
          (Qplus (Qplus (Qmult (Qmult e q1) q2) (Qmult f q1)) (Qmult g q2)) h)).Definition spec_Qquadratic_Qpositive_to_Q (a b c d e f g h : Z)
  (p1 p2 : Qpositive) : Q :=
  Qmult
    (Qplus
       (Qplus
          (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1)))
          (Qmult c (Qpos p2))) d)
    (Qinv
       (Qplus
          (Qplus
             (Qplus (Qmult (Qmult e (Qpos p1)) (Qpos p2)) (Qmult f (Qpos p1)))
             (Qmult g (Qpos p2))) h)).(* Here we should expect that ap1p2+bp1+cp2+d > 0 and ep1p1+fp1+gp2+h > 0, this follow - hopefully - from quadraticAcc *)
Definition spec_Qquadratic_Qpositive_to_Qpositive2 
  (a b c d e f g h : Z) (p1 p2 : Qpositive) : Qpositive :=
  Qpositive_mult
    (Q_tail
       (Qplus
          (Qplus
             (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1)))
             (Qmult c (Qpos p2))) d))
    (Qpositive_inv
       (Q_tail
          (Qplus
             (Qplus
                (Qplus (Qmult (Qmult e (Qpos p1)) (Qpos p2))
                   (Qmult f (Qpos p1))) (Qmult g (Qpos p2))) h))).

(* Specifications: *)
Lemma spec_Qquadratic_Qpositive_to_Q_commut : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h p1 p2 = spec_Qquadratic_Qpositive_to_Q a c b d e g f h p2 p1.
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_commut : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h p1 p2 = spec_Qquadratic_Qpositive_to_Qpositive2 a c b d e g f h p2 p1.
Lemma spec_Qquadratic_Qpositive_to_Q_p_One : forall (a b c d e f g h : Z) (p : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h p One = spec_Qhomographic_Qpositive_to_Q (a + b) (c + d) (e + f) (g + h) p.
Lemma spec_Qquadratic_Qpositive_to_Q_p_One_unfolded : forall (a b c d e f g h : Z) (p : Qpositive), Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos p)) (Qpos One)) (Qmult b (Qpos p))) (Qmult c (Qpos One))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos p)) (Qpos One)) (Qmult f (Qpos p))) (Qmult g (Qpos One))) h)) = Qmult (Qplus (Qmult (a + b)%Z (Qpos p)) (c + d)%Z) (Qinv (Qplus (Qmult (e + f)%Z (Qpos p)) (g + h)%Z)).
Lemma spec_Qquadratic_Qpositive_to_Q_One_p : forall (a b c d e f g h : Z) (p : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h One p = spec_Qhomographic_Qpositive_to_Q (a + c) (b + d) (e + g) (f + h) p.
Lemma spec_Qquadratic_Qpositive_to_Q_One_p_unfolded : forall (a b c d e f g h : Z) (p : Qpositive), Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos One)) (Qpos p)) (Qmult b (Qpos One))) (Qmult c (Qpos p))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos One)) (Qpos p)) (Qmult f (Qpos One))) (Qmult g (Qpos p))) h)) = Qmult (Qplus (Qmult (a + c)%Z (Qpos p)) (b + d)%Z) (Qinv (Qplus (Qmult (e + g)%Z (Qpos p)) (f + h)%Z)).
Lemma spec_Qquadratic_Qpositive_to_Q_nR_nR : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h (nR p1) (nR p2) = spec_Qquadratic_Qpositive_to_Q a (a + b) (a + c) (a + b + c + d) e (e + f) (e + g) (e + f + g + h) p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Q_nR_nR_unfolded : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos (nR p1))) (Qpos (nR p2))) (Qmult b (Qpos (nR p1)))) (Qmult c (Qpos (nR p2)))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos (nR p1))) (Qpos (nR p2))) (Qmult f (Qpos (nR p1)))) (Qmult g (Qpos (nR p2)))) h)) = Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult (a + b)%Z (Qpos p1))) (Qmult (a + c)%Z (Qpos p2))) (a + b + c + d)%Z) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos p1)) (Qpos p2)) (Qmult (e + f)%Z (Qpos p1))) (Qmult (e + g)%Z (Qpos p2))) (e + f + g + h)%Z)).
Lemma spec_Qquadratic_Qpositive_to_Q_nR_dL : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h (nR p1) (dL p2) = spec_Qquadratic_Qpositive_to_Q (a + b) b (a + b + c + d) (b + d) (e + f) f (e + f + g + h) (f + h) p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Q_nR_dL_unfolded : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos (nR p1))) (Qpos (dL p2))) (Qmult b (Qpos (nR p1)))) (Qmult c (Qpos (dL p2)))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos (nR p1))) (Qpos (dL p2))) (Qmult f (Qpos (nR p1)))) (Qmult g (Qpos (dL p2)))) h)) = Qmult (Qplus (Qplus (Qplus (Qmult (Qmult (a + b)%Z (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1))) (Qmult (a + b + c + d)%Z (Qpos p2))) (b + d)%Z) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult (e + f)%Z (Qpos p1)) (Qpos p2)) (Qmult f (Qpos p1))) (Qmult (e + f + g + h)%Z (Qpos p2))) (f + h)%Z)).
Lemma spec_Qquadratic_Qpositive_to_Q_dL_nR : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h (dL p1) (nR p2) = spec_Qquadratic_Qpositive_to_Q (a + c) (a + b + c + d) c (c + d) (e + g) (e + f + g + h) g (g + h) p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Q_dL_nR_unfolded : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos (dL p1))) (Qpos (nR p2))) (Qmult b (Qpos (dL p1)))) (Qmult c (Qpos (nR p2)))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos (dL p1))) (Qpos (nR p2))) (Qmult f (Qpos (dL p1)))) (Qmult g (Qpos (nR p2)))) h)) = Qmult (Qplus (Qplus (Qplus (Qmult (Qmult (a + c)%Z (Qpos p1)) (Qpos p2)) (Qmult (a + b + c + d)%Z (Qpos p1))) (Qmult c (Qpos p2))) (c + d)%Z) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult (e + g)%Z (Qpos p1)) (Qpos p2)) (Qmult (e + f + g + h)%Z (Qpos p1))) (Qmult g (Qpos p2))) (g + h)%Z)).
Lemma spec_Qquadratic_Qpositive_to_Q_dL_dL : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Q a b c d e f g h (dL p1) (dL p2) = spec_Qquadratic_Qpositive_to_Q (a + b + c + d) (b + d) (c + d) d (e + f + g + h) (f + h) (g + h) h p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Q_dL_dL_unfolded : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qmult (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos (dL p1))) (Qpos (dL p2))) (Qmult b (Qpos (dL p1)))) (Qmult c (Qpos (dL p2)))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos (dL p1))) (Qpos (dL p2))) (Qmult f (Qpos (dL p1)))) (Qmult g (Qpos (dL p2)))) h)) = Qmult (Qplus (Qplus (Qplus (Qmult (Qmult (a + b + c + d)%Z (Qpos p1)) (Qpos p2)) (Qmult (b + d)%Z (Qpos p1))) (Qmult (c + d)%Z (Qpos p2))) d) (Qinv (Qplus (Qplus (Qplus (Qmult (Qmult (e + f + g + h)%Z (Qpos p1)) (Qpos p2)) (Qmult (f + h)%Z (Qpos p1))) (Qmult (g + h)%Z (Qpos p2))) h)).
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_nR_nR : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h (nR p1) (nR p2) = spec_Qquadratic_Qpositive_to_Qpositive2 a (a + b) (a + c) (a + b + c + d) e (e + f) (e + g) (e + f + g + h) p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_nR_dL : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qplus (Qplus (Qplus (Qmult (Qmult (Qplus a b) (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1))) (Qmult (Qplus (Qplus (Qplus a b) c) d) (Qpos p2))) (Qplus b d) <> Zero -> Qplus (Qplus (Qplus (Qmult (Qmult (Qplus e f) (Qpos p1)) (Qpos p2)) (Qmult f (Qpos p1))) (Qmult (Qplus (Qplus (Qplus e f) g) h) (Qpos p2))) (Qplus f h) <> Zero -> spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h (nR p1) (dL p2) = spec_Qquadratic_Qpositive_to_Qpositive2 (a + b) b (a + b + c + d) (b + d) (e + f) f (e + f + g + h) (f + h) p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_dL_nR : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qplus (Qplus (Qplus (Qmult (Qmult (Qplus a c) (Qpos p1)) (Qpos p2)) (Qmult (Qplus (Qplus (Qplus a b) c) d) (Qpos p1))) (Qmult c (Qpos p2))) (Qplus c d) <> Zero -> Qplus (Qplus (Qplus (Qmult (Qmult (Qplus e g) (Qpos p1)) (Qpos p2)) (Qmult (Qplus (Qplus (Qplus e f) g) h) (Qpos p1))) (Qmult g (Qpos p2))) (Qplus g h) <> Zero -> spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h (dL p1) (nR p2) = spec_Qquadratic_Qpositive_to_Qpositive2 (a + c) (a + b + c + d) c (c + d) (e + g) (e + f + g + h) g (g + h) p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_dL_dL : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qplus (Qplus (Qplus (Qmult (Qmult (Qplus (Qplus (Qplus a b) c) d) (Qpos p1)) (Qpos p2)) (Qmult (Qplus b d) (Qpos p1))) (Qmult (Qplus c d) (Qpos p2))) d <> Zero -> Qplus (Qplus (Qplus (Qmult (Qmult (Qplus (Qplus (Qplus e f) g) h) (Qpos p1)) (Qpos p2)) (Qmult (Qplus f h) (Qpos p1))) (Qmult (Qplus g h) (Qpos p2))) h <> Zero -> spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h (dL p1) (dL p2) = spec_Qquadratic_Qpositive_to_Qpositive2 (a + b + c + d) (b + d) (c + d) d (e + f + g + h) (f + h) (g + h) h p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_nR_emission : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1))) (Qmult c (Qpos p2))) d) -> Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos p1)) (Qpos p2)) (Qmult f (Qpos p1))) (Qmult g (Qpos p2))) h) -> Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult (a - e)%Z (Qpos p1)) (Qpos p2)) (Qmult (b - f)%Z (Qpos p1))) (Qmult (c - g)%Z (Qpos p2))) (d - h)%Z) -> nR (spec_Qquadratic_Qpositive_to_Qpositive2 (a - e) (b - f) (c - g) (d - h) e f g h p1 p2) = spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h p1 p2.
Lemma spec_Qquadratic_Qpositive_to_Qpositive2_dL_emission : forall (a b c d e f g h : Z) (p1 p2 : Qpositive), Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1))) (Qmult c (Qpos p2))) d) -> Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult e (Qpos p1)) (Qpos p2)) (Qmult f (Qpos p1))) (Qmult g (Qpos p2))) h) -> Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult (e - a)%Z (Qpos p1)) (Qpos p2)) (Qmult (f - b)%Z (Qpos p1))) (Qmult (g - c)%Z (Qpos p2))) (h - d)%Z) -> dL (spec_Qquadratic_Qpositive_to_Qpositive2 a b c d (e - a) (f - b) (g - c) (h - d) p1 p2) = spec_Qquadratic_Qpositive_to_Qpositive2 a b c d e f g h p1 p2.
Lemma outside_square_correct_1 : forall (a b c d : Z) (p1 p2 : Qpositive), (2 < outside_square a b c d)%Z -> ~ (b = 0%Z /\ c = 0%Z /\ d = 0%Z) -> Qlt Zero (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1))) (Qmult c (Qpos p2))) d).
Lemma outside_square_correct_2 : forall (a b c d : Z) (p1 p2 : Qpositive), (outside_square a b c d < -2)%Z -> ~ (b = 0%Z /\ c = 0%Z /\ d = 0%Z) -> Qlt (Qplus (Qplus (Qplus (Qmult (Qmult a (Qpos p1)) (Qpos p2)) (Qmult b (Qpos p1))) (Qmult c (Qpos p2))) d) Zero.