Mercurial > hg > Members > Moririn
annotate hoareBinaryTree.agda @ 655:d0394c191d84
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 21 Nov 2021 10:54:13 +0900 |
parents | 48c6e6961ea5 |
children | 30690aed1819 |
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 |
604 | 67 replace key value tree [] next exit = exit tree |
647 | 68 replace key value tree (leaf ∷ []) next exit = exit (node key value leaf leaf) |
69 replace key value tree (leaf ∷ leaf ∷ st) next exit = exit (node key value leaf leaf) | |
70 replace key value tree (leaf ∷ node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁ | |
71 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ (node key value leaf leaf) right ) st | |
72 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st | |
73 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left (node key value leaf leaf) ) st | |
604 | 74 replace key value tree (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁ |
75 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ tree right ) st | |
76 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st | |
77 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left tree ) 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 |
611 | 86 insertTree tree key value exit = find-loop key 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 ) |
587 | 90 |
605 | 91 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_) |
92 | |
620 | 93 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where |
94 t-leaf : treeInvariant leaf | |
632 | 95 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf) |
96 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂) | |
97 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂)) | |
98 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key₁ < key) → treeInvariant (node key value t₁ t₂) | |
99 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf ) | |
620 | 100 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂) |
101 → treeInvariant (node key value t₁ t₂) | |
102 → treeInvariant (node key₂ value₂ t₃ t₄) | |
103 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) | |
605 | 104 |
652 | 105 data stackInvariant {n : Level} {A : Set n} (key : ℕ) : (top orig : bt A) → (stack : List (bt A)) → Set n where |
655 | 106 s-nil : {tree : bt A} → stackInvariant key tree tree [] |
107 s-single : {tree : bt A} → stackInvariant key tree tree [] → stackInvariant key tree tree (tree ∷ []) | |
653 | 108 s-right : {tree tree0 tree₁ : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} |
655 | 109 → key₁ < key → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → ¬ (st ≡ []) → stackInvariant key tree tree0 (tree ∷ st) |
653 | 110 s-left : {tree₁ tree0 tree : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} |
655 | 111 → key < key₁ → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → ¬ (st ≡ []) → stackInvariant key tree₁ tree0 (tree₁ ∷ st) |
639 | 112 |
113 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (tree tree1 : bt A ) → Set n where | |
114 r-leaf : replacedTree key value leaf (node key value leaf leaf) | |
115 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁) | |
116 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} | |
650 | 117 → k < key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t t1) (node k v1 t t2) |
639 | 118 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A} |
650 | 119 → k > key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t1 t) (node k v1 t2 t) |
639 | 120 |
652 | 121 replFromStack : {n : Level} {A : Set n} {key : ℕ} {top orig : bt A} → {stack : List (bt A)} → stackInvariant key top orig stack → bt A |
655 | 122 replFromStack (s-nil {tree} ) = tree |
123 replFromStack (s-single {tree} _ ) = tree | |
124 replFromStack (s-right {tree} x _ st) = tree | |
125 replFromStack (s-left {tree} x _ st) = tree | |
652 | 126 |
632 | 127 add< : { i : ℕ } (j : ℕ ) → i < suc i + j |
128 add< {i} j = begin | |
129 suc i ≤⟨ m≤m+n (suc i) j ⟩ | |
130 suc i + j ∎ where open ≤-Reasoning | |
131 | |
132 treeTest1 : bt ℕ | |
133 treeTest1 = node 1 0 leaf (node 3 1 (node 2 5 (node 4 7 leaf leaf ) leaf) (node 5 5 leaf leaf)) | |
134 treeTest2 : bt ℕ | |
135 treeTest2 = node 3 1 (node 2 5 (node 4 7 leaf leaf ) leaf) (node 5 5 leaf leaf) | |
136 | |
137 treeInvariantTest1 : treeInvariant treeTest1 | |
138 treeInvariantTest1 = t-right (m≤m+n _ 1) (t-node (add< 0) (add< 1) (t-left (add< 1) (t-single 4 7)) (t-single 5 5) ) | |
605 | 139 |
639 | 140 stack-top : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A) |
141 stack-top [] = nothing | |
142 stack-top (x ∷ s) = just x | |
606 | 143 |
639 | 144 stack-last : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A) |
145 stack-last [] = nothing | |
146 stack-last (x ∷ []) = just x | |
147 stack-last (x ∷ s) = stack-last s | |
632 | 148 |
652 | 149 stackInvariantTest1 : stackInvariant 4 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] ) |
655 | 150 stackInvariantTest1 = s-right (add< 2) (s-single s-nil) (λ ()) |
632 | 151 |
655 | 152 si-property1 : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → ¬ (stack ≡ []) → stackInvariant key tree tree0 stack |
639 | 153 → stack-top stack ≡ just tree |
655 | 154 si-property1 key t t0 [] ne (s-nil ) = ⊥-elim ( ne refl ) |
155 si-property1 key t t0 (t ∷ []) ne (s-single _) = refl | |
156 si-property1 key t t0 (t ∷ st) _ (s-right _ _ si) = refl | |
157 si-property1 key t t0 (t ∷ st) _ (s-left _ _ si) = refl | |
639 | 158 |
655 | 159 si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → ¬ (stack ≡ []) → stackInvariant key tree tree0 stack |
639 | 160 → stack-last stack ≡ just tree0 |
655 | 161 si-property-last key t t0 [] ne s-nil = ⊥-elim ( ne refl ) |
162 si-property-last key t t0 (t ∷ []) _ (s-single _) = refl | |
163 si-property-last key t t0 (t ∷ []) _ (s-right _ _ ne) = ⊥-elim ( ne refl ) | |
164 si-property-last key t t0 (t ∷ []) _ (s-left _ _ ne) = ⊥-elim ( ne refl ) | |
165 si-property-last key t t0 (.t ∷ x ∷ st) ne (s-right _ si _) with si-property1 key _ _ (x ∷ st) (λ ()) si | |
166 ... | refl = si-property-last key x t0 (x ∷ st) (λ ()) si | |
167 si-property-last key t t0 (.t ∷ x ∷ st) ne (s-left _ si _) with si-property1 key _ _ (x ∷ st) (λ ()) si | |
168 ... | refl = si-property-last key x t0 (x ∷ st) (λ ()) si | |
642 | 169 ti-right : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 tree₁ repl) → treeInvariant repl |
170 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf | |
171 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-right x ti) = ti | |
172 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-left x ti) = t-leaf | |
173 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti₁ | |
174 | |
175 ti-left : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 repl tree₁ ) → treeInvariant repl | |
176 ti-left {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf | |
177 ti-left {_} {_} {_} {_} {key₁} {v1} (t-right x ti) = t-leaf | |
178 ti-left {_} {_} {_} {_} {key₁} {v1} (t-left x ti) = ti | |
179 ti-left {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti | |
180 | |
653 | 181 stackTreeInvariant : {n : Level} {A : Set n} (key : ℕ) (sub tree : bt A) → (stack : List (bt A)) |
182 → treeInvariant tree → stackInvariant key sub tree stack → treeInvariant sub | |
655 | 183 stackTreeInvariant {_} {A} key sub tree [] ti s-nil = ti |
184 stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-single _) = ti | |
185 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si _) = ti-right (si1 si) where | |
653 | 186 si1 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 tree₁ sub ) tree st → treeInvariant (node key₁ v1 tree₁ sub ) |
187 si1 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 tree₁ sub ) tree st ti si | |
655 | 188 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si _) = ti-left ( si2 si) where |
653 | 189 si2 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 sub tree₁ ) tree st → treeInvariant (node key₁ v1 sub tree₁ ) |
190 si2 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 sub tree₁ ) tree st ti si | |
640 | 191 |
639 | 192 rt-property1 : {n : Level} {A : Set n} (key : ℕ) (value : A) (tree tree1 : bt A ) → replacedTree key value tree tree1 → ¬ ( tree1 ≡ leaf ) |
193 rt-property1 {n} {A} key value .leaf .(node key value leaf leaf) r-leaf () | |
194 rt-property1 {n} {A} key value .(node key _ _ _) .(node key value _ _) r-node () | |
195 rt-property1 {n} {A} key value .(node _ _ _ _) .(node _ _ _ _) (r-right x rt) () | |
196 rt-property1 {n} {A} key value .(node _ _ _ _) .(node _ _ _ _) (r-left x rt) () | |
197 | |
632 | 198 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j ) |
199 depth-1< {i} {j} = s≤s (m≤m⊔n _ j) | |
200 | |
201 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i ) | |
650 | 202 depth-2< {i} {j} = s≤s (m≤n⊔m j i) |
611 | 203 |
649 | 204 depth-3< : {i : ℕ } → suc i ≤ suc (suc i) |
205 depth-3< {zero} = s≤s ( z≤n ) | |
206 depth-3< {suc i} = s≤s (depth-3< {i} ) | |
207 | |
208 | |
634 | 209 treeLeftDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) |
210 → treeInvariant (node k v1 tree tree₁) | |
211 → treeInvariant tree | |
212 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf | |
213 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf | |
214 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti | |
215 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti | |
216 | |
217 treeRightDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A ) | |
218 → treeInvariant (node k v1 tree tree₁) | |
219 → treeInvariant tree₁ | |
220 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf | |
221 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti | |
222 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf | |
223 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti₁ | |
224 | |
633 | 225 |
226 open _∧_ | |
227 | |
615 | 228 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A)) |
645 | 229 → treeInvariant tree ∧ stackInvariant key tree tree0 stack |
230 → (next : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t ) | |
231 → (exit : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack | |
638 | 232 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t |
233 findP key leaf tree0 st Pre _ exit = exit leaf tree0 st Pre (case1 refl) | |
632 | 234 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁ |
638 | 235 findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n tree0 st Pre (case2 refl) |
655 | 236 findP {_} {_} {A} key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (tree ∷ st) ⟪ treeLeftDown tree tree₁ (proj1 Pre) , findP1 a st (proj2 Pre) ⟫ depth-1< where |
237 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) | |
238 findP1 a [] s-nil = {!!} | |
239 findP1 a (x ∷ st) si = s-left a si (λ ()) | |
240 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (tree₁ ∷ st) ⟪ treeRightDown tree tree₁ (proj1 Pre) , s-right c (proj2 Pre) {!!} ⟫ depth-2< | |
606 | 241 |
638 | 242 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₁) |
243 replaceTree1 k v1 value (t-single .k .v1) = t-single k value | |
244 replaceTree1 k v1 value (t-right x t) = t-right x t | |
245 replaceTree1 k v1 value (t-left x t) = t-left x t | |
246 replaceTree1 k v1 value (t-node x x₁ t t₁) = t-node x x₁ t t₁ | |
247 | |
649 | 248 open import Relation.Binary.Definitions |
249 | |
250 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥ | |
251 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x | |
650 | 252 nat-<> : { x y : ℕ } → x < y → y < x → ⊥ |
253 nat-<> (s≤s x<y) (s≤s y<x) = nat-<> x<y y<x | |
649 | 254 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥ |
255 lemma3 refl () | |
256 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥ | |
257 lemma5 (s≤s z≤n) () | |
258 | |
638 | 259 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) |
260 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key ) | |
261 → (treeInvariant tree ) → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value tree tree1 → t) → t | |
262 replaceNodeP k v1 leaf C P next = next (node k v1 leaf leaf) (t-single k v1 ) r-leaf | |
263 replaceNodeP k v1 (node .k value t t₁) (case2 refl) P next = next (node k v1 t t₁) (replaceTree1 k value v1 P) r-node | |
606 | 264 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
265 replaceP : {n m : Level} {A : Set n} {t : Set m} |
651
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
266 → (key : ℕ) → (value : A) → {tree0 tree tree-st : bt A} ( repl : bt A) |
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
267 → (stack : List (bt A)) → treeInvariant tree0 ∧ stackInvariant key tree-st tree0 stack ∧ replacedTree key value tree repl |
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
268 → (next : ℕ → A → {tree0 tree1 tree-st : bt A } (repl : bt A) → (stack1 : List (bt A)) |
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
269 → treeInvariant tree0 ∧ stackInvariant key tree-st tree0 stack1 ∧ replacedTree key value tree1 repl → length stack1 < length stack → t) |
613 | 270 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t |
651
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
271 replaceP key value {tree0} {tree} {tree-st} repl [] Pre next exit with proj1 (proj2 Pre) |
655 | 272 ... | t = {!!} |
654 | 273 replaceP {_} {_} {A} key value {tree0} {tree} {tree-st} repl (leaf ∷ []) Pre next exit with proj1 (proj2 Pre) |
655 | 274 ... | s-right x t _ = {!!} |
275 ... | s-left x t _ = {!!} | |
654 | 276 replaceP key value {tree0} {tree} {tree-st} repl (leaf ∷ leaf ∷ st) Pre next exit with proj1 (proj2 Pre) |
655 | 277 ... | s-right x t _ = {!!} |
278 ... | s-left x t _ = {!!} | |
651
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
279 replaceP {_} {_} {A} key value {tree0} {tree} {tree-st} repl (leaf ∷ node key₁ value₁ left right ∷ st) Pre next exit with <-cmp key key₁ |
650 | 280 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) (node key₁ value₁ tree right ∷ st) |
281 ⟪ proj1 Pre , ⟪ repl5 (proj1 (proj2 Pre)) , r-left a (proj2 (proj2 Pre)) ⟫ ⟫ ≤-refl where | |
651
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
282 repl5 : stackInvariant key tree-st tree0 (leaf ∷ node key₁ value₁ left right ∷ st) → stackInvariant key (node key₁ value₁ tree right) tree0 (node key₁ value₁ tree right ∷ st ) |
655 | 283 repl5 si with si-property1 _ _ _ _ {!!} si |
284 repl5 (s-right x si _) | refl = s-left a {!!} {!!} | |
285 repl5 (s-left x si _) | refl = s-left a {!!} {!!} | |
649 | 286 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right) st {!!} depth-3< |
287 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ repl right) st {!!} depth-3< | |
651
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
288 replaceP key value {tree0} {tree} {tree-st} repl (node key₁ value₁ left right ∷ st) Pre next exit with <-cmp key key₁ |
644 | 289 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ repl right ) st {!!} ≤-refl |
290 ... | tri≈ ¬a b ¬c = next key value (node key value left right ) st {!!} ≤-refl where -- this case won't happen | |
651
7b9d35f7c033
fix stack top and replaced tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
650
diff
changeset
|
291 ... | tri< a ¬b ¬c with proj1 (proj2 Pre) |
655 | 292 ... | s-right x si1 _ = {!!} |
293 ... | s-left x si1 _ = next key value (node key₁ value₁ repl right ) st ⟪ proj1 Pre , ⟪ si1 , r-left a (proj2 (proj2 Pre)) ⟫ ⟫ ≤-refl | |
652 | 294 -- = next key value (node key₁ value₁ repl right ) st ⟪ proj1 Pre , ⟪ repl2 (proj1 (proj2 Pre)) , r-left a {!!} ⟫ ⟫ ≤-refl where |
295 -- repl2 : stackInvariant key tree tree0 (node key₁ value₁ left right ∷ st) → stackInvariant key (node key₁ value₁ left right) tree0 st | |
296 -- repl2 (s-single .(node key₁ value₁ left right)) = {!!} | |
297 -- repl2 (s-right {_} {_} {_} {key₂} {v1} x si) with si-property1 _ _ _ _ si | |
298 -- ... | eq = {!!} | |
299 -- repl2 (s-left x si) with si-property1 _ _ _ _ (s-left x si) | |
300 -- ... | refl = {!!} | |
301 | |
644 | 302 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
303 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
|
304 → (r : Index) → (p : Invraiant r) |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
305 → (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
|
306 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
|
307 ... | 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
|
308 ... | 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
|
309 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
|
310 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
|
311 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
|
312 ... | 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
|
313 ... | 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
|
314 ... | 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
|
315 |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
316 open _∧_ |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
317 |
615 | 318 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
319 → replacedTree key value tree repl → treeInvariant repl | |
320 RTtoTI0 = {!!} | |
321 | |
322 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl | |
323 → replacedTree key value tree repl → treeInvariant tree | |
324 RTtoTI1 = {!!} | |
614 | 325 |
611 | 326 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
613 | 327 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t |
610 | 328 insertTreeP {n} {m} {A} {t} tree key value P exit = |
645 | 329 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → treeInvariant (proj1 p) ∧ stackInvariant key (proj1 p) tree (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫ ⟪ P , {!!} ⟫ |
615 | 330 $ λ p P loop → findP key (proj1 p) tree (proj2 p) {!!} (λ t _ s P1 lt → loop ⟪ t , s ⟫ {!!} lt ) |
638 | 331 $ λ t _ s P C → replaceNodeP key value t C (proj1 P) |
614 | 332 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A )) |
645 | 333 {λ p → treeInvariant (proj1 (proj2 p)) ∧ stackInvariant key (proj1 (proj2 p)) tree (proj1 p) ∧ replacedTree key value (proj1 (proj2 p)) (proj2 (proj2 p)) } |
639 | 334 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ ⟪ proj1 P , ⟪ {!!} , R ⟫ ⟫ |
644 | 335 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) {!!} |
336 (λ key value repl1 stack P2 lt → loop ⟪ stack , ⟪ {!!} , repl1 ⟫ ⟫ {!!} lt ) exit | |
614 | 337 |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
338 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
|
339 top-value leaf = nothing |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
340 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
|
341 |
612 | 342 insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤ |
609
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
343 insertTreeSpec0 _ _ _ = tt |
79418701a283
add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
606
diff
changeset
|
344 |
627 | 345 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 | 346 field |
619 | 347 tree0 : bt A |
622 | 348 ti : treeInvariant tree0 |
645 | 349 si : stackInvariant key tree tree0 stack |
631 | 350 ci : C tree stack -- data continuation |
618 | 351 |
616 | 352 findPP : {n m : Level} {A : Set n} {t : Set m} |
353 → (key : ℕ) → (tree : bt A ) → (stack : List (bt A)) | |
627 | 354 → (Pre : findPR key tree stack (λ t s → Lift n ⊤)) |
355 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (λ t s → Lift n ⊤) → bt-depth tree1 < bt-depth tree → t ) | |
356 → (exit : (tree1 : bt A) → (stack1 : List (bt A)) → ( tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key) → findPR key tree1 stack1 (λ t s → Lift n ⊤) → t) → t | |
625 | 357 findPP key leaf st Pre next exit = exit leaf st (case1 refl) Pre |
632 | 358 findPP key (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁ |
625 | 359 findPP key n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P |
632 | 360 findPP {_} {_} {A} key n@(node key₁ v1 tree tree₁) st Pre next exit | tri< a ¬b ¬c = |
624 | 361 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = findPP2 st (findPR.si Pre) ; ci = lift tt} ) findPP1 where |
621 | 362 tree0 = findPR.tree0 Pre |
645 | 363 findPP2 : (st : List (bt A)) → stackInvariant key {!!} tree0 st → stackInvariant key {!!} tree0 (node key₁ v1 tree tree₁ ∷ st) |
623 | 364 findPP2 = {!!} |
618 | 365 findPP1 : suc ( bt-depth tree ) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁) |
634 | 366 findPP1 = depth-1< |
632 | 367 findPP key n@(node key₁ v1 tree tree₁) st Pre next exit | tri> ¬a ¬b c = next tree₁ (n ∷ st) {!!} findPP2 where -- Cond n st → Cond tree₁ (n ∷ st) |
618 | 368 findPP2 : suc (bt-depth tree₁) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁) |
634 | 369 findPP2 = depth-2< |
616 | 370 |
618 | 371 insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree |
372 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t | |
624 | 373 insertTreePP {n} {m} {A} {t} tree key value P exit = |
627 | 374 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR key (proj1 p) (proj2 p) (λ t s → Lift n ⊤) } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫ {!!} |
630 | 375 $ λ p P loop → findPP key (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
638 | 376 $ λ t s _ P → replaceNodeP key value t {!!} {!!} |
618 | 377 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A )) |
645 | 378 {λ p → treeInvariant (proj1 (proj2 p)) ∧ stackInvariant key (proj1 (proj2 p)) tree (proj1 p) ∧ replacedTree key value (proj1 (proj2 p)) (proj2 (proj2 p)) } |
639 | 379 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ ⟪ {!!} , ⟪ {!!} , R ⟫ ⟫ |
644 | 380 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) {!!} |
381 (λ key value repl1 stack P2 lt → loop ⟪ stack , ⟪ {!!} , repl1 ⟫ ⟫ {!!} lt ) exit | |
618 | 382 |
629 | 383 record findPC {n : Level} {A : Set n} (key1 : ℕ) (value1 : A) (tree : bt A ) (stack : List (bt A)) : Set n where |
616 | 384 field |
385 tree1 : bt A | |
617 | 386 ci : replacedTree key1 value1 tree tree1 |
616 | 387 |
624 | 388 findPPC : {n m : Level} {A : Set n} {t : Set m} |
628 | 389 → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A)) |
629 | 390 → (Pre : findPR key tree stack (findPC key value)) |
391 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (findPC key value) → bt-depth tree1 < bt-depth tree → t ) | |
392 → (exit : (tree1 : bt A) → (stack1 : List (bt A)) → ( tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key) → findPR key tree1 stack1 (findPC key value) → t) → t | |
393 findPPC key value leaf st Pre next exit = exit leaf st (case1 refl) Pre | |
632 | 394 findPPC key value (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁ |
629 | 395 findPPC key value n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P |
632 | 396 findPPC {_} {_} {A} key value n@(node key₁ v1 tree tree₁) st Pre next exit | tri< a ¬b ¬c = |
629 | 397 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = {!!} ; ci = {!!} } ) {!!} |
398 findPPC key value n st P next exit | tri> ¬a ¬b c = {!!} | |
624 | 399 |
618 | 400 containsTree : {n m : Level} {A : Set n} {t : Set m} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree → ⊤ |
615 | 401 containsTree {n} {m} {A} {t} tree tree1 key value P RT = |
617 | 402 TerminatingLoopS (bt A ∧ List (bt A) ) |
634 | 403 {λ p → findPR key (proj1 p) (proj2 p) (findPC key value ) } (λ p → bt-depth (proj1 p)) -- findPR key tree1 [] (findPC key value) |
404 ⟪ tree1 , [] ⟫ record { tree0 = tree ; ti = {!!} ; si = {!!} ; ci = record { tree1 = tree ; ci = RT } } | |
630 | 405 $ λ p P loop → findPPC key value (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) |
629 | 406 $ λ t1 s1 found? P2 → insertTreeSpec0 t1 value (lemma6 t1 s1 found? P2) where |
407 lemma6 : (t1 : bt A) (s1 : List (bt A)) (found? : (t1 ≡ leaf) ∨ (node-key t1 ≡ just key)) (P2 : findPR key t1 s1 (findPC key value)) → top-value t1 ≡ just value | |
408 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 | |
409 lemma7 : (t1 : bt A) ( s1 : List (bt A) ) (tree0 tree1 : bt A) → | |
645 | 410 replacedTree key value t1 tree1 → stackInvariant key t1 tree0 s1 → ( t1 ≡ leaf ) ∨ ( node-key t1 ≡ just key) → top-value t1 ≡ just value |
629 | 411 lemma7 = {!!} |
615 | 412 |