@@ -14,20 +14,34 @@ open import Data.List.Relation.Unary.AllPairs using (AllPairs)
1414open import Data.List.Relation.Unary.Linked as Linked
1515 using (Linked; []; [-]; _∷_; _∷′_; head′; tail)
1616import Data.List.Relation.Unary.Linked.Properties as Linked
17+ import Data.List.Relation.Binary.Equality.Setoid as Equality
1718import Data.List.Relation.Binary.Sublist.Setoid as Sublist
1819import 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 (_,_)
2129open import Data.Maybe.Base using (just; nothing)
2230open 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)
2536open import Level using (Level)
26- open import Relation.Binary.Core using (_Preserves_⟶_)
37+ open import Relation.Binary.Core using (_Preserves_⟶_; Rel )
2738open import Relation.Binary.Bundles using (TotalOrder; DecTotalOrder; Preorder)
2839import Relation.Binary.Properties.TotalOrder as TotalOrderProperties
40+ import Relation.Binary.Reasoning.PartialOrder as PosetReasoning
2941open import Relation.Unary using (Pred; Decidable)
42+ open import Relation.Nullary using (contradiction)
3043open import Relation.Nullary.Decidable using (yes; no)
44+ open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
3145
3246private
3347 variable
@@ -80,7 +94,7 @@ module _ (O : TotalOrder a ℓ₁ ℓ₂) where
8094module _ (O : TotalOrder a ℓ₁ ℓ₂) where
8195 open TotalOrder O
8296
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)) →
8498 Sorted O (applyUpTo f n)
8599 applyUpTo⁺₁ = Linked.applyUpTo⁺₁
86100
@@ -94,7 +108,7 @@ module _ (O : TotalOrder a ℓ₁ ℓ₂) where
94108module _ (O : TotalOrder a ℓ₁ ℓ₂) where
95109 open TotalOrder O
96110
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) →
98112 Sorted O (applyDownFrom f n)
99113 applyDownFrom⁺₁ = Linked.applyDownFrom⁺₁
100114
@@ -150,3 +164,48 @@ module _ (O : TotalOrder a ℓ₁ ℓ₂) {P : Pred _ p} (P? : Decidable P) wher
150164
151165 filter⁺ : ∀ {xs} → Sorted O xs → Sorted O (filter P? xs)
152166 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