comparison hoareBinaryTree.agda @ 632:b58991f8e2e4

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 11 Nov 2021 15:48:36 +0900
parents 956ee8ae42b9
children 119f340c0b10
comparison
equal deleted inserted replaced
631:956ee8ae42b9 632:b58991f8e2e4
46 bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ )) 46 bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ ))
47 47
48 find : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → List (bt A) 48 find : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → List (bt A)
49 → (next : bt A → List (bt A) → t ) → (exit : bt A → List (bt A) → t ) → t 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 50 find key leaf st _ exit = exit leaf st
51 find key (node key₁ v tree tree₁) st next exit with <-cmp key key₁ 51 find key (node key₁ v1 tree tree₁) st next exit with <-cmp key key₁
52 find key n st _ exit | tri≈ ¬a b ¬c = exit n st 52 find key n st _ exit | tri≈ ¬a b ¬c = exit n st
53 find key n@(node key₁ v tree tree₁) st next _ | tri< a ¬b ¬c = next tree (n ∷ st) 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₁ v 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)
55 55
56 {-# TERMINATING #-} 56 {-# TERMINATING #-}
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 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
58 find-loop {n} {m} {A} {t} key tree st exit = find-loop1 tree st where 58 find-loop {n} {m} {A} {t} key tree st exit = find-loop1 tree st where
59 find-loop1 : bt A → List (bt A) → t 59 find-loop1 : bt A → List (bt A) → t
60 find-loop1 tree st = find key tree st find-loop1 exit 60 find-loop1 tree st = find key tree st find-loop1 exit
61 61
62 replaceNode : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → (bt A → t) → t 62 replaceNode : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → (bt A → t) → t
63 replaceNode k v leaf next = next (node k v leaf leaf) 63 replaceNode k v1 leaf next = next (node k v1 leaf leaf)
64 replaceNode k v (node key value t t₁) next = next (node k v t t₁) 64 replaceNode k v1 (node key value t t₁) next = next (node k v1 t t₁)
65 65
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 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
67 replace key value tree [] next exit = exit tree 67 replace key value tree [] next exit = exit tree
68 replace key value tree (leaf ∷ st) next exit = next key value tree st 68 replace key value tree (leaf ∷ st) next exit = next key value tree st
69 replace key value tree (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁ 69 replace key value tree (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁
85 85
86 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_) 86 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_)
87 87
88 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where 88 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where
89 t-leaf : treeInvariant leaf 89 t-leaf : treeInvariant leaf
90 t-single : {key : ℕ} → {value : A} → treeInvariant (node key value leaf leaf) 90 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf)
91 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂) → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂)) 91 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂)
92 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key₁ < key) → treeInvariant (node key value₁ t₁ t₂) → treeInvariant (node key₁ value₁ (node key value₁ t₁ t₂) leaf ) 92 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂))
93 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key₁ < key) → treeInvariant (node key value t₁ t₂)
94 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf )
93 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂) 95 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂)
94 → treeInvariant (node key value t₁ t₂) 96 → treeInvariant (node key value t₁ t₂)
95 → treeInvariant (node key₂ value₂ t₃ t₄) 97 → treeInvariant (node key₂ value₂ t₃ t₄)
96 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) 98 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄))
97 99
98 treeInvariantTest1 : treeInvariant (node 3 0 leaf (node 1 1 leaf (node 3 5 leaf leaf))) 100 add< : { i : ℕ } (j : ℕ ) → i < suc i + j
99 treeInvariantTest1 = {!!} 101 add< {i} j = begin
102 suc i ≤⟨ m≤m+n (suc i) j ⟩
103 suc i + j ∎ where open ≤-Reasoning
104
105 treeTest1 : bt ℕ
106 treeTest1 = node 1 0 leaf (node 3 1 (node 2 5 (node 4 7 leaf leaf ) leaf) (node 5 5 leaf leaf))
107 treeTest2 : bt ℕ
108 treeTest2 = node 3 1 (node 2 5 (node 4 7 leaf leaf ) leaf) (node 5 5 leaf leaf)
109
110 treeInvariantTest1 : treeInvariant treeTest1
111 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) )
100 112
101 data stackInvariant {n : Level} {A : Set n} (key0 : ℕ) : (tree tree0 : bt A) → (stack : List (bt A)) → Set n where 113 data stackInvariant {n : Level} {A : Set n} (key0 : ℕ) : (tree tree0 : bt A) → (stack : List (bt A)) → Set n where
102 s-nil : stackInvariant key0 leaf leaf [] 114 s-nil : stackInvariant key0 leaf leaf []
103 s-single : (tree : bt A) → stackInvariant key0 tree tree (tree ∷ [] ) 115 s-single : (tree : bt A) → stackInvariant key0 tree tree (tree ∷ [] )
104 s-right : (tree0 tree : bt A) → {key : ℕ } → {value : A } { left : bt A} → {st : List (bt A)} 116 s-right : {tree0 tree : bt A} → {key : ℕ } → {value : A } { left : bt A} → {st : List (bt A)}
105 → key < key0 → stackInvariant key0(node key value left tree ) tree0 (node key value left tree ∷ st ) → stackInvariant key0 tree tree0 (tree ∷ node key value left tree ∷ st ) 117 → key < key0 → stackInvariant key0(node key value left tree ) tree0 (node key value left tree ∷ st ) → stackInvariant key0 tree tree0 (tree ∷ node key value left tree ∷ st )
106 s-left : (tree0 tree : bt A) → {key : ℕ } → {value : A } { right : bt A} → {st : List (bt A)} 118 s-left : {tree0 tree : bt A} → {key : ℕ } → {value : A } { right : bt A} → {st : List (bt A)}
107 → key0 < key → stackInvariant key0(node key value tree right ) tree0 (node key value tree right ∷ st ) → stackInvariant key0 tree tree0 (tree ∷ node key value tree right ∷ st ) 119 → key0 < key → stackInvariant key0(node key value tree right ) tree0 (node key value tree right ∷ st ) → stackInvariant key0 tree tree0 (tree ∷ node key value tree right ∷ st )
120
121 stackInvariantTest0 : stackInvariant {_} {ℕ} 1 leaf leaf []
122 stackInvariantTest0 = s-nil
123
124 stackInvariantTest1 : stackInvariant 3 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] )
125 stackInvariantTest1 = s-right (add< 1) (s-single treeTest1 )
108 126
109 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (tree tree1 : bt A ) → Set n where 127 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (tree tree1 : bt A ) → Set n where
110 r-leaf : replacedTree key value leaf (node key value leaf leaf) 128 r-leaf : replacedTree key value leaf (node key value leaf leaf)
111 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁) 129 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁)
112 r-right : {k : ℕ } {v : A} → {t t1 t2 : bt A} 130 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
113 → k > key → replacedTree key value t1 t2 → replacedTree key value (node k v t t1) (node k v t t2) 131 → k > key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t t1) (node k v1 t t2)
114 r-left : {k : ℕ } {v : A} → {t t1 t2 : bt A} 132 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
115 → k < key → replacedTree key value t1 t2 → replacedTree key value (node k v t1 t) (node k v t2 t) 133 → k < key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t1 t) (node k v1 t2 t)
134
135 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j )
136 depth-1< {i} {j} = s≤s (m≤m⊔n _ j)
137
138 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i )
139 depth-2< {i} {j} = s≤s (m≤n⊔m _ i)
116 140
117 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A)) 141 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A))
118 → treeInvariant tree ∧ stackInvariant key tree tree0 stack 142 → treeInvariant tree ∧ stackInvariant key tree tree0 stack
119 → (next : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t ) 143 → (next : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t )
120 → (exit : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → t ) → t 144 → (exit : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → t ) → t
121 findP key leaf tree0 st Pre _ exit = exit leaf tree0 st Pre 145 findP key leaf tree0 st Pre _ exit = exit leaf tree0 st Pre
122 findP key (node key₁ v tree tree₁) tree0 st Pre next exit with <-cmp key key₁ 146 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁
123 findP key n tree0 st Pre _ exit | tri≈ ¬a b ¬c = exit n tree0 st Pre 147 findP key n tree0 st Pre _ exit | tri≈ ¬a b ¬c = exit n tree0 st Pre
124 findP key n@(node key₁ v tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (n ∷ st) {!!} {!!} 148 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (n ∷ st) {!!} depth-1<
125 findP key n@(node key₁ v tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (n ∷ st) {!!} {!!} 149 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (n ∷ st) {!!} depth-2<
150 -- Pre : treeInvariant (node key₁ v1 tree tree₁)
151 -- → treeInvariant tree ∧
152 -- stackInvariant key (node key₁ v1 tree tree₁) tree0 st
153 - → stackInvariant key tree tree0 (node key₁ v1 tree tree₁ ∷ st)
154
126 155
127 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) → (treeInvariant tree ) 156 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) → (treeInvariant tree )
128 → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value tree tree1 → t) → t 157 → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value tree tree1 → t) → t
129 replaceNodeP k v leaf P next = next (node k v leaf leaf) {!!} {!!} 158 replaceNodeP k v1 leaf P next = next (node k v1 leaf leaf) {!!} {!!}
130 replaceNodeP k v (node key value t t₁) P next = next (node k v t t₁) {!!} {!!} 159 replaceNodeP k v1 (node key value t t₁) P next = next (node k v1 t t₁) {!!} {!!}
131 160
132 replaceP : {n m : Level} {A : Set n} {t : Set m} 161 replaceP : {n m : Level} {A : Set n} {t : Set m}
133 → (key : ℕ) → (value : A) → (tree repl : bt A) → (stack : List (bt A)) → treeInvariant tree ∧ stackInvariant key repl tree stack ∧ replacedTree key value tree repl 162 → (key : ℕ) → (value : A) → (tree repl : bt A) → (stack : List (bt A)) → treeInvariant tree ∧ stackInvariant key repl tree stack ∧ replacedTree key value tree repl
134 → (next : ℕ → A → (tree1 repl : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key repl tree1 stack ∧ replacedTree key value tree1 repl → bt-depth tree1 < bt-depth tree → t ) 163 → (next : ℕ → A → (tree1 repl : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key repl tree1 stack ∧ replacedTree key value tree1 repl → bt-depth tree1 < bt-depth tree → t )
135 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t 164 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t
202 → (key : ℕ) → (tree : bt A ) → (stack : List (bt A)) 231 → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
203 → (Pre : findPR key tree stack (λ t s → Lift n ⊤)) 232 → (Pre : findPR key tree stack (λ t s → Lift n ⊤))
204 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (λ t s → Lift n ⊤) → bt-depth tree1 < bt-depth tree → t ) 233 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (λ t s → Lift n ⊤) → bt-depth tree1 < bt-depth tree → t )
205 → (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 234 → (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
206 findPP key leaf st Pre next exit = exit leaf st (case1 refl) Pre 235 findPP key leaf st Pre next exit = exit leaf st (case1 refl) Pre
207 findPP key (node key₁ v tree tree₁) st Pre next exit with <-cmp key key₁ 236 findPP key (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁
208 findPP key n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P 237 findPP key n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P
209 findPP {_} {_} {A} key n@(node key₁ v tree tree₁) st Pre next exit | tri< a ¬b ¬c = 238 findPP {_} {_} {A} key n@(node key₁ v1 tree tree₁) st Pre next exit | tri< a ¬b ¬c =
210 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = findPP2 st (findPR.si Pre) ; ci = lift tt} ) findPP1 where 239 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = findPP2 st (findPR.si Pre) ; ci = lift tt} ) findPP1 where
211 tree0 = findPR.tree0 Pre 240 tree0 = findPR.tree0 Pre
212 findPP2 : (st : List (bt A)) → stackInvariant key {!!} tree0 st → stackInvariant key {!!} tree0 (node key₁ v tree tree₁ ∷ st) 241 findPP2 : (st : List (bt A)) → stackInvariant key {!!} tree0 st → stackInvariant key {!!} tree0 (node key₁ v1 tree tree₁ ∷ st)
213 findPP2 = {!!} 242 findPP2 = {!!}
214 findPP1 : suc ( bt-depth tree ) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁) 243 findPP1 : suc ( bt-depth tree ) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁)
215 findPP1 = {!!} 244 findPP1 = {!!}
216 findPP key n@(node key₁ v tree tree₁) st Pre next exit | tri> ¬a ¬b c = next tree₁ (n ∷ st) {!!} findPP2 where -- Cond n st → Cond tree₁ (n ∷ st) 245 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)
217 findPP2 : suc (bt-depth tree₁) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁) 246 findPP2 : suc (bt-depth tree₁) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁)
218 findPP2 = {!!} 247 findPP2 = {!!}
219 248
220 insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree 249 insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
221 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t 250 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t
238 → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A)) 267 → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A))
239 → (Pre : findPR key tree stack (findPC key value)) 268 → (Pre : findPR key tree stack (findPC key value))
240 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (findPC key value) → bt-depth tree1 < bt-depth tree → t ) 269 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (findPC key value) → bt-depth tree1 < bt-depth tree → t )
241 → (exit : (tree1 : bt A) → (stack1 : List (bt A)) → ( tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key) → findPR key tree1 stack1 (findPC key value) → t) → t 270 → (exit : (tree1 : bt A) → (stack1 : List (bt A)) → ( tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key) → findPR key tree1 stack1 (findPC key value) → t) → t
242 findPPC key value leaf st Pre next exit = exit leaf st (case1 refl) Pre 271 findPPC key value leaf st Pre next exit = exit leaf st (case1 refl) Pre
243 findPPC key value (node key₁ v tree tree₁) st Pre next exit with <-cmp key key₁ 272 findPPC key value (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁
244 findPPC key value n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P 273 findPPC key value n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P
245 findPPC {_} {_} {A} key value n@(node key₁ v tree tree₁) st Pre next exit | tri< a ¬b ¬c = 274 findPPC {_} {_} {A} key value n@(node key₁ v1 tree tree₁) st Pre next exit | tri< a ¬b ¬c =
246 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = {!!} ; ci = {!!} } ) {!!} 275 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = {!!} ; ci = {!!} } ) {!!}
247 findPPC key value n st P next exit | tri> ¬a ¬b c = {!!} 276 findPPC key value n st P next exit | tri> ¬a ¬b c = {!!}
248 277
249 containsTree : {n m : Level} {A : Set n} {t : Set m} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree → ⊤ 278 containsTree : {n m : Level} {A : Set n} {t : Set m} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree → ⊤
250 containsTree {n} {m} {A} {t} tree tree1 key value P RT = 279 containsTree {n} {m} {A} {t} tree tree1 key value P RT =