Definition ecompare : expr -> expr -> _ := list_compare _ (fun x y => lcompare (snd x) (snd y)).(* Lexicographic comparison *)

Definition tsort (t : tuple) :=
  let (i1, i2, i3) := t in
  match tvcompare i1 i2 with
  | Eq => (S0, t)
  | Lt => match tvcompare i2 i3 with
          | Eq => ( S0, t)
          | Lt => ( S1, t)
          | Gt => (* i1 < i2 > i3 *)
              match tvcompare i1 i3 with 
              | Eq => ( S0, t)
              | Lt => (Sm1, Tuple i1 i3 i2)
              | Gt => ( S1, Tuple i3 i1 i2) 
              end
          end
  | Gt => match tvcompare i1 i3 with 
          | Eq => ( S0, t)
          | Lt => (Sm1, Tuple i2 i1 i3)
          | Gt =>
              match tvcompare i2 i3 with (* i2 < i1 > i3 *)
              | Eq => ( S0, t)
              | Lt => ( S1, Tuple i2 i3 i1)
              | Gt => (Sm1, Tuple i3 i2 i1) 
              end
          end
  end.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).