Debian Codegraph @

src/Data/List/Relation/Binary/Lex.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lexicographic ordering of lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Binary.Lex where
10
11 open import Data.Unit.Base using (⊤; tt)
12 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂; uncurry)
13 open import Data.List.Base using (List; []; _∷_)
14 open import Data.List.Relation.Binary.Pointwise.Base
15 using (Pointwise; []; _∷_; head; tail)
16 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_])
17 open import Function.Base using (_∘_; flip; id)
18 open import Function.Bundles using (_⇔_; mk⇔)
19 open import Level using (_⊔_)
20 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
21 open import Relation.Nullary.Decidable as Dec
22 using (Dec; yes; no; _×-dec_; _⊎-dec_)
23 open import Relation.Binary.Core using (Rel)
24 open import Relation.Binary.Structures using (IsEquivalence)
25 open import Relation.Binary.Definitions
26 using (Symmetric; Transitive; Irreflexive; Asymmetric; Antisymmetric
27 ; Decidable; _Respects₂_; _Respects_)
28
29
30 ------------------------------------------------------------------------
31 -- Re-exporting the core definitions and properties
32
33 open import Data.List.Relation.Binary.Lex.Core public
34
35 ------------------------------------------------------------------------
36 -- Properties
37
38 module _ {a ℓ₁ ℓ₂} {A : Set a} {P : Set}
39 {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
40
41 private
42 _≋_ = Pointwise _≈_
43 _<_ = Lex P _≈_ _≺_
44
45 ¬≤-this : ∀ {x y xs ys} → ¬ (x ≈ y) → ¬ (x ≺ y) →
46 ¬ (x ∷ xs) < (y ∷ ys)
47 ¬≤-this x≉y x≮y (this x≺y) = x≮y x≺y
48 ¬≤-this x≉y x≮y (next x≈y xs<ys) = x≉y x≈y
49
50 ¬≤-next : ∀ {x y xs ys} → ¬ x ≺ y → ¬ xs < ys →
51 ¬ (x ∷ xs) < (y ∷ ys)
52 ¬≤-next x≮y xs≮ys (this x≺y) = x≮y x≺y
53 ¬≤-next x≮y xs≮ys (next _ xs<ys) = xs≮ys xs<ys
54
55 antisymmetric : Symmetric _≈_ → Irreflexive _≈_ _≺_ →
56 Asymmetric _≺_ → Antisymmetric _≋_ _<_
57 antisymmetric sym ir asym = as
58 where
59 as : Antisymmetric _≋_ _<_
60 as (base _) (base _) = []
61 as (this x≺y) (this y≺x) = contradiction y≺x (asym x≺y)
62 as (this x≺y) (next y≈x ys<xs) = contradiction x≺y (ir (sym y≈x))
63 as (next x≈y xs<ys) (this y≺x) = contradiction y≺x (ir (sym x≈y))
64 as (next x≈y xs<ys) (next y≈x ys<xs) = x≈y ∷ as xs<ys ys<xs
65
66 toSum : ∀ {x y xs ys} → (x ∷ xs) < (y ∷ ys) → (x ≺ y ⊎ (x ≈ y × xs < ys))
67 toSum (this x≺y) = inj₁ x≺y
68 toSum (next x≈y xs<ys) = inj₂ (x≈y , xs<ys)
69
70 transitive : IsEquivalence _≈_ → _≺_ Respects₂ _≈_ → Transitive _≺_ →
71 Transitive _<_
72 transitive eq resp tr = trans
73 where
74 trans : Transitive (Lex P _≈_ _≺_)
75 trans (base p) (base _) = base p
76 trans (base y) halt = halt
77 trans halt (this y≺z) = halt
78 trans halt (next y≈z ys<zs) = halt
79 trans (this x≺y) (this y≺z) = this (tr x≺y y≺z)
80 trans (this x≺y) (next y≈z ys<zs) = this (proj₁ resp y≈z x≺y)
81 trans (next x≈y xs<ys) (this y≺z) =
82 this (proj₂ resp (IsEquivalence.sym eq x≈y) y≺z)
83 trans (next x≈y xs<ys) (next y≈z ys<zs) =
84 next (IsEquivalence.trans eq x≈y y≈z) (trans xs<ys ys<zs)
85
86 respects₂ : IsEquivalence _≈_ → _≺_ Respects₂ _≈_ → _<_ Respects₂ _≋_
87 respects₂ eq (resp₁ , resp₂) = resp¹ , resp²
88 where
89 open IsEquivalence eq using (sym; trans)
90 resp¹ : ∀ {xs} → Lex P _≈_ _≺_ xs Respects _≋_
91 resp¹ [] xs<[] = xs<[]
92 resp¹ (_ ∷ _) halt = halt
93 resp¹ (x≈y ∷ _) (this z≺x) = this (resp₁ x≈y z≺x)
94 resp¹ (x≈y ∷ xs≋ys) (next z≈x zs<xs) =
95 next (trans z≈x x≈y) (resp¹ xs≋ys zs<xs)
96
97 resp² : ∀ {ys} → flip (Lex P _≈_ _≺_) ys Respects _≋_
98 resp² [] []<ys = []<ys
99 resp² (x≈z ∷ _) (this x≺y) = this (resp₂ x≈z x≺y)
100 resp² (x≈z ∷ xs≋zs) (next x≈y xs<ys) =
101 next (trans (sym x≈z) x≈y) (resp² xs≋zs xs<ys)
102
103
104 []<[]-⇔ : P ⇔ [] < []
105 []<[]-⇔ = mk⇔ base (λ { (base p) → p })
106
107
108 ∷<∷-⇔ : ∀ {x y xs ys} → (x ≺ y ⊎ (x ≈ y × xs < ys)) ⇔ (x ∷ xs) < (y ∷ ys)
109 ∷<∷-⇔ = mk⇔ [ this , uncurry next ] toSum
110
111 module _ (dec-P : Dec P) (dec-≈ : Decidable _≈_) (dec-≺ : Decidable _≺_)
112 where
113
114 decidable : Decidable _<_
115 decidable [] [] = Dec.map []<[]-⇔ dec-P
116 decidable [] (y ∷ ys) = yes halt
117 decidable (x ∷ xs) [] = no λ()
118 decidable (x ∷ xs) (y ∷ ys) =
119 Dec.map ∷<∷-⇔ (dec-≺ x y ⊎-dec (dec-≈ x y ×-dec decidable xs ys))