(*********************************************)
(* This file is part of the 'Higman' contrib *)
(* file : inductive_wqo.v                    *)
(* contains : inductive definition of a      *)
(*  well quasi ordering                      *)
(* author : W.Delobel                        *)
(*********************************************)
Set Implicit Arguments.
Require Export List.
Section Wrap.
Variable A : Set.
Variable leA : A -> A -> Prop.
Variable leA_dec : forall a a', {leA a a'} + {~ leA a a'}.
(* leA a' a for some a' in second arg *)
Inductive greater : A -> list A -> Prop :=
| Gr0 : forall a a' w, leA a' a -> greater a (a'::w)
| Gr1 : forall a a' w, greater a w -> greater a (a'::w).
(* good sequence - leA a' a, a occurs in arg earlier than a' *)
Inductive good : list A -> Prop :=
| Gd0 : forall a w, greater a w -> good (a::w)
| Gd1 : forall a w, good w -> good (a::w).
Definition bad (l : list A) : Prop := ~ good l.

(* Specifications: *)
Lemma greater_dec : forall a l, {greater a l} + {~ greater a l}.
Lemma good_dec : forall l, {good l} + {bad l}.
Fixpoint bad_subsequence (l : list A) : list A :=
match l with
| nil => nil
| a :: l' => let bl := bad_subsequence l' in
match (greater_dec a bl) with
| left _ => bl
| right _ => a :: bl
end
end.
Lemma bad_subsequence_is_bad : forall l, bad (bad_subsequence l).
Inductive continues : list A -> list A -> Prop :=
| Ct0 : forall a l, ~ greater a l -> continues (a::l) l.