@@ -14,20 +14,34 @@ open import Data.List.Relation.Unary.AllPairs using (AllPairs)
14
14
open import Data.List.Relation.Unary.Linked as Linked
15
15
using (Linked; []; [-]; _∷_; _∷′_; head′; tail)
16
16
import Data.List.Relation.Unary.Linked.Properties as Linked
17
+ import Data.List.Relation.Binary.Equality.Setoid as Equality
17
18
import Data.List.Relation.Binary.Sublist.Setoid as Sublist
18
19
import Data.List.Relation.Binary.Sublist.Setoid.Properties as SublistProperties
19
- open import Data.List.Relation.Unary.Sorted.TotalOrder hiding (head)
20
-
20
+ import Data.List.Relation.Binary.Permutation.Setoid as Permutation
21
+ import Data.List.Relation.Binary.Permutation.Setoid.Properties as PermutationProperties
22
+ import Data.List.Relation.Binary.Pointwise as Pointwise
23
+ open import Data.List.Relation.Unary.Sorted.TotalOrder as Sorted hiding (head)
24
+
25
+ open import Data.Fin.Base as Fin hiding (_<_; _≤_)
26
+ import Data.Fin.Properties as Fin
27
+ open import Data.Fin.Permutation
28
+ open import Data.Product using (_,_)
21
29
open import Data.Maybe.Base using (just; nothing)
22
30
open import Data.Maybe.Relation.Binary.Connected using (Connected; just)
23
- open import Data.Nat.Base using (ℕ; zero; suc; _<_)
24
-
31
+ open import Data.Nat.Base as ℕ using (ℕ; z≤n; s≤s; zero; suc)
32
+ import Data.Nat.Properties as ℕ
33
+ open import Function.Base using (_∘_; const)
34
+ open import Function.Bundles using (Inverse)
35
+ open import Function.Consequences.Propositional using (inverseʳ⇒injective)
25
36
open import Level using (Level)
26
- open import Relation.Binary.Core using (_Preserves_⟶_)
37
+ open import Relation.Binary.Core using (_Preserves_⟶_; Rel )
27
38
open import Relation.Binary.Bundles using (TotalOrder; DecTotalOrder; Preorder)
28
39
import Relation.Binary.Properties.TotalOrder as TotalOrderProperties
40
+ import Relation.Binary.Reasoning.PartialOrder as PosetReasoning
29
41
open import Relation.Unary using (Pred; Decidable)
42
+ open import Relation.Nullary using (contradiction)
30
43
open import Relation.Nullary.Decidable using (yes; no)
44
+ open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
31
45
32
46
private
33
47
variable
@@ -80,7 +94,7 @@ module _ (O : TotalOrder a ℓ₁ ℓ₂) where
80
94
module _ (O : TotalOrder a ℓ₁ ℓ₂) where
81
95
open TotalOrder O
82
96
83
- applyUpTo⁺₁ : ∀ f n → (∀ {i} → suc i < n → f i ≤ f (suc i)) →
97
+ applyUpTo⁺₁ : ∀ f n → (∀ {i} → suc i ℕ. < n → f i ≤ f (suc i)) →
84
98
Sorted O (applyUpTo f n)
85
99
applyUpTo⁺₁ = Linked.applyUpTo⁺₁
86
100
@@ -94,7 +108,7 @@ module _ (O : TotalOrder a ℓ₁ ℓ₂) where
94
108
module _ (O : TotalOrder a ℓ₁ ℓ₂) where
95
109
open TotalOrder O
96
110
97
- applyDownFrom⁺₁ : ∀ f n → (∀ {i} → suc i < n → f (suc i) ≤ f i) →
111
+ applyDownFrom⁺₁ : ∀ f n → (∀ {i} → suc i ℕ. < n → f (suc i) ≤ f i) →
98
112
Sorted O (applyDownFrom f n)
99
113
applyDownFrom⁺₁ = Linked.applyDownFrom⁺₁
100
114
@@ -150,3 +164,48 @@ module _ (O : TotalOrder a ℓ₁ ℓ₂) {P : Pred _ p} (P? : Decidable P) wher
150
164
151
165
filter⁺ : ∀ {xs} → Sorted O xs → Sorted O (filter P? xs)
152
166
filter⁺ = Linked.filter⁺ P? trans
167
+
168
+ ------------------------------------------------------------------------
169
+ -- lookup
170
+
171
+ module _ (O : TotalOrder a ℓ₁ ℓ₂) where
172
+ open TotalOrder O
173
+
174
+ lookup-mono-≤ : ∀ {xs} → Sorted O xs →
175
+ ∀ {i j} → i Fin.≤ j → lookup xs i ≤ lookup xs j
176
+ lookup-mono-≤ {x ∷ xs} xs↗ {zero} {zero} z≤n = refl
177
+ lookup-mono-≤ {x ∷ xs} xs↗ {zero} {suc j} z≤n = Linked.lookup trans xs↗ (just refl) (suc j)
178
+ lookup-mono-≤ {x ∷ xs} xs↗ {suc i} {suc j} (s≤s i≤j) = lookup-mono-≤ (Sorted.tail O {y = x} xs↗) i≤j
179
+
180
+ ------------------------------------------------------------------------
181
+ -- Relationship to binary relations
182
+ ------------------------------------------------------------------------
183
+
184
+ module _ (O : TotalOrder a ℓ₁ ℓ₂) where
185
+ open TotalOrder O
186
+ open Equality Eq.setoid
187
+ open Permutation Eq.setoid hiding (refl; trans)
188
+ open PermutationProperties Eq.setoid
189
+ open PosetReasoning poset
190
+
191
+ -- Proof that any two sorted lists that are a permutation of each
192
+ -- other are pointwise equal
193
+ ↗↭↗⇒≋ : ∀ {xs ys} → Sorted O xs → Sorted O ys → xs ↭ ys → xs ≋ ys
194
+ ↗↭↗⇒≋ {xs} {ys} xs↗ ys↗ xs↭ys = Pointwise.lookup⁻
195
+ (xs↭ys⇒|xs|≡|ys| xs↭ys)
196
+ (λ i≡j → antisym
197
+ (↗↭↗⇒≤ (↭-sym xs↭ys) ys↗ xs↗ (≡.sym i≡j))
198
+ (↗↭↗⇒≤ xs↭ys xs↗ ys↗ i≡j))
199
+ where
200
+ ↗↭↗⇒≤ : ∀ {xs ys}
201
+ (xs↭ys : xs ↭ ys) →
202
+ Sorted O xs → Sorted O ys →
203
+ ∀ {i j} → toℕ i ≡ toℕ j →
204
+ lookup ys j ≤ lookup xs i
205
+ ↗↭↗⇒≤ {xs} {ys} xs↭ys xs↗ ys↗ {i} {j} i≡j
206
+ with Fin.injective⇒existsPivot (inverseʳ⇒injective _ (Inverse.inverseʳ (onIndices xs↭ys))) i
207
+ ... | (k , k≤i , i≤π[k]) = begin
208
+ lookup ys j ≤⟨ lookup-mono-≤ O ys↗ (≡.subst (ℕ._≤ _) i≡j i≤π[k]) ⟩
209
+ lookup ys (onIndices xs↭ys ⟨$⟩ʳ k) ≈⟨ onIndices-lookup xs↭ys k ⟨
210
+ lookup xs k ≤⟨ lookup-mono-≤ O xs↗ k≤i ⟩
211
+ lookup xs i ∎
0 commit comments