Mercurial > hg > Gears > GearsAgda
annotate hoareBinaryTree1.agda @ 807:858655384dea
Change RBtreeInvariant
author | Moririn |
---|---|
date | Sat, 20 Jan 2024 20:01:01 +0900 |
parents | a11ebb914b7c |
children | 5d15ec862bcb |
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 |
663
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
110 si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → stackInvariant key tree tree0 stack |
662 | 111 → stack-last stack ≡ just tree0 |
729 | 112 si-property-last key t t0 (t ∷ []) (s-nil ) = refl |
785 | 113 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
|
114 ... | refl = si-property-last key x t0 (x ∷ st) si |
785 | 115 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
|
116 ... | refl = si-property-last key x t0 (x ∷ st) si |
656 | 117 |
639 | 118 rt-property1 : {n : Level} {A : Set n} (key : ℕ) (value : A) (tree tree1 : bt A ) → replacedTree key value tree tree1 → ¬ ( tree1 ≡ leaf ) |
119 rt-property1 {n} {A} key value .leaf .(node key value leaf leaf) r-leaf () | |
120 rt-property1 {n} {A} key value .(node key _ _ _) .(node key value _ _) r-node () | |
677 | 121 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-right x rt) = λ () |
122 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-left x rt) = λ () | |
639 | 123 |
690 | 124 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 |
125 rt-property-leaf r-leaf = refl | |
126 | |
790 | 127 rt-property-¬leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {tree : bt A} → ¬ replacedTree key value tree leaf |
698 | 128 rt-property-¬leaf () |
129 | |
692 | 130 rt-property-key : {n : Level} {A : Set n} {key key₂ key₃ : ℕ} {value value₂ value₃ : A} {left left₁ right₂ right₃ : bt A} |
131 → replacedTree key value (node key₂ value₂ left right₂) (node key₃ value₃ left₁ right₃) → key₂ ≡ key₃ | |
132 rt-property-key r-node = refl | |
133 rt-property-key (r-right x ri) = refl | |
134 rt-property-key (r-left x ri) = refl | |
135 | |
698 | 136 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥ |
137 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x | |
138 nat-<> : { x y : ℕ } → x < y → y < x → ⊥ | |
139 nat-<> (s≤s x<y) (s≤s y<x) = nat-<> x<y y<x | |
140 | |
141 open _∧_ | |
142 | |
143 | |
632 | 144 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j ) |
145 depth-1< {i} {j} = s≤s (m≤m⊔n _ j) | |
146 | |
147 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i ) | |
650 | 148 depth-2< {i} {j} = s≤s (m≤n⊔m j i) |
611 | 149 |
649 | 150 depth-3< : {i : ℕ } → suc i ≤ suc (suc i) |
151 depth-3< {zero} = s≤s ( z≤n ) | |
152 depth-3< {suc i} = s≤s (depth-3< {i} ) | |
153 | |
154 | |
634 | 155 treeLeftDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) |
156 → treeInvariant (node k v1 tree tree₁) | |
790 | 157 → treeInvariant tree |
634 | 158 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf |
159 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf | |
790 | 160 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti |
634 | 161 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti |
162 | |
163 treeRightDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) | |
164 → treeInvariant (node k v1 tree tree₁) | |
790 | 165 → treeInvariant tree₁ |
634 | 166 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf |
167 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti | |
168 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf | |
169 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti₁ | |
170 | |
615 | 171 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A)) |
790 | 172 → treeInvariant tree ∧ stackInvariant key tree tree0 stack |
693 | 173 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t ) |
174 → (exit : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
638 | 175 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
693 | 176 findP key leaf tree0 st Pre _ exit = exit leaf st Pre (case1 refl) |
632 | 177 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁ |
693 | 178 findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) |
179 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
|
180 ⟪ 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
|
181 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) |
785 | 182 findP1 a (x ∷ st) si = s-left _ _ _ a si |
183 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 | 184 |
638 | 185 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₁) |
186 replaceTree1 k v1 value (t-single .k .v1) = t-single k value | |
187 replaceTree1 k v1 value (t-right x t) = t-right x t | |
188 replaceTree1 k v1 value (t-left x t) = t-left x t | |
189 replaceTree1 k v1 value (t-node x x₁ t t₁) = t-node x x₁ t t₁ | |
190 | |
649 | 191 open import Relation.Binary.Definitions |
192 | |
193 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥ | |
194 lemma3 refl () | |
195 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥ | |
196 lemma5 (s≤s z≤n) () | |
700 | 197 ¬x<x : {x : ℕ} → ¬ (x < x) |
198 ¬x<x (s≤s lt) = ¬x<x lt | |
649 | 199 |
687 | 200 child-replaced : {n : Level} {A : Set n} (key : ℕ) (tree : bt A) → bt A |
201 child-replaced key leaf = leaf | |
202 child-replaced key (node key₁ value left right) with <-cmp key key₁ | |
203 ... | tri< a ¬b ¬c = left | |
204 ... | tri≈ ¬a b ¬c = node key₁ value left right | |
205 ... | tri> ¬a ¬b c = right | |
807 | 206 |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
207 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
|
208 field |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
209 tree0 : bt A |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
210 ti : treeInvariant tree0 |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
211 si : stackInvariant key tree tree0 stack |
687 | 212 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
|
213 ci : C tree repl stack -- data continuation |
790 | 214 |
785 | 215 record replacePR' {n : Level} {A : Set n} (key : ℕ) (value : A) (orig : bt A ) (stack : List (bt A)) : Set n where |
216 field | |
217 tree repl : bt A | |
218 ti : treeInvariant orig | |
219 si : stackInvariant key tree orig stack | |
220 ri : replacedTree key value (child-replaced key tree) repl | |
221 -- treeInvariant of tree and repl is inferred from ti, si and ri. | |
790 | 222 |
638 | 223 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) |
785 | 224 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key ) |
694 | 225 → (treeInvariant tree ) → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value (child-replaced key tree) tree1 → t) → t |
226 replaceNodeP k v1 leaf C P next = next (node k v1 leaf leaf) (t-single k v1 ) r-leaf | |
227 replaceNodeP k v1 (node .k value t t₁) (case2 refl) P next = next (node k v1 t t₁) (replaceTree1 k value v1 P) | |
790 | 228 (subst (λ j → replacedTree k v1 j (node k v1 t t₁) ) repl00 r-node) where |
694 | 229 repl00 : node k value t t₁ ≡ child-replaced k (node k value t t₁) |
230 repl00 with <-cmp k k | |
231 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl) | |
232 ... | tri≈ ¬a b ¬c = refl | |
233 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl) | |
606 | 234 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
235 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
|
236 → (key : ℕ) → (value : A) → {tree : bt A} ( repl : bt A) |
790 | 237 → (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
|
238 → (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
|
239 → replacePR key value tree1 repl stack1 (λ _ _ _ → Lift n ⊤) → length stack1 < length stack → t) |
613 | 240 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t |
675 | 241 replaceP key value {tree} repl [] Pre next exit = ⊥-elim ( si-property0 (replacePR.si Pre) refl ) -- can't happen |
242 replaceP key value {tree} repl (leaf ∷ []) Pre next exit with si-property-last _ _ _ _ (replacePR.si Pre)-- tree0 ≡ leaf | |
677 | 243 ... | refl = exit (replacePR.tree0 Pre) (node key value leaf leaf) ⟪ replacePR.ti Pre , r-leaf ⟫ |
689 | 244 replaceP key value {tree} repl (node key₁ value₁ left right ∷ []) Pre next exit with <-cmp key key₁ |
245 ... | tri< a ¬b ¬c = exit (replacePR.tree0 Pre) (node key₁ value₁ repl right ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
246 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right ) | |
790 | 247 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) |
689 | 248 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ k right ) (node key₁ value₁ repl right )) repl02 (r-left a repl03) where |
249 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
250 repl03 = replacePR.ri Pre | |
251 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left | |
252 repl02 with <-cmp key key₁ | |
790 | 253 ... | tri< a ¬b ¬c = refl |
689 | 254 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a) |
255 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a) | |
256 ... | tri≈ ¬a b ¬c = exit (replacePR.tree0 Pre) repl ⟪ replacePR.ti Pre , repl01 ⟫ where | |
790 | 257 repl01 : replacedTree key value (replacePR.tree0 Pre) repl |
258 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
689 | 259 repl01 | refl | refl = subst (λ k → replacedTree key value k repl) repl02 (replacePR.ri Pre) where |
260 repl02 : child-replaced key (node key₁ value₁ left right) ≡ node key₁ value₁ left right | |
261 repl02 with <-cmp key key₁ | |
262 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b) | |
263 ... | tri≈ ¬a b ¬c = refl | |
264 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b) | |
265 ... | tri> ¬a ¬b c = exit (replacePR.tree0 Pre) (node key₁ value₁ left repl ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
266 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl ) | |
790 | 267 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) |
689 | 268 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ left k ) (node key₁ value₁ left repl )) repl02 (r-right c repl03) where |
269 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
270 repl03 = replacePR.ri Pre | |
271 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right | |
272 repl02 with <-cmp key key₁ | |
273 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c) | |
274 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c) | |
790 | 275 ... | tri> ¬a ¬b c = refl |
690 | 276 replaceP {n} {_} {A} key value {tree} repl (leaf ∷ st@(tree1 ∷ st1)) Pre next exit = next key value repl st Post ≤-refl where |
277 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) | |
790 | 278 Post with replacePR.si Pre |
785 | 279 ... | 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 | 280 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf |
281 repl09 = si-property1 si | |
282 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
283 repl10 with si-property1 si | |
284 ... | refl = si | |
285 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf | |
790 | 286 repl07 with <-cmp key key₂ |
690 | 287 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) |
288 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
289 ... | tri> ¬a ¬b c = refl | |
290 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
291 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 | 292 ... | 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 | 293 repl09 : tree1 ≡ node key₂ v1 leaf tree₁ |
690 | 294 repl09 = si-property1 si |
295 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
296 repl10 with si-property1 si | |
297 ... | refl = si | |
298 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf | |
790 | 299 repl07 with <-cmp key key₂ |
690 | 300 ... | tri< a ¬b ¬c = refl |
301 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
302 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
303 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
787 | 304 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 | 305 -- 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 | 306 replaceP {n} {_} {A} key value {tree} repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre next exit with <-cmp key key₁ |
683 | 307 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st Post ≤-refl where |
790 | 308 Post : replacePR key value tree1 (node key₁ value₁ repl right ) st (λ _ _ _ → Lift n ⊤) |
309 Post with replacePR.si Pre | |
785 | 310 ... | 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 | 311 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) |
688 | 312 repl09 = si-property1 si |
313 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
314 repl10 with si-property1 si | |
315 ... | refl = si | |
316 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left | |
790 | 317 repl03 with <-cmp key key₁ |
688 | 318 ... | tri< a1 ¬b ¬c = refl |
319 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
320 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
321 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right | |
322 repl02 with repl09 | <-cmp key key₂ | |
323 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
689 | 324 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) |
688 | 325 ... | refl | tri> ¬a ¬b c = refl |
326 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
327 repl04 = begin | |
328 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
329 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
330 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
331 child-replaced key tree1 ∎ where open ≡-Reasoning | |
332 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
790 | 333 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) |
785 | 334 ... | 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 | 335 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ |
683 | 336 repl09 = si-property1 si |
337 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
338 repl10 with si-property1 si | |
339 ... | refl = si | |
687 | 340 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left |
790 | 341 repl03 with <-cmp key key₁ |
687 | 342 ... | tri< a1 ¬b ¬c = refl |
343 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
344 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
345 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right | |
346 repl02 with repl09 | <-cmp key key₂ | |
347 ... | refl | tri< a ¬b ¬c = refl | |
348 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
349 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
350 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
351 repl04 = begin | |
352 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
353 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
354 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
355 child-replaced key tree1 ∎ where open ≡-Reasoning | |
356 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
790 | 357 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) |
358 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st Post ≤-refl where | |
690 | 359 Post : replacePR key value tree1 (node key₁ value left right ) (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) |
790 | 360 Post with replacePR.si Pre |
785 | 361 ... | 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 | 362 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right) |
363 repl09 = si-property1 si | |
364 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
365 repl10 with si-property1 si | |
366 ... | refl = si | |
367 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree | |
790 | 368 repl07 with <-cmp key key₂ |
690 | 369 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) |
370 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
371 ... | tri> ¬a ¬b c = refl | |
691 | 372 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
790 | 373 repl12 refl with repl09 |
691 | 374 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node |
785 | 375 ... | 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 | 376 repl09 : tree1 ≡ node key₂ v1 tree tree₁ |
690 | 377 repl09 = si-property1 si |
378 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
379 repl10 with si-property1 si | |
380 ... | refl = si | |
381 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree | |
790 | 382 repl07 with <-cmp key key₂ |
690 | 383 ... | tri< a ¬b ¬c = refl |
384 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
385 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
691 | 386 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
790 | 387 repl12 refl with repl09 |
691 | 388 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node |
690 | 389 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st Post ≤-refl where |
790 | 390 Post : replacePR key value tree1 (node key₁ value₁ left repl ) st (λ _ _ _ → Lift n ⊤) |
391 Post with replacePR.si Pre | |
785 | 392 ... | 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 | 393 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) |
690 | 394 repl09 = si-property1 si |
395 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
396 repl10 with si-property1 si | |
397 ... | refl = si | |
398 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
790 | 399 repl03 with <-cmp key key₁ |
690 | 400 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) |
401 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
790 | 402 ... | tri> ¬a ¬b c = refl |
690 | 403 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right |
404 repl02 with repl09 | <-cmp key key₂ | |
405 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
406 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) | |
407 ... | refl | tri> ¬a ¬b c = refl | |
408 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
409 repl04 = begin | |
410 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
411 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
412 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
413 child-replaced key tree1 ∎ where open ≡-Reasoning | |
414 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
790 | 415 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) |
785 | 416 ... | 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 | 417 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ |
690 | 418 repl09 = si-property1 si |
419 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
420 repl10 with si-property1 si | |
421 ... | refl = si | |
422 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
790 | 423 repl03 with <-cmp key key₁ |
690 | 424 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) |
425 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
790 | 426 ... | tri> ¬a ¬b c = refl |
690 | 427 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right |
428 repl02 with repl09 | <-cmp key key₂ | |
429 ... | refl | tri< a ¬b ¬c = refl | |
430 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
431 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
432 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
433 repl04 = begin | |
434 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
435 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
436 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
437 child-replaced key tree1 ∎ where open ≡-Reasoning | |
438 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
790 | 439 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) |
644 | 440 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
441 TerminatingLoopS : {l m : Level} {t : Set l} (Index : Set m ) → {Invraiant : Index → Set m } → ( reduce : Index → ℕ) |
790 | 442 → (r : Index) → (p : Invraiant r) |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
443 → (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
|
444 TerminatingLoopS {_} {_} {t} Index {Invraiant} reduce r p loop with <-cmp 0 (reduce r) |
790 | 445 ... | tri≈ ¬a b ¬c = loop r p (λ r1 p1 lt → ⊥-elim (lemma3 b lt) ) |
446 ... | 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
|
447 TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → Invraiant r1 → reduce r1 < reduce r → t |
790 | 448 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
|
449 TerminatingLoop1 (suc j) r r1 n≤j p1 lt with <-cmp (reduce r1) (suc j) |
790 | 450 ... | 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
|
451 ... | tri≈ ¬a b ¬c = loop r1 p1 (λ r2 p2 lt1 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b lt1 ) p2 lt1 ) |
790 | 452 ... | 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
|
453 |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
454 open _∧_ |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
455 |
615 | 456 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
457 → replacedTree key value tree repl → treeInvariant repl | |
692 | 458 RTtoTI0 .leaf .(node key value leaf leaf) key value ti r-leaf = t-single key value |
459 RTtoTI0 .(node key _ leaf leaf) .(node key value leaf leaf) key value (t-single .key _) r-node = t-single key value | |
790 | 460 RTtoTI0 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti |
461 RTtoTI0 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
692 | 462 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 | 463 -- r-right case |
692 | 464 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) |
465 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 | 466 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 | 467 t-right (subst (λ k → key₁ < k ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) |
692 | 468 RTtoTI0 (node key₁ _ (node _ _ _ _) leaf) (node key₁ _ (node key₃ value left right) leaf) key value₁ (t-left x₁ ti) (r-right x ()) |
469 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 | 470 t-node x₁ x ti (t-single key value) |
693 | 471 RTtoTI0 (node key₁ _ (node _ _ _ _) (node key₂ _ _ _)) (node key₁ _ (node _ _ _ _) (node key₃ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) = |
472 t-node x₁ (subst (λ k → key₁ < k) (rt-property-key ri) x₂) ti (RTtoTI0 _ _ key value ti₁ ri) | |
701 | 473 -- r-left case |
700 | 474 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 | 475 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 |
476 RTtoTI0 (node key₃ _ (node key₂ _ _ _) leaf) (node key₃ _ (node key₁ value₁ left left₁) leaf) key value (t-left x₁ ti) (r-left x ri) = | |
477 t-left (subst (λ k → k < key₃ ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) -- key₁ < key₃ | |
478 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 | 479 |
480 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl | |
481 → replacedTree key value tree repl → treeInvariant tree | |
701 | 482 RTtoTI1 .leaf .(node key value leaf leaf) key value ti r-leaf = t-leaf |
483 RTtoTI1 (node key value₁ leaf leaf) .(node key value leaf leaf) key value (t-single .key .value) r-node = t-single key value₁ | |
484 RTtoTI1 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti | |
485 RTtoTI1 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
486 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₁ | |
487 -- r-right case | |
488 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₁ | |
489 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) = | |
490 t-right (subst (λ k → key₁ < k ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₁ < key₂ | |
491 RTtoTI1 (node _ _ (node _ _ _ _) leaf) (node _ _ (node _ _ _ _) (node key value _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x r-leaf) = | |
492 t-left x₁ ti | |
493 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₁ | |
494 -- r-left case | |
495 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ _ leaf) key value (t-left x₁ ti) (r-left x ri) = t-single key₁ value₁ | |
790 | 496 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 | 497 t-left (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₂ < key₁ |
498 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 | 499 RTtoTI1 (node key₁ value₁ (node key₂ value₂ n n₁) _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) = |
701 | 500 t-node (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) x₂ (RTtoTI1 _ _ key value ti ri) ti₁ -- key₂ < key₁ |
614 | 501 |
611 | 502 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
696 | 503 → (exit : (tree repl : bt A) → treeInvariant repl ∧ replacedTree key value tree repl → t ) → t |
693 | 504 insertTreeP {n} {m} {A} {t} tree key value P0 exit = |
729 | 505 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 | 506 $ λ p P loop → findP key (proj1 p) tree (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
693 | 507 $ λ t s P C → replaceNodeP key value t C (proj1 P) |
508 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A ) | |
509 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) } | |
790 | 510 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = tree ; ti = P0 ; si = proj2 P ; ri = R ; ci = lift tt } |
693 | 511 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1 |
696 | 512 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt ) |
807 | 513 $ λ tree repl P → {!!} --exit tree repl ⟪ RTtoTI0 _ _ _ _ (proj1 P) (proj2 P) , proj2 P ⟫ |
614 | 514 |
696 | 515 insertTestP1 = insertTreeP leaf 1 1 t-leaf |
790 | 516 $ λ _ x0 P0 → insertTreeP x0 2 1 (proj1 P0) |
517 $ λ _ x1 P1 → insertTreeP x1 3 2 (proj1 P1) | |
727 | 518 $ λ _ x2 P2 → insertTreeP x2 2 2 (proj1 P2) (λ _ x P → x ) |
694 | 519 |
790 | 520 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
|
521 top-value leaf = nothing |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
522 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
|
523 |
722 | 524 -- is realy inserted? |
525 | |
526 -- other element is preserved? | |
702 | 527 |
722 | 528 -- deletion? |
807 | 529 |
530 | |
722 | 531 data Color : Set where |
532 Red : Color | |
533 Black : Color | |
618 | 534 |
790 | 535 RB→bt : {n : Level} (A : Set n) → (bt (Color ∧ A)) → bt A |
785 | 536 RB→bt {n} A leaf = leaf |
537 RB→bt {n} A (node key ⟪ C , value ⟫ tr t1) = (node key value (RB→bt A tr) (RB→bt A t1)) | |
538 | |
539 color : {n : Level} {A : Set n} → (bt (Color ∧ A)) → Color | |
540 color leaf = Black | |
541 color (node key ⟪ C , value ⟫ rb rb₁) = C | |
542 | |
543 black-depth : {n : Level} {A : Set n} → (tree : bt (Color ∧ A) ) → ℕ | |
544 black-depth leaf = 0 | |
790 | 545 black-depth (node key ⟪ Red , value ⟫ t t₁) = black-depth t ⊔ black-depth t₁ |
785 | 546 black-depth (node key ⟪ Black , value ⟫ t t₁) = suc (black-depth t ⊔ black-depth t₁ ) |
547 | |
802 | 548 zero≢suc : { m : ℕ } → zero ≡ suc m → ⊥ |
549 zero≢suc () | |
550 suc≢zero : {m : ℕ } → suc m ≡ zero → ⊥ | |
551 suc≢zero () | |
800 | 552 {-# TERMINATING #-} |
553 DepthCal : ( l m n : ℕ ) → l ≡ m ⊔ n | |
554 DepthCal zero zero zero = refl | |
802 | 555 DepthCal zero zero (suc n) = ⊥-elim (zero≢suc (DepthCal zero zero (suc n))) |
556 DepthCal zero (suc m) zero = ⊥-elim (zero≢suc (DepthCal zero (suc m) zero)) | |
557 DepthCal zero (suc m) (suc n) = ⊥-elim (zero≢suc (DepthCal zero (suc m) (suc n))) | |
558 DepthCal (suc l) zero zero = ⊥-elim (suc≢zero (DepthCal (suc l) zero zero )) | |
800 | 559 DepthCal (suc l) zero (suc n) with <-cmp (suc l) (suc n) |
560 ... | tri< a ¬b ¬c = ⊥-elim (¬b (DepthCal (suc l) zero (suc n) )) | |
561 ... | tri≈ ¬a b ¬c = cong suc (suc-injective b) | |
562 ... | tri> ¬a ¬b c = ⊥-elim (¬b (DepthCal (suc l) zero (suc n) )) | |
563 DepthCal (suc l) (suc m) zero with <-cmp (suc l) (suc m) | |
564 ... | tri< a ¬b ¬c = ⊥-elim (¬b (DepthCal (suc l) (suc m) zero )) | |
565 ... | tri≈ ¬a b ¬c = cong suc (suc-injective b) | |
566 ... | tri> ¬a ¬b c = ⊥-elim (¬b (DepthCal (suc l) (suc m) zero )) | |
803 | 567 DepthCal (suc l) (suc m) (suc n) = cong suc (DepthCal l m n ) |
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 |
785 | 600 data rotatedTree {n : Level} {A : Set n} : (before after : bt A) → Set n where |
791 | 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') | |
785 | 604 -- a b |
605 -- b c d a | |
606 -- d e e c | |
790 | 607 -- |
785 | 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 | |
790 | 620 → {ctl1 ctr1 dtl1 dtr1 etl1 etr1 : bt A} -- after child |
785 | 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)) | |
806 | 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 |
790 | 669 findRBT : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt (Color ∧ A) ) |
785 | 670 → (stack : List (bt (Color ∧ A))) |
671 → treeInvariant tree ∧ stackInvariant key tree tree0 stack | |
672 → RBtreeInvariant tree | |
673 → (next : (tree1 : bt (Color ∧ A) ) → (stack : List (bt (Color ∧ A))) | |
674 → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
790 | 675 → RBtreeInvariant tree1 |
785 | 676 → bt-depth tree1 < bt-depth tree → t ) |
790 | 677 → (exit : (tree1 : bt (Color ∧ A)) → (stack : List (bt (Color ∧ A))) |
785 | 678 → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack |
790 | 679 → RBtreeInvariant tree1 |
785 | 680 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
681 findRBT key leaf tree0 stack ti rb0 next exit = exit leaf stack ti rb0 (case1 refl) | |
682 findRBT key n@(node key₁ value left right) tree0 stack ti rb0 next exit with <-cmp key key₁ | |
683 findRBT key (node key₁ value left right) tree0 stack ti rb0 next exit | tri< a ¬b ¬c | |
684 = next left (left ∷ stack) ⟪ treeLeftDown left right (_∧_.proj1 ti) , s-left _ _ _ a (_∧_.proj2 ti) ⟫ (RBtreeLeftDown left right rb0) depth-1< | |
685 findRBT key n tree0 stack ti rb0 _ exit | tri≈ ¬a refl ¬c = exit n stack ti rb0 (case2 refl) | |
686 findRBT key (node key₁ value left right) tree0 stack ti rb0 next exit | tri> ¬a ¬b c | |
687 = next right (right ∷ stack) ⟪ treeRightDown left right (_∧_.proj1 ti), s-right _ _ _ c (_∧_.proj2 ti) ⟫ (RBtreeRightDown left right rb0) depth-2< | |
688 | |
689 | |
803 | 690 blackdepth≡ : {n : Level } {A : Set n} → {C : Color} {key key1 : ℕ} {value value1 : A} → (tree1 tree2 : bt (Color ∧ A)) |
691 → RBtreeInvariant tree1 | |
692 → RBtreeInvariant tree2 | |
693 → RBtreeInvariant (node key ⟪ C , value ⟫ tree1 tree2) | |
694 → black-depth tree1 ≡ black-depth tree2 | |
695 blackdepth≡ leaf leaf ri1 ri2 rip = refl | |
696 blackdepth≡ {n} {A} leaf (node key .(⟪ Black , _ ⟫) t2 t3) ri1 ri2 (rb-right-red x x₁ rip) = DepthCal (black-depth {n} {A} leaf) (black-depth (node key ⟪ Black , _ ⟫ t2 t3)) 0 | |
697 blackdepth≡ {n} {A} leaf (node key .(⟪ _ , _ ⟫) t2 t3) ri1 ri2 (rb-right-black x x₁ rip) = DepthCal (black-depth {n} {A} leaf) (black-depth (node key ⟪ _ , _ ⟫ t2 t3) ) (black-depth (node key ⟪ _ , _ ⟫ t2 t3)) | |
698 blackdepth≡ {n} {A} (node key .(⟪ Black , _ ⟫) t1 t3) leaf ri1 ri2 (rb-left-red x x₁ rip) = DepthCal (black-depth (node key ⟪ Black , _ ⟫ t1 t3)) (black-depth {n} {A} leaf) 0 | |
699 blackdepth≡ {n} {A} (node key .(⟪ _ , _ ⟫) t1 t3) leaf ri1 ri2 (rb-left-black x x₁ rip) = DepthCal (black-depth (node key ⟪ _ , _ ⟫ t1 t3)) (black-depth {n} {A} leaf) 0 | |
807 | 700 blackdepth≡ (node key .(⟪ Black , _ ⟫) t1 t3) (node key₁ .(⟪ Black , _ ⟫) t2 t4) ri1 ri2 (rb-node-red x x₁ x₂ rip rip₁) = DepthCal (black-depth (node key ⟪ Black , _ ⟫ t1 t3)) (black-depth (node key₁ ⟪ Black , _ ⟫ t2 t4)) 0 |
701 blackdepth≡ (node key .(⟪ _ , _ ⟫) t1 t3) (node key₁ .(⟪ _ , _ ⟫) t2 t4) ri1 ri2 (rb-node-black x x₁ x₂ rip rip₁) = DepthCal (black-depth (node key ⟪ _ , _ ⟫ t1 t3)) ( black-depth (node key₁ ⟪ _ , _ ⟫ t2 t4)) (black-depth (node key₁ (⟪ _ , _ ⟫) t2 t4)) | |
803 | 702 |
785 | 703 data replacedRBTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt (Color ∧ A) ) → Set n where |
704 rbr-leaf : {ca cb : Color} → replacedRBTree key value leaf (node key ⟪ cb , value ⟫ leaf leaf) | |
790 | 705 rbr-node : {value₁ : A} → {ca cb : Color } → {t t₁ : bt (Color ∧ A)} → replacedRBTree key value (node key ⟪ ca , value₁ ⟫ t t₁) (node key ⟪ cb , value ⟫ t t₁) |
785 | 706 rbr-right : {k : ℕ } {v1 : A} → {ca cb : Color} → {t t1 t2 : bt (Color ∧ A)} |
790 | 707 → k < key → replacedRBTree key value t2 t → replacedRBTree key value (node k ⟪ ca , v1 ⟫ t1 t2) (node k ⟪ cb , v1 ⟫ t1 t) |
785 | 708 rbr-left : {k : ℕ } {v1 : A} → {ca cb : Color} → {t t1 t2 : bt (Color ∧ A)} |
807 | 709 → key < k → replacedRBTree key value t1 t → replacedRBTree key value (node k ⟪ ca , v1 ⟫ t1 t2) (node k ⟪ cb , v1 ⟫ t t2) -- k < key → key < k |
723 | 710 |
754 | 711 data ParentGrand {n : Level} {A : Set n} (self : bt A) : (parent uncle grand : bt A) → Set n where |
790 | 712 s2-s1p2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } |
713 → parent ≡ node kp vp self n1 → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand | |
714 s2-1sp2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
715 → parent ≡ node kp vp n1 self → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand | |
716 s2-s12p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
717 → parent ≡ node kp vp self n1 → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand | |
718 s2-1s2p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
719 → parent ≡ node kp vp n1 self → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand | |
734 | 720 |
721 | |
749 | 722 record PG {n : Level } (A : Set n) (self : bt A) (stack : List (bt A)) : Set n where |
740 | 723 field |
754 | 724 parent grand uncle : bt A |
725 pg : ParentGrand self parent uncle grand | |
740 | 726 rest : List (bt A) |
727 stack=gp : stack ≡ ( self ∷ parent ∷ grand ∷ rest ) | |
728 | |
785 | 729 record RBI {n : Level} {A : Set n} (key : ℕ) (value : A) (orig repl : bt (Color ∧ A) ) (stack : List (bt (Color ∧ A))) : Set n where |
730 field | |
731 tree rot : bt (Color ∧ A) | |
790 | 732 origti : treeInvariant orig |
733 origrb : RBtreeInvariant orig | |
794 | 734 treerb : RBtreeInvariant tree -- tree node te be replaced |
790 | 735 replrb : RBtreeInvariant repl |
785 | 736 si : stackInvariant key tree orig stack |
737 rotated : rotatedTree tree rot | |
738 ri : replacedRBTree key value (child-replaced key rot ) repl | |
794 | 739 repl-red : color repl ≡ Red |
740 repl-eq : black-depth (child-replaced key rot ) ≡ black-depth repl | |
785 | 741 |
740 | 742 stackToPG : {n : Level} {A : Set n} → {key : ℕ } → (tree orig : bt A ) |
743 → (stack : List (bt A)) → stackInvariant key tree orig stack | |
749 | 744 → ( stack ≡ orig ∷ [] ) ∨ ( stack ≡ tree ∷ orig ∷ [] ) ∨ PG A tree stack |
740 | 745 stackToPG {n} {A} {key} tree .tree .(tree ∷ []) s-nil = case1 refl |
785 | 746 stackToPG {n} {A} {key} tree .(node _ _ _ tree) .(tree ∷ node _ _ _ tree ∷ []) (s-right _ _ _ x s-nil) = case2 (case1 refl) |
790 | 747 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 | 748 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) |
790 | 749 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 | 750 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 751 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 | 752 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 753 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 | 754 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 755 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 | 756 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 757 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 | 758 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 759 stackToPG {n} {A} {key} tree .(node _ _ tree _) .(tree ∷ node _ _ tree _ ∷ []) (s-left _ _ t1 {k1} {v1} x s-nil) = case2 (case1 refl) |
760 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 | 761 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 762 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 | 763 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 764 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 | 765 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 766 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 | 767 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 768 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 | 769 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) |
785 | 770 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 | 771 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) |
740 | 772 |
785 | 773 stackCase1 : {n : Level} {A : Set n} → {key : ℕ } → {tree orig : bt A } |
774 → {stack : List (bt A)} → stackInvariant key tree orig stack | |
775 → stack ≡ orig ∷ [] → tree ≡ orig | |
776 stackCase1 s-nil refl = refl | |
760 | 777 |
749 | 778 PGtoRBinvariant : {n : Level} {A : Set n} → {key d0 ds dp dg : ℕ } → (tree orig : bt (Color ∧ A) ) |
790 | 779 → RBtreeInvariant orig |
749 | 780 → (stack : List (bt (Color ∧ A))) → (pg : PG (Color ∧ A) tree stack ) |
790 | 781 → RBtreeInvariant tree ∧ RBtreeInvariant (PG.parent pg) ∧ RBtreeInvariant (PG.grand pg) |
758 | 782 PGtoRBinvariant = {!!} |
734 | 783 |
790 | 784 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
|
785 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
|
786 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
|
787 ... | 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
|
788 ... | 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
|
789 ... | tri> ¬a ¬b c = RBtreeRightDown _ _ rbi |
802 | 790 |
790 | 791 -- findRBT : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt (Color ∧ A) ) |
785 | 792 -- → (stack : List (bt (Color ∧ A))) → treeInvariant tree0 ∧ stackInvariant key tree tree0 stack |
790 | 793 -- → {d : ℕ} → RBtreeInvariant tree0 |
794 -- → (exit : (tree1 : bt (Color ∧ A)) → (stack : List (bt (Color ∧ A))) | |
785 | 795 -- → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack |
790 | 796 -- → {d1 : ℕ} → RBtreeInvariant tree1 |
785 | 797 -- → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
798 --findRBT {_} {_} {A} {t} key tree0 leaf stack ti {d} rb0 exit = {!!} | |
799 --findRBT {_} {_} {A} {t} key tree0 (node key₁ value left right) stack ti {d} rb0 exit with <-cmp key key₁ | |
800 --... | tri< a ¬b ¬c = {!!} | |
801 --... | tri≈ ¬a b ¬c = {!!} | |
802 --... | tri> ¬a ¬b c = {!!} | |
723 | 803 |
754 | 804 rotateLeft : {n m : Level} {A : Set n} {t : Set m} |
790 | 805 → (key : ℕ) → (value : A) |
806 → (orig tree : bt (Color ∧ A)) | |
807 → (stack : List (bt (Color ∧ A))) | |
785 | 808 → (r : RBI key value orig tree stack ) |
790 | 809 → (next : (current : bt (Color ∧ A)) → (stack1 : List (bt (Color ∧ A))) |
810 → (r : RBI key value orig current stack1 ) | |
754 | 811 → length stack1 < length stack → t ) |
785 | 812 → (exit : (repl : bt (Color ∧ A) ) → (stack1 : List (bt (Color ∧ A))) |
813 → stack1 ≡ (orig ∷ []) | |
814 → RBI key value orig repl stack1 | |
815 → t ) → t | |
816 rotateLeft {n} {m} {A} {t} key value = {!!} where | |
756 | 817 rotateLeft1 : t |
785 | 818 rotateLeft1 with stackToPG {!!} {!!} {!!} {!!} |
819 ... | case1 x = {!!} -- {!!} {!!} {!!} {!!} rr | |
758 | 820 ... | case2 (case1 x) = {!!} |
821 ... | case2 (case2 pg) = {!!} | |
754 | 822 |
823 rotateRight : {n m : Level} {A : Set n} {t : Set m} | |
790 | 824 → (key : ℕ) → (value : A) |
825 → (orig tree : bt (Color ∧ A)) | |
826 → (stack : List (bt (Color ∧ A))) | |
785 | 827 → (r : RBI key value orig tree stack ) |
790 | 828 → (next : (current : bt (Color ∧ A)) → (stack1 : List (bt (Color ∧ A))) |
829 → (r : RBI key value orig current stack1 ) | |
754 | 830 → length stack1 < length stack → t ) |
785 | 831 → (exit : (repl : bt (Color ∧ A) ) → (stack1 : List (bt (Color ∧ A))) |
832 → stack1 ≡ (orig ∷ []) | |
833 → RBI key value orig repl stack1 | |
834 → t ) → t | |
835 rotateRight {n} {m} {A} {t} key value = {!!} where | |
756 | 836 rotateRight1 : t |
785 | 837 rotateRight1 with stackToPG {!!} {!!} {!!} {!!} |
758 | 838 ... | case1 x = {!!} |
839 ... | case2 (case1 x) = {!!} | |
840 ... | case2 (case2 pg) = {!!} | |
754 | 841 |
729 | 842 insertCase5 : {n m : Level} {A : Set n} {t : Set m} |
790 | 843 → (key : ℕ) → (value : A) |
844 → (orig tree : bt (Color ∧ A)) | |
845 → (stack : List (bt (Color ∧ A))) | |
785 | 846 → (r : RBI key value orig tree stack ) |
790 | 847 → (next : (tree1 : (bt (Color ∧ A))) → (stack1 : List (bt (Color ∧ A))) |
848 → (r : RBI key value orig tree1 stack1 ) | |
751 | 849 → length stack1 < length stack → t ) |
785 | 850 → (exit : (repl : bt (Color ∧ A) ) → (stack1 : List (bt (Color ∧ A))) |
851 → stack1 ≡ (orig ∷ []) | |
852 → RBI key value orig repl stack1 | |
853 → t ) → t | |
854 insertCase5 {n} {m} {A} {t} key value = {!!} where | |
755 | 855 insertCase51 : t |
785 | 856 insertCase51 with stackToPG {!!} {!!} {!!} {!!} |
758 | 857 ... | case1 eq = {!!} |
858 ... | case2 (case1 eq ) = {!!} | |
755 | 859 ... | case2 (case2 pg) with PG.pg pg |
785 | 860 ... | s2-s1p2 x x₁ = {!!} -- rotateRight {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit |
755 | 861 -- x : PG.parent pg ≡ node kp vp tree n1 |
862 -- x₁ : PG.grand pg ≡ node kg vg (PG.parent pg) (PG.uncle pg) | |
785 | 863 ... | s2-1sp2 x x₁ = {!!} -- rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit |
864 ... | s2-s12p x x₁ = {!!} -- rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit | |
865 ... | s2-1s2p x x₁ = {!!} -- rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit | |
790 | 866 -- = insertCase2 tree (PG.parent pg) (PG.uncle pg) (PG.grand pg) stack si (PG.pg pg) |
751 | 867 |
723 | 868 |
749 | 869 -- if we have replacedNode on RBTree, we have RBtreeInvariant |
870 | |
724 | 871 replaceRBP : {n m : Level} {A : Set n} {t : Set m} |
790 | 872 → (key : ℕ) → (value : A) |
873 → (orig repl : bt (Color ∧ A)) | |
874 → (stack : List (bt (Color ∧ A))) | |
785 | 875 → (r : RBI key value orig repl stack ) |
790 | 876 → (next : (repl1 : (bt (Color ∧ A))) → (stack1 : List (bt (Color ∧ A))) |
877 → (r : RBI key value orig repl1 stack1 ) | |
770 | 878 → length stack1 < length stack → t ) |
785 | 879 → (exit : (repl : bt (Color ∧ A) ) → (stack1 : List (bt (Color ∧ A))) |
880 → stack1 ≡ (orig ∷ []) | |
881 → RBI key value orig repl stack1 | |
882 → t ) → t | |
883 replaceRBP {n} {m} {A} {t} key value orig repl stack r next exit = insertCase1 where | |
790 | 884 insertCase2 : (tree parent uncle grand : bt (Color ∧ A)) |
885 → (stack : List (bt (Color ∧ A))) → (si : stackInvariant key tree orig stack ) | |
754 | 886 → (pg : ParentGrand tree parent uncle grand ) → t |
790 | 887 insertCase2 tree leaf uncle grand stack si (s2-s1p2 () x₁) |
888 insertCase2 tree leaf uncle grand stack si (s2-1sp2 () x₁) | |
889 insertCase2 tree leaf uncle grand stack si (s2-s12p () x₁) | |
890 insertCase2 tree leaf uncle grand stack si (s2-1s2p () x₁) | |
785 | 891 insertCase2 tree parent@(node kp ⟪ Red , _ ⟫ _ _) uncle grand stack si pg = {!!} -- next {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} |
758 | 892 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) leaf grand stack si pg = {!!} |
785 | 893 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Red , _ ⟫ _ _ ) grand stack si pg = {!!} -- next {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} |
894 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-s1p2 x x₁) = {!!} | |
895 -- insertCase5 key value orig tree {!!} rbio {!!} {!!} stack si {!!} ri {!!} {!!} next exit | |
754 | 896 -- tree is left of parent, parent is left of grand |
897 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp tree n1 | |
898 -- grand ≡ node kg vg (node kp ⟪ Black , proj3 ⟫ left right) (node ku ⟪ Black , proj4 ⟫ left₁ right₁) | |
785 | 899 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-1sp2 x x₁) = {!!} |
790 | 900 -- rotateLeft key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!} |
785 | 901 -- (λ a b c d e f h i j k l m → insertCase5 key value a b c d {!!} {!!} h i j k l {!!} {!!} next exit ) exit |
754 | 902 -- tree is right of parent, parent is left of grand rotateLeft |
903 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp n1 tree | |
904 -- grand ≡ node kg vg (node kp ⟪ Black , proj3 ⟫ left right) (node ku ⟪ Black , proj4 ⟫ left₁ right₁) | |
785 | 905 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-s12p x x₁) = {!!} |
790 | 906 -- rotateRight key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!} |
785 | 907 -- (λ a b c d e f h i j k l m → insertCase5 key value a b c d {!!} {!!} h i j k l {!!} {!!} next exit ) exit |
754 | 908 -- tree is left of parent, parent is right of grand, rotateRight |
909 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp tree n1 | |
910 -- grand ≡ node kg vg (node ku ⟪ Black , proj4 ⟫ left₁ right₁) (node kp ⟪ Black , proj3 ⟫ left right) | |
785 | 911 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-1s2p x x₁) = {!!} |
912 -- insertCase5 key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!} next exit | |
754 | 913 -- tree is right of parent, parent is right of grand |
914 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp n1 tree | |
915 -- grand ≡ node kg vg (node ku ⟪ Black , proj4 ⟫ left₁ right₁) (node kp ⟪ Black , proj3 ⟫ left right) | |
749 | 916 insertCase1 : t |
785 | 917 insertCase1 with stackToPG (RBI.tree r) orig stack (RBI.si r) |
794 | 918 ... | case1 eq = exit repl stack eq r -- no stack, replace top node |
792 | 919 ... | case2 (case1 eq) = insertCase12 orig refl (RBI.si r) where |
794 | 920 -- one level stack, orig is parent of repl |
789
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
921 rb01 : stackInvariant key (RBI.tree r) orig stack |
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
922 rb01 = RBI.si r |
792 | 923 insertCase12 : (tr0 : bt (Color ∧ A)) → tr0 ≡ orig |
789
b85b2a8e40c1
insertcase12 has only one level stack, we may ommit this.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
788
diff
changeset
|
924 → stackInvariant key (RBI.tree r) orig stack |
790 | 925 → t |
798 | 926 insertCase12 leaf eq1 si = {!!} -- can't happen |
795 | 927 insertCase12 tr0@(node key₁ value₁ left right) refl si with <-cmp key key₁ | child-replaced key (node key₁ value₁ left (RBI.rot r)) in eqcr |
798 | 928 ... | tri< a ¬b ¬c | cr = {!!} |
929 ... | tri≈ ¬a b ¬c | cr = {!!} -- can't happen | |
793 | 930 ... | tri> ¬a ¬b c | cr = insertCase13 value₁ refl where |
794 | 931 -- |
932 -- orig B | |
933 -- / \ | |
934 -- left tree → rot → repl R | |
935 -- | |
936 -- B => B B => B | |
937 -- / \ / \ / \ rotate L / \ | |
802 | 938 -- L L1 L R3 L R -- bad B B |
794 | 939 -- / \ / \ / \ 1 : child-replace |
796 | 940 --- L L2 L B L L 2: child-replace ( unbalanced ) |
794 | 941 -- / \ 3: child-replace ( rotated / balanced ) |
942 -- L L | |
943 -- | |
792 | 944 rb04 : stackInvariant key (RBI.tree r) orig stack → stack ≡ RBI.tree r ∷ orig ∷ [] → tr0 ≡ orig → right ≡ RBI.tree r |
945 rb04 (s-right tree .(node key₁ _ tree₁ tree) tree₁ x s-nil) refl refl = refl | |
946 rb04 (s-left tree₁ .(node key₁ value₁ left right) tree {key₂} x si) refl refl with si-property1 si | |
947 ... | refl = ⊥-elim ( nat-<> x c ) | |
948 insertCase13 : (v : Color ∧ A ) → v ≡ value₁ → t | |
795 | 949 insertCase13 ⟪ Black , value₄ ⟫ refl = exit (node key₁ ⟪ Red , value₄ ⟫ left repl) (orig ∷ []) refl record { |
791 | 950 tree = orig ; rot = node key₁ value₁ left (RBI.rot r) |
790 | 951 ; origti = RBI.origti r |
952 ; origrb = RBI.origrb r | |
791 | 953 ; treerb = RBI.origrb r |
793 | 954 ; replrb = proj1 rb05 |
791 | 955 ; si = s-nil |
956 ; rotated = subst (λ k → rotatedTree k (node key₁ value₁ left (RBI.rot r))) ( | |
793 | 957 trans (cong (λ k → node key₁ value₁ left k) (sym (rb04 si eq refl))) refl) (rtt-node rtt-unit (RBI.rotated r)) |
958 ; ri = proj2 rb05 | |
795 | 959 ; repl-red = refl |
960 ; repl-eq = rb07 | |
792 | 961 } where |
795 | 962 rb07 : black-depth (child-replaced key (node key₁ ⟪ Black , value₄ ⟫ left (RBI.rot r))) ≡ black-depth left ⊔ black-depth repl |
798 | 963 rb07 with <-cmp key key₁ |
800 | 964 ... | tri< a ¬b ¬c = DepthCal (black-depth (left)) (black-depth left) (black-depth repl) |
965 ... | tri≈ ¬a b ¬c = DepthCal (black-depth (node key₁ ⟪ Black , value₄ ⟫ left (RBI.rot r))) (black-depth left) (black-depth repl) | |
966 ... | tri> ¬a ¬b c = DepthCal (black-depth (RBI.rot r)) (black-depth left) (black-depth repl) | |
802 | 967 |
968 rb09 : {n : Level} {A : Set n} → {key key1 key2 : ℕ} {value value1 : A} {t1 t2 : bt (Color ∧ A)} | |
969 → RBtreeInvariant (node key ⟪ Red , value ⟫ leaf (node key1 ⟪ Black , value1 ⟫ t1 t2)) | |
970 → key < key1 | |
971 rb09 (rb-right-red x x1 x2) = x | |
803 | 972 -- rb05 should more general |
804 | 973 tkey : {n : Level} {A : Set n } → (rbt : bt (Color ∧ A)) → ℕ |
974 tkey (node key value t t2) = key | |
975 tkey leaf = {!!} -- key is none | |
807 | 976 rb051 : {n : Level} {A : Set n} {key key1 : ℕ } {value : A} {t t1 t2 : bt (Color ∧ A)} {c : Color} → replacedRBTree key value (node key1 ⟪ c , value ⟫ t1 t2) (node key1 ⟪ c , value ⟫ t1 t) → key1 < key |
977 rb051 = {!!} | |
978 rb052 : {key key₁ : ℕ} → stackInvariant key (RBI.tree r) orig stack → key < key₁ | |
979 rb052 = {!!} | |
795 | 980 rb05 : RBtreeInvariant (node key₁ ⟪ Red , value₄ ⟫ left repl) ∧ replacedRBTree key value |
981 (child-replaced key (node key₁ ⟪ Black , value₄ ⟫ left (RBI.rot r))) (node key₁ ⟪ Red , value₄ ⟫ left repl) | |
807 | 982 rb05 with RBI.origrb r | rb04 si eq refl | RBI.replrb r in eqrepl | <-cmp key₁ (tkey repl) -- si and ri have key's relation |
983 ... | rb-single key₂ value₂ | refl | rb-single key₃ value | tri< a ¬b ¬c = {!!} -- need paternmatch with c | |
804 | 984 ... | rb-single key₂ value₂ | refl | rb-single key₃ value | tri≈ ¬a b ¬c = {!!} -- key is unique ? |
807 | 985 ... | rb-single key₂ value₂ | refl | rb-single key₃ value | tri> ¬a ¬b c = ⊥-elim (nat-<> {!!} {!!}) --⊥-elim (¬a (rb052 {!!})) |
803 | 986 ... | rb-single key₂ value₂ | refl | rb-right-red x x₁ rb | t = {!!} -- cant happen ? red - red |
805 | 987 ... | rb-single key₂ value₂ | refl | rb-right-black x x₁ rb | tri< a ¬b ¬c = ⟪ rb-right-red a (blackdepth≡ leaf (RightDown repl) rb-leaf (RBtreeRightDown leaf (RightDown repl) (RBI.replrb r)) (RBI.replrb r)) (RBI.replrb r) , {!!} ⟫ -- t₁ t₂ = leaf , c₁ = Red |
807 | 988 ... | rb-single key₂ value₂ | refl | rb-right-black x x₁ rb | tri≈ ¬a b ¬c = {!!} --rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} |
989 ... | rb-single key₂ value₂ | refl | rb-right-black x x₁ rb | tri> ¬a ¬b c = ⊥-elim ({!!} ) | |
803 | 990 ... | rb-single key₂ value₂ | refl | rb-left-red x x₁ rb | t = {!!} --red -red |
806 | 991 ... | rb-single key₂ value₂ | refl | rb-left-black x x₁ rb | tri< a ¬b ¬c = ⟪ rb-right-red a (blackdepth≡ (LeftDown repl) leaf (RBtreeLeftDown (LeftDown repl) leaf (RBI.replrb r)) rb-leaf (RBI.replrb r)) (RBI.replrb r) , {!!} ⟫ |
807 | 992 ... | rb-single key₁ value₂ | refl | rb-left-black x x₁ rb | tri≈ ¬a b ¬c = ⊥-elim (¬c {!!} ) --(rb09 (RBI.origrb r))) |
993 ... | rb-single key₂ value₂ | refl | rb-left-black x x₁ rb | tri> ¬a ¬b c = {!!} | |
994 ... | rb-single key₂ value₂ | refl | rb-node-red x x₁ x₂ rb rb₁ | t = {!!} -- red-red | |
995 ... | rb-single key₂ value₂ | refl | rb-node-black x x₁ x₂ rb rb₁ | tri< a ¬b ¬c = {!!} | |
996 ... | rb-single key₂ value₂ | refl | rb-node-black x x₁ x₂ rb rb₁ | tri≈ ¬a b ¬c = {!!} | |
997 ... | rb-single key₂ value₂ | refl | rb-node-black x x₁ x₂ rb rb₁ | tri> ¬a ¬b c = ⊥-elim {!!} | |
803 | 998 --... | rb-right-black x x₁ t | refl | rb | t = ? --⟪ proj1 {!!} , {!!} ⟫ |
999 --... | rb-left-black x x₁ t | refl | rb | t = {!!} --⟪ rb-node-red ? ? ? ? ? ? , ? ⟫ | |
1000 --... | rb-node-black x x₁ x₂ t x₃ t₁ | refl | rb | t = {!!} | |
792 | 1001 insertCase13 ⟪ Red , value₄ ⟫ eq with color (RBI.tree r) |
798 | 1002 ... | Black = exit {!!} {!!} {!!} {!!} |
1003 ... | Red = exit {!!} {!!} {!!} {!!} | |
791 | 1004 -- r = orig RBI.tree b |
1005 -- / \ => / \ | |
1006 -- b b → r RBI.tree r r = orig o (r/b) | |
790 | 1007 ... | case2 (case2 pg) = {!!} -- insertCase2 tree (PG.parent pg) (PG.uncle pg) (PG.grand pg) stack si (PG.pg pg) |
724 | 1008 |
740 | 1009 |
785 | 1010 |
807 | 1011 |