annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
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
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
21 _iso_ : {n : Level} {a : Set n} → ℕ → ℕ → Set
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
22 d iso d' = (¬ (suc d ≤ d')) ∧ (¬ (suc d' ≤ d))
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
23
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
24 iso-intro : {n : Level} {a : Set n} {x y : ℕ} → ¬ (suc x ≤ y) → ¬ (suc y ≤ x) → _iso_ {n} {a} x y
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
25 iso-intro = λ z z₁ → record { proj1 = z ; proj2 = z₁ }
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
26
590
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
27 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
28 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
29 -- no children , having left node , having right node , having both
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
30 --
597
ryokka
parents: 596
diff changeset
31 data bt {n : Level} (A : Set n) : Set n where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
32 leaf : bt A
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
33 node : (key : ℕ) → (value : A) →
610
8239583dac0b add one more stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 609
diff changeset
34 (left : bt A ) → (right : bt A ) → bt A
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
35
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
36 node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
37 node-key (node key _ _ _) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
38 node-key _ = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
39
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
40 node-value : {n : Level} {A : Set n} → bt A → Maybe A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
41 node-value (node _ value _ _) = just value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
42 node-value _ = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
46 bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ ))
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
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
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
49 → (next : bt A → List (bt A) → t ) → (exit : bt A → List (bt A) → t ) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
50 find key leaf st _ exit = exit leaf st
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
51 find key (node key₁ v1 tree tree₁) st next exit with <-cmp key key₁
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
52 find key n st _ exit | tri≈ ¬a b ¬c = exit n st
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
53 find key n@(node key₁ v1 tree tree₁) st next _ | tri< a ¬b ¬c = next tree (n ∷ st)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
54 find key n@(node key₁ v1 tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (n ∷ st)
597
ryokka
parents: 596
diff changeset
55
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
58 find-loop {n} {m} {A} {t} key tree st exit = find-loop1 tree st where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
59 find-loop1 : bt A → List (bt A) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
60 find-loop1 tree st = find key tree st find-loop1 exit
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
61
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
62 replaceNode : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → (bt A → t) → t
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
63 replaceNode k v1 leaf next = next (node k v1 leaf leaf)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
64 replaceNode k v1 (node key value t t₁) next = next (node k v1 t t₁)
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
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
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
67 replace key value tree [] next exit = exit tree
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
68 replace key value tree (leaf ∷ st) next exit = next key value tree st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
69 replace key value tree (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
70 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ tree right ) st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
71 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
72 ... | 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
73
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
74 {-# TERMINATING #-}
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
75 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
76 replace-loop {_} {_} {A} {t} key value tree st exit = replace-loop1 key value tree st where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
77 replace-loop1 : (key : ℕ) → (value : A) → bt A → List (bt A) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
78 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
79
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
80 insertTree : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → (next : bt A → t ) → t
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
81 insertTree tree key value exit = find-loop key tree [] $ λ t st → replaceNode key value t $ λ t1 → replace-loop key value t1 st exit
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
82
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
83 insertTest1 = insertTree leaf 1 1 (λ x → x )
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
84 insertTest2 = insertTree insertTest1 2 1 (λ x → x )
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
85
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
86 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_)
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
87
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
88 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
89 t-leaf : treeInvariant leaf
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
90 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
91 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
92 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
93 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key₁ < key) → treeInvariant (node key value t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
94 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf )
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
95 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
96 → treeInvariant (node key value t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
97 → treeInvariant (node key₂ value₂ t₃ t₄)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
98 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄))
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
99
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
100 add< : { i : ℕ } (j : ℕ ) → i < suc i + j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
101 add< {i} j = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
102 suc i ≤⟨ m≤m+n (suc i) j ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
103 suc i + j ∎ where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
104
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
105 treeTest1 : bt ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
106 treeTest1 = node 1 0 leaf (node 3 1 (node 2 5 (node 4 7 leaf leaf ) leaf) (node 5 5 leaf leaf))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
107 treeTest2 : bt ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
108 treeTest2 = node 3 1 (node 2 5 (node 4 7 leaf leaf ) leaf) (node 5 5 leaf leaf)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
109
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
110 treeInvariantTest1 : treeInvariant treeTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
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) )
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
112
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
113 data stackInvariant {n : Level} {A : Set n} (key0 : ℕ) : (tree tree0 : bt A) → (stack : List (bt A)) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
114 s-nil : stackInvariant key0 leaf leaf []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
115 s-single : (tree : bt A) → stackInvariant key0 tree tree (tree ∷ [] )
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
116 s-right : {tree0 tree : bt A} → {key : ℕ } → {value : A } { left : bt A} → {st : List (bt A)}
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
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 )
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
118 s-left : {tree0 tree : bt A} → {key : ℕ } → {value : A } { right : bt A} → {st : List (bt A)}
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
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 )
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
120
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
121 stackInvariantTest0 : stackInvariant {_} {ℕ} 1 leaf leaf []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
122 stackInvariantTest0 = s-nil
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
123
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
124 stackInvariantTest1 : stackInvariant 3 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
125 stackInvariantTest1 = s-right (add< 1) (s-single treeTest1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
126
613
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
127 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (tree tree1 : bt A ) → Set n where
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
128 r-leaf : replacedTree key value leaf (node key value leaf leaf)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
129 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁)
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
130 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
131 → k > key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t t1) (node k v1 t t2)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
132 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
133 → k < key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t1 t) (node k v1 t2 t)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
134
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
135 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
136 depth-1< {i} {j} = s≤s (m≤m⊔n _ j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
137
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
138 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
139 depth-2< {i} {j} = s≤s (m≤n⊔m _ i)
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
140
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
141 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A))
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
142 → treeInvariant tree ∧ stackInvariant key tree tree0 stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
143 → (next : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
144 → (exit : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → t ) → t
630
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 629
diff changeset
145 findP key leaf tree0 st Pre _ exit = exit leaf tree0 st Pre
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
146 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁
630
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 629
diff changeset
147 findP key n tree0 st Pre _ exit | tri≈ ¬a b ¬c = exit n tree0 st Pre
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
148 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (n ∷ st) {!!} depth-1<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
149 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (n ∷ st) {!!} depth-2<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
150 -- Pre : treeInvariant (node key₁ v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
151 -- → treeInvariant tree ∧
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
152 -- stackInvariant key (node key₁ v1 tree tree₁) tree0 st
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
153 - → stackInvariant key tree tree0 (node key₁ v1 tree tree₁ ∷ st)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
154
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
155
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
156 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) → (treeInvariant tree )
613
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
157 → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value tree tree1 → t) → t
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
158 replaceNodeP k v1 leaf P next = next (node k v1 leaf leaf) {!!} {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
159 replaceNodeP k v1 (node key value t t₁) P next = next (node k v1 t t₁) {!!} {!!}
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
160
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
161 replaceP : {n m : Level} {A : Set n} {t : Set m}
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
162 → (key : ℕ) → (value : A) → (tree repl : bt A) → (stack : List (bt A)) → treeInvariant tree ∧ stackInvariant key repl tree stack ∧ replacedTree key value tree repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
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 )
613
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
164 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
165 replaceP key value tree repl [] Pre next exit = exit tree repl {!!}
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
166 replaceP key value tree repl (leaf ∷ st) Pre next exit = next key value tree {!!} st {!!} {!!}
613
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
167 replaceP key value tree repl (node key₁ value₁ left right ∷ st) Pre next exit with <-cmp key key₁
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
168 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ tree right ) {!!} st {!!} {!!}
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
169 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) {!!} st {!!} {!!}
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
170 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left tree ) {!!} st {!!} {!!}
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
171
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
172 open import Relation.Binary.Definitions
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
173
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
174 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
175 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
176 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
177 lemma3 refl ()
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
178 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
179 lemma5 (s≤s z≤n) ()
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
180
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
181 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
182 → (r : Index) → (p : Invraiant r)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
183 → (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
184 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
185 ... | 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
186 ... | 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
187 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
188 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
189 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
190 ... | 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
191 ... | 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
192 ... | 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
193
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
194 open _∧_
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
195
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
196 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
197 → replacedTree key value tree repl → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
198 RTtoTI0 = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
199
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
200 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
201 → replacedTree key value tree repl → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
202 RTtoTI1 = {!!}
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
203
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
204 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
613
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
205 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t
610
8239583dac0b add one more stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 609
diff changeset
206 insertTreeP {n} {m} {A} {t} tree key value P exit =
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
207 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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
208 $ λ p P loop → findP key (proj1 p) tree (proj2 p) {!!} (λ t _ s P1 lt → loop ⟪ t , s ⟫ {!!} lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
209 $ λ t _ s P → replaceNodeP key value t (proj1 P)
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
210 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A ))
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
211 {λ p → treeInvariant (proj1 (proj2 p)) ∧ stackInvariant key (proj1 (proj2 p)) tree (proj1 p) ∧ replacedTree key value (proj1 (proj2 p)) (proj2 (proj2 p)) }
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
212 (λ p → bt-depth (proj1 (proj2 p))) ⟪ s , ⟪ t , t1 ⟫ ⟫ ⟪ proj1 P , ⟪ {!!} , R ⟫ ⟫
621
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 620
diff changeset
213 $ λ p P1 loop → replaceP key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 620
diff changeset
214 (λ key value tree1 repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ {!!} lt ) exit
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
215
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
216 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
217 top-value leaf = nothing
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
218 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
219
612
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 611
diff changeset
220 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
221 insertTreeSpec0 _ _ _ = tt
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
222
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
223 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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
224 field
619
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 618
diff changeset
225 tree0 : bt A
622
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 621
diff changeset
226 ti : treeInvariant tree0
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
227 si : stackInvariant key tree tree0 stack
631
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 630
diff changeset
228 ci : C tree stack -- data continuation
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
229
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
230 findPP : {n m : Level} {A : Set n} {t : Set m}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
231 → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
232 → (Pre : findPR key tree stack (λ t s → Lift n ⊤))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
233 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (λ t s → Lift n ⊤) → bt-depth tree1 < bt-depth tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
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
625
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 624
diff changeset
235 findPP key leaf st Pre next exit = exit leaf st (case1 refl) Pre
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
236 findPP key (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁
625
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 624
diff changeset
237 findPP key n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
238 findPP {_} {_} {A} key n@(node key₁ v1 tree tree₁) st Pre next exit | tri< a ¬b ¬c =
624
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 623
diff changeset
239 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = findPP2 st (findPR.si Pre) ; ci = lift tt} ) findPP1 where
621
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 620
diff changeset
240 tree0 = findPR.tree0 Pre
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
241 findPP2 : (st : List (bt A)) → stackInvariant key {!!} tree0 st → stackInvariant key {!!} tree0 (node key₁ v1 tree tree₁ ∷ st)
623
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 622
diff changeset
242 findPP2 = {!!}
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
243 findPP1 : suc ( bt-depth tree ) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
244 findPP1 = {!!}
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
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)
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
246 findPP2 : suc (bt-depth tree₁) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
247 findPP2 = {!!}
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
248
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
249 insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
250 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t
624
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 623
diff changeset
251 insertTreePP {n} {m} {A} {t} tree key value P exit =
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
252 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR key (proj1 p) (proj2 p) (λ t s → Lift n ⊤) } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫ {!!}
630
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 629
diff changeset
253 $ λ p P loop → findPP key (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt )
625
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 624
diff changeset
254 $ λ t s _ P → replaceNodeP key value t {!!}
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
255 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A ))
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 626
diff changeset
256 {λ p → treeInvariant (proj1 (proj2 p)) ∧ stackInvariant key (proj1 (proj2 p)) tree (proj1 p) ∧ replacedTree key value (proj1 (proj2 p)) (proj2 (proj2 p)) }
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
257 (λ p → bt-depth (proj1 (proj2 p))) ⟪ s , ⟪ t , t1 ⟫ ⟫ ⟪ {!!} , ⟪ {!!} , R ⟫ ⟫
621
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 620
diff changeset
258 $ λ p P1 loop → replaceP key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 620
diff changeset
259 (λ key value tree1 repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ {!!} lt ) exit
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
260
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
261 record findPC {n : Level} {A : Set n} (key1 : ℕ) (value1 : A) (tree : bt A ) (stack : List (bt A)) : Set n where
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
262 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
263 tree1 : bt A
617
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 616
diff changeset
264 ci : replacedTree key1 value1 tree tree1
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
265
624
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 623
diff changeset
266 findPPC : {n m : Level} {A : Set n} {t : Set m}
628
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 627
diff changeset
267 → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A))
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
268 → (Pre : findPR key tree stack (findPC key value))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
269 → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR key tree1 stack1 (findPC key value) → bt-depth tree1 < bt-depth tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
271 findPPC key value leaf st Pre next exit = exit leaf st (case1 refl) Pre
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
272 findPPC key value (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
273 findPPC key value n st P next exit | tri≈ ¬a b ¬c = exit n st (case2 {!!}) P
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
274 findPPC {_} {_} {A} key value n@(node key₁ v1 tree tree₁) st Pre next exit | tri< a ¬b ¬c =
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
275 next tree (n ∷ st) (record {ti = findPR.ti Pre ; si = {!!} ; ci = {!!} } ) {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
276 findPPC key value n st P next exit | tri> ¬a ¬b c = {!!}
624
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 623
diff changeset
277
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
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 → ⊤
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
279 containsTree {n} {m} {A} {t} tree tree1 key value P RT =
617
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 616
diff changeset
280 TerminatingLoopS (bt A ∧ List (bt A) )
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
281 {λ p → findPR key (proj1 p) (proj2 p) (findPC key value ) } (λ p → bt-depth (proj1 p))
624
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 623
diff changeset
282 ⟪ tree1 , [] ⟫ {!!}
630
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 629
diff changeset
283 $ λ p P loop → findPPC key value (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt )
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
284 $ λ t1 s1 found? P2 → insertTreeSpec0 t1 value (lemma6 t1 s1 found? P2) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
285 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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
286 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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
287 lemma7 : (t1 : bt A) ( s1 : List (bt A) ) (tree0 tree1 : bt A) →
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
288 replacedTree key value t1 tree1 → stackInvariant key t1 tree0 s1 → ( t1 ≡ leaf ) ∨ ( node-key t1 ≡ just key) → top-value t1 ≡ just value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 628
diff changeset
289 lemma7 = {!!}
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
290