(* This function will be only call with first vector is hom 1 *)
Fixpoint factor n: (vect n) -> (vect n) -> vect n :=
match n return vect n -> vect n -> vect n with
| O => fun x1 x2 => (x2 * x1^-1)%f
| S n1 =>
fun x1 x2 =>
let (x11, x12) := x1 in
let (x21, x22) := x2 in
if x12 ?= 0 then
(* if x<> 0 then x11 <> 0 *)
((0: vect n1), ('C[x11]^-1).* x21: vect n1) else
let x32 := factor n1 x12 x22 in
(* We have x12 ∨ x32 = x22 *)
(* let x31 such that x12 /\ x31 = x11 ∨ x32 - x21                            *)
(* (x11,x12) /\ (x31,x32) = (x11 /\ x32 - x12 /\ x31, x12 /\ x32) (x21, x22) *)
(factor n1 x12 (add n1 (('C[x11]) .* x32: vect n1) (scal n1 (-(1))%f  x21: vect n1))
, x32)
end.

(* Specifications: *)
Lemma factor0 n x : factor n x 0 = 0.
Lemma factor_scal n k x M : factor n x (k .* M) = k .* factor n x M.
Lemma factor_id n x : x <> 0 -> hom n 1 x -> factor n x x = 1.
Lemma factor_hom0E n x1 x2 : x1 <> 0 ->  hom n 1 x1 -> hom n 0 x2 ->
factor n x1 (x1 ∨ x2) = x2.
Lemma factor_factor n x1 x2 : hom n 1 x1 -> x1 <> 0 ->
x1 ∨ x2 = 0 -> x2 = x1 ∨ factor n x1 x2.
Lemma factork n x k : x <> 0 -> hom n 1 x -> factor n x [k] = 0.
Lemma factor0_hom0 n x1 x2 : x1 <> 0 -> hom n 1 x1 -> hom n 0 x2 ->
factor n x1 x2 = 0.
Lemma factor_hom n k x1 x2 : x1 <> 0 -> x1 ∨ x2 = 0 ->
hom n 1 x1 -> hom n k.+1 x2 -> hom n k (factor n x1 x2).
Lemma factor_add n k x1 x2 x3 : x1 <> 0 ->
hom n 1 x1 -> hom n k.+1 x2 -> hom n k.+1 x3 ->
x1 ∨ x2 = 0 -> x1 ∨ x3 = 0 ->
factor n x1 (x2 + x3) = factor n x1 x2 + factor n x1 x3.