Fixpoint linsert (t1 : tuple) (l : line) :=
match l with
|   nil => t1 :: l
| t2::l1 => match tcompare t1 t2 with
|  Gt => t2 :: linsert t1 l1
| res => t1 :: l
end
end.

(* Specifications: *)
Lemma linsert_length t l : length (linsert t l) = S (length l).
Lemma linsert_c t l : {l[linsert t l]} = ({t[t]} * {l[l]})%f.
Fixpoint lsort (l : line) :=
match l with
|   nil => l
| t ::l1 => linsert t (lsort l1)
end.
Lemma lsort_c l : {l[lsort l]} = {l[l]}.
Fixpoint ltsort (c : sign) (l : line) :=
match l with
|   nil => (c,l)
| t ::l1 =>  let (c1,t1) := tsort t in
let (c2,l2) := ltsort (smult c1 c) l1 in
(c2, linsert t1 l2)
end.
Lemma ltsort_c c l :
let (s,l1) := ltsort c l in
(s2k s * {l[l1]})%f = (s2k c * {l[l]})%f.