| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Lexicographic ordering of lists |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- The definitions of lexicographic orderings used here is suitable if |
| 8 |
-- the argument order is a (non-strict) partial order. |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
module Data.List.Relation.Binary.Lex.NonStrict where |
| 13 |
|
| 14 |
open import Data.Empty using (⊥) |
| 15 |
open import Data.Unit.Base using (⊤; tt) |
| 16 |
open import Data.List.Base using (List; []; _∷_) |
| 17 |
open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise; []) |
| 18 |
open import Function.Base using (const; id) |
| 19 |
import Data.List.Relation.Binary.Lex.Strict as Strict |
| 20 |
open import Level using (Level; _⊔_) |
| 21 |
open import Relation.Nullary.Negation.Core using (¬_) |
| 22 |
open import Relation.Nullary using (yes; no) |
| 23 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 24 |
open import Relation.Binary.Bundles |
| 25 |
open import Relation.Binary.Structures |
| 26 |
using (IsEquivalence; IsPartialOrder; IsStrictPartialOrder; IsTotalOrder |
| 27 |
; IsStrictTotalOrder; IsPreorder; IsDecTotalOrder) |
| 28 |
open import Relation.Binary.Definitions |
| 29 |
using (Irreflexive; _Respects₂_; Antisymmetric; Asymmetric; Symmetric |
| 30 |
; Transitive; Decidable; Total; Trichotomous) |
| 31 |
import Relation.Binary.Construct.NonStrictToStrict as Conv |
| 32 |
|
| 33 |
import Data.List.Relation.Binary.Lex as Core |
| 34 |
|
| 35 |
------------------------------------------------------------------------ |
| 36 |
-- Publically re-export definitions from Core |
| 37 |
|
| 38 |
open Core public |
| 39 |
using (base; halt; this; next; ²-this; ²-next) |
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Strict lexicographic ordering. |
| 43 |
|
| 44 |
module _ {a ℓ₁ ℓ₂} {A : Set a} where |
| 45 |
|
| 46 |
Lex-< : (_≈_ : Rel A ℓ₁) (_≼_ : Rel A ℓ₂) → Rel (List A) (a ⊔ ℓ₁ ⊔ ℓ₂) |
| 47 |
Lex-< _≈_ _≼_ = Core.Lex ⊥ _≈_ (Conv._<_ _≈_ _≼_) |
| 48 |
|
| 49 |
-- Properties |
| 50 |
|
| 51 |
module _ {_≈_ : Rel A ℓ₁} {_≼_ : Rel A ℓ₂} where |
| 52 |
|
| 53 |
private |
| 54 |
_≋_ = Pointwise _≈_ |
| 55 |
_<_ = Lex-< _≈_ _≼_ |
| 56 |
|
| 57 |
<-irreflexive : Irreflexive _≋_ _<_ |
| 58 |
<-irreflexive = Strict.<-irreflexive (Conv.<-irrefl _≈_ _≼_) |
| 59 |
|
| 60 |
<-asymmetric : IsEquivalence _≈_ → _≼_ Respects₂ _≈_ → |
| 61 |
Antisymmetric _≈_ _≼_ → Asymmetric _<_ |
| 62 |
<-asymmetric eq resp antisym = |
| 63 |
Strict.<-asymmetric sym (Conv.<-resp-≈ _ _ eq resp) |
| 64 |
(Conv.<-asym _≈_ _ antisym) |
| 65 |
where open IsEquivalence eq |
| 66 |
|
| 67 |
<-antisymmetric : Symmetric _≈_ → Antisymmetric _≈_ _≼_ → |
| 68 |
Antisymmetric _≋_ _<_ |
| 69 |
<-antisymmetric sym antisym = |
| 70 |
Core.antisymmetric sym |
| 71 |
(Conv.<-irrefl _≈_ _≼_) |
| 72 |
(Conv.<-asym _ _≼_ antisym) |
| 73 |
|
| 74 |
<-transitive : IsPartialOrder _≈_ _≼_ → Transitive _<_ |
| 75 |
<-transitive po = |
| 76 |
Core.transitive isEquivalence |
| 77 |
(Conv.<-resp-≈ _ _ isEquivalence ≤-resp-≈) |
| 78 |
(Conv.<-trans _ _≼_ po) |
| 79 |
where open IsPartialOrder po |
| 80 |
|
| 81 |
<-resp₂ : IsEquivalence _≈_ → _≼_ Respects₂ _≈_ → _<_ Respects₂ _≋_ |
| 82 |
<-resp₂ eq resp = Core.respects₂ eq (Conv.<-resp-≈ _ _ eq resp) |
| 83 |
|
| 84 |
<-compare : Symmetric _≈_ → Decidable _≈_ → Antisymmetric _≈_ _≼_ → |
| 85 |
Total _≼_ → Trichotomous _≋_ _<_ |
| 86 |
<-compare sym _≟_ antisym tot = |
| 87 |
Strict.<-compare sym (Conv.<-trichotomous _ _ sym _≟_ antisym tot) |
| 88 |
|
| 89 |
<-decidable : Decidable _≈_ → Decidable _≼_ → Decidable _<_ |
| 90 |
<-decidable _≟_ _≼?_ = |
| 91 |
Core.decidable (no id) _≟_ (Conv.<-decidable _ _ _≟_ _≼?_) |
| 92 |
|
| 93 |
<-isStrictPartialOrder : IsPartialOrder _≈_ _≼_ → |
| 94 |
IsStrictPartialOrder _≋_ _<_ |
| 95 |
<-isStrictPartialOrder po = |
| 96 |
Strict.<-isStrictPartialOrder |
| 97 |
(Conv.<-isStrictPartialOrder _ _ po) |
| 98 |
|
| 99 |
<-isStrictTotalOrder : Decidable _≈_ → IsTotalOrder _≈_ _≼_ → |
| 100 |
IsStrictTotalOrder _≋_ _<_ |
| 101 |
<-isStrictTotalOrder dec tot = |
| 102 |
Strict.<-isStrictTotalOrder |
| 103 |
(Conv.<-isStrictTotalOrder₁ _ _ dec tot) |
| 104 |
|
| 105 |
<-strictPartialOrder : ∀ {a ℓ₁ ℓ₂} → Poset a ℓ₁ ℓ₂ → |
| 106 |
StrictPartialOrder _ _ _ |
| 107 |
<-strictPartialOrder po = record |
| 108 |
{ isStrictPartialOrder = <-isStrictPartialOrder isPartialOrder |
| 109 |
} where open Poset po |
| 110 |
|
| 111 |
<-strictTotalOrder : ∀ {a ℓ₁ ℓ₂} → DecTotalOrder a ℓ₁ ℓ₂ → |
| 112 |
StrictTotalOrder _ _ _ |
| 113 |
<-strictTotalOrder dtot = record |
| 114 |
{ isStrictTotalOrder = <-isStrictTotalOrder _≟_ isTotalOrder |
| 115 |
} where open DecTotalOrder dtot |
| 116 |
|
| 117 |
------------------------------------------------------------------------ |
| 118 |
-- Non-strict lexicographic ordering. |
| 119 |
|
| 120 |
module _ {a ℓ₁ ℓ₂} {A : Set a} where |
| 121 |
|
| 122 |
Lex-≤ : (_≈_ : Rel A ℓ₁) (_≼_ : Rel A ℓ₂) → Rel (List A) (a ⊔ ℓ₁ ⊔ ℓ₂) |
| 123 |
Lex-≤ _≈_ _≼_ = Core.Lex ⊤ _≈_ (Conv._<_ _≈_ _≼_) |
| 124 |
|
| 125 |
≤-reflexive : ∀ _≈_ _≼_ → Pointwise _≈_ ⇒ Lex-≤ _≈_ _≼_ |
| 126 |
≤-reflexive _≈_ _≼_ = Strict.≤-reflexive _≈_ (Conv._<_ _≈_ _≼_) |
| 127 |
|
| 128 |
-- Properties |
| 129 |
module _ {_≈_ : Rel A ℓ₁} {_≼_ : Rel A ℓ₂} where |
| 130 |
|
| 131 |
private |
| 132 |
_≋_ = Pointwise _≈_ |
| 133 |
_≤_ = Lex-≤ _≈_ _≼_ |
| 134 |
|
| 135 |
≤-antisymmetric : Symmetric _≈_ → Antisymmetric _≈_ _≼_ → |
| 136 |
Antisymmetric _≋_ _≤_ |
| 137 |
≤-antisymmetric sym antisym = |
| 138 |
Core.antisymmetric sym |
| 139 |
(Conv.<-irrefl _≈_ _≼_) |
| 140 |
(Conv.<-asym _ _≼_ antisym) |
| 141 |
|
| 142 |
≤-transitive : IsPartialOrder _≈_ _≼_ → Transitive _≤_ |
| 143 |
≤-transitive po = |
| 144 |
Core.transitive isEquivalence |
| 145 |
(Conv.<-resp-≈ _ _ isEquivalence ≤-resp-≈) |
| 146 |
(Conv.<-trans _ _≼_ po) |
| 147 |
where open IsPartialOrder po |
| 148 |
|
| 149 |
≤-resp₂ : IsEquivalence _≈_ → _≼_ Respects₂ _≈_ → _≤_ Respects₂ _≋_ |
| 150 |
≤-resp₂ eq resp = Core.respects₂ eq (Conv.<-resp-≈ _ _ eq resp) |
| 151 |
|
| 152 |
≤-decidable : Decidable _≈_ → Decidable _≼_ → Decidable _≤_ |
| 153 |
≤-decidable _≟_ _≼?_ = |
| 154 |
Core.decidable (yes tt) _≟_ (Conv.<-decidable _ _ _≟_ _≼?_) |
| 155 |
|
| 156 |
≤-total : Symmetric _≈_ → Decidable _≈_ → Antisymmetric _≈_ _≼_ → |
| 157 |
Total _≼_ → Total _≤_ |
| 158 |
≤-total sym dec-≈ antisym tot = |
| 159 |
Strict.≤-total sym (Conv.<-trichotomous _ _ sym dec-≈ antisym tot) |
| 160 |
|
| 161 |
≤-isPreorder : IsPartialOrder _≈_ _≼_ → IsPreorder _≋_ _≤_ |
| 162 |
≤-isPreorder po = |
| 163 |
Strict.≤-isPreorder |
| 164 |
isEquivalence (Conv.<-trans _ _ po) |
| 165 |
(Conv.<-resp-≈ _ _ isEquivalence ≤-resp-≈) |
| 166 |
where open IsPartialOrder po |
| 167 |
|
| 168 |
≤-isPartialOrder : IsPartialOrder _≈_ _≼_ → IsPartialOrder _≋_ _≤_ |
| 169 |
≤-isPartialOrder po = |
| 170 |
Strict.≤-isPartialOrder |
| 171 |
(Conv.<-isStrictPartialOrder _ _ po) |
| 172 |
|
| 173 |
≤-isTotalOrder : Decidable _≈_ → IsTotalOrder _≈_ _≼_ → |
| 174 |
IsTotalOrder _≋_ _≤_ |
| 175 |
≤-isTotalOrder dec tot = |
| 176 |
Strict.≤-isTotalOrder |
| 177 |
(Conv.<-isStrictTotalOrder₁ _ _ dec tot) |
| 178 |
|
| 179 |
≤-isDecTotalOrder : IsDecTotalOrder _≈_ _≼_ → |
| 180 |
IsDecTotalOrder _≋_ _≤_ |
| 181 |
≤-isDecTotalOrder dtot = |
| 182 |
Strict.≤-isDecTotalOrder |
| 183 |
(Conv.<-isStrictTotalOrder₂ _ _ dtot) |
| 184 |
|
| 185 |
≤-preorder : ∀ {a ℓ₁ ℓ₂} → Poset a ℓ₁ ℓ₂ → Preorder _ _ _ |
| 186 |
≤-preorder po = record |
| 187 |
{ isPreorder = ≤-isPreorder isPartialOrder |
| 188 |
} where open Poset po |
| 189 |
|
| 190 |
≤-partialOrder : ∀ {a ℓ₁ ℓ₂} → Poset a ℓ₁ ℓ₂ → Poset _ _ _ |
| 191 |
≤-partialOrder po = record |
| 192 |
{ isPartialOrder = ≤-isPartialOrder isPartialOrder |
| 193 |
} where open Poset po |
| 194 |
|
| 195 |
≤-decTotalOrder : ∀ {a ℓ₁ ℓ₂} → DecTotalOrder a ℓ₁ ℓ₂ → |
| 196 |
DecTotalOrder _ _ _ |
| 197 |
≤-decTotalOrder dtot = record |
| 198 |
{ isDecTotalOrder = ≤-isDecTotalOrder isDecTotalOrder |
| 199 |
} where open DecTotalOrder dtot |