Search code examples
sortingcoqproofinduction

Lemma about Sortedness of concatenated lists


I have the following inductive definition for sortedness of a list:


Class DecTotalOrder (A : Type) := {
  leb : A -> A -> bool;
  leb_total_dec : forall x y, {leb x y}+{leb y x};
  leb_antisym : forall x y, leb x y -> leb y x -> x = y;
  leb_trans : forall x y z, leb x y -> leb y z -> leb x z }.

Inductive Sorted {A} {dto : DecTotalOrder A} : list A -> Prop :=
| Sorted_0 : Sorted []
| Sorted_1 : forall x, Sorted [x]
| Sorted_2 : forall x y, leb x y ->
                         forall l, Sorted (y :: l) ->
                                   Sorted (x :: y :: l).

And the following two definitions to declare that an element x is smaller or equal than each element of the list (LeLst) and bigger or equal than each element of the list (LstLe) :

Definition LeLst {A} {dto : DecTotalOrder A} (x : A) (l : list A) :=
  List.Forall (leb x) l.

Definition LstLe {A} {dto : DecTotalOrder A} (x : A) (l : list A) := 
   List.Forall (fun y => leb y x) l.

I am trying to prove the following lemma about sortedness which basically states that if we know that h is greater or equal to each element in l and h is smaller or equal than each element in l' we can put it in between the two:

Lemma lem_lstle_lelst {A} {dto: DecTotalOrder A} : forall h l l',
LstLe h l -> LeLst h l' -> Sorted (l ++ h :: l').

It seems very intuitiv but i get stuck every time in the proof. This is my current attempt:

Lemma lem_lstle_lelst {A} {dto: DecTotalOrder A} : forall h l l',
LstLe h l -> LeLst h l' -> Sorted (l ++ h :: l').
Proof.
intros h l l' H_LstLe.
induction H_LstLe.
- intros. simpl. Search (Sorted (_ :: _)).
  unfold LeLst in H. Search (List.Forall _ _).
  induction l'.
  + constructor.
  + Search (List.Forall _ _). 
    constructor. 
      { hauto use: List.Forall_inv. }
      { generalize (List.Forall_inv_tail H).
        intros.
        generalize (List.Forall_inv H).
        intros. 
        generalize (IHl' H0).
        intros.
        generalize (lem_sorted_tail H2).
        intros.

However I get stuck here, because the hypotheses just don't seem strong enough:

1 subgoal
A : Type
dto : DecTotalOrder A
h, a : A
l' : list A
H : List.Forall (fun x : A => leb h x) (a :: l')
IHl' : List.Forall (fun x : A => leb h x) l' -> Sorted (h :: l')
H0 : List.Forall (fun x : A => leb h x) l'
H1 : leb h a
H2 : Sorted (h :: l')
H3 : Sorted l'
______________________________________(1/1)
Sorted (a :: l')

I'd be really glad if someone could give me a hint, maybe something is wrong with my definitions and that is why i can't get on with the proof? Or am I just missing out on some tactics that I could use?

Here is a list of lemmata allready proven about sortedness:

Lemma lem_sorted_tail {A} {dto : DecTotalOrder A}{l x} :
  Sorted (x :: l) -> Sorted l.

Lemma lem_sorted_prepend {A} {dto: DecTotalOrder A} : forall x l l',
  Sorted((x :: l) ++ l') -> Sorted(l ++ l').

Lemma lem_sort_conc_mid {A} {dto: DecTotalOrder A} : forall x y l, 
  Sorted (x :: y :: l) -> Sorted (x :: l).


Solution

  • As stated in a comment the Lemma is not provable. Instead its defintion has to be expanded by adding properties about the sortedness of l and l':

    Lemma lem_lstle_lelst {A} {dto: DecTotalOrder A} : forall h l l',
    LstLe h l -> LeLst h l' -> Sorted l -> Sorted l' -> Sorted (l ++ h :: l').
    

    This is possible to prove with the following:

    Proof.
    intros h l l' H_Lstle_h_l.
    induction H_Lstle_h_l.
    - intros H_Lelst_h_l' H_Sort_1 H_Sort_2.
      simpl;inversion H_Lelst_h_l';sauto.
    - intros H_Lelst_h_l' H_Sort_1 H_Sort_2.
      generalize (lem_sorted_tail H_Sort_1).
      intros H_Sort_l.
      generalize (IHH_Lstle_h_l H_Lelst_h_l' H_Sort_l H_Sort_2).
      intros H_Sort_l_h_l'.
      generalize (lem_sorted_lelst x l H_Sort_1).
      intros H_Lelst_x_l.
      hauto use: lem_Sorted_prepend_inv.
    Qed. 
    
    

    introducing new helper lemmata:

    Lemma lem_Sorted_prepend_inv {A} {dto: DecTotalOrder A} : 
      forall x h l l', leb x h ->  Sorted(l ++ h :: l') -> LeLst x l -> Sorted(x::l++ h::l').
    
    Lemma lem_sorted_lelst {A} {dto: DecTotalOrder A} :
      forall x l, Sorted(x :: l) -> LeLst x l.