Mercurial > hg > Members > Moririn
annotate hoareBinaryTree.agda @ 712:64a86fde1f90
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 05 May 2022 19:23:26 +0900 |
parents | c588b77bc197 |
children | a36147bb596d |
rev | line source |
---|---|
586
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
1 module hoareBinaryTree where |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
2 |
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
3 open import Level renaming (zero to Z ; suc to succ) |
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 |
588 | 21 _iso_ : {n : Level} {a : Set n} → ℕ → ℕ → Set |
22 d iso d' = (¬ (suc d ≤ d')) ∧ (¬ (suc d' ≤ d)) | |
23 | |
24 iso-intro : {n : Level} {a : Set n} {x y : ℕ} → ¬ (suc x ≤ y) → ¬ (suc y ≤ x) → _iso_ {n} {a} x y | |
25 iso-intro = λ z z₁ → record { proj1 = z ; proj2 = z₁ } | |
26 | |
590 | 27 -- |
28 -- | |
29 -- no children , having left node , having right node , having both | |
30 -- | |
597 | 31 data bt {n : Level} (A : Set n) : Set n where |
604 | 32 leaf : bt A |
33 node : (key : ℕ) → (value : A) → | |
610 | 34 (left : bt A ) → (right : bt A ) → bt A |
600 | 35 |
620 | 36 node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ |
37 node-key (node key _ _ _) = just key | |
38 node-key _ = nothing | |
39 | |
40 node-value : {n : Level} {A : Set n} → bt A → Maybe A | |
41 node-value (node _ value _ _) = just value | |
42 node-value _ = nothing | |
43 | |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
44 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
|
45 bt-depth leaf = 0 |
618 | 46 bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ )) |
606 | 47 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
48 find : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → List (bt A) |
604 | 49 → (next : bt A → List (bt A) → t ) → (exit : bt A → List (bt A) → t ) → t |
50 find key leaf st _ exit = exit leaf st | |
632 | 51 find key (node key₁ v1 tree tree₁) st next exit with <-cmp key key₁ |
604 | 52 find key n st _ exit | tri≈ ¬a b ¬c = exit n st |
632 | 53 find key n@(node key₁ v1 tree tree₁) st next _ | tri< a ¬b ¬c = next tree (n ∷ st) |
54 find key n@(node key₁ v1 tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (n ∷ st) | |
597 | 55 |
604 | 56 {-# TERMINATING #-} |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
57 find-loop : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → bt A → List (bt A) → (exit : bt A → List (bt A) → t) → t |
611 | 58 find-loop {n} {m} {A} {t} key tree st exit = find-loop1 tree st where |
604 | 59 find-loop1 : bt A → List (bt A) → t |
60 find-loop1 tree st = find key tree st find-loop1 exit | |
600 | 61 |
611 | 62 replaceNode : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → (bt A → t) → t |
632 | 63 replaceNode k v1 leaf next = next (node k v1 leaf leaf) |
64 replaceNode k v1 (node key value t t₁) next = next (node k v1 t t₁) | |
611 | 65 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
66 replace : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → List (bt A) → (next : ℕ → A → bt A → List (bt A) → t ) → (exit : bt A → t) → t |
669 | 67 replace key value repl [] next exit = exit repl -- can't happen |
690 | 68 replace key value repl (leaf ∷ []) next exit = exit repl |
669 | 69 replace key value repl (node key₁ value₁ left right ∷ []) next exit with <-cmp key key₁ |
70 ... | tri< a ¬b ¬c = exit (node key₁ value₁ repl right ) | |
664 | 71 ... | tri≈ ¬a b ¬c = exit (node key₁ value left right ) |
669 | 72 ... | tri> ¬a ¬b c = exit (node key₁ value₁ left repl ) |
690 | 73 replace key value repl (leaf ∷ st) next exit = next key value repl st |
669 | 74 replace key value repl (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁ |
75 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st | |
604 | 76 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st |
669 | 77 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st |
586
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
78 |
604 | 79 {-# TERMINATING #-} |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
80 replace-loop : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → List (bt A) → (exit : bt A → t) → t |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
81 replace-loop {_} {_} {A} {t} key value tree st exit = replace-loop1 key value tree st where |
604 | 82 replace-loop1 : (key : ℕ) → (value : A) → bt A → List (bt A) → t |
83 replace-loop1 key value tree st = replace key value tree st replace-loop1 exit | |
586
0ddfa505d612
isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff
changeset
|
84 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
85 insertTree : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → (next : bt A → t ) → t |
662 | 86 insertTree tree key value exit = find-loop key tree ( tree ∷ [] ) $ λ t st → replaceNode key value t $ λ t1 → replace-loop key value t1 st exit |
587 | 87 |
604 | 88 insertTest1 = insertTree leaf 1 1 (λ x → x ) |
611 | 89 insertTest2 = insertTree insertTest1 2 1 (λ x → x ) |
669 | 90 insertTest3 = insertTree insertTest2 3 2 (λ x → x ) |
696 | 91 insertTest4 = insertTree insertTest3 2 2 (λ x → x ) -- this is wrong |
587 | 92 |
710 | 93 updateTree : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → (empty : bt A → t ) → (next : A → bt A → t ) → t |
94 updateTree {_} {_} {A} {t} tree key value empty next = find-loop key tree ( tree ∷ [] ) | |
95 $ λ t st → replaceNode key value t $ λ t1 → replace-loop key value t1 st (found? st) where | |
96 found? : List (bt A) → bt A → t | |
97 found? [] tree = empty tree -- can't happen | |
98 found? (leaf ∷ st) tree = empty tree | |
99 found? (node key value x x₁ ∷ st) tree = next value tree | |
100 | |
605 | 101 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_) |
102 | |
620 | 103 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where |
104 t-leaf : treeInvariant leaf | |
632 | 105 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf) |
106 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂) | |
107 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂)) | |
692 | 108 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key value t₁ t₂) |
632 | 109 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf ) |
620 | 110 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂) |
111 → treeInvariant (node key value t₁ t₂) | |
112 → treeInvariant (node key₂ value₂ t₃ t₄) | |
113 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) | |
605 | 114 |
662 | 115 -- |
116 -- stack always contains original top at end | |
117 -- | |
118 data stackInvariant {n : Level} {A : Set n} (key : ℕ) : (top orig : bt A) → (stack : List (bt A)) → Set n where | |
675 | 119 s-single : {tree0 : bt A} → stackInvariant key tree0 tree0 (tree0 ∷ []) |
653 | 120 s-right : {tree tree0 tree₁ : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} |
662 | 121 → key₁ < key → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree tree0 (tree ∷ st) |
653 | 122 s-left : {tree₁ tree0 tree : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} |
662 | 123 → key < key₁ → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree₁ tree0 (tree₁ ∷ st) |
639 | 124 |
677 | 125 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt A ) → Set n where |
639 | 126 r-leaf : replacedTree key value leaf (node key value leaf leaf) |
127 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁) | |
128 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} | |
677 | 129 → k < key → replacedTree key value t2 t → replacedTree key value (node k v1 t1 t2) (node k v1 t1 t) |
639 | 130 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} |
687 | 131 → key < k → replacedTree key value t1 t → replacedTree key value (node k v1 t1 t2) (node k v1 t t2) |
652 | 132 |
632 | 133 add< : { i : ℕ } (j : ℕ ) → i < suc i + j |
134 add< {i} j = begin | |
135 suc i ≤⟨ m≤m+n (suc i) j ⟩ | |
136 suc i + j ∎ where open ≤-Reasoning | |
137 | |
138 treeTest1 : bt ℕ | |
692 | 139 treeTest1 = node 0 0 leaf (node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf)) |
632 | 140 treeTest2 : bt ℕ |
692 | 141 treeTest2 = node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf) |
632 | 142 |
143 treeInvariantTest1 : treeInvariant treeTest1 | |
692 | 144 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 | 145 |
639 | 146 stack-top : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A) |
147 stack-top [] = nothing | |
148 stack-top (x ∷ s) = just x | |
606 | 149 |
639 | 150 stack-last : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A) |
151 stack-last [] = nothing | |
152 stack-last (x ∷ []) = just x | |
153 stack-last (x ∷ s) = stack-last s | |
632 | 154 |
662 | 155 stackInvariantTest1 : stackInvariant 4 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] ) |
692 | 156 stackInvariantTest1 = s-right (add< 3) (s-single ) |
662 | 157 |
666 | 158 si-property0 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 stack → ¬ ( stack ≡ [] ) |
675 | 159 si-property0 (s-single ) () |
666 | 160 si-property0 (s-right x si) () |
161 si-property0 (s-left x si) () | |
665 | 162 |
666 | 163 si-property1 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 tree1 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 (tree1 ∷ stack) |
164 → tree1 ≡ tree | |
675 | 165 si-property1 (s-single ) = refl |
666 | 166 si-property1 (s-right _ si) = refl |
167 si-property1 (s-left _ si) = refl | |
662 | 168 |
663
cf5095488bbd
stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
662
diff
changeset
|
169 si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → stackInvariant key tree tree0 stack |
662 | 170 → stack-last stack ≡ just tree0 |
675 | 171 si-property-last key t t0 (t ∷ []) (s-single ) = refl |
666 | 172 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
|
173 ... | refl = si-property-last key x t0 (x ∷ st) si |
666 | 174 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
|
175 ... | refl = si-property-last key x t0 (x ∷ st) si |
656 | 176 |
642 | 177 ti-right : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 tree₁ repl) → treeInvariant repl |
178 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf | |
179 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-right x ti) = ti | |
180 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-left x ti) = t-leaf | |
181 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti₁ | |
182 | |
183 ti-left : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 repl tree₁ ) → treeInvariant repl | |
184 ti-left {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf | |
185 ti-left {_} {_} {_} {_} {key₁} {v1} (t-right x ti) = t-leaf | |
186 ti-left {_} {_} {_} {_} {key₁} {v1} (t-left x ti) = ti | |
187 ti-left {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti | |
188 | |
662 | 189 stackTreeInvariant : {n : Level} {A : Set n} (key : ℕ) (sub tree : bt A) → (stack : List (bt A)) |
190 → treeInvariant tree → stackInvariant key sub tree stack → treeInvariant sub | |
675 | 191 stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-single ) = ti |
662 | 192 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si ) = ti-right (si1 si) where |
193 si1 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 tree₁ sub ) tree st → treeInvariant (node key₁ v1 tree₁ sub ) | |
194 si1 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 tree₁ sub ) tree st ti si | |
195 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si ) = ti-left ( si2 si) where | |
196 si2 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 sub tree₁ ) tree st → treeInvariant (node key₁ v1 sub tree₁ ) | |
197 si2 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 sub tree₁ ) tree st ti si | |
198 | |
639 | 199 rt-property1 : {n : Level} {A : Set n} (key : ℕ) (value : A) (tree tree1 : bt A ) → replacedTree key value tree tree1 → ¬ ( tree1 ≡ leaf ) |
200 rt-property1 {n} {A} key value .leaf .(node key value leaf leaf) r-leaf () | |
201 rt-property1 {n} {A} key value .(node key _ _ _) .(node key value _ _) r-node () | |
677 | 202 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-right x rt) = λ () |
203 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-left x rt) = λ () | |
639 | 204 |
690 | 205 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 |
206 rt-property-leaf r-leaf = refl | |
207 | |
698 | 208 rt-property-¬leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {tree : bt A} → ¬ replacedTree key value tree leaf |
209 rt-property-¬leaf () | |
210 | |
692 | 211 rt-property-key : {n : Level} {A : Set n} {key key₂ key₃ : ℕ} {value value₂ value₃ : A} {left left₁ right₂ right₃ : bt A} |
212 → replacedTree key value (node key₂ value₂ left right₂) (node key₃ value₃ left₁ right₃) → key₂ ≡ key₃ | |
213 rt-property-key r-node = refl | |
214 rt-property-key (r-right x ri) = refl | |
215 rt-property-key (r-left x ri) = refl | |
216 | |
698 | 217 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥ |
218 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x | |
219 nat-<> : { x y : ℕ } → x < y → y < x → ⊥ | |
220 nat-<> (s≤s x<y) (s≤s y<x) = nat-<> x<y y<x | |
221 | |
222 open _∧_ | |
223 | |
224 | |
632 | 225 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j ) |
226 depth-1< {i} {j} = s≤s (m≤m⊔n _ j) | |
227 | |
228 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i ) | |
650 | 229 depth-2< {i} {j} = s≤s (m≤n⊔m j i) |
611 | 230 |
649 | 231 depth-3< : {i : ℕ } → suc i ≤ suc (suc i) |
232 depth-3< {zero} = s≤s ( z≤n ) | |
233 depth-3< {suc i} = s≤s (depth-3< {i} ) | |
234 | |
235 | |
634 | 236 treeLeftDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) |
237 → treeInvariant (node k v1 tree tree₁) | |
238 → treeInvariant tree | |
239 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf | |
240 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf | |
241 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti | |
242 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti | |
243 | |
244 treeRightDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) | |
245 → treeInvariant (node k v1 tree tree₁) | |
246 → treeInvariant tree₁ | |
247 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf | |
248 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti | |
249 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf | |
250 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti₁ | |
251 | |
704 | 252 |
705 | 253 record FindCond {n : Level} {A : Set n} (C : ℕ → bt A → Set n) : Set (Level.suc n) where |
704 | 254 field |
705 | 255 c1 : {key key₁ : ℕ} {v1 : A } { tree tree₁ : bt A } → C key (node key₁ v1 tree tree₁) → key < key₁ → C key tree |
256 c2 : {key key₁ : ℕ} {v1 : A } { tree tree₁ : bt A } → C key (node key₁ v1 tree tree₁) → key > key₁ → C key tree₁ | |
704 | 257 |
258 | |
259 findP0 : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A)) | |
705 | 260 → {C : ℕ → bt A → Set n} → C key tree → FindCond C |
704 | 261 → (next : (tree1 : bt A) → (stack : List (bt A)) → C key tree1 → bt-depth tree1 < bt-depth tree → t ) |
262 → (exit : (tree1 : bt A) → (stack : List (bt A)) → C key tree1 | |
263 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t | |
705 | 264 findP0 key leaf st Pre _ _ exit = exit leaf st Pre (case1 refl) |
265 findP0 key (node key₁ v1 tree tree₁) st Pre _ next exit with <-cmp key key₁ | |
266 findP0 key n st Pre e _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) | |
267 findP0 {n} {_} {A} key (node key₁ v1 tree tree₁) st Pre e next _ | tri< a ¬b ¬c = next tree (tree ∷ st) (FindCond.c1 e Pre a) depth-1< | |
268 findP0 key n@(node key₁ v1 tree tree₁) st Pre e next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) (FindCond.c2 e Pre c) depth-2< | |
704 | 269 |
615 | 270 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A)) |
662 | 271 → treeInvariant tree ∧ stackInvariant key tree tree0 stack |
693 | 272 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t ) |
273 → (exit : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
638 | 274 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
693 | 275 findP key leaf tree0 st Pre _ exit = exit leaf st Pre (case1 refl) |
632 | 276 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁ |
693 | 277 findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) |
278 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
|
279 ⟪ 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
|
280 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) |
664 | 281 findP1 a (x ∷ st) si = s-left a si |
693 | 282 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 | 283 |
638 | 284 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₁) |
285 replaceTree1 k v1 value (t-single .k .v1) = t-single k value | |
286 replaceTree1 k v1 value (t-right x t) = t-right x t | |
287 replaceTree1 k v1 value (t-left x t) = t-left x t | |
288 replaceTree1 k v1 value (t-node x x₁ t t₁) = t-node x x₁ t t₁ | |
289 | |
649 | 290 open import Relation.Binary.Definitions |
291 | |
292 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥ | |
293 lemma3 refl () | |
294 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥ | |
295 lemma5 (s≤s z≤n) () | |
700 | 296 ¬x<x : {x : ℕ} → ¬ (x < x) |
297 ¬x<x (s≤s lt) = ¬x<x lt | |
649 | 298 |
687 | 299 child-replaced : {n : Level} {A : Set n} (key : ℕ) (tree : bt A) → bt A |
300 child-replaced key leaf = leaf | |
301 child-replaced key (node key₁ value left right) with <-cmp key key₁ | |
302 ... | tri< a ¬b ¬c = left | |
303 ... | tri≈ ¬a b ¬c = node key₁ value left right | |
304 ... | tri> ¬a ¬b c = right | |
677 | 305 |
671
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
306 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
|
307 field |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
308 tree0 : bt A |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
309 ti : treeInvariant tree0 |
b5fde9727830
use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
670
diff
changeset
|
310 si : stackInvariant key tree tree0 stack |
687 | 311 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
|
312 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
|
313 |
638 | 314 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) |
315 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key ) | |
694 | 316 → (treeInvariant tree ) → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value (child-replaced key tree) tree1 → t) → t |
317 replaceNodeP k v1 leaf C P next = next (node k v1 leaf leaf) (t-single k v1 ) r-leaf | |
318 replaceNodeP k v1 (node .k value t t₁) (case2 refl) P next = next (node k v1 t t₁) (replaceTree1 k value v1 P) | |
695 | 319 (subst (λ j → replacedTree k v1 j (node k v1 t t₁) ) repl00 r-node) where |
694 | 320 repl00 : node k value t t₁ ≡ child-replaced k (node k value t t₁) |
321 repl00 with <-cmp k k | |
322 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl) | |
323 ... | tri≈ ¬a b ¬c = refl | |
324 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl) | |
606 | 325 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
326 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
|
327 → (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
|
328 → (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
|
329 → (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
|
330 → replacePR key value tree1 repl stack1 (λ _ _ _ → Lift n ⊤) → length stack1 < length stack → t) |
613 | 331 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t |
675 | 332 replaceP key value {tree} repl [] Pre next exit = ⊥-elim ( si-property0 (replacePR.si Pre) refl ) -- can't happen |
333 replaceP key value {tree} repl (leaf ∷ []) Pre next exit with si-property-last _ _ _ _ (replacePR.si Pre)-- tree0 ≡ leaf | |
677 | 334 ... | refl = exit (replacePR.tree0 Pre) (node key value leaf leaf) ⟪ replacePR.ti Pre , r-leaf ⟫ |
689 | 335 replaceP key value {tree} repl (node key₁ value₁ left right ∷ []) Pre next exit with <-cmp key key₁ |
336 ... | tri< a ¬b ¬c = exit (replacePR.tree0 Pre) (node key₁ value₁ repl right ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
337 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right ) | |
338 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
339 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ k right ) (node key₁ value₁ repl right )) repl02 (r-left a repl03) where | |
340 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
341 repl03 = replacePR.ri Pre | |
342 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left | |
343 repl02 with <-cmp key key₁ | |
344 ... | tri< a ¬b ¬c = refl | |
345 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a) | |
346 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a) | |
347 ... | tri≈ ¬a b ¬c = exit (replacePR.tree0 Pre) repl ⟪ replacePR.ti Pre , repl01 ⟫ where | |
678 | 348 repl01 : replacedTree key value (replacePR.tree0 Pre) repl |
349 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
689 | 350 repl01 | refl | refl = subst (λ k → replacedTree key value k repl) repl02 (replacePR.ri Pre) where |
351 repl02 : child-replaced key (node key₁ value₁ left right) ≡ node key₁ value₁ left right | |
352 repl02 with <-cmp key key₁ | |
353 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b) | |
354 ... | tri≈ ¬a b ¬c = refl | |
355 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b) | |
356 ... | tri> ¬a ¬b c = exit (replacePR.tree0 Pre) (node key₁ value₁ left repl ) ⟪ replacePR.ti Pre , repl01 ⟫ where | |
357 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl ) | |
358 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre) | |
359 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ left k ) (node key₁ value₁ left repl )) repl02 (r-right c repl03) where | |
360 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl | |
361 repl03 = replacePR.ri Pre | |
362 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right | |
363 repl02 with <-cmp key key₁ | |
364 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c) | |
365 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c) | |
366 ... | tri> ¬a ¬b c = refl | |
690 | 367 replaceP {n} {_} {A} key value {tree} repl (leaf ∷ st@(tree1 ∷ st1)) Pre next exit = next key value repl st Post ≤-refl where |
368 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) | |
369 Post with replacePR.si Pre | |
370 ... | s-right {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
371 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf | |
372 repl09 = si-property1 si | |
373 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
374 repl10 with si-property1 si | |
375 ... | refl = si | |
376 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf | |
377 repl07 with <-cmp key key₂ | |
378 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) | |
379 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
380 ... | tri> ¬a ¬b c = refl | |
381 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
382 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 | |
383 ... | s-left {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
384 repl09 : tree1 ≡ node key₂ v1 leaf tree₁ | |
385 repl09 = si-property1 si | |
386 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
387 repl10 with si-property1 si | |
388 ... | refl = si | |
389 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf | |
390 repl07 with <-cmp key key₂ | |
391 ... | tri< a ¬b ¬c = refl | |
392 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
393 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
394 repl12 : replacedTree key value (child-replaced key tree1 ) repl | |
395 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 | 396 replaceP {n} {_} {A} key value {tree} repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre next exit with <-cmp key key₁ |
397 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st Post ≤-refl where | |
675 | 398 Post : replacePR key value tree1 (node key₁ value₁ repl right ) st (λ _ _ _ → Lift n ⊤) |
687 | 399 Post with replacePR.si Pre |
688 | 400 ... | s-right {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where |
401 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) | |
402 repl09 = si-property1 si | |
403 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
404 repl10 with si-property1 si | |
405 ... | refl = si | |
406 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left | |
407 repl03 with <-cmp key key₁ | |
408 ... | tri< a1 ¬b ¬c = refl | |
409 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
410 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
411 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right | |
412 repl02 with repl09 | <-cmp key key₂ | |
413 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
689 | 414 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) |
688 | 415 ... | refl | tri> ¬a ¬b c = refl |
416 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
417 repl04 = begin | |
418 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
419 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
420 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
421 child-replaced key tree1 ∎ where open ≡-Reasoning | |
422 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
423 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) | |
687 | 424 ... | 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 | 425 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ |
683 | 426 repl09 = si-property1 si |
427 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
428 repl10 with si-property1 si | |
429 ... | refl = si | |
687 | 430 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left |
431 repl03 with <-cmp key key₁ | |
432 ... | tri< a1 ¬b ¬c = refl | |
433 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a) | |
434 ... | tri> ¬a ¬b c = ⊥-elim (¬a a) | |
435 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right | |
436 repl02 with repl09 | <-cmp key key₂ | |
437 ... | refl | tri< a ¬b ¬c = refl | |
438 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
439 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
440 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1 | |
441 repl04 = begin | |
442 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩ | |
443 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
444 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
445 child-replaced key tree1 ∎ where open ≡-Reasoning | |
446 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right) | |
447 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre)) | |
705 | 448 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st Post ≤-refl where |
690 | 449 Post : replacePR key value tree1 (node key₁ value left right ) (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤) |
450 Post with replacePR.si Pre | |
691 | 451 ... | 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 | 452 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right) |
453 repl09 = si-property1 si | |
454 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
455 repl10 with si-property1 si | |
456 ... | refl = si | |
457 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree | |
458 repl07 with <-cmp key key₂ | |
459 ... | tri< a ¬b ¬c = ⊥-elim (¬c x) | |
460 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x) | |
461 ... | tri> ¬a ¬b c = refl | |
691 | 462 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
463 repl12 refl with repl09 | |
464 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node | |
465 ... | 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 | 466 repl09 : tree1 ≡ node key₂ v1 tree tree₁ |
467 repl09 = si-property1 si | |
468 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
469 repl10 with si-property1 si | |
470 ... | refl = si | |
471 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree | |
472 repl07 with <-cmp key key₂ | |
473 ... | tri< a ¬b ¬c = refl | |
474 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x) | |
475 ... | tri> ¬a ¬b c = ⊥-elim (¬a x) | |
691 | 476 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right ) |
477 repl12 refl with repl09 | |
478 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node | |
690 | 479 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st Post ≤-refl where |
480 Post : replacePR key value tree1 (node key₁ value₁ left repl ) st (λ _ _ _ → Lift n ⊤) | |
481 Post with replacePR.si Pre | |
482 ... | s-right {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
483 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right) | |
484 repl09 = si-property1 si | |
485 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
486 repl10 with si-property1 si | |
487 ... | refl = si | |
488 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
489 repl03 with <-cmp key key₁ | |
490 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) | |
491 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
492 ... | tri> ¬a ¬b c = refl | |
493 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right | |
494 repl02 with repl09 | <-cmp key key₂ | |
495 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt) | |
496 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt) | |
497 ... | refl | tri> ¬a ¬b c = refl | |
498 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
499 repl04 = begin | |
500 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
501 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
502 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
503 child-replaced key tree1 ∎ where open ≡-Reasoning | |
504 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
505 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) | |
506 ... | s-left {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where | |
507 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁ | |
508 repl09 = si-property1 si | |
509 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1) | |
510 repl10 with si-property1 si | |
511 ... | refl = si | |
512 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right | |
513 repl03 with <-cmp key key₁ | |
514 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c) | |
515 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c) | |
516 ... | tri> ¬a ¬b c = refl | |
517 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right | |
518 repl02 with repl09 | <-cmp key key₂ | |
519 ... | refl | tri< a ¬b ¬c = refl | |
520 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt) | |
521 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt) | |
522 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1 | |
523 repl04 = begin | |
524 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩ | |
525 node key₁ value₁ left right ≡⟨ sym repl02 ⟩ | |
526 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩ | |
527 child-replaced key tree1 ∎ where open ≡-Reasoning | |
528 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl) | |
529 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre)) | |
644 | 530 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
531 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
|
532 → (r : Index) → (p : Invraiant r) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
533 → (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
|
534 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
|
535 ... | 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
|
536 ... | 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
|
537 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
|
538 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
|
539 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
|
540 ... | 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
|
541 ... | 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
|
542 ... | 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
|
543 |
707 | 544 record LoopTermination {n : Level} {Index : Set n } { reduce : Index → ℕ } |
545 (r : Index ) (C : Set n) : Set (Level.suc n) where | |
546 field | |
547 rd : (r1 : Index) → reduce r1 < reduce r | |
548 ci : C -- data continuation | |
549 | |
710 | 550 -- TerminatingLoopC : {l n : Level} {t : Set l} (Index : Set n ) → {C : Set n } → ( reduce : Index → ℕ) |
551 -- → (r : Index) → (P : LoopTermination r C ) | |
552 -- → (loop : (r : Index) → LoopTermination {_} {_} {reduce} r C → (next : (r1 : Index) → LoopTermination r1 C → t ) → t) → t | |
553 -- TerminatingLoopC {_} {_} {t} Index {C} reduce r P loop with <-cmp 0 (reduce r) | |
554 -- ... | tri≈ ¬a b ¬c = loop r P (λ r1 p1 → ⊥-elim (lemma3 b (LoopTermination.rd P r1))) | |
555 -- ... | tri< a ¬b ¬c = loop r P (λ r1 p1 → TerminatingLoop1 (reduce r) r r1 (≤-step (LoopTermination.rd P r1)) p1 (LoopTermination.rd P r1)) where | |
556 -- TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → {!!} → reduce r1 < reduce r → t | |
557 -- TerminatingLoop1 zero r r1 n≤j p1 lt = loop r1 {!!} (λ r2 P1 → ⊥-elim (lemma5 n≤j (LoopTermination.rd P1 r2))) | |
558 -- TerminatingLoop1 (suc j) r r1 n≤j p1 lt with <-cmp (reduce r1) (suc j) | |
559 -- ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a p1 lt | |
560 -- ... | tri≈ ¬a b ¬c = loop r1 {!!} (λ r2 p2 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b {!!} ) p2 {!!} ) | |
561 -- ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c n≤j ) | |
562 -- | |
563 -- record ReplCond {n : Level} {A : Set n} (C : ℕ → bt A → List (bt A) → Set n) : Set (Level.suc n) where | |
564 -- field | |
565 -- c1 : (key : ℕ) → (repl : bt A) → (stack : List (bt A)) → C key repl stack | |
566 -- | |
567 -- replaceP0 : {n m : Level} {A : Set n} {t : Set m} | |
568 -- → (key : ℕ) → (value : A) → ( repl : bt A) | |
569 -- → (stack : List (bt A)) | |
570 -- → {C : ℕ → (repl : bt A ) → List (bt A) → Set n} → C key repl stack → ReplCond C | |
571 -- → (next : ℕ → A → (repl : bt A) → (stack1 : List (bt A)) | |
572 -- → C key repl stack → length stack1 < length stack → t) | |
573 -- → (exit : (repl : bt A) → C key repl [] → t) → t | |
574 -- replaceP0 key value repl [] Pre _ next exit = exit repl {!!} | |
575 -- replaceP0 key value repl (leaf ∷ []) Pre _ next exit = exit (node key value leaf leaf) {!!} | |
576 -- replaceP0 key value repl (node key₁ value₁ left right ∷ []) Pre e next exit with <-cmp key key₁ | |
577 -- ... | tri< a ¬b ¬c = exit (node key₁ value₁ repl right ) {!!} | |
578 -- ... | tri≈ ¬a b ¬c = exit repl {!!} | |
579 -- ... | tri> ¬a ¬b c = exit (node key₁ value₁ left repl ) {!!} | |
580 -- replaceP0 {n} {_} {A} key value repl (leaf ∷ st@(tree1 ∷ st1)) Pre e next exit = next key value repl st {!!} ≤-refl | |
581 -- replaceP0 {n} {_} {A} key value repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre e next exit with <-cmp key key₁ | |
582 -- ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st {!!} ≤-refl | |
583 -- ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st {!!} ≤-refl | |
584 -- ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st {!!} ≤-refl | |
585 -- | |
586 -- | |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
587 open _∧_ |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
588 |
615 | 589 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
590 → replacedTree key value tree repl → treeInvariant repl | |
692 | 591 RTtoTI0 .leaf .(node key value leaf leaf) key value ti r-leaf = t-single key value |
592 RTtoTI0 .(node key _ leaf leaf) .(node key value leaf leaf) key value (t-single .key _) r-node = t-single key value | |
593 RTtoTI0 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti | |
594 RTtoTI0 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
595 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 | 596 -- r-right case |
692 | 597 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) |
598 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 | 599 RTtoTI0 (node key₁ _ leaf right@(node key₂ _ _ _)) (node key₁ value₁ leaf right₁@(node key₃ _ _ _)) key value (t-right x₁ ti) (r-right x ri) = |
600 t-right (subst (λ k → key₁ < k ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) | |
692 | 601 RTtoTI0 (node key₁ _ (node _ _ _ _) leaf) (node key₁ _ (node key₃ value left right) leaf) key value₁ (t-left x₁ ti) (r-right x ()) |
602 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) = | |
603 t-node x₁ x ti (t-single key value) | |
693 | 604 RTtoTI0 (node key₁ _ (node _ _ _ _) (node key₂ _ _ _)) (node key₁ _ (node _ _ _ _) (node key₃ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) = |
605 t-node x₁ (subst (λ k → key₁ < k) (rt-property-key ri) x₂) ti (RTtoTI0 _ _ key value ti₁ ri) | |
701 | 606 -- r-left case |
700 | 607 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 | 608 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 |
609 RTtoTI0 (node key₃ _ (node key₂ _ _ _) leaf) (node key₃ _ (node key₁ value₁ left left₁) leaf) key value (t-left x₁ ti) (r-left x ri) = | |
610 t-left (subst (λ k → k < key₃ ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) -- key₁ < key₃ | |
611 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 | 612 |
613 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl | |
614 → replacedTree key value tree repl → treeInvariant tree | |
701 | 615 RTtoTI1 .leaf .(node key value leaf leaf) key value ti r-leaf = t-leaf |
616 RTtoTI1 (node key value₁ leaf leaf) .(node key value leaf leaf) key value (t-single .key .value) r-node = t-single key value₁ | |
617 RTtoTI1 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti | |
618 RTtoTI1 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti | |
619 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₁ | |
620 -- r-right case | |
621 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₁ | |
622 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) = | |
623 t-right (subst (λ k → key₁ < k ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₁ < key₂ | |
624 RTtoTI1 (node _ _ (node _ _ _ _) leaf) (node _ _ (node _ _ _ _) (node key value _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x r-leaf) = | |
625 t-left x₁ ti | |
626 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₁ | |
627 -- r-left case | |
628 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ _ leaf) key value (t-left x₁ ti) (r-left x ri) = t-single key₁ value₁ | |
629 RTtoTI1 (node key₁ _ (node key₂ value₁ n n₁) leaf) (node key₁ _ (node key₃ _ _ _) leaf) key value (t-left x₁ ti) (r-left x ri) = | |
630 t-left (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₂ < key₁ | |
631 RTtoTI1 (node key₁ value₁ leaf _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x r-leaf) = t-right x₂ ti₁ | |
632 RTtoTI1 (node key₁ value₁ (node key₂ value₂ n n₁) _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) = | |
633 t-node (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) x₂ (RTtoTI1 _ _ key value ti ri) ti₁ -- key₂ < key₁ | |
614 | 634 |
611 | 635 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
696 | 636 → (exit : (tree repl : bt A) → treeInvariant repl ∧ replacedTree key value tree repl → t ) → t |
693 | 637 insertTreeP {n} {m} {A} {t} tree key value P0 exit = |
638 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-single ⟫ | |
696 | 639 $ λ p P loop → findP key (proj1 p) tree (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
693 | 640 $ λ t s P C → replaceNodeP key value t C (proj1 P) |
641 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A ) | |
642 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) } | |
696 | 643 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = tree ; ti = P0 ; si = proj2 P ; ri = R ; ci = lift tt } |
693 | 644 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1 |
696 | 645 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt ) |
646 $ λ tree repl P → exit tree repl ⟪ RTtoTI0 _ _ _ _ (proj1 P) (proj2 P) , proj2 P ⟫ | |
614 | 647 |
696 | 648 insertTestP1 = insertTreeP leaf 1 1 t-leaf |
649 $ λ _ x P → insertTreeP x 2 1 (proj1 P) | |
650 $ λ _ x P → insertTreeP x 3 2 (proj1 P) | |
651 $ λ _ x P → insertTreeP x 2 2 (proj1 P) (λ _ x _ → x ) | |
694 | 652 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
653 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
|
654 top-value leaf = nothing |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
655 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
|
656 |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
657 record findPR {n : Level} {A : Set n} (key : ℕ) (tree : bt A ) (stack : List (bt A)) (C : ℕ → bt A → List (bt A) → Set n) : Set n where |
618 | 658 field |
619 | 659 tree0 : bt A |
696 | 660 ti0 : treeInvariant tree0 |
695 | 661 ti : treeInvariant tree |
662 | 662 si : stackInvariant key tree tree0 stack |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
663 ci : C key tree stack -- data continuation |
702 | 664 |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
665 record findExt {n : Level} {A : Set n} (key : ℕ) (C : ℕ → bt A → List (bt A) → Set n) : Set (Level.suc n) where |
702 | 666 field |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
667 c1 : {key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A} |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
668 → findPR key (node key₁ v1 tree tree₁) st C → key < key₁ → C key tree (tree ∷ st) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
669 c2 : {key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A} |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
670 → findPR key (node key₁ v1 tree tree₁) st C → key > key₁ → C key tree₁ (tree₁ ∷ st) |
618 | 671 |
695 | 672 findPP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A)) |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
673 → {C : ℕ → bt A → List (bt A) → Set n } → findPR key tree stack C → findExt key C |
702 | 674 → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack C → bt-depth tree1 < bt-depth tree → t ) |
675 → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack C | |
695 | 676 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
702 | 677 findPP key leaf st Pre _ _ exit = exit leaf st Pre (case1 refl) |
678 findPP key (node key₁ v1 tree tree₁) st Pre _ next exit with <-cmp key key₁ | |
679 findPP key n st Pre _ _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) | |
680 findPP {n} {_} {A} key (node key₁ v1 tree tree₁) st Pre e next _ | tri< a ¬b ¬c = next tree (tree ∷ st) | |
705 | 681 record { tree0 = findPR.tree0 Pre ; ti0 = findPR.ti0 Pre ; ti = treeLeftDown tree tree₁ (findPR.ti Pre) ; si = findP1 a st (findPR.si Pre) ; ci = findExt.c1 e Pre a } depth-1< where |
695 | 682 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) |
683 (findPR.tree0 Pre) st → stackInvariant key tree (findPR.tree0 Pre) (tree ∷ st) | |
684 findP1 a (x ∷ st) si = s-left a si | |
702 | 685 findPP key n@(node key₁ v1 tree tree₁) st Pre e next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) |
686 record { tree0 = findPR.tree0 Pre ; ti0 = findPR.ti0 Pre ; ti = treeRightDown tree tree₁ (findPR.ti Pre) ; si = s-right c (findPR.si Pre) ; ci = findExt.c2 e Pre c } depth-2< | |
616 | 687 |
618 | 688 insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
689 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t | |
695 | 690 insertTreePP {n} {m} {A} {t} tree key value P0 exit = |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
691 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR key (proj1 p) (proj2 p) (λ _ _ _ → Lift n ⊤) } (λ p → bt-depth (proj1 p)) ⟪ tree , tree ∷ [] ⟫ |
696 | 692 record { tree0 = tree ; ti = P0 ; ti0 = P0 ;si = s-single ; ci = lift tt } |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
693 $ λ p P loop → findPP key (proj1 p) (proj2 p) P record { c1 = λ _ _ → lift tt ; c2 = λ _ _ → lift tt } (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
695 | 694 $ λ t s P C → replaceNodeP key value t C (findPR.ti P) |
695 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A ) | |
696 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) } | |
696 | 697 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = findPR.tree0 P ; ti = findPR.ti0 P ; si = findPR.si P ; ri = R ; ci = lift tt } |
695 | 698 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1 |
699 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt ) exit | |
618 | 700 |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
701 record findPC {n : Level} {A : Set n} (value : A) (key1 : ℕ) (tree : bt A ) (stack : List (bt A)) : Set n where |
616 | 702 field |
703 tree1 : bt A | |
698 | 704 ci : replacedTree key1 value tree1 tree |
616 | 705 |
702 | 706 findPPC1 : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A)) |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
707 → findPR key tree stack (findPC value ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
708 → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC value ) → bt-depth tree1 < bt-depth tree → t ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
709 → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC value ) |
702 | 710 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
711 findPPC1 {n} {_} {A} key value tree stack Pr next exit = findPP key tree stack Pr findext next exit where | |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
712 findext01 : {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A} |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
713 → (Pre : findPR key (node key₂ v1 tree₁ tree₂) st (findPC value) ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
714 → key < key₂ → findPC value key tree₁ (tree₁ ∷ st) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
715 findext01 Pre a with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) | inspect findPC.tree1 (findPR.ci Pre) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
716 ... | r-leaf | leaf | record { eq = refl } = ⊥-elim ( nat-≤> a ≤-refl) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
717 ... | r-node | node key value t1 t3 | record { eq = refl } = ⊥-elim ( nat-≤> a ≤-refl ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
718 ... | r-right x t | t1 | t2 = ⊥-elim (nat-<> x a) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
719 ... | r-left x ri | node key value t1 t3 | record { eq = refl } = record { tree1 = t1 ; ci = ri } |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
720 findext02 : {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A} |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
721 → (Pre : findPR key (node key₂ v1 tree₁ tree₂) st (findPC value) ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
722 → key > key₂ → findPC value key tree₂ (tree₂ ∷ st) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
723 findext02 Pre c with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) | inspect findPC.tree1 (findPR.ci Pre) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
724 ... | r-leaf | leaf | record { eq = refl } = ⊥-elim ( nat-≤> c ≤-refl) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
725 ... | r-node | node key value t1 t3 | record { eq = refl } = ⊥-elim ( nat-≤> c ≤-refl ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
726 ... | r-left x t | t1 | t2 = ⊥-elim (nat-<> x c) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
727 ... | r-right x ri | node key value t1 t3 | record { eq = refl } = record { tree1 = t3 ; ci = ri } |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
728 findext : findExt key (findPC value ) |
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
729 findext = record { c1 = findext01 ; c2 = findext02 } |
702 | 730 |
731 insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤ | |
732 insertTreeSpec0 _ _ _ = tt | |
733 | |
700 | 734 containsTree : {n : Level} {A : Set n} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree → ⊤ |
735 containsTree {n} {A} tree tree1 key value P RT = | |
617 | 736 TerminatingLoopS (bt A ∧ List (bt A) ) |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
737 {λ p → findPR key (proj1 p) (proj2 p) (findPC value ) } (λ p → bt-depth (proj1 p)) |
698 | 738 ⟪ tree , tree ∷ [] ⟫ record { tree0 = tree ; ti0 = RTtoTI0 _ _ _ _ P RT ; ti = RTtoTI0 _ _ _ _ P RT ; si = s-single |
739 ; ci = record { tree1 = tree1 ; ci = RT } } | |
702 | 740 $ λ p P loop → findPPC1 key value (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
695 | 741 $ λ t1 s1 P2 found? → insertTreeSpec0 t1 value (lemma6 t1 s1 found? P2) where |
703
23e0b9df7896
embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
702
diff
changeset
|
742 lemma6 : (t1 : bt A) (s1 : List (bt A)) (found? : (t1 ≡ leaf) ∨ (node-key t1 ≡ just key)) (P2 : findPR key t1 s1 (findPC value )) → top-value t1 ≡ just value |
698 | 743 lemma6 t1 s1 found? P2 = lemma7 t1 s1 (findPR.tree0 P2) ( findPC.tree1 (findPR.ci P2)) (findPC.ci (findPR.ci P2)) (findPR.si P2) found? where |
700 | 744 lemma8 : {tree1 t1 : bt A } → replacedTree key value tree1 t1 → node-key t1 ≡ just key → top-value t1 ≡ just value |
745 lemma8 {.leaf} {node key value .leaf .leaf} r-leaf refl = refl | |
746 lemma8 {.(node key _ t1 t2)} {node key value t1 t2} r-node refl = refl | |
747 lemma8 {.(node key value t1 _)} {node key value t1 t2} (r-right x ri) refl = ⊥-elim (¬x<x x) | |
748 lemma8 {.(node key value _ t2)} {node key value t1 t2} (r-left x ri) refl = ⊥-elim (¬x<x x) | |
698 | 749 lemma7 : (t1 : bt A) ( s1 : List (bt A) ) (tree0 tree1 : bt A) |
750 → replacedTree key value tree1 t1 → stackInvariant key t1 tree0 s1 | |
751 → ( t1 ≡ leaf ) ∨ ( node-key t1 ≡ just key) → top-value t1 ≡ just value | |
752 lemma7 .leaf (.leaf ∷ []) .leaf tree1 () s-single (case1 refl) | |
700 | 753 lemma7 (node key value t1 t2) (.(node key value t1 t2) ∷ []) .(node key value t1 t2) tree1 ri s-single (case2 x) = lemma8 ri x |
754 lemma7 (node key value t1 t2) (.(node key value t1 t2) ∷ x₁ ∷ s1) tree0 tree1 ri (s-right x si) (case2 x₂) = lemma8 ri x₂ | |
755 lemma7 (node key value t1 t2) (.(node key value t1 t2) ∷ x₁ ∷ s1) tree0 tree1 ri (s-left x si) (case2 x₂) = lemma8 ri x₂ | |
756 | |
615 | 757 |
700 | 758 containsTree1 : {n : Level} {A : Set n} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree → ⊤ |
759 containsTree1 {n} {A} tree key value ti = | |
760 insertTreeP tree key value ti | |
702 | 761 $ λ tree0 tree1 P → containsTree tree1 tree0 key value (RTtoTI1 _ _ _ _ (proj1 P) (proj2 P) ) (proj2 P) |
700 | 762 |
763 |