Debian Codegraph @

src/Data/Vec/Relation/Binary/Lex/Core.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lexicographic ordering of same-length vectors
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Vec.Relation.Binary.Lex.Core {a} {A : Set a} where
10
11 open import Data.Nat.Base using (ℕ; suc)
12 import Data.Nat.Properties as ℕ using (_≟_; ≡-irrelevant)
13 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂; uncurry)
14 open import Data.Vec.Base using (Vec; []; _∷_)
15 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_])
16 open import Data.Vec.Relation.Binary.Pointwise.Inductive using (Pointwise; []; _∷_)
17 open import Function.Base using (flip)
18 open import Function.Bundles using (_⇔_; mk⇔)
19 open import Level using (Level; _⊔_)
20 open import Relation.Binary.Core using (Rel; REL)
21 open import Relation.Binary.Definitions
22 using (Transitive; Symmetric; Asymmetric; Antisymmetric; Irreflexive; Trans
23 ; _Respects₂_; _Respectsˡ_; _Respectsʳ_; Decidable; Irrelevant)
24 open import Relation.Binary.Structures using (IsPartialEquivalence)
25 open import Relation.Binary.PropositionalEquality.Core as ≡
26 using (_≡_; refl; cong)
27 import Relation.Nullary as Nullary
28 open import Relation.Nullary.Decidable as Dec using (Dec; yes; no; _×-dec_; _⊎-dec_)
29 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
30
31 private
32 variable
33 ℓ₁ ℓ₂ : Level
34
35 ------------------------------------------------------------------------
36 -- Definition
37
38 -- The lexicographic ordering itself can be either strict or non-strict,
39 -- depending on whether type P is inhabited.
40
41 data Lex {A : Set a} (P : Set) (_≈_ : Rel A ℓ₁) (_≺_ : Rel A ℓ₂)
42 : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂) where
43 base : (p : P) → Lex P _≈_ _≺_ [] []
44 this : ∀ {x y m n} {xs : Vec A m} {ys : Vec A n}
45 (x≺y : x ≺ y) (m≡n : m ≡ n) → Lex P _≈_ _≺_ (x ∷ xs) (y ∷ ys)
46 next : ∀ {x y m n} {xs : Vec A m} {ys : Vec A n}
47 (x≈y : x ≈ y) (xs<ys : Lex P _≈_ _≺_ xs ys) → Lex P _≈_ _≺_ (x ∷ xs) (y ∷ ys)
48
49 ------------------------------------------------------------------------
50 -- Operations
51
52 map-P : ∀ {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} {P₁ P₂ : Set} → (P₁ → P₂) →
53 ∀ {m n} {xs : Vec A m} {ys : Vec A n} →
54 Lex P₁ _≈_ _≺_ xs ys → Lex P₂ _≈_ _≺_ xs ys
55 map-P f (base p) = base (f p)
56 map-P f (this x≺y m≡n) = this x≺y m≡n
57 map-P f (next x≈y xs<ys) = next x≈y (map-P f xs<ys)
58
59 ------------------------------------------------------------------------
60 -- Properties
61
62 module _ {P : Set} {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
63
64 length-equal : ∀ {m n} {xs : Vec A m} {ys : Vec A n} →
65 Lex P _≈_ _≺_ xs ys → m ≡ n
66 length-equal (base _) = refl
67 length-equal (this x≺y m≡n) = cong suc m≡n
68 length-equal (next x≈y xs<ys) = cong suc (length-equal xs<ys)
69
70 module _ {P : Set} {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
71
72 private
73 _≋_ = Pointwise _≈_
74 _<ₗₑₓ_ = Lex P _≈_ _≺_
75
76 ≰-this : ∀ {x y m n} {xs : Vec A m} {ys : Vec A n} →
77 ¬ (x ≈ y) → ¬ (x ≺ y) → ¬ (x ∷ xs) <ₗₑₓ (y ∷ ys)
78 ≰-this x≉y x≮y (this x≺y m≡n) = contradiction x≺y x≮y
79 ≰-this x≉y x≮y (next x≈y xs<ys) = contradiction x≈y x≉y
80
81 ≰-next : ∀ {x y m n} {xs : Vec A m} {ys : Vec A n} →
82 ¬ (x ≺ y) → ¬ (xs <ₗₑₓ ys) → ¬ (x ∷ xs) <ₗₑₓ (y ∷ ys)
83 ≰-next x≮y xs≮ys (this x≺y m≡n) = contradiction x≺y x≮y
84 ≰-next x≮y xs≮ys (next x≈y xs<ys) = contradiction xs<ys xs≮ys
85
86 P⇔[]<[] : P ⇔ [] <ₗₑₓ []
87 P⇔[]<[] = mk⇔ base (λ { (base p) → p })
88
89 toSum : ∀ {x y n} {xs ys : Vec A n} → (x ∷ xs) <ₗₑₓ (y ∷ ys) → (x ≺ y ⊎ (x ≈ y × xs <ₗₑₓ ys))
90 toSum (this x≺y m≡n) = inj₁ x≺y
91 toSum (next x≈y xs<ys) = inj₂ (x≈y , xs<ys)
92
93 ∷<∷-⇔ : ∀ {x y n} {xs ys : Vec A n} → (x ≺ y ⊎ (x ≈ y × xs <ₗₑₓ ys)) ⇔ (x ∷ xs) <ₗₑₓ (y ∷ ys)
94 ∷<∷-⇔ = mk⇔ [ flip this refl , uncurry next ] toSum
95
96 module _ (≈-equiv : IsPartialEquivalence _≈_)
97 ((≺-respʳ-≈ , ≺-respˡ-≈) : _≺_ Respects₂ _≈_)
98 (≺-trans : Transitive _≺_)
99 (open IsPartialEquivalence ≈-equiv)
100 where
101
102 transitive′ : ∀ {m n o P₂} → Trans (Lex P _≈_ _≺_ {m} {n}) (Lex P₂ _≈_ _≺_ {n} {o}) (Lex (P × P₂) _≈_ _≺_)
103 transitive′ (base p₁) (base p₂) = base (p₁ , p₂)
104 transitive′ (this x≺y m≡n) (this y≺z n≡o) = this (≺-trans x≺y y≺z) (≡.trans m≡n n≡o)
105 transitive′ (this x≺y m≡n) (next y≈z ys<zs) = this (≺-respʳ-≈ y≈z x≺y) (≡.trans m≡n (length-equal ys<zs))
106 transitive′ (next x≈y xs<ys) (this y≺z n≡o) = this (≺-respˡ-≈ (sym x≈y) y≺z) (≡.trans (length-equal xs<ys) n≡o)
107 transitive′ (next x≈y xs<ys) (next y≈z ys<zs) = next (trans x≈y y≈z) (transitive′ xs<ys ys<zs)
108
109 transitive : ∀ {m n o} → Trans (_<ₗₑₓ_ {m} {n}) (_<ₗₑₓ_ {n} {o}) _<ₗₑₓ_
110 transitive xs<ys ys<zs = map-P proj₁ (transitive′ xs<ys ys<zs)
111
112 module _ (≈-sym : Symmetric _≈_) (≺-irrefl : Irreflexive _≈_ _≺_) (≺-asym : Asymmetric _≺_) where
113
114 antisym : ∀ {n} → Antisymmetric (_≋_ {n}) (_<ₗₑₓ_)
115 antisym (base _) (base _) = []
116 antisym (this x≺y m≡n) (this y≺x n≡m) = contradiction y≺x (≺-asym x≺y)
117 antisym (this x≺y m≡n) (next y≈x ys<xs) = contradiction x≺y (≺-irrefl (≈-sym y≈x))
118 antisym (next x≈y xs<ys) (this y≺x m≡n) = contradiction y≺x (≺-irrefl (≈-sym x≈y))
119 antisym (next x≈y xs<ys) (next y≈x ys<xs) = x≈y ∷ (antisym xs<ys ys<xs)
120
121 module _ (≈-equiv : IsPartialEquivalence _≈_) (open IsPartialEquivalence ≈-equiv) where
122
123 respectsˡ : _≺_ Respectsˡ _≈_ → ∀ {m n} → (_<ₗₑₓ_ {m} {n}) Respectsˡ _≋_
124 respectsˡ resp [] (base p) = base p
125 respectsˡ resp (x≈y ∷ xs≋ys) (this x≺z m≡n) = this (resp x≈y x≺z) m≡n
126 respectsˡ resp (x≈y ∷ xs≋ys) (next x≈z xs<zs) = next (trans (sym x≈y) x≈z) (respectsˡ resp xs≋ys xs<zs)
127
128 respectsʳ : _≺_ Respectsʳ _≈_ → ∀ {m n} → (_<ₗₑₓ_ {m} {n}) Respectsʳ _≋_
129 respectsʳ resp [] (base p) = base p
130 respectsʳ resp (x≈y ∷ xs≋ys) (this x≺z m≡n) = this (resp x≈y x≺z) m≡n
131 respectsʳ resp (x≈y ∷ xs≋ys) (next x≈z xs<zs) = next (trans x≈z x≈y) (respectsʳ resp xs≋ys xs<zs)
132
133 respects₂ : _≺_ Respects₂ _≈_ → ∀ {n} → (_<ₗₑₓ_ {n} {n}) Respects₂ _≋_
134 respects₂ (≺-resp-≈ʳ , ≺-resp-≈ˡ) = respectsʳ ≺-resp-≈ʳ , respectsˡ ≺-resp-≈ˡ
135
136 module _ (P? : Dec P) (_≈?_ : Decidable _≈_) (_≺?_ : Decidable _≺_) where
137
138 decidable : ∀ {m n} → Decidable (_<ₗₑₓ_ {m} {n})
139 decidable {m} {n} xs ys with m ℕ.≟ n
140 decidable {_} {_} [] [] | yes refl = Dec.map P⇔[]<[] P?
141 decidable {_} {_} (x ∷ xs) (y ∷ ys) | yes refl = Dec.map ∷<∷-⇔ ((x ≺? y) ⊎-dec (x ≈? y) ×-dec (decidable xs ys))
142 decidable {_} {_} _ _ | no m≢n = no (λ xs<ys → contradiction (length-equal xs<ys) m≢n)
143
144 module _ (P-irrel : Nullary.Irrelevant P)
145 (≈-irrel : Irrelevant _≈_)
146 (≺-irrel : Irrelevant _≺_)
147 (≺-irrefl : Irreflexive _≈_ _≺_)
148 where
149
150 irrelevant : ∀ {m n} → Irrelevant (_<ₗₑₓ_ {m} {n})
151 irrelevant (base p₁) (base p₂) rewrite P-irrel p₁ p₂ = refl
152 irrelevant (this x≺y₁ m≡n₁) (this x≺y₂ m≡n₂) rewrite ≺-irrel x≺y₁ x≺y₂ | ℕ.≡-irrelevant m≡n₁ m≡n₂ = refl
153 irrelevant (this x≺y m≡n) (next x≈y xs<ys₂) = contradiction x≺y (≺-irrefl x≈y)
154 irrelevant (next x≈y xs<ys₁) (this x≺y m≡n) = contradiction x≺y (≺-irrefl x≈y)
155 irrelevant (next x≈y₁ xs<ys₁) (next x≈y₂ xs<ys₂) rewrite ≈-irrel x≈y₁ x≈y₂ | irrelevant xs<ys₁ xs<ys₂ = refl