Fixpoint insert (l: list T) (x: T) {struct l}: nat * list T :=
      match l with
      | nil => (0, x :: nil)
      | h :: t =>
          if le x h
            then (1, x :: h :: t)
            else let (n, t') := insert t x in (S n, h :: t')
      end.Fixpoint insert_many (l l': list T) {struct l'}: nat * list T :=
      match l' with
      | nil => (0, l)
      | h :: t =>
          let (n, u) := insert l h in
          let (m, v) := insert_many u t in
            (n + m, v)
      end.Definition isort: list T -> (nat * list T) := insert_many nil.Definition isort: list T -> M (list T) := foldlM insert nil.

Lemma insert_length (x: T) (l: list T): length (run (insert l x)) = S (length l).