diff --git a/CHANGELOG.md b/CHANGELOG.md index b32fa73de5..9f69962d0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,7 @@ Minor improvements levels to be used. * Due to becoming large, `Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties` - has been split into small modules + has been split into smaller modules `Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.*` that are reexported by the original `Properties`. @@ -109,6 +109,14 @@ Deprecated names nonPos*nonPos⇒nonPos ↦ nonPos*nonPos⇒nonNeg ``` +* In `Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Insert`: + ```agda + Any-insertWith-nothing ↦ insertWith-nothing + Any-insertWith-just ↦ insertWith-just + Any-insert-nothing ↦ insert-nothing + Any-insert-just ↦ insert-just + ``` + * In `Data.Vec.Properties`: ```agda truncate-irrelevant ↦ Relation.Binary.PropositionalEquality.Core.refl @@ -183,9 +191,8 @@ New modules Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Delete Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.HeadTail Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Insert - Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.JoinConstFuns Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Join - Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.LookupFun + Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.JoinLemmas Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Singleton ``` @@ -415,6 +422,27 @@ Additions to existing modules showAtPrecision : ℕ → ℚᵘ → String ``` +* In `Data.Tree.AVL.Height`: + ```agda + 0∼⊔ : 0 ∼ j ⊔ m → j ≡ m + ∼0⊔ : i ∼ 0 ⊔ m → i ≡ m + ``` + +* In `Data.Tree.AVL.Indexed`: + ```agda + Tree⁺ Tree⁻ : (V : Value v) (l u : Key⁺) (h : ℕ) → Set _ + pattern leaf⁻ l) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) -private - variable - l v : Level - A : Set l - -open StrictTotalOrder strictTotalOrder renaming (Carrier to Key) +open StrictTotalOrder strictTotalOrder + using (module Eq; compare) renaming (Carrier to Key) ------------------------------------------------------------------------ -- Re-export core definitions publicly @@ -37,33 +33,76 @@ open import Data.Tree.AVL.Value Eq.setoid public open import Data.Tree.AVL.Height public ------------------------------------------------------------------------ --- Definitions of the tree +-- Now we have vocabulary in which to state things more cleanly + +private + variable + ℓ v w : Level + A : Set ℓ + l m u : Key⁺ + hˡ hʳ h : ℕ + + +------------------------------------------------------------------------ +-- Definitions of the tree types -- The trees have three parameters/indices: a lower bound on the -- keys, an upper bound, and a height. -- -- (The bal argument is the balance factor.) -data Tree {v} (V : Value v) (l u : Key⁺) : ℕ → Set (a ⊔ v ⊔ ℓ₂) where +data Tree (V : Value v) (l u : Key⁺) : ℕ → Set (a ⊔ v ⊔ ℓ₂) where leaf : (l _ _ k′ _ _ k′ _ _ k′>k = joinˡ⁻ _ p (delete k lp (lk ]ᴿ)) pu bal - ... | tri≈ _ k′≡k _ = join lp pu bal + delete : ∀ k → Tree V l u h → l < k < u → Tree⁻ V l u h + delete k t@(leaf _) _ = 0# , t + delete k (node kv@(k′ , v) lk ku bal) (l _ _ k′>k = joinˡ⁻ _ kv (delete k lk (lk ]ᴿ)) ku bal + ... | tri≈ _ k′≈k _ = join lk ku bal -- Looks up a key. Logarithmic in the size of the tree (assuming -- constant-time comparisons). - lookup : ∀ {l u h} → Tree V l u h → (k : Key) → l < k < u → Maybe (Val k) - lookup (leaf _) k l _ _ k′>k = lookup lk′ k (lk ]ᴿ) - ... | tri≈ _ k′≡k _ = just (V≈ k′≡k v) + lookup : Tree V l u h → ∀ k → l < k < u → Maybe (Val k) + lookup (leaf _) _ _ = nothing + lookup (node (k′ , v) lk ku _) k (l _ _ k′>k = lookup lk k (lk ]ᴿ) + ... | tri≈ _ k′≈k _ = just (respects k′≈k v) -- Converts the tree to an ordered list. Linear in the size of the -- tree. - foldr : ∀ {l u h} → (∀ {k} → Val k → A → A) → A → Tree V l u h → A + foldr : (∀ {k} → Val k → A → A) → A → Tree V l u h → A foldr cons nil (leaf l _ k′≉k k′>k @@ -112,27 +116,27 @@ module _ (k : Key) where ... | inj₁ kp≈k′ = contradiction (trans (sym kp≈k′) kp≈k) k′≉k ... | inj₂ (inj₁ pl) = delete-key-∈⁻ lk′ (lk ]ᴿ) pl kp≈k ... | inj₂ (inj₂ pr) = begin-contradiction - [ k ] <⟨ [ k′>k ]ᴿ ⟩ - [ k′ ] <⟨ proj₁ (lookup-bounded pr) ⟩ - [ Any.lookupKey pr ] ≈⟨ [ sym (lookup-result pr) ]ᴱ ⟩ - [ kp ] ≈⟨ [ kp≈k ]ᴱ ⟩ - [ k ] ∎ + [ k ] <⟨ [ k′>k ]ᴿ ⟩ + [ k′ ] <⟨ proj₁ (lookup-bounded pr) ⟩ + [ lookupKey pr ] ≈⟨ [ lookup-result pr ]ᴱ ⟨ + [ kp ] ≈⟨ [ kp≈k ]ᴱ ⟩ + [ k ] ∎ delete-key-∈⁻ (node (k′ , _) _ k′u bal) _ {kp} p kp≈k | tri≈ k′≮k _ k′≯k with join⁻ _ k′u bal p ... | inj₁ p₁ = contradiction (begin-strict - [ k ] ≈⟨ [ sym kp≈k ]ᴱ ⟩ - [ kp ] ≈⟨ [ lookup-result p₁ ]ᴱ ⟩ - [ Any.lookupKey p₁ ] <⟨ proj₂ (lookup-bounded p₁) ⟩ - [ k′ ] ∎) + [ k ] ≈⟨ [ kp≈k ]ᴱ ⟨ + [ kp ] ≈⟨ [ lookup-result p₁ ]ᴱ ⟩ + [ lookupKey p₁ ] <⟨ proj₂ (lookup-bounded p₁) ⟩ + [ k′ ] ∎) (k′≯k ∘′ [<]-injective) ... | inj₂ p₂ = contradiction (begin-strict - [ k′ ] <⟨ proj₁ (lookup-bounded p₂) ⟩ - [ Any.lookupKey p₂ ] ≈⟨ [ sym (lookup-result p₂) ]ᴱ ⟩ - [ kp ] ≈⟨ [ kp≈k ]ᴱ ⟩ - [ k ] ∎) + [ k′ ] <⟨ proj₁ (lookup-bounded p₂) ⟩ + [ lookupKey p₂ ] ≈⟨ [ lookup-result p₂ ]ᴱ ⟨ + [ kp ] ≈⟨ [ kp≈k ]ᴱ ⟩ + [ k ] ∎) (k′≮k ∘′ [<]-injective) @@ -140,6 +144,6 @@ module _ (k : Key) where delete-key⁻ : (t : Tree V l u h) (seg : l < k < u) → (p : Any P (proj₂ (delete k t seg))) → - Any.lookupKey p ≉ k + k # p delete-key⁻ t seg p kp≈k = delete-key-∈⁻ k t seg (lookup-rebuild p Eq.refl) kp≈k diff --git a/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/HeadTail.agda b/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/HeadTail.agda index 39d17648ef..2a3916ba3a 100644 --- a/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/HeadTail.agda +++ b/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/HeadTail.agda @@ -12,7 +12,7 @@ module Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.HeadTail {a ℓ₁ ℓ₂} (sto : StrictTotalOrder a ℓ₁ ℓ₂) where -open import Data.Nat.Base using (suc; _+_) +open import Data.Nat.Base using (ℕ; suc) open import Data.Product.Base using (_,_; proj₁; proj₂) open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) open import Function using (id) @@ -22,7 +22,7 @@ open import Relation.Unary using (Pred) open import Data.Tree.AVL.Indexed sto open import Data.Tree.AVL.Indexed.Relation.Unary.Any sto as Any -open import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.JoinConstFuns sto +open import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.JoinLemmas sto using (joinˡ⁻-here⁺; joinˡ⁻-left⁺; joinˡ⁻-right⁺; joinˡ⁻⁻) private @@ -30,8 +30,11 @@ private v p : Level V : Value v P : Pred (K& V) p + l m u : Key⁺ + h : ℕ -headTail⁺ : ∀ {l u h} (t : Tree V l u (1 + h)) → + +headTail⁺ : (t : Tree V l u (suc h)) → let kv , _ , _ , t⁻ = headTail t in Any P t → P kv ⊎ Any P t⁻ headTail⁺ (node _ (leaf _) _ ∼+) (here p) = inj₁ p @@ -47,14 +50,14 @@ headTail⁺ (node k₃ t₁₂@(node _ _ _ _) t₄ bal) (right p) = let _ , _ , t₂ = headTail t₁₂ in inj₂ (joinˡ⁻-right⁺ k₃ t₂ t₄ bal p) -headTail-head⁻ : ∀ {l u h} → (t : Tree V l u (suc h)) → +headTail-head⁻ : (t : Tree V l u (suc h)) → P (proj₁ (headTail t)) → Any P t headTail-head⁻ (node _ (leaf _) _ ∼+) p = here p headTail-head⁻ (node _ (leaf _) _ ∼0) p = here p headTail-head⁻ (node _ t₁₂@(node _ _ _ _) _ _) p = left (headTail-head⁻ t₁₂ p) -headTail-tail⁻ : ∀ {l u h} (t : Tree V l u (1 + h)) → +headTail-tail⁻ : (t : Tree V l u (suc h)) → let _ , _ , _ , t⁻ = headTail t in Any P t⁻ → Any P t headTail-tail⁻ (node _ (leaf _) _ ∼+) p = right p diff --git a/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/Insert.agda b/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/Insert.agda index 9283c834c3..6a3608d7b3 100644 --- a/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/Insert.agda +++ b/src/Data/Tree/AVL/Indexed/Relation/Unary/Any/Properties/Insert.agda @@ -26,10 +26,13 @@ open import Data.Tree.AVL.Indexed sto as AVL open import Data.Tree.AVL.Indexed.Relation.Unary.Any sto as Any open import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Lookup sto using (lookup-result; lookup-bounded; lookup-rebuild-accum) -open import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.JoinConstFuns sto +open import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.JoinLemmas sto using (joinˡ⁺-left⁺; joinʳ⁺-right⁺; joinˡ⁺-here⁺; joinʳ⁺-here⁺; joinʳ⁺-left⁺; joinˡ⁺-right⁺; joinˡ⁺⁻; joinʳ⁺⁻) -open StrictTotalOrder sto renaming (Carrier to Key; trans to <-trans); open Eq using (sym; trans) +open StrictTotalOrder sto + using (_<_; _≈_; module Eq; compare; irrefl) + renaming (Carrier to Key; trans to <-trans) +open Eq using (_≉_; sym; trans) open import Relation.Binary.Construct.Add.Extrema.Strict _<_ using ([<]-injective) @@ -41,54 +44,52 @@ private v p : Level V : Value v l u : Key⁺ - n : ℕ + h : ℕ P : Pred (K& V) p -module _ {V : Value v} where - private - Val = Value.family V - Val≈ = Value.respects V +module _ {V : Value v} (open Value V using (respects) renaming (family to Val)) where module _ (k : Key) (f : Maybe (Val k) → Val k) where open <-Reasoning AVL.strictPartialOrder - Any-insertWith-nothing : (t : Tree V l u n) (seg : l < k < u) → - P (k , f nothing) → - ¬ (Any ((k ≈_) ∘′ key) t) → Any P (proj₂ (insertWith k f t seg)) - Any-insertWith-nothing (leaf l _ _ k>k′ = joinʳ⁺-right⁺ kv lk ku′ bal ih where - seg′ = [ k>k′ ]ᴿ , kk′ ]ᴿ , k _ _ k>k′ = - joinʳ⁺-right⁺ kv lk ku′ bal (Any-insertWith-just ku seg′ pr rp) + joinʳ⁺-right⁺ kv lk ku′ bal (insertWith-just ku lk′ ]ᴿ , kk′ ]ᴿ , k) +open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) open import Relation.Unary using (Pred; _∩_) -open import Data.Tree.AVL.Indexed sto hiding (lookup) -open import Data.Tree.AVL.Indexed.Relation.Unary.Any sto +open import Data.Tree.AVL.Indexed sto as AVL hiding (lookup) +open import Data.Tree.AVL.Indexed.Relation.Unary.Any sto as Any + +open StrictTotalOrder sto + using (_<_; module Eq; compare; irrefl) + renaming (Carrier to Key; trans to <-trans) +open Eq using (sym; trans) + +open import Relation.Binary.Construct.Add.Extrema.Strict _<_ using ([<]-injective) private variable v p : Level V : Value v l u : Key⁺ - n : ℕ + h : ℕ P Q : Pred (K& V) p + t : Tree V l u h + + + +---------------------------------------------------------------------- +-- lookup -lookup-result : {t : Tree V l u n} (p : Any P t) → P (lookup p) +lookup-result : (p : Any P t) → P (lookup p) lookup-result (here p) = p lookup-result (left p) = lookup-result p lookup-result (right p) = lookup-result p -lookup-bounded : {t : Tree V l u n} (p : Any P t) → l < lookup p .key < u -lookup-bounded {t = node kv lk ku bal} (here p) = ordered lk , ordered ku -lookup-bounded {t = node kv lk ku bal} (left p) = - Prod.map₂ (flip (trans⁺ _) (ordered ku)) (lookup-bounded p) -lookup-bounded {t = node kv lk ku bal} (right p) = - Prod.map₁ (trans⁺ _ (ordered lk)) (lookup-bounded p) +lookup-bounded : (p : Any P {l = l} {u = u} t) → l < lookupKey p < u +lookup-bounded {t = node _ lk ku _} (here p) = ordered lk , ordered ku +lookup-bounded {t = node _ _ ku _} (left p) = + Product.map₂ (flip (trans⁺ _) (ordered ku)) (lookup-bounded p) +lookup-bounded {t = node _ lk _ _} (right p) = + Product.map₁ (trans⁺ _ (ordered lk)) (lookup-bounded p) -lookup-rebuild : {t : Tree V l u n} (p : Any P t) → Q (lookup p) → Any Q t +lookup-rebuild : (p : Any P t) → Q (lookup p) → Any Q t lookup-rebuild (here _) q = here q lookup-rebuild (left p) q = left (lookup-rebuild p q) lookup-rebuild (right p) q = right (lookup-rebuild p q) -lookup-rebuild-accum : {t : Tree V l u n} (p : Any P t) → Q (lookup p) → Any (Q ∩ P) t +lookup-rebuild-accum : (p : Any P t) → Q (lookup p) → Any (Q ∩ P) t lookup-rebuild-accum p q = lookup-rebuild p (q , lookup-result p) + + +---------------------------------------------------------------------- +-- Relating Any.lookup to AVL.lookup + +module _ {V : Value v} (open Value V using (respects) renaming (family to Val)) where + + lookup⁺ : (t : Tree V l u h) (k : Key) (l _ _ k _ _ k _ _ k _ _ k) -open import Relation.Binary.PropositionalEquality.Core using (_≡_) renaming (refl to ≡-refl) -open import Relation.Unary using (Pred) - -open import Data.Tree.AVL.Indexed sto as AVL -open import Data.Tree.AVL.Indexed.Relation.Unary.Any sto as Any -open StrictTotalOrder sto renaming (Carrier to Key; trans to <-trans); open Eq using (sym; trans) - -open import Relation.Binary.Construct.Add.Extrema.Strict _<_ using ([<]-injective) - -private - variable - v p : Level - V : Value v - l u : Key⁺ - n : ℕ - P : Pred (K& V) p - -open import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties.Lookup sto - using (lookup-bounded) - -module _ {V : Value v} where - - private - Val = Value.family V - Val≈ = Value.respects V - - lookup⁺ : (t : Tree V l u n) (k : Key) (seg : l < k < u) → - (p : Any P t) → - (key (Any.lookup p) ≉ k) - ⊎ (∃[ p≈k ] AVL.lookup t k seg - ≡ just (Val≈ p≈k (value (Any.lookup p)))) - lookup⁺ (node (k′ , v′) l r bal) k (l _ _ k _ _ k _ _ k _ _ k