Mercurial > hg > Gears > GearsAgda
annotate hoareBinaryTree1.agda @ 823:ebee6945c697
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 27 Jan 2024 18:24:40 +0900 |
parents | 3f6e13350420 |
children | 7d73749f097e |
rev | line source |
---|---|
722 | 1 module hoareBinaryTree1 where |
586
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
2 |
727 | 3 open import Level hiding (suc ; zero ; _⊔_ ) |
586
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
4 |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
5 open import Data.Nat hiding (compare) |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
6 open import Data.Nat.Properties as NatProp |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
7 open import Data.Maybe |
785 | 8 -- open import Data.Maybe.Properties |
586
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
9 open import Data.Empty |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
10 open import Data.List |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
11 open import Data.Product |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
12 |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
13 open import Function as F hiding (const) |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
14 |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
15 open import Relation.Binary |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
16 open import Relation.Binary.PropositionalEquality |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
17 open import Relation.Nullary |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
18 open import logic |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
19 |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
20 |
590 | 21 -- |
22 -- | |
23 -- no children , having left node , having right node , having both | |
24 -- | |
597 | 25 data bt {n : Level} (A : Set n) : Set n where |
604 | 26 leaf : bt A |
27 node : (key : ℕ) → (value : A) → | |
610 | 28 (left : bt A ) → (right : bt A ) → bt A |
600 | 29 |
620 | 30 node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ |
31 node-key (node key _ _ _) = just key | |
32 node-key _ = nothing | |
33 | |
34 node-value : {n : Level} {A : Set n} → bt A → Maybe A | |
35 node-value (node _ value _ _) = just value | |
36 node-value _ = nothing | |
37 | |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
38 bt-depth : {n : Level} {A : Set n} → (tree : bt A ) → ℕ |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
39 bt-depth leaf = 0 |
727 | 40 bt-depth (node key value t t₁) = suc (bt-depth t ⊔ bt-depth t₁ ) |
606 | 41 |
789
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
42 open import Data.Unit hiding ( _≟_ ) -- ; _≤?_ ; _≤_) |
605 | 43 |
620 | 44 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where |
790 | 45 t-leaf : treeInvariant leaf |
46 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf) | |
745 | 47 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → key < key₁ → treeInvariant (node key₁ value₁ t₁ t₂) |
790 | 48 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂)) |
745 | 49 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → key < key₁ → treeInvariant (node key value t₁ t₂) |
790 | 50 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf ) |
745 | 51 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → key < key₁ → key₁ < key₂ |
790 | 52 → treeInvariant (node key value t₁ t₂) |
620 | 53 → treeInvariant (node key₂ value₂ t₃ t₄) |
790 | 54 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) |
605 | 55 |
662 | 56 -- |
745 | 57 -- stack always contains original top at end (path of the tree) |
662 | 58 -- |
59 data stackInvariant {n : Level} {A : Set n} (key : ℕ) : (top orig : bt A) → (stack : List (bt A)) → Set n where | |
729 | 60 s-nil : {tree0 : bt A} → stackInvariant key tree0 tree0 (tree0 ∷ []) |
790 | 61 s-right : (tree tree0 tree₁ : bt A) → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} |
662 | 62 → key₁ < key → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree tree0 (tree ∷ st) |
790 | 63 s-left : (tree₁ tree0 tree : bt A) → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} |
662 | 64 → key < key₁ → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree₁ tree0 (tree₁ ∷ st) |
639 | 65 |
677 | 66 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt A ) → Set n where |
639 | 67 r-leaf : replacedTree key value leaf (node key value leaf leaf) |
790 | 68 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁) |
639 | 69 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} |
790 | 70 → k < key → replacedTree key value t2 t → replacedTree key value (node k v1 t1 t2) (node k v1 t1 t) |
639 | 71 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} |
790 | 72 → key < k → replacedTree key value t1 t → replacedTree key value (node k v1 t1 t2) (node k v1 t t2) |
652 | 73 |
632 | 74 add< : { i : ℕ } (j : ℕ ) → i < suc i + j |
75 add< {i} j = begin | |
76 suc i ≤⟨ m≤m+n (suc i) j ⟩ | |
77 suc i + j ∎ where open ≤-Reasoning | |
78 | |
79 treeTest1 : bt ℕ | |
692 | 80 treeTest1 = node 0 0 leaf (node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf)) |
632 | 81 treeTest2 : bt ℕ |
692 | 82 treeTest2 = node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf) |
632 | 83 |
84 treeInvariantTest1 : treeInvariant treeTest1 | |
692 | 85 treeInvariantTest1 = t-right (m≤m+n _ 2) (t-node (add< 0) (add< 1) (t-left (add< 0) (t-single 1 7)) (t-single 5 5) ) |
605 | 86 |
639 | 87 stack-top : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A) |
88 stack-top [] = nothing | |
89 stack-top (x ∷ s) = just x | |
606 | 90 |
639 | 91 stack-last : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A) |
92 stack-last [] = nothing | |
93 stack-last (x ∷ []) = just x | |
94 stack-last (x ∷ s) = stack-last s | |
632 | 95 |
662 | 96 stackInvariantTest1 : stackInvariant 4 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] ) |
785 | 97 stackInvariantTest1 = s-right _ _ _ (add< 3) (s-nil ) |
662 | 98 |
666 | 99 si-property0 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 stack → ¬ ( stack ≡ [] ) |
729 | 100 si-property0 (s-nil ) () |
785 | 101 si-property0 (s-right _ _ _ x si) () |
102 si-property0 (s-left _ _ _ x si) () | |
665 | 103 |
666 | 104 si-property1 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 tree1 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 (tree1 ∷ stack) |
105 → tree1 ≡ tree | |
729 | 106 si-property1 (s-nil ) = refl |
785 | 107 si-property1 (s-right _ _ _ _ si) = refl |
108 si-property1 (s-left _ _ _ _ si) = refl | |
662 | 109 |
816 | 110 si-property2 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 tree1 : bt A} → (stack : List (bt A)) → stackInvariant key tree tree0 (tree1 ∷ stack) |
111 → ¬ ( just leaf ≡ stack-last stack ) | |
112 si-property2 (.leaf ∷ []) (s-right _ _ tree₁ x ()) refl | |
113 si-property2 (x₁ ∷ x₂ ∷ stack) (s-right _ _ tree₁ x si) eq = si-property2 (x₂ ∷ stack) si eq | |
114 si-property2 (.leaf ∷ []) (s-left _ _ tree₁ x ()) refl | |
115 si-property2 (x₁ ∷ x₂ ∷ stack) (s-left _ _ tree₁ x si) eq = si-property2 (x₂ ∷ stack) si eq | |
116 | |
663
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
117 si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → stackInvariant key tree tree0 stack |
662 | 118 → stack-last stack ≡ just tree0 |
729 | 119 si-property-last key t t0 (t ∷ []) (s-nil ) = refl |
785 | 120 si-property-last key t t0 (.t ∷ x ∷ st) (s-right _ _ _ _ si ) with si-property1 si |
663
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
121 ... | refl = si-property-last key x t0 (x ∷ st) si |
785 | 122 si-property-last key t t0 (.t ∷ x ∷ st) (s-left _ _ _ _ si ) with si-property1 si |
663
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
123 ... | refl = si-property-last key x t0 (x ∷ st) si |
656 | 124 |
639 | 125 rt-property1 : {n : Level} {A : Set n} (key : ℕ) (value : A) (tree tree1 : bt A ) → replacedTree key value tree tree1 → ¬ ( tree1 ≡ leaf ) |
126 rt-property1 {n} {A} key value .leaf .(node key value leaf leaf) r-leaf () | |
127 rt-property1 {n} {A} key value .(node key _ _ _) .(node key value _ _) r-node () | |
677 | 128 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-right x rt) = λ () |
129 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-left x rt) = λ () | |
639 | 130 |
690 | 131 rt-property-leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {repl : bt A} → replacedTree key value leaf repl → repl ≡ node key value leaf leaf |
132 rt-property-leaf r-leaf = refl | |
133 | |
790 | 134 rt-property-¬leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {tree : bt A} → ¬ replacedTree key value tree leaf |
698 | 135 rt-property-¬leaf () |
136 | |
692 | 137 rt-property-key : {n : Level} {A : Set n} {key key₂ key₃ : ℕ} {value value₂ value₃ : A} {left left₁ right₂ right₃ : bt A} |
138 → replacedTree key value (node key₂ value₂ left right₂) (node key₃ value₃ left₁ right₃) → key₂ ≡ key₃ | |
139 rt-property-key r-node = refl | |
140 rt-property-key (r-right x ri) = refl | |
141 rt-property-key (r-left x ri) = refl | |
142 | |
698 | 143 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥ |
144 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x | |
145 nat-<> : { x y : ℕ } → x < y → y < x → ⊥ | |
146 nat-<> (s≤s x<y) (s≤s y<x) = nat-<> x<y y<x | |
147 | |
148 open _∧_ | |
149 | |
150 | |
632 | 151 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j ) |
152 depth-1< {i} {j} = s≤s (m≤m⊔n _ j) | |
153 | |
154 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i ) | |
650 | 155 depth-2< {i} {j} = s≤s (m≤n⊔m j i) |
611 | 156 |
649 | 157 depth-3< : {i : ℕ } → suc i ≤ suc (suc i) |
158 depth-3< {zero} = s≤s ( z≤n ) | |
159 depth-3< {suc i} = s≤s (depth-3< {i} ) | |
160 | |
161 | |
634 | 162 treeLeftDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) |
163 → treeInvariant (node k v1 tree tree₁) | |
790 | 164 → treeInvariant tree |
634 | 165 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf |
166 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf | |
790 | 167 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti |
634 | 168 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti |
169 | |
170 treeRightDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) | |
171 → treeInvariant (node k v1 tree tree₁) | |
790 | 172 → treeInvariant tree₁ |
634 | 173 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf |
174 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti | |
175 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf | |
176 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti₁ | |
177 | |
615 | 178 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A)) |
790 | 179 → treeInvariant tree ∧ stackInvariant key tree tree0 stack |
693 | 180 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t ) |
181 → (exit : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
638 | 182 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
693 | 183 findP key leaf tree0 st Pre _ exit = exit leaf st Pre (case1 refl) |
632 | 184 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁ |
693 | 185 findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) |
186 findP {n} {_} {A} key (node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree (tree ∷ st) | |
663
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
187 ⟪ treeLeftDown tree tree₁ (proj1 Pre) , findP1 a st (proj2 Pre) ⟫ depth-1< where |
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
188 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) |
785 | 189 findP1 a (x ∷ st) si = s-left _ _ _ a si |
190 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) ⟪ treeRightDown tree tree₁ (proj1 Pre) , s-right _ _ _ c (proj2 Pre) ⟫ depth-2< | |
606 | 191 |
638 | 192 replaceTree1 : {n : Level} {A : Set n} {t t₁ : bt A } → ( k : ℕ ) → (v1 value : A ) → treeInvariant (node k v1 t t₁) → treeInvariant (node k value t t₁) |
193 replaceTree1 k v1 value (t-single .k .v1) = t-single k value | |
194 replaceTree1 k v1 value (t-right x t) = t-right x t | |
195 replaceTree1 k v1 value (t-left x t) = t-left x t | |
196 replaceTree1 k v1 value (t-node x x₁ t t₁) = t-node x x₁ t t₁ | |
197 | |
649 | 198 open import Relation.Binary.Definitions |
199 | |
200 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥ | |
201 lemma3 refl () | |
202 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥ | |
203 lemma5 (s≤s z≤n) () | |
700 | 204 ¬x<x : {x : ℕ} → ¬ (x < x) |
205 ¬x<x (s≤s lt) = ¬x<x lt | |
649 | 206 |
687 | 207 child-replaced : {n : Level} {A : Set n} (key : ℕ) (tree : bt A) → bt A |
208 child-replaced key leaf = leaf | |
209 child-replaced key (node key₁ value left right) with <-cmp key key₁ | |
210 ... | tri< a ¬b ¬c = left | |
211 ... | tri≈ ¬a b ¬c = node key₁ value left right | |
212 ... | tri> ¬a ¬b c = right | |
807 | 213 |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
214 record replacePR {n : Level} {A : Set n} (key : ℕ) (value : A) (tree repl : bt A ) (stack : List (bt A)) (C : bt A → bt A → List (bt A) → Set n) : Set n where |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
215 field |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
216 tree0 : bt A |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
217 ti : treeInvariant tree0 |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
218 si : stackInvariant key tree tree0 stack |
687 | 219 ri : replacedTree key value (child-replaced key tree ) repl |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
220 ci : C tree repl stack -- data continuation |
790 | 221 |
785 | 222 record replacePR' {n : Level} {A : Set n} (key : ℕ) (value : A) (orig : bt A ) (stack : List (bt A)) : Set n where |
223 field | |
224 tree repl : bt A | |
225 ti : treeInvariant orig | |
226 si : stackInvariant key tree orig stack | |
227 ri : replacedTree key value (child-replaced key tree) repl | |
228 -- treeInvariant of tree and repl is inferred from ti, si and ri. | |
790 | 229 |
638 | 230 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) |
785 | 231 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key ) |
694 | 232 → (treeInvariant tree ) → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value (child-replaced key tree) tree1 → t) → t |
233 replaceNodeP k v1 leaf C P next = next (node k v1 leaf leaf) (t-single k v1 ) r-leaf | |
234 replaceNodeP k v1 (node .k value t t₁) (case2 refl) P next = next (node k v1 t t₁) (replaceTree1 k value v1 P) | |
790 | 235 (subst (λ j → replacedTree k v1 j (node k v1 t t₁) ) repl00 r-node) where |
694 | 236 repl00 : node k value t t₁ ≡ child-replaced k (node k value t t₁) |
237 repl00 with <-cmp k k | |
238 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl) | |
239 ... | tri≈ ¬a b ¬c = refl | |
240 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl) | |
606 | 241 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
242 replaceP : {n m : Level} {A : Set n} {t : Set m} |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
243 → (key : ℕ) → (value : A) → {tree : bt A} ( repl : bt A) |
790 | 244 → (stack : List (bt A)) → replacePR key value tree repl stack (λ _ _ _ → Lift n ⊤) |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
245 → (next : ℕ → A → {tree1 : bt A } (repl : bt A) → (stack1 : List (bt A)) |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
246 → replacePR key value tree1 repl stack1 (λ _ _ _ → Lift n ⊤) → length stack1 < length stack → t) |
613 | 247 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t |
675 | 248 replaceP key value {tree} repl [] Pre next exit = ⊥-elim ( si-property0 (replacePR.si Pre) refl ) -- can't happen |
249 replaceP key value {tree} repl (leaf ∷ []) Pre next exit with si-property-last _ _ _ _ (replacePR.si Pre)-- tree0 ≡ leaf | |
677 | 250 ... | refl = exit (replacePR.tree0 Pre) (node key value leaf leaf) ⟪ replacePR.ti Pre , r-leaf ⟫ |
689 | 251 replaceP key value {tree} repl (node key₁ value₁ left right ∷ []) Pre next exit with <-cmp key key₁ |
252 ... | tri< a ¬b ¬c = exit (replacePR.tree0 Pre) (node key₁ value₁ repl right ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
253 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right ) | |
790 | 254 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) |
689 | 255 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ k right ) (node key₁ value₁ repl right )) repl02 (r-left a repl03) where |
256 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
257 repl03 = replacePR.ri Pre | |
258 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left | |
259 repl02 with <-cmp key key₁ | |
790 | 260 ... | tri< a ¬b ¬c = refl |
689 | 261 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a) |
262 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a) | |
263 ... | tri≈ ¬a b ¬c = exit (replacePR.tree0 Pre) repl ⟪ replacePR.ti Pre , repl01 ⟫ where | |
790 | 264 repl01 : replacedTree key value (replacePR.tree0 Pre) repl |
265 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
689 | 266 repl01 | refl | refl = subst (λ k → replacedTree key value k repl) repl02 (replacePR.ri Pre) where |
267 repl02 : child-replaced key (node key₁ value₁ left right) ≡ node key₁ value₁ left right | |
268 repl02 with <-cmp key key₁ | |
269 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b) | |
270 ... | tri≈ ¬a b ¬c = refl | |
271 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b) | |
272 ... | tri> ¬a ¬b c = exit (replacePR.tree0 Pre) (node key₁ value₁ left repl ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
273 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl ) | |
790 | 274 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) |
689 | 275 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ left k ) (node key₁ value₁ left repl )) repl02 (r-right c repl03) where |
276 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
277 repl03 = replacePR.ri Pre | |
278 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right | |
279 repl02 with <-cmp key key₁ | |
280 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c) | |
281 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c) | |
790 | 282 ... | tri> ¬a ¬b c = refl |
690 | 283 replaceP {n} {_} {A} key value {tree} repl (leaf ∷ st@(tree1 ∷ st1)) Pre next exit = next key value repl st Post ≤-refl where |
284 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) | |
790 | 285 Post with replacePR.si Pre |
785 | 286 ... | s-right _ _ tree₁ {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
690 | 287 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf |
288 repl09 = si-property1 si | |
289 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
290 repl10 with si-property1 si | |
291 ... | refl = si | |
292 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf | |
790 | 293 repl07 with <-cmp key key₂ |
690 | 294 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) |
295 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
296 ... | tri> ¬a ¬b c = refl | |
297 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
298 repl12 = subst₂ (λ j k → replacedTree key value j k ) (sym (subst (λ k → child-replaced key k ≡ leaf) (sym repl09) repl07 ) ) (sym (rt-property-leaf (replacePR.ri Pre))) r-leaf | |
785 | 299 ... | s-left _ _ tree₁ {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
790 | 300 repl09 : tree1 ≡ node key₂ v1 leaf tree₁ |
690 | 301 repl09 = si-property1 si |
302 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
303 repl10 with si-property1 si | |
304 ... | refl = si | |
305 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf | |
790 | 306 repl07 with <-cmp key key₂ |
690 | 307 ... | tri< a ¬b ¬c = refl |
308 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
309 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
310 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
787 | 311 repl12 = subst₂ (λ j k → replacedTree key value j k ) (sym (subst (λ k → child-replaced key k ≡ leaf) (sym repl09) repl07) ) (sym (rt-property-leaf (replacePR.ri Pre ))) r-leaf |
786 | 312 -- repl12 = subst₂ (λ j k → replacedTree key value j k ) (sym (subst (λ k → child-replaced key k ≡ leaf) (sym repl09) repl07 ) ) (sym (rt-property-leaf (replacePR.ri Pre))) r-leaf |
790 | 313 replaceP {n} {_} {A} key value {tree} repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre next exit with <-cmp key key₁ |
683 | 314 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st Post ≤-refl where |
790 | 315 Post : replacePR key value tree1 (node key₁ value₁ repl right ) st (λ _ _ _ → Lift n ⊤) |
316 Post with replacePR.si Pre | |
785 | 317 ... | s-right _ _ tree₁ {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
790 | 318 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) |
688 | 319 repl09 = si-property1 si |
320 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
321 repl10 with si-property1 si | |
322 ... | refl = si | |
323 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left | |
790 | 324 repl03 with <-cmp key key₁ |
688 | 325 ... | tri< a1 ¬b ¬c = refl |
326 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
327 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
328 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right | |
329 repl02 with repl09 | <-cmp key key₂ | |
330 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
689 | 331 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) |
688 | 332 ... | refl | tri> ¬a ¬b c = refl |
333 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
334 repl04 = begin | |
335 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
336 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
337 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
338 child-replaced key tree1 ∎ where open ≡-Reasoning | |
339 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
790 | 340 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) |
785 | 341 ... | s-left _ _ tree₁ {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
790 | 342 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ |
683 | 343 repl09 = si-property1 si |
344 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
345 repl10 with si-property1 si | |
346 ... | refl = si | |
687 | 347 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left |
790 | 348 repl03 with <-cmp key key₁ |
687 | 349 ... | tri< a1 ¬b ¬c = refl |
350 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
351 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
352 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right | |
353 repl02 with repl09 | <-cmp key key₂ | |
354 ... | refl | tri< a ¬b ¬c = refl | |
355 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
356 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
357 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
358 repl04 = begin | |
359 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
360 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
361 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
362 child-replaced key tree1 ∎ where open ≡-Reasoning | |
363 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
790 | 364 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) |
365 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st Post ≤-refl where | |
690 | 366 Post : replacePR key value tree1 (node key₁ value left right ) (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) |
790 | 367 Post with replacePR.si Pre |
785 | 368 ... | s-right _ _ tree₁ {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 b ; ci = lift tt } where |
690 | 369 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right) |
370 repl09 = si-property1 si | |
371 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
372 repl10 with si-property1 si | |
373 ... | refl = si | |
374 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree | |
790 | 375 repl07 with <-cmp key key₂ |
690 | 376 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) |
377 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
378 ... | tri> ¬a ¬b c = refl | |
691 | 379 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
790 | 380 repl12 refl with repl09 |
691 | 381 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node |
785 | 382 ... | s-left _ _ tree₁ {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 b ; ci = lift tt } where |
790 | 383 repl09 : tree1 ≡ node key₂ v1 tree tree₁ |
690 | 384 repl09 = si-property1 si |
385 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
386 repl10 with si-property1 si | |
387 ... | refl = si | |
388 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree | |
790 | 389 repl07 with <-cmp key key₂ |
690 | 390 ... | tri< a ¬b ¬c = refl |
391 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
392 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
691 | 393 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
790 | 394 repl12 refl with repl09 |
691 | 395 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node |
690 | 396 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st Post ≤-refl where |
790 | 397 Post : replacePR key value tree1 (node key₁ value₁ left repl ) st (λ _ _ _ → Lift n ⊤) |
398 Post with replacePR.si Pre | |
785 | 399 ... | s-right _ _ tree₁ {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
790 | 400 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) |
690 | 401 repl09 = si-property1 si |
402 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
403 repl10 with si-property1 si | |
404 ... | refl = si | |
405 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
790 | 406 repl03 with <-cmp key key₁ |
690 | 407 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) |
408 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
790 | 409 ... | tri> ¬a ¬b c = refl |
690 | 410 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right |
411 repl02 with repl09 | <-cmp key key₂ | |
412 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
413 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) | |
414 ... | refl | tri> ¬a ¬b c = refl | |
415 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
416 repl04 = begin | |
417 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
418 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
419 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
420 child-replaced key tree1 ∎ where open ≡-Reasoning | |
421 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
790 | 422 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) |
785 | 423 ... | s-left _ _ tree₁ {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
790 | 424 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ |
690 | 425 repl09 = si-property1 si |
426 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
427 repl10 with si-property1 si | |
428 ... | refl = si | |
429 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
790 | 430 repl03 with <-cmp key key₁ |
690 | 431 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) |
432 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
790 | 433 ... | tri> ¬a ¬b c = refl |
690 | 434 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right |
435 repl02 with repl09 | <-cmp key key₂ | |
436 ... | refl | tri< a ¬b ¬c = refl | |
437 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
438 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
439 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
440 repl04 = begin | |
441 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
442 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
443 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
444 child-replaced key tree1 ∎ where open ≡-Reasoning | |
445 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
790 | 446 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) |
644 | 447 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
448 TerminatingLoopS : {l m : Level} {t : Set l} (Index : Set m ) → {Invraiant : Index → Set m } → ( reduce : Index → ℕ) |
790 | 449 → (r : Index) → (p : Invraiant r) |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
450 → (loop : (r : Index) → Invraiant r → (next : (r1 : Index) → Invraiant r1 → reduce r1 < reduce r → t ) → t) → t |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
451 TerminatingLoopS {_} {_} {t} Index {Invraiant} reduce r p loop with <-cmp 0 (reduce r) |
790 | 452 ... | tri≈ ¬a b ¬c = loop r p (λ r1 p1 lt → ⊥-elim (lemma3 b lt) ) |
453 ... | tri< a ¬b ¬c = loop r p (λ r1 p1 lt1 → TerminatingLoop1 (reduce r) r r1 (m≤n⇒m≤1+n lt1) p1 lt1 ) where | |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
454 TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → Invraiant r1 → reduce r1 < reduce r → t |
790 | 455 TerminatingLoop1 zero r r1 n≤j p1 lt = loop r1 p1 (λ r2 p1 lt1 → ⊥-elim (lemma5 n≤j lt1)) |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
456 TerminatingLoop1 (suc j) r r1 n≤j p1 lt with <-cmp (reduce r1) (suc j) |
790 | 457 ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a p1 lt |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
458 ... | tri≈ ¬a b ¬c = loop r1 p1 (λ r2 p2 lt1 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b lt1 ) p2 lt1 ) |
790 | 459 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c n≤j ) |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
460 |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
461 open _∧_ |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
462 |
615 | 463 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
464 → replacedTree key value tree repl → treeInvariant repl | |
692 | 465 RTtoTI0 .leaf .(node key value leaf leaf) key value ti r-leaf = t-single key value |
466 RTtoTI0 .(node key _ leaf leaf) .(node key value leaf leaf) key value (t-single .key _) r-node = t-single key value | |
790 | 467 RTtoTI0 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti |
468 RTtoTI0 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
692 | 469 RTtoTI0 .(node key _ (node _ _ _ _) (node _ _ _ _)) .(node key value (node _ _ _ _) (node _ _ _ _)) key value (t-node x x₁ ti ti₁) r-node = t-node x x₁ ti ti₁ |
701 | 470 -- r-right case |
692 | 471 RTtoTI0 (node _ _ leaf leaf) (node _ _ leaf .(node key value leaf leaf)) key value (t-single _ _) (r-right x r-leaf) = t-right x (t-single key value) |
472 RTtoTI0 (node _ _ leaf right@(node _ _ _ _)) (node key₁ value₁ leaf leaf) key value (t-right x₁ ti) (r-right x ri) = t-single key₁ value₁ | |
790 | 473 RTtoTI0 (node key₁ _ leaf right@(node key₂ _ _ _)) (node key₁ value₁ leaf right₁@(node key₃ _ _ _)) key value (t-right x₁ ti) (r-right x ri) = |
693 | 474 t-right (subst (λ k → key₁ < k ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) |
692 | 475 RTtoTI0 (node key₁ _ (node _ _ _ _) leaf) (node key₁ _ (node key₃ value left right) leaf) key value₁ (t-left x₁ ti) (r-right x ()) |
476 RTtoTI0 (node key₁ _ (node key₃ _ _ _) leaf) (node key₁ _ (node key₃ value₃ _ _) (node key value leaf leaf)) key value (t-left x₁ ti) (r-right x r-leaf) = | |
790 | 477 t-node x₁ x ti (t-single key value) |
693 | 478 RTtoTI0 (node key₁ _ (node _ _ _ _) (node key₂ _ _ _)) (node key₁ _ (node _ _ _ _) (node key₃ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) = |
479 t-node x₁ (subst (λ k → key₁ < k) (rt-property-key ri) x₂) ti (RTtoTI0 _ _ key value ti₁ ri) | |
701 | 480 -- r-left case |
700 | 481 RTtoTI0 .(node _ _ leaf leaf) .(node _ _ (node key value leaf leaf) leaf) key value (t-single _ _) (r-left x r-leaf) = t-left x (t-single _ _ ) |
701 | 482 RTtoTI0 .(node _ _ leaf (node _ _ _ _)) (node key₁ value₁ (node key value leaf leaf) (node _ _ _ _)) key value (t-right x₁ ti) (r-left x r-leaf) = t-node x x₁ (t-single key value) ti |
483 RTtoTI0 (node key₃ _ (node key₂ _ _ _) leaf) (node key₃ _ (node key₁ value₁ left left₁) leaf) key value (t-left x₁ ti) (r-left x ri) = | |
484 t-left (subst (λ k → k < key₃ ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) -- key₁ < key₃ | |
485 RTtoTI0 (node key₁ _ (node key₂ _ _ _) (node _ _ _ _)) (node key₁ _ (node key₃ _ _ _) (node _ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) = t-node (subst (λ k → k < key₁ ) (rt-property-key ri) x₁) x₂ (RTtoTI0 _ _ key value ti ri) ti₁ | |
615 | 486 |
487 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl | |
488 → replacedTree key value tree repl → treeInvariant tree | |
701 | 489 RTtoTI1 .leaf .(node key value leaf leaf) key value ti r-leaf = t-leaf |
490 RTtoTI1 (node key value₁ leaf leaf) .(node key value leaf leaf) key value (t-single .key .value) r-node = t-single key value₁ | |
491 RTtoTI1 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti | |
492 RTtoTI1 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
493 RTtoTI1 .(node key _ (node _ _ _ _) (node _ _ _ _)) .(node key value (node _ _ _ _) (node _ _ _ _)) key value (t-node x x₁ ti ti₁) r-node = t-node x x₁ ti ti₁ | |
494 -- r-right case | |
495 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ leaf (node _ _ _ _)) key value (t-right x₁ ti) (r-right x r-leaf) = t-single key₁ value₁ | |
496 RTtoTI1 (node key₁ value₁ leaf (node key₂ value₂ t2 t3)) (node key₁ _ leaf (node key₃ _ _ _)) key value (t-right x₁ ti) (r-right x ri) = | |
497 t-right (subst (λ k → key₁ < k ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₁ < key₂ | |
498 RTtoTI1 (node _ _ (node _ _ _ _) leaf) (node _ _ (node _ _ _ _) (node key value _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x r-leaf) = | |
499 t-left x₁ ti | |
500 RTtoTI1 (node key₄ _ (node key₃ _ _ _) (node key₁ value₁ n n₁)) (node key₄ _ (node key₃ _ _ _) (node key₂ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) = t-node x₁ (subst (λ k → key₄ < k ) (sym (rt-property-key ri)) x₂) ti (RTtoTI1 _ _ key value ti₁ ri) -- key₄ < key₁ | |
501 -- r-left case | |
502 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ _ leaf) key value (t-left x₁ ti) (r-left x ri) = t-single key₁ value₁ | |
790 | 503 RTtoTI1 (node key₁ _ (node key₂ value₁ n n₁) leaf) (node key₁ _ (node key₃ _ _ _) leaf) key value (t-left x₁ ti) (r-left x ri) = |
701 | 504 t-left (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₂ < key₁ |
505 RTtoTI1 (node key₁ value₁ leaf _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x r-leaf) = t-right x₂ ti₁ | |
790 | 506 RTtoTI1 (node key₁ value₁ (node key₂ value₂ n n₁) _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) = |
701 | 507 t-node (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) x₂ (RTtoTI1 _ _ key value ti ri) ti₁ -- key₂ < key₁ |
614 | 508 |
611 | 509 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
696 | 510 → (exit : (tree repl : bt A) → treeInvariant repl ∧ replacedTree key value tree repl → t ) → t |
693 | 511 insertTreeP {n} {m} {A} {t} tree key value P0 exit = |
729 | 512 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → treeInvariant (proj1 p) ∧ stackInvariant key (proj1 p) tree (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tree , tree ∷ [] ⟫ ⟪ P0 , s-nil ⟫ |
790 | 513 $ λ p P loop → findP key (proj1 p) tree (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
693 | 514 $ λ t s P C → replaceNodeP key value t C (proj1 P) |
515 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A ) | |
516 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) } | |
790 | 517 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = tree ; ti = P0 ; si = proj2 P ; ri = R ; ci = lift tt } |
693 | 518 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1 |
696 | 519 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt ) |
823 | 520 $ λ tree repl P → exit tree repl {!!} --exit tree repl ⟪ RTtoTI0 _ _ _ _ (proj1 P) (proj2 P) , proj2 P ⟫ |
614 | 521 |
696 | 522 insertTestP1 = insertTreeP leaf 1 1 t-leaf |
790 | 523 $ λ _ x0 P0 → insertTreeP x0 2 1 (proj1 P0) |
524 $ λ _ x1 P1 → insertTreeP x1 3 2 (proj1 P1) | |
727 | 525 $ λ _ x2 P2 → insertTreeP x2 2 2 (proj1 P2) (λ _ x P → x ) |
694 | 526 |
790 | 527 top-value : {n : Level} {A : Set n} → (tree : bt A) → Maybe A |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
528 top-value leaf = nothing |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
529 top-value (node key value tree tree₁) = just value |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
530 |
722 | 531 -- is realy inserted? |
532 | |
533 -- other element is preserved? | |
702 | 534 |
722 | 535 -- deletion? |
807 | 536 |
537 | |
722 | 538 data Color : Set where |
539 Red : Color | |
540 Black : Color | |
618 | 541 |
790 | 542 RB→bt : {n : Level} (A : Set n) → (bt (Color ∧ A)) → bt A |
785 | 543 RB→bt {n} A leaf = leaf |
544 RB→bt {n} A (node key ⟪ C , value ⟫ tr t1) = (node key value (RB→bt A tr) (RB→bt A t1)) | |
545 | |
546 color : {n : Level} {A : Set n} → (bt (Color ∧ A)) → Color | |
547 color leaf = Black | |
548 color (node key ⟪ C , value ⟫ rb rb₁) = C | |
549 | |
819 | 550 to-red : {n : Level} {A : Set n} → (tree : bt (Color ∧ A)) → bt (Color ∧ A) |
551 to-red leaf = leaf | |
552 to-red (node key ⟪ _ , value ⟫ t t₁) = (node key ⟪ Red , value ⟫ t t₁) | |
553 | |
554 to-black : {n : Level} {A : Set n} → (tree : bt (Color ∧ A)) → bt (Color ∧ A) | |
555 to-black leaf = leaf | |
556 to-black (node key ⟪ _ , value ⟫ t t₁) = (node key ⟪ Black , value ⟫ t t₁) | |
557 | |
785 | 558 black-depth : {n : Level} {A : Set n} → (tree : bt (Color ∧ A) ) → ℕ |
559 black-depth leaf = 0 | |
790 | 560 black-depth (node key ⟪ Red , value ⟫ t t₁) = black-depth t ⊔ black-depth t₁ |
785 | 561 black-depth (node key ⟪ Black , value ⟫ t t₁) = suc (black-depth t ⊔ black-depth t₁ ) |
562 | |
802 | 563 zero≢suc : { m : ℕ } → zero ≡ suc m → ⊥ |
564 zero≢suc () | |
565 suc≢zero : {m : ℕ } → suc m ≡ zero → ⊥ | |
566 suc≢zero () | |
810 | 567 |
800 | 568 |
785 | 569 data RBtreeInvariant {n : Level} {A : Set n} : (tree : bt (Color ∧ A)) → Set n where |
807 | 570 rb-leaf : RBtreeInvariant leaf |
571 rb-single : {c : Color} → (key : ℕ) → (value : A) → RBtreeInvariant (node key ⟪ c , value ⟫ leaf leaf) | |
785 | 572 rb-right-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ |
790 | 573 → black-depth t ≡ black-depth t₁ |
574 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁) | |
785 | 575 → RBtreeInvariant (node key ⟪ Red , value ⟫ leaf (node key₁ ⟪ Black , value₁ ⟫ t t₁)) |
576 rb-right-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {c : Color} | |
577 → black-depth t ≡ black-depth t₁ | |
790 | 578 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁) |
579 → RBtreeInvariant (node key ⟪ Black , value ⟫ leaf (node key₁ ⟪ c , value₁ ⟫ t t₁)) | |
807 | 580 rb-left-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key₁ < key |
785 | 581 → black-depth t ≡ black-depth t₁ |
790 | 582 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁) |
583 → RBtreeInvariant (node key ⟪ Red , value ⟫ (node key₁ ⟪ Black , value₁ ⟫ t t₁) leaf ) | |
807 | 584 rb-left-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → {c : Color} → key₁ < key |
785 | 585 → black-depth t ≡ black-depth t₁ |
790 | 586 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁) |
587 → RBtreeInvariant (node key ⟪ Black , value ⟫ (node key₁ ⟪ c , value₁ ⟫ t t₁) leaf) | |
785 | 588 rb-node-red : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂ |
807 | 589 → black-depth (node key ⟪ Black , value ⟫ t₁ t₂) ≡ black-depth (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄) |
785 | 590 → RBtreeInvariant (node key ⟪ Black , value ⟫ t₁ t₂) |
591 → RBtreeInvariant (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄) | |
592 → RBtreeInvariant (node key₁ ⟪ Red , value₁ ⟫ (node key ⟪ Black , value ⟫ t₁ t₂) (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄)) | |
807 | 593 rb-node-black : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂ |
746 | 594 → {c c₁ : Color} |
807 | 595 → black-depth (node key ⟪ c , value ⟫ t₁ t₂) ≡ black-depth (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄) |
785 | 596 → RBtreeInvariant (node key ⟪ c , value ⟫ t₁ t₂) |
790 | 597 → RBtreeInvariant (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄) |
785 | 598 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ (node key ⟪ c , value ⟫ t₁ t₂) (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄)) |
779
7e5dfe642064
remove black depth on RBTreeInvariant and introduce black-count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
778
diff
changeset
|
599 |
814 | 600 -- data rotatedTree {n : Level} {A : Set n} : (before after : bt A) → Set n where |
601 -- rtt-unit : {t : bt A} → rotatedTree t t | |
602 -- rtt-node : {left left' right right' : bt A} → {ke : ℕ} {ve : A} → | |
603 -- rotatedTree left left' → rotatedTree right right' → rotatedTree (node ke ve left right) (node ke ve left' right') | |
604 -- -- a b | |
605 -- -- b c d a | |
606 -- -- d e e c | |
607 -- -- | |
608 -- rtt-right : {ka kb kc kd ke : ℕ} {va vb vc vd ve : A} → {c d e c1 d1 e1 : bt A} → {ctl ctr dtl dtr etl etr : bt A} | |
609 -- --kd < kb < ke < ka< kc | |
610 -- → {ctl1 ctr1 dtl1 dtr1 etl1 etr1 : bt A} | |
611 -- → kd < kb → kb < ke → ke < ka → ka < kc | |
612 -- → rotatedTree (node ke ve etl etr) (node ke ve etl1 etr1) | |
613 -- → rotatedTree (node kd vd dtl dtr) (node kd vd dtl1 dtr1) | |
614 -- → rotatedTree (node kc vc ctl ctr) (node kc vc ctl1 ctr1) | |
615 -- → rotatedTree (node ka va (node kb vb (node kd vd dtl dtr) (node ke ve etl etr)) (node kc vc ctl ctr)) | |
616 -- (node kb vb (node kd vd dtl1 dtr1) (node ka va (node ke ve etl1 etr1) (node kc vc ctl1 ctr1))) | |
617 -- | |
618 -- rtt-left : {ka kb kc kd ke : ℕ} {va vb vc vd ve : A} → {c d e c1 d1 e1 : bt A} → {ctl ctr dtl dtr etl etr : bt A} | |
619 -- --kd < kb < ke < ka< kc | |
620 -- → {ctl1 ctr1 dtl1 dtr1 etl1 etr1 : bt A} -- after child | |
621 -- → kd < kb → kb < ke → ke < ka → ka < kc | |
622 -- → rotatedTree (node ke ve etl etr) (node ke ve etl1 etr1) | |
623 -- → rotatedTree (node kd vd dtl dtr) (node kd vd dtl1 dtr1) | |
624 -- → rotatedTree (node kc vc ctl ctr) (node kc vc ctl1 ctr1) | |
625 -- → rotatedTree (node kb vb (node kd vd dtl1 dtr1) (node ka va (node ke ve etl1 etr1) (node kc vc ctl1 ctr1))) | |
626 -- (node ka va (node kb vb (node kd vd dtl dtr) (node ke ve etl etr)) (node kc vc ctl ctr)) | |
627 -- | |
805 | 628 RightDown : {n : Level} {A : Set n} → bt (Color ∧ A) → bt (Color ∧ A) |
629 RightDown leaf = leaf | |
630 RightDown (node key ⟪ c , value ⟫ t1 t2) = t2 | |
806 | 631 LeftDown : {n : Level} {A : Set n} → bt (Color ∧ A) → bt (Color ∧ A) |
632 LeftDown leaf = leaf | |
633 LeftDown (node key ⟪ c , value ⟫ t1 t2 ) = t1 | |
634 | |
785 | 635 RBtreeLeftDown : {n : Level} {A : Set n} {key : ℕ} {value : A} {c : Color} |
636 → (tleft tright : bt (Color ∧ A)) | |
790 | 637 → RBtreeInvariant (node key ⟪ c , value ⟫ tleft tright) |
785 | 638 → RBtreeInvariant tleft |
639 RBtreeLeftDown leaf leaf (rb-single k1 v) = rb-leaf | |
640 RBtreeLeftDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-red x bde rbti) = rb-leaf | |
641 RBtreeLeftDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-black x bde rbti) = rb-leaf | |
642 RBtreeLeftDown leaf (node key ⟪ Red , value ⟫ t1 t2 ) (rb-right-black x bde rbti)= rb-leaf | |
643 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-black x bde ti) = ti | |
644 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-red x bde ti)= ti | |
645 RBtreeLeftDown (node key ⟪ Red , value ⟫ t t₁) leaf (rb-left-black x bde ti) = ti | |
807 | 646 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = til |
647 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-red x x1 bde1 til tir) = til | |
648 RBtreeLeftDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = til | |
649 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = til | |
650 RBtreeLeftDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = til | |
758 | 651 |
785 | 652 RBtreeRightDown : {n : Level} {A : Set n} { key : ℕ} {value : A} {c : Color} |
653 → (tleft tright : bt (Color ∧ A)) | |
790 | 654 → RBtreeInvariant (node key ⟪ c , value ⟫ tleft tright) |
655 → RBtreeInvariant tright | |
785 | 656 RBtreeRightDown leaf leaf (rb-single k1 v1 ) = rb-leaf |
657 RBtreeRightDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-red x bde rbti) = rbti | |
658 RBtreeRightDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-black x bde rbti) = rbti | |
659 RBtreeRightDown leaf (node key ⟪ Red , value ⟫ t1 t2 ) (rb-right-black x bde rbti)= rbti | |
660 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-black x bde ti) = rb-leaf | |
661 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-red x bde ti) = rb-leaf | |
662 RBtreeRightDown (node key ⟪ Red , value ⟫ t t₁) leaf (rb-left-black x bde ti) = rb-leaf | |
807 | 663 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = tir |
664 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-red x x1 bde1 til tir) = tir | |
665 RBtreeRightDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = tir | |
666 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = tir | |
667 RBtreeRightDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til tir) = tir | |
723 | 668 |
814 | 669 -- |
670 -- findRBT exit with replaced node | |
671 -- case-eq node value is replaced, just do replacedTree and rebuild rb-invariant | |
672 -- case-leaf insert new single node | |
673 -- case1 if parent node is black, just do replacedTree and rebuild rb-invariant | |
674 -- case2 if parent node is red, increase blackdepth, do rotatation | |
675 -- | |
676 | |
790 | 677 findRBT : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt (Color ∧ A) ) |
785 | 678 → (stack : List (bt (Color ∧ A))) |
808 | 679 → RBtreeInvariant tree ∧ stackInvariant key tree tree0 stack |
785 | 680 → (next : (tree1 : bt (Color ∧ A) ) → (stack : List (bt (Color ∧ A))) |
808 | 681 → RBtreeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack |
785 | 682 → bt-depth tree1 < bt-depth tree → t ) |
790 | 683 → (exit : (tree1 : bt (Color ∧ A)) → (stack : List (bt (Color ∧ A))) |
808 | 684 → RBtreeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack |
785 | 685 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
808 | 686 findRBT key leaf tree0 stack rb0 next exit = exit leaf stack rb0 (case1 refl) |
687 findRBT key (node key₁ value left right) tree0 stack rb0 next exit with <-cmp key key₁ | |
688 findRBT key (node key₁ value left right) tree0 stack rb0 next exit | tri< a ¬b ¬c | |
689 = next left (left ∷ stack) ⟪ RBtreeLeftDown left right (_∧_.proj1 rb0) , s-left _ _ _ a (_∧_.proj2 rb0) ⟫ depth-1< | |
690 findRBT key n tree0 stack rb0 _ exit | tri≈ ¬a refl ¬c = exit n stack rb0 (case2 refl) | |
691 findRBT key (node key₁ value left right) tree0 stack rb0 next exit | tri> ¬a ¬b c | |
692 = next right (right ∷ stack) ⟪ RBtreeRightDown left right (_∧_.proj1 rb0), s-right _ _ _ c (_∧_.proj2 rb0) ⟫ depth-2< | |
785 | 693 |
694 | |
808 | 695 |
696 findTest : {n m : Level} {A : Set n } {t : Set m } | |
697 → (key : ℕ) | |
698 → (tree0 : bt (Color ∧ A)) | |
699 → RBtreeInvariant tree0 | |
700 → (exit : (tree1 : bt (Color ∧ A)) | |
701 → (stack : List (bt (Color ∧ A))) | |
702 → RBtreeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
703 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t | |
704 findTest {n} {m} {A} {t} k tr0 rb0 exit = TerminatingLoopS (bt (Color ∧ A) ∧ List (bt (Color ∧ A))) | |
705 {λ p → RBtreeInvariant (proj1 p) ∧ stackInvariant k (proj1 p) tr0 (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tr0 , tr0 ∷ [] ⟫ ⟪ rb0 , s-nil ⟫ | |
706 $ λ p RBP loop → findRBT k (proj1 p) tr0 (proj2 p) RBP (λ t1 s1 P2 lt1 → loop ⟪ t1 , s1 ⟫ P2 lt1 ) | |
707 $ λ tr1 st P2 O → exit tr1 st P2 O | |
708 | |
709 | |
710 testRBTree0 : bt (Color ∧ ℕ) | |
711 testRBTree0 = node 8 ⟪ Black , 800 ⟫ (node 5 ⟪ Red , 500 ⟫ (node 2 ⟪ Black , 200 ⟫ leaf leaf) (node 6 ⟪ Black , 600 ⟫ leaf leaf)) (node 10 ⟪ Red , 1000 ⟫ (leaf) (node 15 ⟪ Black , 1500 ⟫ (node 14 ⟪ Red , 1400 ⟫ leaf leaf) leaf)) | |
712 | |
713 record result {n : Level} {A : Set n} {key : ℕ} {tree0 : bt (Color ∧ A)} : Set n where | |
714 field | |
715 tree : bt (Color ∧ A) | |
716 stack : List (bt (Color ∧ A)) | |
717 ti : RBtreeInvariant tree | |
718 si : stackInvariant key tree tree0 stack | |
719 | |
720 testRBI0 : RBtreeInvariant testRBTree0 | |
721 testRBI0 = rb-node-black (add< 2) (add< 1) refl (rb-node-red (add< 2) (add< 0) refl (rb-single 2 200) (rb-single 6 600)) (rb-right-red (add< 4) refl (rb-left-black (add< 0) refl (rb-single 14 1400) )) | |
722 | |
723 findRBTreeTest : result | |
724 findRBTreeTest = findTest 14 testRBTree0 testRBI0 | |
725 $ λ tr s P O → (record {tree = tr ; stack = s ; ti = (proj1 P) ; si = (proj2 P)}) | |
726 | |
814 | 727 -- create replaceRBTree with rotate |
728 | |
729 data replacedRBTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt (Color ∧ A) ) → Set n where | |
821 | 730 rbr-leaf : replacedRBTree key value leaf (node key ⟪ Red , value ⟫ leaf leaf) |
731 rbr-node : {value₁ : A} → {ca : Color } → {t t₁ : bt (Color ∧ A)} → replacedRBTree key value (node key ⟪ ca , value₁ ⟫ t t₁) (node key ⟪ ca , value ⟫ t t₁) | |
732 rbr-right : {k : ℕ } {v1 : A} → {ca : Color} → {t t1 t2 : bt (Color ∧ A)} | |
733 → k < key → replacedRBTree key value t2 t → replacedRBTree key value (node k ⟪ ca , v1 ⟫ t1 t2) (node k ⟪ ca , v1 ⟫ t1 t) | |
734 rbr-left : {k : ℕ } {v1 : A} → {ca : Color} → {t t1 t2 : bt (Color ∧ A)} | |
735 → key < k → replacedRBTree key value t1 t → replacedRBTree key value (node k ⟪ ca , v1 ⟫ t1 t2) (node k ⟪ ca , v1 ⟫ t t2) -- k < key → key < k | |
736 | |
737 rbr-black-right : {t t₁ t₂ : bt (Color ∧ A)} {value₁ : A} {key₁ : ℕ} | |
738 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ | |
739 → replacedRBTree key value (node key₁ ⟪ Black , value₁ ⟫ t t₁) (node key₁ ⟪ Black , value₁ ⟫ t t₂) | |
740 rbr-black-left : {t t₁ t₂ : bt (Color ∧ A)} {value₁ : A} {key₁ : ℕ} | |
741 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ | |
742 → replacedRBTree key value (node key₁ ⟪ Black , value₁ ⟫ t₁ t) (node key₁ ⟪ Black , value₁ ⟫ t₂ t) | |
743 | |
744 rbr-flip-ll : {t t₁ t₂ uncle : bt (Color ∧ A)} {kg kp : ℕ} {vg vp : A} | |
745 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ | |
746 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ (node kp ⟪ Red , vp ⟫ t₁ t) uncle) | |
747 (node kg ⟪ Red , vg ⟫ (node kp ⟪ Black , vp ⟫ t₂ t) (to-black uncle)) | |
748 rbr-flip-lr : {t t₁ t₂ uncle : bt (Color ∧ A)} {kg kp : ℕ} {vg vp : A} | |
749 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ | |
750 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ (node kp ⟪ Red , vp ⟫ t t₁) uncle) | |
751 (node kg ⟪ Red , vg ⟫ (node kp ⟪ Black , vp ⟫ t t₂) (to-black uncle)) | |
752 rbr-flip-rl : {t t₁ t₂ uncle : bt (Color ∧ A)} {kg kp : ℕ} {vg vp : A} | |
753 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ | |
754 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ uncle (node kp ⟪ Red , vp ⟫ t₁ t)) | |
755 (node kg ⟪ Red , vg ⟫ (to-black uncle) (node kp ⟪ Black , vp ⟫ t₂ t)) | |
756 rbr-flip-rr : {t t₁ t₂ uncle : bt (Color ∧ A)} {kg kp : ℕ} {vg vp : A} | |
757 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ | |
758 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ uncle (node kp ⟪ Red , vp ⟫ t t₁)) | |
759 (node kg ⟪ Red , vg ⟫ (to-black uncle) (node kp ⟪ Black , vp ⟫ t t₂)) | |
760 | |
822 | 761 rbr-rotate-ll : {t t₁ t₂ uncle : bt (Color ∧ A)} {kg kp : ℕ} {vg vp : A} -- case6 |
821 | 762 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ |
763 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ (node kp ⟪ Red , vp ⟫ t₁ t) uncle) | |
764 (node kp ⟪ Black , vp ⟫ t₂ (node kg ⟪ Red , vg ⟫ t uncle)) | |
822 | 765 rbr-rotate-rr : {t t₁ t₂ uncle : bt (Color ∧ A)} {kg kp : ℕ} {vg vp : A} -- case6 |
821 | 766 → color t₂ ≡ Red → replacedRBTree key value t₁ t₂ |
767 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ uncle (node kp ⟪ Red , vp ⟫ t t₁)) | |
768 (node kp ⟪ Black , vp ⟫ (node kg ⟪ Red , vg ⟫ uncle t) t₂ ) | |
822 | 769 rbr-rotate-lr : {t t₁ t₂ t₃ uncle : bt (Color ∧ A)} {kg kp kn : ℕ} {vg vp vn : A} -- case56 |
821 | 770 → replacedRBTree key value t (node kn ⟪ Red , vn ⟫ t₁ t₂) |
771 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ (node kp ⟪ Red , vp ⟫ t₃ t) uncle) | |
772 (node kn ⟪ Black , vn ⟫ (node kp ⟪ Red , vp ⟫ t₃ t₁) (node kg ⟪ Red , vg ⟫ t₂ uncle)) | |
822 | 773 rbr-rotate-rl : {t t₁ t₂ t₃ uncle : bt (Color ∧ A)} {kg kp kn : ℕ} {vg vp vn : A} -- case56 |
821 | 774 → replacedRBTree key value t (node kn ⟪ Red , vn ⟫ t₁ t₂) |
775 → replacedRBTree key value (node kg ⟪ Black , vg ⟫ uncle (node kp ⟪ Red , vp ⟫ t t₃)) | |
776 (node kn ⟪ Black , vn ⟫ (node kg ⟪ Red , vg ⟫ t₁ uncle) (node kp ⟪ Red , vp ⟫ t₂ t₃)) | |
777 | |
723 | 778 |
754 | 779 data ParentGrand {n : Level} {A : Set n} (self : bt A) : (parent uncle grand : bt A) → Set n where |
790 | 780 s2-s1p2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } |
781 → parent ≡ node kp vp self n1 → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand | |
782 s2-1sp2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
783 → parent ≡ node kp vp n1 self → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand | |
784 s2-s12p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
785 → parent ≡ node kp vp self n1 → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand | |
786 s2-1s2p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
787 → parent ≡ node kp vp n1 self → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand | |
734 | 788 |
749 | 789 record PG {n : Level } (A : Set n) (self : bt A) (stack : List (bt A)) : Set n where |
740 | 790 field |
754 | 791 parent grand uncle : bt A |
792 pg : ParentGrand self parent uncle grand | |
740 | 793 rest : List (bt A) |
794 stack=gp : stack ≡ ( self ∷ parent ∷ grand ∷ rest ) | |
795 | |
814 | 796 -- |
797 -- RBI : Invariant on InsertCase2 | |
798 -- color repl ≡ Red ∧ black-depth repl ≡ suc (black-depth tree) | |
799 -- | |
800 | |
801 data RBI-state {n : Level} {A : Set n} (key : ℕ) : (tree repl : bt (Color ∧ A) ) → Set n where | |
821 | 802 rebuild : {tree repl : bt (Color ∧ A) } → black-depth repl ≡ black-depth (child-replaced key tree) |
803 → RBI-state key tree repl -- one stage up | |
804 rotate : {tree repl : bt (Color ∧ A) } → color repl ≡ Red → black-depth repl ≡ black-depth (child-replaced key tree) | |
805 → RBI-state key tree repl -- two stages up | |
814 | 806 |
785 | 807 record RBI {n : Level} {A : Set n} (key : ℕ) (value : A) (orig repl : bt (Color ∧ A) ) (stack : List (bt (Color ∧ A))) : Set n where |
808 field | |
814 | 809 tree : bt (Color ∧ A) |
790 | 810 origti : treeInvariant orig |
811 origrb : RBtreeInvariant orig | |
794 | 812 treerb : RBtreeInvariant tree -- tree node te be replaced |
790 | 813 replrb : RBtreeInvariant repl |
785 | 814 si : stackInvariant key tree orig stack |
814 | 815 rotated : replacedRBTree key value tree repl |
816 state : RBI-state key tree repl | |
785 | 817 |
819 | 818 -- |
819 -- if we consider tree invariant, this may be much simpler and faster | |
820 -- | |
740 | 821 stackToPG : {n : Level} {A : Set n} → {key : ℕ } → (tree orig : bt A ) |
822 → (stack : List (bt A)) → stackInvariant key tree orig stack | |
749 | 823 → ( stack ≡ orig ∷ [] ) ∨ ( stack ≡ tree ∷ orig ∷ [] ) ∨ PG A tree stack |
740 | 824 stackToPG {n} {A} {key} tree .tree .(tree ∷ []) s-nil = case1 refl |
785 | 825 stackToPG {n} {A} {key} tree .(node _ _ _ tree) .(tree ∷ node _ _ _ tree ∷ []) (s-right _ _ _ x s-nil) = case2 (case1 refl) |
790 | 826 stackToPG {n} {A} {key} tree .(node k2 v2 t5 (node k1 v1 t2 tree)) (tree ∷ node _ _ _ tree ∷ .(node k2 v2 t5 (node k1 v1 t2 tree) ∷ [])) (s-right tree (node k2 v2 t5 (node k1 v1 t2 tree)) t2 {k1} {v1} x (s-right (node k1 v1 t2 tree) (node k2 v2 t5 (node k1 v1 t2 tree)) t5 {k2} {v2} x₁ s-nil)) = case2 (case2 |
749 | 827 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) |
790 | 828 stackToPG {n} {A} {key} tree orig (tree ∷ node _ _ _ tree ∷ .(node k2 v2 t5 (node k1 v1 t2 tree) ∷ _)) (s-right tree orig t2 {k1} {v1} x (s-right (node k1 v1 t2 tree) orig t5 {k2} {v2} x₁ (s-right _ _ _ x₂ si))) = case2 (case2 |
749 | 829 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 830 stackToPG {n} {A} {key} tree orig (tree ∷ node _ _ _ tree ∷ .(node k2 v2 t5 (node k1 v1 t2 tree) ∷ _)) (s-right tree orig t2 {k1} {v1} x (s-right (node k1 v1 t2 tree) orig t5 {k2} {v2} x₁ (s-left _ _ _ x₂ si))) = case2 (case2 |
749 | 831 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 832 stackToPG {n} {A} {key} tree .(node k2 v2 (node k1 v1 t1 tree) t2) .(tree ∷ node k1 v1 t1 tree ∷ node k2 v2 (node k1 v1 t1 tree) t2 ∷ []) (s-right _ _ t1 {k1} {v1} x (s-left _ _ t2 {k2} {v2} x₁ s-nil)) = case2 (case2 |
749 | 833 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 834 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 t1 tree ∷ node k2 v2 (node k1 v1 t1 tree) t2 ∷ _) (s-right _ _ t1 {k1} {v1} x (s-left _ _ t2 {k2} {v2} x₁ (s-right _ _ _ x₂ si))) = case2 (case2 |
749 | 835 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 836 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 t1 tree ∷ node k2 v2 (node k1 v1 t1 tree) t2 ∷ _) (s-right _ _ t1 {k1} {v1} x (s-left _ _ t2 {k2} {v2} x₁ (s-left _ _ _ x₂ si))) = case2 (case2 |
749 | 837 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 838 stackToPG {n} {A} {key} tree .(node _ _ tree _) .(tree ∷ node _ _ tree _ ∷ []) (s-left _ _ t1 {k1} {v1} x s-nil) = case2 (case1 refl) |
839 stackToPG {n} {A} {key} tree .(node _ _ _ (node k1 v1 tree t1)) .(tree ∷ node k1 v1 tree t1 ∷ node _ _ _ (node k1 v1 tree t1) ∷ []) (s-left _ _ t1 {k1} {v1} x (s-right _ _ _ x₁ s-nil)) = case2 (case2 | |
749 | 840 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 841 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ _ (node k1 v1 tree t1) ∷ _) (s-left _ _ t1 {k1} {v1} x (s-right _ _ _ x₁ (s-right _ _ _ x₂ si))) = case2 (case2 |
749 | 842 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 843 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ _ (node k1 v1 tree t1) ∷ _) (s-left _ _ t1 {k1} {v1} x (s-right _ _ _ x₁ (s-left _ _ _ x₂ si))) = case2 (case2 |
749 | 844 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 845 stackToPG {n} {A} {key} tree .(node _ _ (node k1 v1 tree t1) _) .(tree ∷ node k1 v1 tree t1 ∷ node _ _ (node k1 v1 tree t1) _ ∷ []) (s-left _ _ t1 {k1} {v1} x (s-left _ _ _ x₁ s-nil)) = case2 (case2 |
749 | 846 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 847 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ (node k1 v1 tree t1) _ ∷ _) (s-left _ _ t1 {k1} {v1} x (s-left _ _ _ x₁ (s-right _ _ _ x₂ si))) = case2 (case2 |
749 | 848 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 849 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ (node k1 v1 tree t1) _ ∷ _) (s-left _ _ t1 {k1} {v1} x (s-left _ _ _ x₁ (s-left _ _ _ x₂ si))) = case2 (case2 |
749 | 850 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) |
740 | 851 |
785 | 852 stackCase1 : {n : Level} {A : Set n} → {key : ℕ } → {tree orig : bt A } |
853 → {stack : List (bt A)} → stackInvariant key tree orig stack | |
854 → stack ≡ orig ∷ [] → tree ≡ orig | |
855 stackCase1 s-nil refl = refl | |
760 | 856 |
749 | 857 PGtoRBinvariant : {n : Level} {A : Set n} → {key d0 ds dp dg : ℕ } → (tree orig : bt (Color ∧ A) ) |
790 | 858 → RBtreeInvariant orig |
749 | 859 → (stack : List (bt (Color ∧ A))) → (pg : PG (Color ∧ A) tree stack ) |
790 | 860 → RBtreeInvariant tree ∧ RBtreeInvariant (PG.parent pg) ∧ RBtreeInvariant (PG.grand pg) |
758 | 861 PGtoRBinvariant = {!!} |
734 | 862 |
790 | 863 RBI-child-replaced : {n : Level} {A : Set n} (tr : bt (Color ∧ A)) (key : ℕ) → RBtreeInvariant tr → RBtreeInvariant (child-replaced key tr) |
789
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
864 RBI-child-replaced {n} {A} leaf key rbi = rbi |
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
865 RBI-child-replaced {n} {A} (node key₁ value tr tr₁) key rbi with <-cmp key key₁ |
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
866 ... | tri< a ¬b ¬c = RBtreeLeftDown _ _ rbi |
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
867 ... | tri≈ ¬a b ¬c = rbi |
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
868 ... | tri> ¬a ¬b c = RBtreeRightDown _ _ rbi |
802 | 869 |
823 | 870 -- this is too complacted to extend all arguments at once |
871 -- | |
822 | 872 RBTtoRBI : {n : Level} {A : Set n} → (tree repl : bt (Color ∧ A)) → (key : ℕ) → (value : A) → RBtreeInvariant tree |
873 → replacedRBTree key value tree repl → RBtreeInvariant repl | |
823 | 874 RBTtoRBI {_} {A} tree repl key value rbi rlt = ? |
815 | 875 -- |
876 -- create RBT invariant after findRBT, continue to replaceRBT | |
877 -- | |
878 replaceRBTNode : {n m : Level} {A : Set n } {t : Set m } | |
879 → (key : ℕ) (value : A) | |
880 → (tree0 : bt (Color ∧ A)) | |
881 → RBtreeInvariant tree0 | |
882 → (tree1 : bt (Color ∧ A)) | |
883 → (stack : List (bt (Color ∧ A))) | |
884 → RBtreeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
885 → (exit : (r : RBI key value tree0 tree1 stack ) → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key )) → t | |
886 replaceRBTNode = ? | |
887 | |
888 -- | |
889 -- RBT is blanced with the stack, simply rebuild tree without totation | |
890 -- | |
891 rebuildRBT : {n m : Level} {A : Set n} {t : Set m} | |
892 → (key : ℕ) → (value : A) | |
893 → (orig repl : bt (Color ∧ A)) | |
894 → (stack : List (bt (Color ∧ A))) | |
895 → (r : RBI key value orig repl stack ) | |
896 → black-depth repl ≡ black-depth (child-replaced key (RBI.tree r)) | |
897 → (next : (repl1 : (bt (Color ∧ A))) → (stack1 : List (bt (Color ∧ A))) | |
898 → (r : RBI key value orig repl1 stack1 ) | |
899 → length stack1 < length stack → t ) | |
900 → (exit : (repl : bt (Color ∧ A) ) → (stack1 : List (bt (Color ∧ A))) | |
901 → stack1 ≡ (orig ∷ []) | |
902 → RBI key value orig repl stack1 | |
903 → t ) → t | |
904 rebuildRBT = ? | |
723 | 905 |
729 | 906 insertCase5 : {n m : Level} {A : Set n} {t : Set m} |
790 | 907 → (key : ℕ) → (value : A) |
908 → (orig tree : bt (Color ∧ A)) | |
909 → (stack : List (bt (Color ∧ A))) | |
785 | 910 → (r : RBI key value orig tree stack ) |
819 | 911 → (pg : PG (Color ∧ A) tree stack) |
823 | 912 → color (PG.uncle pg) ≡ Black → color (PG.parent pg) ≡ Red |
790 | 913 → (next : (tree1 : (bt (Color ∧ A))) → (stack1 : List (bt (Color ∧ A))) |
914 → (r : RBI key value orig tree1 stack1 ) | |
823 | 915 → length stack1 < length stack → t ) → t |
916 insertCase5 {n} {m} {A} {t} key value orig tree stack r pg cu=b cp=r next = {!!} where | |
818 | 917 -- check inner repl case |
918 -- node-key parent < node-key repl < node-key grand → rotateLeft parent then insertCase6 | |
919 -- node-key grand < node-key repl < node-key parent → rotateRight parent then insertCase6 | |
920 -- else insertCase6 | |
820 | 921 insertCase51 : (tree1 grand : bt (Color ∧ A)) → tree1 ≡ tree → grand ≡ PG.grand pg → t |
922 insertCase51 leaf grand teq geq = ? -- can't happen | |
823 | 923 insertCase51 (node kr vr rleft rright) leaf teq geq = ? -- can't happen |
924 insertCase51 (node kr vr rleft rright) (node kg vg grand grand₁) teq geq with <-cmp kr kg | |
820 | 925 ... | tri< a ¬b ¬c = ? where |
926 insertCase511 : (parent : bt (Color ∧ A)) → parent ≡ PG.parent pg → t | |
927 insertCase511 leaf peq = ? -- can't happen | |
928 insertCase511 (node key₂ ⟪ co , value ⟫ n1 n2) peq with <-cmp key key₂ | |
823 | 929 ... | tri< a ¬b ¬c = next ? ? ? ? |
820 | 930 ... | tri≈ ¬a b ¬c = ? -- can't happen |
823 | 931 ... | tri> ¬a ¬b c = next ? ? ? ? --- rotareRight → insertCase6 key value orig ? stack ? pg next exit |
820 | 932 ... | tri≈ ¬a b ¬c = ? -- can't happen |
823 | 933 ... | tri> ¬a ¬b c = ? where |
934 insertCase511 : (parent : bt (Color ∧ A)) → parent ≡ PG.parent pg → t | |
935 insertCase511 leaf peq = ? -- can't happen | |
936 insertCase511 (node key₂ ⟪ co , value ⟫ n1 n2) peq with <-cmp key key₂ | |
937 ... | tri< a ¬b ¬c = next ? ? ? ? --- rotareLeft → insertCase6 key value orig ? stack ? pg next exit | |
938 ... | tri≈ ¬a b ¬c = ? -- can't happen | |
939 ... | tri> ¬a ¬b c = next ? ? ? ? | |
723 | 940 |
814 | 941 -- |
942 -- replaced node increase blackdepth, so we need tree rotate | |
943 -- | |
944 -- case2 tree is Red | |
945 -- | |
946 -- go upward until | |
947 -- | |
948 -- if root | |
949 -- insert top | |
950 -- if unkle is leaf or Black | |
951 -- go insertCase5/6 | |
952 -- | |
953 -- make color tree ≡ Black , color unkle ≡ Black, color grand ≡ Red | |
954 -- loop with grand as repl | |
955 -- | |
956 -- case5/case6 rotation | |
957 -- | |
958 -- rotate and rebuild replaceTree and rb-invariant | |
959 | |
749 | 960 |
724 | 961 replaceRBP : {n m : Level} {A : Set n} {t : Set m} |
790 | 962 → (key : ℕ) → (value : A) |
963 → (orig repl : bt (Color ∧ A)) | |
964 → (stack : List (bt (Color ∧ A))) | |
785 | 965 → (r : RBI key value orig repl stack ) |
790 | 966 → (next : (repl1 : (bt (Color ∧ A))) → (stack1 : List (bt (Color ∧ A))) |
967 → (r : RBI key value orig repl1 stack1 ) | |
770 | 968 → length stack1 < length stack → t ) |
785 | 969 → (exit : (repl : bt (Color ∧ A) ) → (stack1 : List (bt (Color ∧ A))) |
970 → stack1 ≡ (orig ∷ []) | |
971 → RBI key value orig repl stack1 | |
972 → t ) → t | |
815 | 973 replaceRBP {n} {m} {A} {t} key value orig repl stack r next exit with RBI.state r |
974 ... | rebuild bdepth-eq = rebuildRBT key value orig repl stack r bdepth-eq next exit | |
975 ... | rotate repl-red pbdeth< with stackToPG (RBI.tree r) orig stack (RBI.si r) | |
976 ... | case1 eq = exit repl stack eq r -- no stack, replace top node | |
977 ... | case2 (case1 eq) = insertCase12 orig refl (RBI.si r) where | |
817
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
978 -- |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
979 -- we have no grand parent |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
980 -- eq : stack₁ ≡ RBI.tree r ∷ orig ∷ [] |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
981 -- change parent color ≡ Black and exit |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
982 -- |
815 | 983 -- one level stack, orig is parent of repl |
984 rb01 : stackInvariant key (RBI.tree r) orig stack | |
985 rb01 = RBI.si r | |
986 insertCase12 : (tr0 : bt (Color ∧ A)) → tr0 ≡ orig | |
987 → stackInvariant key (RBI.tree r) orig stack | |
988 → t | |
816 | 989 insertCase12 leaf eq1 si = ⊥-elim (rb04 eq eq1 si) where -- can't happen |
990 rb04 : {stack : List ( bt ( Color ∧ A))} → stack ≡ RBI.tree r ∷ orig ∷ [] → leaf ≡ orig → stackInvariant key (RBI.tree r) orig stack → ⊥ | |
991 rb04 refl refl (s-right tree leaf tree₁ x si) = si-property2 _ (s-right tree leaf tree₁ x si) refl | |
992 rb04 refl refl (s-left tree₁ leaf tree x si) = si-property2 _ (s-left tree₁ leaf tree x si) refl | |
815 | 993 insertCase12 tr0@(node key₁ value₁ left right) refl si with <-cmp key key₁ |
994 ... | tri< a ¬b ¬c = {!!} where | |
995 rb04 : stackInvariant key (RBI.tree r) orig stack → stack ≡ RBI.tree r ∷ orig ∷ [] → tr0 ≡ orig → left ≡ RBI.tree r | |
996 rb04 (s-left tree₁ .(node key₁ value₁ left right) tree {key₂} x s-nil) refl refl = refl | |
997 rb04 (s-right tree .(node key₁ _ tree₁ tree) tree₁ x s-nil) refl refl with si-property1 si | |
998 ... | refl = ⊥-elim ( nat-<> x a ) | |
999 ... | tri≈ ¬a b ¬c = {!!} -- can't happen | |
817
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1000 ... | tri> ¬a ¬b c = insertCase13 value₁ refl pbdeth< where |
815 | 1001 rb04 : stackInvariant key (RBI.tree r) orig stack → stack ≡ RBI.tree r ∷ orig ∷ [] → tr0 ≡ orig → right ≡ RBI.tree r |
1002 rb04 (s-right tree .(node key₁ _ tree₁ tree) tree₁ x s-nil) refl refl = refl | |
1003 rb04 (s-left tree₁ .(node key₁ value₁ left right) tree {key₂} x si) refl refl with si-property1 si | |
1004 ... | refl = ⊥-elim ( nat-<> x c ) | |
1005 -- | |
1006 -- RBI key value (node key₁ ⟪ Black , value₄ ⟫ left right) repl stack | |
1007 -- | |
817
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1008 insertCase13 : (v : Color ∧ A ) → v ≡ value₁ → black-depth repl ≡ black-depth (child-replaced key (RBI.tree r)) → t |
819 | 1009 insertCase13 ⟪ cl , value₄ ⟫ refl beq with <-cmp key key₁ | child-replaced key (node key₁ ⟪ cl , value₄ ⟫ left right) in creq |
817
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1010 ... | tri< a ¬b ¬c | cr = ⊥-elim (¬c c) |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1011 ... | tri≈ ¬a b ¬c | cr = ⊥-elim (¬c c) |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1012 ... | tri> ¬a ¬b c | cr = exit (node key₁ ⟪ Black , value₄ ⟫ left repl) (orig ∷ []) refl record { |
815 | 1013 tree = orig |
1014 ; origti = RBI.origti r | |
1015 ; origrb = RBI.origrb r | |
1016 ; treerb = RBI.origrb r | |
1017 ; replrb = ? | |
1018 ; si = s-nil | |
1019 ; rotated = ? | |
817
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1020 ; state = rebuild ? |
815 | 1021 } where |
1022 rb09 : {n : Level} {A : Set n} → {key key1 key2 : ℕ} {value value1 : A} {t1 t2 : bt (Color ∧ A)} | |
1023 → RBtreeInvariant (node key ⟪ Red , value ⟫ leaf (node key1 ⟪ Black , value1 ⟫ t1 t2)) | |
1024 → key < key1 | |
1025 rb09 (rb-right-red x x0 x2) = x | |
1026 -- rb05 should more general | |
1027 tkey : {n : Level} {A : Set n } → (rbt : bt (Color ∧ A)) → ℕ | |
1028 tkey (node key value t t2) = key | |
1029 tkey leaf = {!!} -- key is none | |
819 | 1030 ... | case2 (case2 pg) with PG.uncle pg in uneq |
817
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1031 ... | leaf = ? -- insertCase5 |
dfa764ddced2
reached to insertCase5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
816
diff
changeset
|
1032 ... | node key₁ ⟪ Black , value₁ ⟫ t₁ t₂ = ? -- insertCase5 |
819 | 1033 ... | node key₁ ⟪ Red , value₁ ⟫ t₁ t₂ with PG.pg pg |
1034 ... | s2-s1p2 {kp} {kg} {vp} {vg} {n1} {n2} x x₁ = next (to-red (node kg vg (to-black (node kp vp repl n1)) (to-black (PG.uncle pg)))) (PG.rest pg) | |
1035 record { | |
1036 tree = PG.grand pg | |
1037 ; origti = RBI.origti r | |
1038 ; origrb = RBI.origrb r | |
1039 ; treerb = ? | |
1040 ; replrb = ? | |
1041 ; si = ? | |
1042 ; rotated = ? | |
1043 ; state = rotate refl ? | |
1044 } ? | |
1045 ... | s2-1sp2 {kp} {kg} {vp} {vg} {n1} {n2} x x₁ = ? | |
1046 ... | s2-s12p {kp} {kg} {vp} {vg} {n1} {n2} x x₁ = ? | |
1047 ... | s2-1s2p {kp} {kg} {vp} {vg} {n1} {n2} x x₁ = ? | |
724 | 1048 |
740 | 1049 |
785 | 1050 |