Built with Alectryon, running Coq+SerAPI v8.10.0+0.7.0. Coq sources are in this panel; goals and messages will appear in the other. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑ Ctrl+↓ to navigate, Ctrl+🖱️ to focus.
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * INRIA, CNRS and contributors - Copyright 1999-2018 *)
(* <O___,, * (see CREDITS file for the list of authors) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
(* Made by Hugo Herbelin *)
This file defines two notions of sorted list:
The two notions are equivalent if the order is transitive.
- a list is locally sorted if any element is smaller or equal than its successor in the list
- a list is sorted if any element coming before another one is smaller or equal than this other element
Require Import List Relations Relations_1. (* Set Universe Polymorphism. *)
Preambule
Set Implicit Arguments. Local Notation "[ ]" := nil (at level 0). Local Notation "[ a ; .. ; b ]" := (a :: .. (b :: []) ..) (at level 0). Arguments Transitive [U] R. Section defs. Variable A : Type. Variable R : A -> A -> Prop.
Locally sorted: consecutive elements of the list are ordered
Inductive LocallySorted : list A -> Prop := | LSorted_nil : LocallySorted [] | LSorted_cons1 a : LocallySorted [a] | LSorted_consn a b l : LocallySorted (b :: l) -> R a b -> LocallySorted (a :: b :: l).
Alternative two-step definition of being locally sorted
Inductive HdRel a : list A -> Prop := | HdRel_nil : HdRel a [] | HdRel_cons b l : R a b -> HdRel a (b :: l). Inductive Sorted : list A -> Prop := | Sorted_nil : Sorted [] | Sorted_cons a l : Sorted l -> HdRel a l -> Sorted (a :: l).A:TypeR:A -> A -> Propforall (a b : A) (l : list A), HdRel a (b :: l) -> R a binversion 1; auto. Qed.A:TypeR:A -> A -> Propforall (a b : A) (l : list A), HdRel a (b :: l) -> R a bA:TypeR:A -> A -> Propforall (a : A) (l : list A), Sorted (a :: l) -> Sorted l /\ HdRel a lintros a l H; inversion H; auto. Qed.A:TypeR:A -> A -> Propforall (a : A) (l : list A), Sorted (a :: l) -> Sorted l /\ HdRel a lA:TypeR:A -> A -> Propforall P : list A -> Type, P [ ] -> (forall (a : A) (l : list A), Sorted l -> P l -> HdRel a l -> P (a :: l)) -> forall l : list A, Sorted l -> P lA:TypeR:A -> A -> Propforall P : list A -> Type, P [ ] -> (forall (a : A) (l : list A), Sorted l -> P l -> HdRel a l -> P (a :: l)) -> forall l : list A, Sorted l -> P lA:TypeR:A -> A -> PropP:list A -> TypeX:P [ ]X0:forall (a : A) (l : list A), Sorted l -> P l -> HdRel a l -> P (a :: l)Sorted [ ] -> P [ ]A:TypeR:A -> A -> PropP:list A -> TypeX:P [ ]X0:forall (a0 : A) (l0 : list A), Sorted l0 -> P l0 -> HdRel a0 l0 -> P (a0 :: l0)a:Al:list AIHl:Sorted l -> P lSorted (a :: l) -> P (a :: l)firstorder using Sorted_inv. Qed.A:TypeR:A -> A -> PropP:list A -> TypeX:P [ ]X0:forall (a0 : A) (l0 : list A), Sorted l0 -> P l0 -> HdRel a0 l0 -> P (a0 :: l0)a:Al:list AIHl:Sorted l -> P lSorted (a :: l) -> P (a :: l)A:TypeR:A -> A -> Propforall l : list A, Sorted l <-> LocallySorted lA:TypeR:A -> A -> Propforall l : list A, Sorted l <-> LocallySorted linversion H1; subst; auto using LocallySorted. Qed.A:TypeR:A -> A -> Propa:Al:list Aa0:Al0:list AH:Sorted l0H0:HdRel a0 l0H1:HdRel a (a0 :: l0)IHSorted:LocallySorted (a0 :: l0)LocallySorted (a :: a0 :: l0)
Strongly sorted: elements of the list are pairwise ordered
Inductive StronglySorted : list A -> Prop := | SSorted_nil : StronglySorted [] | SSorted_cons a l : StronglySorted l -> Forall (R a) l -> StronglySorted (a :: l).A:TypeR:A -> A -> Propforall (a : A) (l : list A), StronglySorted (a :: l) -> StronglySorted l /\ Forall (R a) lintros; inversion H; auto. Defined.A:TypeR:A -> A -> Propforall (a : A) (l : list A), StronglySorted (a :: l) -> StronglySorted l /\ Forall (R a) lA:TypeR:A -> A -> Propforall P : list A -> Type, P [ ] -> (forall (a : A) (l : list A), StronglySorted l -> P l -> Forall (R a) l -> P (a :: l)) -> forall l : list A, StronglySorted l -> P linduction l; firstorder using StronglySorted_inv. Defined.A:TypeR:A -> A -> Propforall P : list A -> Type, P [ ] -> (forall (a : A) (l : list A), StronglySorted l -> P l -> Forall (R a) l -> P (a :: l)) -> forall l : list A, StronglySorted l -> P lA:TypeR:A -> A -> Propforall P : list A -> Type, P [ ] -> (forall (a : A) (l : list A), StronglySorted l -> P l -> Forall (R a) l -> P (a :: l)) -> forall l : list A, StronglySorted l -> P lfirstorder using StronglySorted_rect. Qed.A:TypeR:A -> A -> Propforall P : list A -> Type, P [ ] -> (forall (a : A) (l : list A), StronglySorted l -> P l -> Forall (R a) l -> P (a :: l)) -> forall l : list A, StronglySorted l -> P lA:TypeR:A -> A -> Propforall l : list A, StronglySorted l -> Sorted lA:TypeR:A -> A -> Propforall l : list A, StronglySorted l -> Sorted ldestruct HForall; constructor; trivial. Qed.A:TypeR:A -> A -> Propa:Al:list AH:StronglySorted lHForall:Forall (R a) lIHStronglySorted:Sorted lHdRel a lA:TypeR:A -> A -> PropTransitive R -> forall (a : A) (l : list A), Sorted (a :: l) -> Forall (R a) lA:TypeR:A -> A -> PropTransitive R -> forall (a : A) (l : list A), Sorted (a :: l) -> Forall (R a) lA:TypeR:A -> A -> PropH:Transitive Ra:Al:list AH0:Sorted (a :: l)Forall (R a) lA:TypeR:A -> A -> PropH:Transitive Ra:Al:list AH0:Sorted (a :: l)match a :: l with | [ ] => True | a0 :: l0 => Forall (R a0) l0 endA:TypeR:A -> A -> PropH:Transitive Ra:Al:list Aa0:Al0:list AH0:Sorted l0H1:HdRel a0 l0IHSorted:match l0 with | [ ] => True | a1 :: l1 => Forall (R a1) l1 endForall (R a0) l0A:TypeR:A -> A -> PropH:Transitive Ra:Al:list Aa0, b:Al0:list AH0:Sorted (b :: l0)H1:R a0 bIHSorted:Forall (R b) l0Forall (R a0) l0firstorder. Qed.A:TypeR:A -> A -> PropH:Transitive Ra:Al:list Aa0, b:Al0:list AH0:Sorted (b :: l0)H1:R a0 bIHSorted:Forall (R b) l0forall a1 : A, R b a1 -> R a0 a1A:TypeR:A -> A -> PropTransitive R -> forall l : list A, Sorted l -> StronglySorted lA:TypeR:A -> A -> PropTransitive R -> forall l : list A, Sorted l -> StronglySorted lA:TypeR:A -> A -> PropH:Transitive Ra:Al:list AH0:Sorted lH1:HdRel a lIHSorted:StronglySorted lForall (R a) lconstructor; trivial. Qed. End defs. Hint Constructors HdRel : core. Hint Constructors Sorted : core. (* begin hide *) (* Compatibility with deprecated file Sorting.v *) Notation lelistA := HdRel (only parsing). Notation nil_leA := HdRel_nil (only parsing). Notation cons_leA := HdRel_cons (only parsing). Notation sort := Sorted (only parsing). Notation nil_sort := Sorted_nil (only parsing). Notation cons_sort := Sorted_cons (only parsing). Notation lelistA_inv := HdRel_inv (only parsing). Notation sort_inv := Sorted_inv (only parsing). Notation sort_rect := Sorted_rect (only parsing). (* end hide *)A:TypeR:A -> A -> PropH:Transitive Ra:Al:list AH0:Sorted lH1:HdRel a lIHSorted:StronglySorted lSorted (a :: l)