Mercurial > hg > Gears > GearsAgda
annotate hoareBinaryTree1.agda @ 747:70ed4cbeaafb
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 25 Apr 2023 09:05:03 +0900 |
parents | 4edec19e8356 |
children | 1d7803a2c4c0 |
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 |
588 | 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 |
605 | 42 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_) |
43 | |
620 | 44 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where |
45 t-leaf : treeInvariant leaf | |
632 | 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₂) |
632 | 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₂) |
632 | 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₂ |
620 | 52 → treeInvariant (node key value t₁ t₂) |
53 → treeInvariant (node key₂ value₂ t₃ t₄) | |
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 ∷ []) |
653 | 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) |
653 | 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) |
68 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁) | |
69 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} | |
677 | 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} |
687 | 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 ∷ [] ) |
729 | 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 ) () |
666 | 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 |
666 | 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 |
666 | 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 |
666 | 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 |
642 | 118 ti-right : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 tree₁ repl) → treeInvariant repl |
119 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf | |
120 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-right x ti) = ti | |
121 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-left x ti) = t-leaf | |
122 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti₁ | |
123 | |
124 ti-left : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 repl tree₁ ) → treeInvariant repl | |
125 ti-left {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf | |
126 ti-left {_} {_} {_} {_} {key₁} {v1} (t-right x ti) = t-leaf | |
127 ti-left {_} {_} {_} {_} {key₁} {v1} (t-left x ti) = ti | |
128 ti-left {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti | |
129 | |
662 | 130 stackTreeInvariant : {n : Level} {A : Set n} (key : ℕ) (sub tree : bt A) → (stack : List (bt A)) |
131 → treeInvariant tree → stackInvariant key sub tree stack → treeInvariant sub | |
729 | 132 stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-nil ) = ti |
662 | 133 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si ) = ti-right (si1 si) where |
134 si1 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 tree₁ sub ) tree st → treeInvariant (node key₁ v1 tree₁ sub ) | |
135 si1 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 tree₁ sub ) tree st ti si | |
136 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si ) = ti-left ( si2 si) where | |
137 si2 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 sub tree₁ ) tree st → treeInvariant (node key₁ v1 sub tree₁ ) | |
138 si2 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 sub tree₁ ) tree st ti si | |
139 | |
639 | 140 rt-property1 : {n : Level} {A : Set n} (key : ℕ) (value : A) (tree tree1 : bt A ) → replacedTree key value tree tree1 → ¬ ( tree1 ≡ leaf ) |
141 rt-property1 {n} {A} key value .leaf .(node key value leaf leaf) r-leaf () | |
142 rt-property1 {n} {A} key value .(node key _ _ _) .(node key value _ _) r-node () | |
677 | 143 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-right x rt) = λ () |
144 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-left x rt) = λ () | |
639 | 145 |
690 | 146 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 |
147 rt-property-leaf r-leaf = refl | |
148 | |
698 | 149 rt-property-¬leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {tree : bt A} → ¬ replacedTree key value tree leaf |
150 rt-property-¬leaf () | |
151 | |
692 | 152 rt-property-key : {n : Level} {A : Set n} {key key₂ key₃ : ℕ} {value value₂ value₃ : A} {left left₁ right₂ right₃ : bt A} |
153 → replacedTree key value (node key₂ value₂ left right₂) (node key₃ value₃ left₁ right₃) → key₂ ≡ key₃ | |
154 rt-property-key r-node = refl | |
155 rt-property-key (r-right x ri) = refl | |
156 rt-property-key (r-left x ri) = refl | |
157 | |
698 | 158 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥ |
159 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x | |
160 nat-<> : { x y : ℕ } → x < y → y < x → ⊥ | |
161 nat-<> (s≤s x<y) (s≤s y<x) = nat-<> x<y y<x | |
162 | |
163 open _∧_ | |
164 | |
165 | |
632 | 166 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j ) |
167 depth-1< {i} {j} = s≤s (m≤m⊔n _ j) | |
168 | |
169 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i ) | |
650 | 170 depth-2< {i} {j} = s≤s (m≤n⊔m j i) |
611 | 171 |
649 | 172 depth-3< : {i : ℕ } → suc i ≤ suc (suc i) |
173 depth-3< {zero} = s≤s ( z≤n ) | |
174 depth-3< {suc i} = s≤s (depth-3< {i} ) | |
175 | |
176 | |
634 | 177 treeLeftDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) |
178 → treeInvariant (node k v1 tree tree₁) | |
179 → treeInvariant tree | |
180 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf | |
181 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf | |
182 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti | |
183 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti | |
184 | |
185 treeRightDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) | |
186 → treeInvariant (node k v1 tree tree₁) | |
187 → treeInvariant tree₁ | |
188 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf | |
189 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti | |
190 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf | |
191 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti₁ | |
192 | |
615 | 193 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A)) |
662 | 194 → treeInvariant tree ∧ stackInvariant key tree tree0 stack |
693 | 195 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t ) |
196 → (exit : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
638 | 197 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
693 | 198 findP key leaf tree0 st Pre _ exit = exit leaf st Pre (case1 refl) |
632 | 199 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁ |
693 | 200 findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) |
201 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
|
202 ⟪ 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
|
203 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) |
664 | 204 findP1 a (x ∷ st) si = s-left a si |
693 | 205 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 | 206 |
638 | 207 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₁) |
208 replaceTree1 k v1 value (t-single .k .v1) = t-single k value | |
209 replaceTree1 k v1 value (t-right x t) = t-right x t | |
210 replaceTree1 k v1 value (t-left x t) = t-left x t | |
211 replaceTree1 k v1 value (t-node x x₁ t t₁) = t-node x x₁ t t₁ | |
212 | |
649 | 213 open import Relation.Binary.Definitions |
214 | |
215 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥ | |
216 lemma3 refl () | |
217 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥ | |
218 lemma5 (s≤s z≤n) () | |
700 | 219 ¬x<x : {x : ℕ} → ¬ (x < x) |
220 ¬x<x (s≤s lt) = ¬x<x lt | |
649 | 221 |
687 | 222 child-replaced : {n : Level} {A : Set n} (key : ℕ) (tree : bt A) → bt A |
223 child-replaced key leaf = leaf | |
224 child-replaced key (node key₁ value left right) with <-cmp key key₁ | |
225 ... | tri< a ¬b ¬c = left | |
226 ... | tri≈ ¬a b ¬c = node key₁ value left right | |
227 ... | tri> ¬a ¬b c = right | |
677 | 228 |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
229 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
|
230 field |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
231 tree0 : bt A |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
232 ti : treeInvariant tree0 |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
233 si : stackInvariant key tree tree0 stack |
687 | 234 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
|
235 ci : C tree repl stack -- data continuation |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
236 |
638 | 237 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) |
238 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key ) | |
694 | 239 → (treeInvariant tree ) → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value (child-replaced key tree) tree1 → t) → t |
240 replaceNodeP k v1 leaf C P next = next (node k v1 leaf leaf) (t-single k v1 ) r-leaf | |
241 replaceNodeP k v1 (node .k value t t₁) (case2 refl) P next = next (node k v1 t t₁) (replaceTree1 k value v1 P) | |
695 | 242 (subst (λ j → replacedTree k v1 j (node k v1 t t₁) ) repl00 r-node) where |
694 | 243 repl00 : node k value t t₁ ≡ child-replaced k (node k value t t₁) |
244 repl00 with <-cmp k k | |
245 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl) | |
246 ... | tri≈ ¬a b ¬c = refl | |
247 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl) | |
606 | 248 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
249 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
|
250 → (key : ℕ) → (value : A) → {tree : bt A} ( repl : bt A) |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
251 → (stack : List (bt A)) → replacePR key value tree repl stack (λ _ _ _ → Lift n ⊤) |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
252 → (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
|
253 → replacePR key value tree1 repl stack1 (λ _ _ _ → Lift n ⊤) → length stack1 < length stack → t) |
613 | 254 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t |
675 | 255 replaceP key value {tree} repl [] Pre next exit = ⊥-elim ( si-property0 (replacePR.si Pre) refl ) -- can't happen |
256 replaceP key value {tree} repl (leaf ∷ []) Pre next exit with si-property-last _ _ _ _ (replacePR.si Pre)-- tree0 ≡ leaf | |
677 | 257 ... | refl = exit (replacePR.tree0 Pre) (node key value leaf leaf) ⟪ replacePR.ti Pre , r-leaf ⟫ |
689 | 258 replaceP key value {tree} repl (node key₁ value₁ left right ∷ []) Pre next exit with <-cmp key key₁ |
259 ... | tri< a ¬b ¬c = exit (replacePR.tree0 Pre) (node key₁ value₁ repl right ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
260 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right ) | |
261 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
262 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ k right ) (node key₁ value₁ repl right )) repl02 (r-left a repl03) where | |
263 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
264 repl03 = replacePR.ri Pre | |
265 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left | |
266 repl02 with <-cmp key key₁ | |
267 ... | tri< a ¬b ¬c = refl | |
268 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a) | |
269 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a) | |
270 ... | tri≈ ¬a b ¬c = exit (replacePR.tree0 Pre) repl ⟪ replacePR.ti Pre , repl01 ⟫ where | |
678 | 271 repl01 : replacedTree key value (replacePR.tree0 Pre) repl |
272 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
689 | 273 repl01 | refl | refl = subst (λ k → replacedTree key value k repl) repl02 (replacePR.ri Pre) where |
274 repl02 : child-replaced key (node key₁ value₁ left right) ≡ node key₁ value₁ left right | |
275 repl02 with <-cmp key key₁ | |
276 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b) | |
277 ... | tri≈ ¬a b ¬c = refl | |
278 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b) | |
279 ... | tri> ¬a ¬b c = exit (replacePR.tree0 Pre) (node key₁ value₁ left repl ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
280 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl ) | |
281 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
282 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ left k ) (node key₁ value₁ left repl )) repl02 (r-right c repl03) where | |
283 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
284 repl03 = replacePR.ri Pre | |
285 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right | |
286 repl02 with <-cmp key key₁ | |
287 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c) | |
288 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c) | |
289 ... | tri> ¬a ¬b c = refl | |
690 | 290 replaceP {n} {_} {A} key value {tree} repl (leaf ∷ st@(tree1 ∷ st1)) Pre next exit = next key value repl st Post ≤-refl where |
291 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) | |
292 Post with replacePR.si Pre | |
293 ... | s-right {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
294 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf | |
295 repl09 = si-property1 si | |
296 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
297 repl10 with si-property1 si | |
298 ... | refl = si | |
299 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf | |
300 repl07 with <-cmp key key₂ | |
301 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) | |
302 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
303 ... | tri> ¬a ¬b c = refl | |
304 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
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 | |
306 ... | s-left {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
307 repl09 : tree1 ≡ node key₂ v1 leaf tree₁ | |
308 repl09 = si-property1 si | |
309 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
310 repl10 with si-property1 si | |
311 ... | refl = si | |
312 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf | |
313 repl07 with <-cmp key key₂ | |
314 ... | tri< a ¬b ¬c = refl | |
315 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
316 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
317 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
318 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 | |
683 | 319 replaceP {n} {_} {A} key value {tree} repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre next exit with <-cmp key key₁ |
320 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st Post ≤-refl where | |
675 | 321 Post : replacePR key value tree1 (node key₁ value₁ repl right ) st (λ _ _ _ → Lift n ⊤) |
687 | 322 Post with replacePR.si Pre |
688 | 323 ... | s-right {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
324 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) | |
325 repl09 = si-property1 si | |
326 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
327 repl10 with si-property1 si | |
328 ... | refl = si | |
329 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left | |
330 repl03 with <-cmp key key₁ | |
331 ... | tri< a1 ¬b ¬c = refl | |
332 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
333 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
334 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right | |
335 repl02 with repl09 | <-cmp key key₂ | |
336 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
689 | 337 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) |
688 | 338 ... | refl | tri> ¬a ¬b c = refl |
339 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
340 repl04 = begin | |
341 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
342 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
343 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
344 child-replaced key tree1 ∎ where open ≡-Reasoning | |
345 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
346 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) | |
687 | 347 ... | s-left {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
688 | 348 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ |
683 | 349 repl09 = si-property1 si |
350 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
351 repl10 with si-property1 si | |
352 ... | refl = si | |
687 | 353 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left |
354 repl03 with <-cmp key key₁ | |
355 ... | tri< a1 ¬b ¬c = refl | |
356 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
357 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
358 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right | |
359 repl02 with repl09 | <-cmp key key₂ | |
360 ... | refl | tri< a ¬b ¬c = refl | |
361 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
362 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
363 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
364 repl04 = begin | |
365 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
366 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
367 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
368 child-replaced key tree1 ∎ where open ≡-Reasoning | |
369 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
370 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) | |
705 | 371 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st Post ≤-refl where |
690 | 372 Post : replacePR key value tree1 (node key₁ value left right ) (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) |
373 Post with replacePR.si Pre | |
691 | 374 ... | 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 | 375 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right) |
376 repl09 = si-property1 si | |
377 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
378 repl10 with si-property1 si | |
379 ... | refl = si | |
380 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree | |
381 repl07 with <-cmp key key₂ | |
382 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) | |
383 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
384 ... | tri> ¬a ¬b c = refl | |
691 | 385 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
386 repl12 refl with repl09 | |
387 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node | |
388 ... | 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 | |
690 | 389 repl09 : tree1 ≡ node key₂ v1 tree tree₁ |
390 repl09 = si-property1 si | |
391 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
392 repl10 with si-property1 si | |
393 ... | refl = si | |
394 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree | |
395 repl07 with <-cmp key key₂ | |
396 ... | tri< a ¬b ¬c = refl | |
397 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
398 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
691 | 399 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
400 repl12 refl with repl09 | |
401 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node | |
690 | 402 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st Post ≤-refl where |
403 Post : replacePR key value tree1 (node key₁ value₁ left repl ) st (λ _ _ _ → Lift n ⊤) | |
404 Post with replacePR.si Pre | |
405 ... | s-right {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
406 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) | |
407 repl09 = si-property1 si | |
408 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
409 repl10 with si-property1 si | |
410 ... | refl = si | |
411 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
412 repl03 with <-cmp key key₁ | |
413 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) | |
414 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
415 ... | tri> ¬a ¬b c = refl | |
416 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right | |
417 repl02 with repl09 | <-cmp key key₂ | |
418 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
419 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) | |
420 ... | refl | tri> ¬a ¬b c = refl | |
421 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
422 repl04 = begin | |
423 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
424 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
425 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
426 child-replaced key tree1 ∎ where open ≡-Reasoning | |
427 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
428 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) | |
429 ... | s-left {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
430 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ | |
431 repl09 = si-property1 si | |
432 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
433 repl10 with si-property1 si | |
434 ... | refl = si | |
435 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
436 repl03 with <-cmp key key₁ | |
437 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) | |
438 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
439 ... | tri> ¬a ¬b c = refl | |
440 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right | |
441 repl02 with repl09 | <-cmp key key₂ | |
442 ... | refl | tri< a ¬b ¬c = refl | |
443 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
444 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
445 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
446 repl04 = begin | |
447 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
448 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
449 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
450 child-replaced key tree1 ∎ where open ≡-Reasoning | |
451 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
452 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) | |
644 | 453 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
454 TerminatingLoopS : {l m : Level} {t : Set l} (Index : Set m ) → {Invraiant : Index → Set m } → ( reduce : Index → ℕ) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
455 → (r : Index) → (p : Invraiant r) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
456 → (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
|
457 TerminatingLoopS {_} {_} {t} Index {Invraiant} reduce r p loop with <-cmp 0 (reduce r) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
458 ... | tri≈ ¬a b ¬c = loop r p (λ r1 p1 lt → ⊥-elim (lemma3 b lt) ) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
459 ... | tri< a ¬b ¬c = loop r p (λ r1 p1 lt1 → TerminatingLoop1 (reduce r) r r1 (≤-step lt1) p1 lt1 ) where |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
460 TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → Invraiant r1 → reduce r1 < reduce r → t |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
461 TerminatingLoop1 zero r r1 n≤j p1 lt = loop r1 p1 (λ r2 p1 lt1 → ⊥-elim (lemma5 n≤j lt1)) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
462 TerminatingLoop1 (suc j) r r1 n≤j p1 lt with <-cmp (reduce r1) (suc j) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
463 ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a p1 lt |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
464 ... | tri≈ ¬a b ¬c = loop r1 p1 (λ r2 p2 lt1 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b lt1 ) p2 lt1 ) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
465 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c n≤j ) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
466 |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
467 open _∧_ |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
468 |
615 | 469 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
470 → replacedTree key value tree repl → treeInvariant repl | |
692 | 471 RTtoTI0 .leaf .(node key value leaf leaf) key value ti r-leaf = t-single key value |
472 RTtoTI0 .(node key _ leaf leaf) .(node key value leaf leaf) key value (t-single .key _) r-node = t-single key value | |
473 RTtoTI0 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti | |
474 RTtoTI0 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
475 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 | 476 -- r-right case |
692 | 477 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) |
478 RTtoTI0 (node _ _ leaf right@(node _ _ _ _)) (node key₁ value₁ leaf leaf) key value (t-right x₁ ti) (r-right x ri) = t-single key₁ value₁ | |
693 | 479 RTtoTI0 (node key₁ _ leaf right@(node key₂ _ _ _)) (node key₁ value₁ leaf right₁@(node key₃ _ _ _)) key value (t-right x₁ ti) (r-right x ri) = |
480 t-right (subst (λ k → key₁ < k ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) | |
692 | 481 RTtoTI0 (node key₁ _ (node _ _ _ _) leaf) (node key₁ _ (node key₃ value left right) leaf) key value₁ (t-left x₁ ti) (r-right x ()) |
482 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) = | |
483 t-node x₁ x ti (t-single key value) | |
693 | 484 RTtoTI0 (node key₁ _ (node _ _ _ _) (node key₂ _ _ _)) (node key₁ _ (node _ _ _ _) (node key₃ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) = |
485 t-node x₁ (subst (λ k → key₁ < k) (rt-property-key ri) x₂) ti (RTtoTI0 _ _ key value ti₁ ri) | |
701 | 486 -- r-left case |
700 | 487 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 | 488 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 |
489 RTtoTI0 (node key₃ _ (node key₂ _ _ _) leaf) (node key₃ _ (node key₁ value₁ left left₁) leaf) key value (t-left x₁ ti) (r-left x ri) = | |
490 t-left (subst (λ k → k < key₃ ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) -- key₁ < key₃ | |
491 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 | 492 |
493 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl | |
494 → replacedTree key value tree repl → treeInvariant tree | |
701 | 495 RTtoTI1 .leaf .(node key value leaf leaf) key value ti r-leaf = t-leaf |
496 RTtoTI1 (node key value₁ leaf leaf) .(node key value leaf leaf) key value (t-single .key .value) r-node = t-single key value₁ | |
497 RTtoTI1 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti | |
498 RTtoTI1 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
499 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₁ | |
500 -- r-right case | |
501 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₁ | |
502 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) = | |
503 t-right (subst (λ k → key₁ < k ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₁ < key₂ | |
504 RTtoTI1 (node _ _ (node _ _ _ _) leaf) (node _ _ (node _ _ _ _) (node key value _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x r-leaf) = | |
505 t-left x₁ ti | |
506 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₁ | |
507 -- r-left case | |
508 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ _ leaf) key value (t-left x₁ ti) (r-left x ri) = t-single key₁ value₁ | |
509 RTtoTI1 (node key₁ _ (node key₂ value₁ n n₁) leaf) (node key₁ _ (node key₃ _ _ _) leaf) key value (t-left x₁ ti) (r-left x ri) = | |
510 t-left (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₂ < key₁ | |
511 RTtoTI1 (node key₁ value₁ leaf _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x r-leaf) = t-right x₂ ti₁ | |
512 RTtoTI1 (node key₁ value₁ (node key₂ value₂ n n₁) _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) = | |
513 t-node (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) x₂ (RTtoTI1 _ _ key value ti ri) ti₁ -- key₂ < key₁ | |
614 | 514 |
611 | 515 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
696 | 516 → (exit : (tree repl : bt A) → treeInvariant repl ∧ replacedTree key value tree repl → t ) → t |
693 | 517 insertTreeP {n} {m} {A} {t} tree key value P0 exit = |
729 | 518 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 ⟫ |
696 | 519 $ λ p P loop → findP key (proj1 p) tree (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
693 | 520 $ λ t s P C → replaceNodeP key value t C (proj1 P) |
521 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A ) | |
522 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) } | |
696 | 523 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = tree ; ti = P0 ; si = proj2 P ; ri = R ; ci = lift tt } |
693 | 524 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1 |
696 | 525 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt ) |
526 $ λ tree repl P → exit tree repl ⟪ RTtoTI0 _ _ _ _ (proj1 P) (proj2 P) , proj2 P ⟫ | |
614 | 527 |
696 | 528 insertTestP1 = insertTreeP leaf 1 1 t-leaf |
722 | 529 $ λ _ x0 P0 → insertTreeP x0 2 1 (proj1 P0) |
530 $ λ _ x1 P1 → insertTreeP x1 3 2 (proj1 P1) | |
727 | 531 $ λ _ x2 P2 → insertTreeP x2 2 2 (proj1 P2) (λ _ x P → x ) |
694 | 532 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
533 top-value : {n : Level} {A : Set n} → (tree : bt A) → Maybe A |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
534 top-value leaf = nothing |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
535 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
|
536 |
722 | 537 -- is realy inserted? |
538 | |
539 -- other element is preserved? | |
702 | 540 |
722 | 541 -- deletion? |
616 | 542 |
722 | 543 data Color : Set where |
544 Red : Color | |
545 Black : Color | |
618 | 546 |
746 | 547 data RBtreeInvariant {n : Level} {A : Set n} : (tree : bt (Color ∧ A)) → (bd : ℕ) → Set n where |
548 rb-leaf : RBtreeInvariant leaf 0 | |
549 rb-single : (key : ℕ) → (value : A) → (c : Color) → RBtreeInvariant (node key ⟪ c , value ⟫ leaf leaf) 1 | |
550 rb-right-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {d : ℕ } | |
551 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁) d | |
552 → RBtreeInvariant (node key ⟪ Red , value ⟫ leaf (node key₁ ⟪ Black , value₁ ⟫ t t₁)) d | |
553 rb-right-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {d : ℕ } {c : Color} | |
554 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁) d | |
555 → RBtreeInvariant (node key ⟪ Black , value ⟫ leaf (node key₁ ⟪ c , value₁ ⟫ t t₁)) (suc d) | |
556 rb-left-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {d : ℕ } | |
557 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁) d | |
558 → RBtreeInvariant (node key₁ ⟪ Red , value ⟫ (node key ⟪ Black , value₁ ⟫ t t₁) leaf ) d | |
559 rb-left-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {d : ℕ } {c : Color} | |
560 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁) d | |
561 → RBtreeInvariant (node key₁ ⟪ Black , value ⟫ (node key ⟪ c , value₁ ⟫ t t₁) leaf) (suc d) | |
562 rb-node-red : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂ → {d : ℕ} | |
563 → RBtreeInvariant (node key ⟪ Black , value ⟫ t₁ t₂) d | |
564 → RBtreeInvariant (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄) d | |
565 → RBtreeInvariant (node key₁ ⟪ Red , value₁ ⟫ (node key ⟪ Black , value ⟫ t₁ t₂) (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄)) d | |
566 rb-node-black : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂ → {d : ℕ} | |
567 → {c c₁ : Color} | |
568 → RBtreeInvariant (node key ⟪ c , value ⟫ t₁ t₂) d | |
569 → RBtreeInvariant (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄) d | |
570 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ (node key ⟪ c , value ⟫ t₁ t₂) (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄)) (suc d) | |
745 | 571 |
746 | 572 |
573 -- This one can be very difficult | |
574 -- data replacedRBTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt (Color ∧ A) ) → Set n where | |
575 -- rb-leaf : replacedRBTree key value leaf (node key ⟪ Black , value ⟫ leaf leaf) | |
745 | 576 |
747 | 577 color : {n : Level} (A : Set n) → (rb : bt (Color ∧ A)) → Color |
578 color {n} A rb = ? | |
723 | 579 |
747 | 580 RB→bt : {n : Level} (A : Set n) → (rb : bt (Color ∧ A)) → bt A |
746 | 581 RB→bt {n} A leaf = leaf |
747 | 582 RB→bt {n} A (node key ⟪ c , value ⟫ rb rb₁) = node key value (RB→bt A rb) (RB→bt A rb₁) |
723 | 583 |
737 | 584 data ParentGrand {n : Level} {A : Set n} (self : bt A) : (parent grand : bt A) → Set n where |
740 | 585 s2-s1p2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } |
586 → parent ≡ node kp vp self n1 → grand ≡ node kg vg parent n2 → ParentGrand self parent grand | |
587 s2-1sp2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
588 → parent ≡ node kp vp n1 self → grand ≡ node kg vg parent n2 → ParentGrand self parent grand | |
589 s2-s12p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
590 → parent ≡ node kp vp self n1 → grand ≡ node kg vg n2 parent → ParentGrand self parent grand | |
591 s2-1s2p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A } | |
592 → parent ≡ node kp vp n1 self → grand ≡ node kg vg n2 parent → ParentGrand self parent grand | |
734 | 593 |
746 | 594 -- with color |
736 | 595 data rotatedTree {n : Level} {A : Set n} : (before after : bt A ) → Set n where |
596 rr-node : {t : bt A} → rotatedTree t t | |
746 | 597 -- a b |
598 -- b c d a | |
599 -- d e e c | |
600 rr-right : {ka kb : ℕ } {va vb : A} → {c c₁ d d₁ e e₁ : bt A} | |
736 | 601 → ka < kb |
746 | 602 → rotatedTree d d₁ → rotatedTree e e₁ → rotatedTree c c₁ |
747 | 603 → rotatedTree (node ka va (node kb vb d e) c) (node kb vb d₁ (node ka va e₁ c₁) ) |
745 | 604 -- b a |
605 -- d a b c | |
746 | 606 -- e c d e |
607 rr-left : {ka kb : ℕ } {va vb : A} → {c c₁ d d₁ e e₁ : bt A} | |
736 | 608 → ka < kb |
746 | 609 → rotatedTree d d₁ → rotatedTree e e₁ → rotatedTree c c₁ |
610 → rotatedTree (node kb vb d (node ka va e c) ) (node ka va (node kb vb d₁ e₁) c₁) | |
734 | 611 |
740 | 612 record PG {n : Level } (A : Set n) (stack : List (bt A)) : Set n where |
613 field | |
614 self parent grand : bt A | |
615 pg : ParentGrand self parent grand | |
616 rest : List (bt A) | |
617 stack=gp : stack ≡ ( self ∷ parent ∷ grand ∷ rest ) | |
618 | |
619 stackToPG : {n : Level} {A : Set n} → {key : ℕ } → (tree orig : bt A ) | |
620 → (stack : List (bt A)) → stackInvariant key tree orig stack | |
621 → ( stack ≡ orig ∷ [] ) ∨ ( stack ≡ tree ∷ orig ∷ [] ) ∨ PG A stack | |
622 stackToPG {n} {A} {key} tree .tree .(tree ∷ []) s-nil = case1 refl | |
623 stackToPG {n} {A} {key} tree .(node _ _ _ tree) .(tree ∷ node _ _ _ tree ∷ []) (s-right x s-nil) = case2 (case1 refl) | |
741 | 624 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 |
625 record { self = tree ; parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) | |
626 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 | |
627 record { self = tree ; parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) | |
628 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 | |
629 record { self = tree ; parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } ) | |
742 | 630 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 |
631 record { self = tree ; parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) | |
632 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 | |
633 record { self = tree ; parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) | |
634 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 | |
635 record { self = tree ; parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } ) | |
636 stackToPG {n} {A} {key} tree .(node _ _ tree _) .(tree ∷ node _ _ tree _ ∷ []) (s-left {_} {_} {t1} {k1} {v1} x s-nil) = case2 (case1 refl) | |
637 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 | |
638 record { self = tree ; parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) | |
639 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 | |
640 record { self = tree ; parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) | |
641 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 | |
642 record { self = tree ; parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } ) | |
643 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 | |
644 record { self = tree ; parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) | |
645 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 | |
646 record { self = tree ; parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) | |
647 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 | |
648 record { self = tree ; parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } ) | |
740 | 649 |
650 | |
736 | 651 rbsi-len : {n : Level} {A : Set n} {orig parent grand : bt A} |
737 | 652 → ParentGrand orig parent grand → ℕ |
736 | 653 rbsi-len {n} {A} {s} {p} {g} st = ? |
734 | 654 |
747 | 655 -- findRBP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) {key1 d d1 : ℕ} → {c c1 : Color} → (tree : RBTree A key c d ) (orig : RBTree A key1 c1 d1 ) |
656 -- → (stack : List (bt A)) → stackInvariant key (RB→bt A tree) (RB→bt A orig) stack | |
657 -- → (next : {key0 d0 : ℕ} {c0 : Color} → (tree1 : RBTree A key0 c0 d0 ) → (stack : List (bt A)) → stackInvariant key (RB→bt A tree1) (RB→bt A orig) stack → rbt-depth A tree1 < rbt-depth A tree → t ) | |
658 -- → (exit : {key0 d0 : ℕ} {c0 : Color} → (tree1 : RBTree A key0 c0 d0 ) → (stack : List (bt A)) → stackInvariant key (RB→bt A tree1) (RB→bt A orig) stack | |
659 -- → (rbt-depth A tree1 ≡ 0 ) ∨ ( rbt-key A tree1 ≡ just key ) → t ) → t | |
660 --findRBP {n} {m} {A} {t} key {key1} tree orig st si next exit = ? | |
723 | 661 |
731 | 662 rotateRight : ? |
663 rotateRight = ? | |
733 | 664 |
731 | 665 rotateLeft : ? |
666 rotateLeft = ? | |
667 | |
729 | 668 insertCase5 : {n m : Level} {A : Set n} {t : Set m} |
669 → (key : ℕ) → (value : A) → {key0 key1 key2 d0 d1 d2 : ℕ} {c0 c1 c2 : Color} | |
747 | 670 → (orig tree repl : bt (Color ∧ A) ) |
737 | 671 → (si : ParentGrand ? ? ?) |
736 | 672 → (ri : rotatedTree (RB→bt A tree) (RB→bt A repl)) |
747 | 673 → (next : ℕ → A → {k1 k2 d1 d2 : ℕ} {c1 c2 : Color} → (tree1 repl1 : bt (Color ∧ A)) |
737 | 674 → (si1 : ParentGrand ? ? ?) |
736 | 675 → (ri : rotatedTree (RB→bt A tree1) (RB→bt A repl1)) |
676 → rbsi-len si1 < rbsi-len si → t ) | |
747 | 677 → (exit : {k1 k2 d1 d2 : ℕ} {c1 c2 : Color} (tree1 repl1 : bt (Color ∧ A)) |
736 | 678 → (ri : rotatedTree (RB→bt A orig) (RB→bt A repl1)) |
729 | 679 → t ) → t |
680 insertCase5 {n} {m} {A} {t} key value orig tree repl si ri next exit = ? where | |
737 | 681 insertCase51 : (key1 : ℕ) (si : ParentGrand ? ? ? ) → t |
729 | 682 insertCase51 = ? |
723 | 683 |
724 | 684 replaceRBP : {n m : Level} {A : Set n} {t : Set m} |
737 | 685 → (key : ℕ) → (value : A) → {key0 key1 d0 d1 : ℕ} {c0 c1 : Color} |
747 | 686 → (orig tree : bt (Color ∧ A)) |
687 → (stack : List (bt (Color ∧ A))) → (si : stackInvariant key tree orig stack ) | |
688 → (next : {key2 d2 : ℕ} {c2 : Color} | |
689 → (to tr : bt (Color ∧ A)) | |
690 → (stack1 : List (bt (Color ∧ A))) → stackInvariant key tr to stack1 | |
737 | 691 → length stack1 < length stack → t ) |
747 | 692 → (exit : {k1 d1 : ℕ} {c1 : Color} → (rot repl : bt (Color ∧ A) ) |
693 → (ri : rotatedTree (RB→bt A orig) (RB→bt A rot) ) → replacedTree key value (RB→bt A rot) (RB→bt A repl) → t ) → t | |
694 replaceRBP {n} {m} {A} {t} key value {_} {key1} orig tree stack si next exit = ? where | |
695 insertCase2 : {k0 k1 d0 d1 d2 : ℕ} {c0 c1 c2 : Color} → (tree parent grand : bt (Color ∧ A)) | |
696 → (stack : List (bt (Color ∧ A))) → (tr to : bt (Color ∧ A)) → (si : stackInvariant key tr to stack ) | |
697 → (pg : ParentGrand tree parent grand ) → t | |
698 insertCase2 tree parent grand stack tr to = ? | |
699 insertCase1 : (stack : List (bt (Color ∧ A))) → (to tr : bt (Color ∧ A)) → (si : stackInvariant key tr to stack ) → t | |
700 insertCase1 stack to tr si with stackToPG tr to stack si | |
743 | 701 ... | case1 eq = ? |
702 ... | case2 (case1 eq ) = ? | |
747 | 703 ... | case2 (case2 pg) = insertCase2 ? ? ? ? ? ? ? (PG.pg pg) where |
744 | 704 si00 : stackInvariant key ? ? ? |
705 si00 = ? | |
724 | 706 |
740 | 707 |