(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU Lesser General Public License for more details.                *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)
(***********************************************************************)
(*    Proof of Huffman algorithm: ISort.v                              *)
(*                                                                     *)
(*    Definition of sorting by insertion and its proof of correctness  *)
(*                                                                     *)
(*    Definitions: isort, insert                                       *)
(*                                                                     *)
(*                                    Laurent.Thery@inria.fr (2003)    *)
(***********************************************************************)
Section ISortExample.
Variable A : Type.
Variable order : A -> A -> Prop.
Variable order_fun : A -> A -> bool.
Hypothesis order_fun_true : forall a b : A, order_fun a b = true -> order a b.
Hypothesis order_fun_false : forall a b : A, order_fun a b = false -> order b a.
(* Insert an element *)
Fixpoint insert (a : A) (l : list A) {struct l} : list A :=
match l with
| nil => a :: nil
| b :: l1 =>
match order_fun a b with
| true => a :: l
| false => b :: insert a l1
end
end.

(* Specifications: *)
(* Inserting preserves ordering *)
Theorem insert_ordered :
forall l : list A,
ordered order l -> forall a : A, ordered order (insert a l).