------------------------------------------------------------------------ -- The Agda standard library -- -- Lexicographic ordering of lists of same-length vectors ------------------------------------------------------------------------ -- The definitions of lexicographic ordering used here are suitable if -- the argument order is a strict partial order. {-# OPTIONS --cubical-compatible --safe #-} module Data.Vec.Relation.Binary.Lex.Strict where open import Data.Empty using (⊥; ⊥-elim) open import Data.Unit.Base using (⊤; tt) open import Data.Unit.Properties using (⊤-irrelevant) open import Data.Nat.Base using (ℕ; suc) open import Data.Product.Base using (_×_; _,_; proj₁; proj₂) open import Data.Product.Relation.Binary.Lex.Strict open import Data.Sum.Base using (inj₁; inj₂) open import Data.Vec.Base using (Vec; []; _∷_; uncons) open import Data.Vec.Relation.Binary.Pointwise.Inductive as Pointwise using (Pointwise; []; _∷_; head; tail) open import Function.Base using (id; _on_; _∘_) open import Induction.WellFounded open import Level using (Level; _⊔_) open import Relation.Binary.Core using (REL; Rel; _⇒_) open import Relation.Binary.Bundles using (Poset; StrictPartialOrder; DecPoset; DecStrictPartialOrder ; DecTotalOrder; StrictTotalOrder; Preorder; TotalOrder) open import Relation.Binary.Structures using (IsEquivalence; IsPartialOrder; IsStrictPartialOrder; IsDecPartialOrder ; IsDecStrictPartialOrder; IsDecTotalOrder; IsStrictTotalOrder ; IsPreorder; IsTotalOrder; IsPartialEquivalence) open import Relation.Binary.Definitions using (Irreflexive; _Respects₂_; _Respectsˡ_; _Respectsʳ_; Antisymmetric ; Asymmetric; Symmetric; Trans; Decidable; Total; Trichotomous ; Transitive; Irrelevant; tri≈; tri>; tri<) open import Relation.Binary.Consequences using (asym⇒irr) open import Relation.Binary.Construct.On as On using (wellFounded) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) open import Relation.Nullary.Decidable.Core using (yes; no) open import Relation.Nullary.Negation.Core using (¬_; contradiction) private variable a ℓ₁ ℓ₂ : Level A : Set a ------------------------------------------------------------------------ -- Re-exports ------------------------------------------------------------------------ open import Data.Vec.Relation.Binary.Lex.Core as Core public using (base; this; next; ≰-this; ≰-next) ------------------------------------------------------------------------ -- Definitions ------------------------------------------------------------------------ module _ {A : Set a} (_≈_ : Rel A ℓ₁) (_≺_ : Rel A ℓ₂) where Lex-< : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂) Lex-< = Core.Lex {A = A} ⊥ _≈_ _≺_ Lex-≤ : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂) Lex-≤ = Core.Lex {A = A} ⊤ _≈_ _≺_ ------------------------------------------------------------------------ -- Properties of Lex-< ------------------------------------------------------------------------ module _ {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where private _≋_ = Pointwise _≈_ _<_ = Lex-< _≈_ _≺_ xs≮[] : ∀ {n} {xs : Vec A n} → ¬ xs < [] xs≮[] (base ()) ¬[]<[] : ¬ [] < [] ¬[]<[] = xs≮[] module _ (≺-irrefl : Irreflexive _≈_ _≺_) where <-irrefl : ∀ {m n} → Irreflexive (_≋_ {m} {n}) (_<_ {m} {n}) <-irrefl [] (base ()) <-irrefl (x≈y ∷ xs≋ys) (this x≺y m≡n) = ≺-irrefl x≈y x≺y <-irrefl (x≈y ∷ xs≋ys) (next _ xs x⊀y x≉y x≻y = tri> (≰-this x≉y x⊀y) (x≉y ∘ head) (this x≻y refl) ... | tri≈ x⊀y x≈y x⊁y with <-cmp xs ys ... | tri< xs xs≮ys xs≋̸ys xs>ys = tri> (≰-next x⊀y xs≮ys) (xs≋̸ys ∘ tail) (next (≈-sym x≈y) xs>ys) <-decidable : Decidable _≈_ → Decidable _≺_ → ∀ {m n} → Decidable (_<_ {m} {n}) <-decidable = Core.decidable (no id) <-respectsˡ : IsPartialEquivalence _≈_ → _≺_ Respectsˡ _≈_ → ∀ {m n} → _Respectsˡ_ (_<_ {m} {n}) _≋_ <-respectsˡ = Core.respectsˡ <-respectsʳ : IsPartialEquivalence _≈_ → _≺_ Respectsʳ _≈_ → ∀ {m n} → _Respectsʳ_ (_<_ {m} {n}) _≋_ <-respectsʳ = Core.respectsʳ <-respects₂ : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ → ∀ {n} → _Respects₂_ (_<_ {n} {n}) _≋_ <-respects₂ = Core.respects₂ <-irrelevant : Irrelevant _≈_ → Irrelevant _≺_ → Irreflexive _≈_ _≺_ → ∀ {m n} → Irrelevant (_<_ {m} {n}) <-irrelevant = Core.irrelevant (λ ()) module _ (≈-trans : Transitive _≈_) (≺-respʳ : _≺_ Respectsʳ _≈_ ) (≺-wf : WellFounded _≺_) where <-wellFounded : ∀ {n} → WellFounded (_<_ {n}) <-wellFounded {0} [] = acc λ ys<[] → contradiction ys<[] xs≮[] <-wellFounded {suc n} xs = Subrelation.wellFounded <⇒uncons-Lex uncons-Lex-wellFounded xs where <⇒uncons-Lex : {xs ys : Vec A (suc n)} → xs < ys → (×-Lex _≈_ _≺_ _<_ on uncons) xs ys <⇒uncons-Lex {x ∷ xs} {y ∷ ys} (this x _ _ x≻y = inj₂ (this x≻y refl) ... | tri≈ _ x≈y _ with ≤-total xs ys ... | inj₁ xsys = inj₂ (next (≈-sym x≈y) xs>ys) ≤-dec : Decidable _≈_ → Decidable _≺_ → ∀ {m n} → Decidable (_≤_ {m} {n}) ≤-dec = Core.decidable (yes tt) ≤-irrelevant : Irrelevant _≈_ → Irrelevant _≺_ → Irreflexive _≈_ _≺_ → ∀ {m n} → Irrelevant (_≤_ {m} {n}) ≤-irrelevant = Core.irrelevant ⊤-irrelevant ------------------------------------------------------------------------ -- Structures ≤-isPreorder : IsEquivalence _≈_ → Transitive _≺_ → _≺_ Respects₂ _≈_ → ∀ {n} → IsPreorder (_≋_ {n} {n}) _≤_ ≤-isPreorder ≈-equiv ≺-trans ≺-resp-≈ {n} = record { isEquivalence = Pointwise.isEquivalence ≈-equiv n ; reflexive = ≤-refl ; trans = ≤-trans (IsEquivalence.isPartialEquivalence ≈-equiv) ≺-resp-≈ ≺-trans } ≤-isPartialOrder : IsStrictPartialOrder _≈_ _≺_ → ∀ {n} → IsPartialOrder (_≋_ {n} {n}) _≤_ ≤-isPartialOrder ≺-isStrictPartialOrder = record { isPreorder = ≤-isPreorder isEquivalence trans <-resp-≈ ; antisym = ≤-antisym Eq.sym irrefl asym } where open IsStrictPartialOrder ≺-isStrictPartialOrder ≤-isDecPartialOrder : IsDecStrictPartialOrder _≈_ _≺_ → ∀ {n} → IsDecPartialOrder (_≋_ {n} {n}) _≤_ ≤-isDecPartialOrder ≺-isDecStrictPartialOrder = record { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder ; _≟_ = Pointwise.decidable _≟_ ; _≤?_ = ≤-dec _≟_ _