annotate work.agda @ 781:68904fdaab71

te
author Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
date Mon, 10 Jul 2023 19:59:14 +0900
parents
children dca93aef5e36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
781
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module work where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 open import Level hiding (suc ; zero ; _⊔_ )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Data.Nat hiding (compare)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Nat.Properties as NatProp
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import Data.Maybe
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 -- open import Data.Maybe.Properties
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Data.Empty
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Data.List
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 open import Data.Product
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 open import Function as F hiding (const)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 open import Relation.Binary
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 open import Relation.Binary.PropositionalEquality
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 open import Relation.Nullary
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 open import logic
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 data bt {n : Level} (A : Set n) : Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 leaf : bt A
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 node : (key : ℕ) → (value : A) → (left : bt A) → (right : bt A) → bt A
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 node-key : {n : Level}{A : Set n} → bt A → Maybe ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 node-key leaf = nothing
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 node-key (node key value tree tree₁) = just key
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 node-value : {n : Level} {A : Set n} → bt A → Maybe A
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 node-value leaf = nothing
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 node-value (node key value tree tree₁) = just value
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 bt-depth : {n : Level} {A : Set n} → (tree : bt A) → ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 bt-depth leaf = 0
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 bt-depth (node key value tree tree₁) = suc (bt-depth tree ⊔ bt-depth tree₁)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 --一番下のleaf =0から戻るたびにsucをしていく
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 treeTest1 : bt ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 treeTest1 = node 0 0 leaf (node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 -- 0 0
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 -- / \
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 -- leaf 3 1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 -- / \
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 -- 2 5 2
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 -- / \
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 -- 1 leaf 3
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 -- / \
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 -- leaf leaf 4
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 treeTest2 : bt ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 treeTest2 = node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 testdb : ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 testdb = bt-depth treeTest1 -- 4
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 t-leaf : treeInvariant leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 t-left : {key key1 : ℕ} → {value value1 : A} → {t1 t2 : bt A} → key < key1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 → treeInvariant (node key value t1 t2)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 → treeInvariant (node key1 value1 (node key value t1 t2) leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 t-right : {key key1 : ℕ} → {value value1 : A} → {t1 t2 : bt A} → key < key1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 → treeInvariant (node key1 value1 t1 t2)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 → treeInvariant (node key value leaf (node key1 value1 t1 t2))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 t-node : {key key1 key2 : ℕ}→ {value value1 value2 : A} → {t1 t2 t3 t4 : bt A} → key1 < key → key < key2
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 → treeInvariant (node key1 value1 t1 t2)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 → treeInvariant (node key2 value2 t3 t4)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 → treeInvariant (node key value (node key1 value1 t1 t2) (node key2 value2 t3 t4))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 data stackInvariant {n : Level} {A : Set n} (key : ℕ ) : (top orig : bt A)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 → (stack : List (bt A)) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 s-nil : {tree0 : bt A} → stackInvariant key tree0 tree0 (tree0 ∷ [] )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 s-right : {key1 : ℕ } → {value : A } → {tree0 t1 t2 : bt A } → {st : List (bt A)}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 → key1 < key
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 → stackInvariant key (node key1 value t1 t2) tree0 st
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 → stackInvariant key t2 tree0 (t2 ∷ st)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 s-left : {key1 : ℕ } → {value : A } → {tree0 t1 t2 : bt A } → {st : List (bt A)}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 → key < key1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 → stackInvariant key (node key1 value t1 t2) tree0 st
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 → stackInvariant key t1 tree0 (t1 ∷ st)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 data replacedTree {n : Level } {A : Set n} (key : ℕ) (value : A) : (before after : bt A) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 r-leaf : replacedTree key value leaf (node key value leaf leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 r-node : {value1 : A} → {left right : bt A} → replacedTree key value (node key value left right) (node key value1 left right)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 -- key is the repl's key , so need comp key and key1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 r-left : {key1 : ℕ} {value1 : A }→ {left right repl : bt A} → key < key1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 → replacedTree key value left repl → replacedTree key value (node key1 value1 left right) (node key1 value1 repl right)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 r-right : {key1 : ℕ } {value1 : A} → {left right repl : bt A} → key1 < key
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 → replacedTree key value right repl → replacedTree key value (node key1 value1 left right) (node key1 value1 left repl)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 {-
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 RTtoTI0 : {n : Level} {A : Set n } → (key : ℕ ) → (value : A) → (tree repl : bt A)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 → treeInvariant tree → replacedTree key value tree repl → treeInvariant repl
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 RTtoTI0 key value leaf (node key value leaf leaf) tr r-leaf = t-single key value
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 RTtoTI0 key value (node key₁ value₁ tree tree₁) (node key₂ value₂ repl repl₁) (t-node x x₁ s s₁) r-node = t-node x x₁ s s₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 -}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 depth-1< {i} {j} = s≤s (m≤m⊔n _ j)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 depth-2< {i} {j} = s≤s (m≤n⊔m j i)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 depth-3< : {i : ℕ } → suc i ≤ suc (suc i)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 depth-3< {zero} = s≤s ( z≤n )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 depth-3< {suc i} = s≤s (depth-3< {i} )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 treeLeftDown : {n : Level} {A : Set n} {key : ℕ} {value : A} → (tleft tright : bt A)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 → treeInvariant (node key value tleft tright)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 → treeInvariant tleft
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 treeLeftDown leaf leaf (t-single key value) = t-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 treeLeftDown leaf (node key value t1 t2) (t-right x ti) = t-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 treeLeftDown (node key value t t₁) leaf (t-left x ti) = ti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 treeLeftDown (node key value t t₁) (node key₁ value₁ t1 t2) (t-node x x1 ti ti2 ) = ti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 treeRightDown : {n : Level} {A : Set n} {key : ℕ} {value : A} → (tleft tright : bt A)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 → treeInvariant (node key value tleft tright)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 → treeInvariant tright
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 treeRightDown leaf leaf (t-single key value) = t-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 treeRightDown leaf (node key value t1 t2) (t-right x ti) = ti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 treeRightDown (node key value t t₁) leaf (t-left x ti) = t-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 treeRightDown (node key value t t₁) (node key₁ value₁ t1 t2) (t-node x x1 ti ti2 ) = ti2
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 findP : {n m : Level} {A : Set n} {t : Set n} → (tkey : ℕ) → (top orig : bt A) → (st : List (bt A))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 → (treeInvariant top)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 → stackInvariant tkey top orig st
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 → (next : (newtop : bt A) → (stack : List (bt A))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 → (treeInvariant newtop)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 → (stackInvariant tkey newtop orig stack)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 → bt-depth newtop < bt-depth top → t)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 → (exit : (newtop : bt A) → (stack : List (bt A))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 → (treeInvariant newtop)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 → (stackInvariant tkey newtop orig stack) --need new stack ?
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 → (newtop ≡ leaf) ∨ (node-key newtop ≡ just tkey) → t)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 → t
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 findP tkey leaf orig st ti si next exit = exit leaf st ti si (case1 refl)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 findP tkey (node key value tl tr) orig st ti si next exit with <-cmp tkey key
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 findP tkey top orig st ti si next exit | tri≈ ¬a refl ¬c = exit top st ti si (case2 refl)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 findP tkey (node key value tl tr) orig st ti si next exit | tri< a ¬b ¬c = next tl (tl ∷ st) (treeLeftDown tl tr ti) (s-left a si) (s≤s (m≤m⊔n (bt-depth tl) (bt-depth tr)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 findP tkey (node key value tl tr) orig st ti si next exit | tri> ¬a ¬b c = next tr (tr ∷ st) (treeRightDown tl tr ti) (s-right c si) (s≤s (m≤n⊔m (bt-depth tl) (bt-depth tr)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 --RBT
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 data Color : Set where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 Red : Color
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 Black : Color
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 RB→bt : {n : Level} (A : Set n) → (bt (Color ∧ A)) → bt A
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 RB→bt {n} A leaf = leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 RB→bt {n} A (node key ⟪ C , value ⟫ tr t1) = (node key value (RB→bt A tr) (RB→bt A t1))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 color : {n : Level} {A : Set n} → (bt (Color ∧ A)) → Color
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 color leaf = Black
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 color (node key ⟪ C , value ⟫ rb rb₁) = C
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 black-depth : {n : Level} {A : Set n} → (tree : bt (Color ∧ A) ) → ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 black-depth leaf = 0
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 black-depth (node key ⟪ Red , value ⟫ t t₁) = black-depth t ⊔ black-depth t₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 black-depth (node key ⟪ Black , value ⟫ t t₁) = suc (black-depth t ⊔ black-depth t₁ )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 data RBtreeInvariant {n : Level} {A : Set n} : (tree : bt (Color ∧ A)) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 rb-leaf : RBtreeInvariant leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 rb-single : (key : ℕ) → (value : A) → RBtreeInvariant (node key ⟪ Black , value ⟫ leaf leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 rb-right-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 → black-depth t ≡ black-depth t₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 → RBtreeInvariant (node key ⟪ Red , value ⟫ leaf (node key₁ ⟪ Black , value₁ ⟫ t t₁))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 rb-right-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {c : Color}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 → black-depth t ≡ black-depth t₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 → RBtreeInvariant (node key ⟪ Black , value ⟫ leaf (node key₁ ⟪ c , value₁ ⟫ t t₁))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 rb-left-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 → black-depth t ≡ black-depth t₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 → RBtreeInvariant (node key ⟪ Red , value ⟫ (node key₁ ⟪ Black , value₁ ⟫ t t₁) leaf )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 rb-left-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {c : Color}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 → black-depth t ≡ black-depth t₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 → RBtreeInvariant (node key ⟪ Black , value ⟫ (node key₁ ⟪ c , value₁ ⟫ t t₁) leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 rb-node-red : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 → black-depth t₁ ≡ black-depth t₂
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 → RBtreeInvariant (node key ⟪ Black , value ⟫ t₁ t₂)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 → black-depth t₃ ≡ black-depth t₄
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 → RBtreeInvariant (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 → RBtreeInvariant (node key₁ ⟪ Red , value₁ ⟫ (node key ⟪ Black , value ⟫ t₁ t₂) (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 rb-node-black : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 → {c c₁ : Color}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 → black-depth t₁ ≡ black-depth t₂
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 → RBtreeInvariant (node key ⟪ c , value ⟫ t₁ t₂)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 → black-depth t₃ ≡ black-depth t₄
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 → RBtreeInvariant (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ (node key ⟪ c , value ⟫ t₁ t₂) (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
202
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 data rotatedTree {n : Level} {A : Set n} : (before after : bt A) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 rtt-node : {t : bt A } → rotatedTree t t
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 -- a b
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 -- b c d a
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 -- d e e c
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 --
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 rtt-right : {ka kb kc kd ke : ℕ} {va vb vc vd ve : A} → {c d e c1 d1 e1 : bt A} → {ctl ctr dtl dtr etl etr : bt A}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 --kd < kb < ke < ka< kc
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 → {ctl1 ctr1 dtl1 dtr1 etl1 etr1 : bt A}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 → kd < kb → kb < ke → ke < ka → ka < kc
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 → rotatedTree (node ke ve etl etr) (node ke ve etl1 etr1)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 → rotatedTree (node kd vd dtl dtr) (node kd vd dtl1 dtr1)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 → rotatedTree (node kc vc ctl ctr) (node kc vc ctl1 ctr1)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 → rotatedTree (node ka va (node kb vb (node kd vd dtl dtr) (node ke ve etl etr)) (node kc vc ctl ctr))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 (node kb vb (node kd vd dtl1 dtr1) (node ka va (node ke ve etl1 etr1) (node kc vc ctl1 ctr1)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 rtt-left : {ka kb kc kd ke : ℕ} {va vb vc vd ve : A} → {c d e c1 d1 e1 : bt A} → {ctl ctr dtl dtr etl etr : bt A}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 --kd < kb < ke < ka< kc
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 → {ctl1 ctr1 dtl1 dtr1 etl1 etr1 : bt A} -- after child
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 → kd < kb → kb < ke → ke < ka → ka < kc
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 → rotatedTree (node ke ve etl etr) (node ke ve etl1 etr1)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 → rotatedTree (node kd vd dtl dtr) (node kd vd dtl1 dtr1)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 → rotatedTree (node kc vc ctl ctr) (node kc vc ctl1 ctr1)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 → rotatedTree (node kb vb (node kd vd dtl1 dtr1) (node ka va (node ke ve etl1 etr1) (node kc vc ctl1 ctr1)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 (node ka va (node kb vb (node kd vd dtl dtr) (node ke ve etl etr)) (node kc vc ctl ctr))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 RBtreeLeftDown : {n : Level} {A : Set n} {key : ℕ} {value : A} {c : Color}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 → (tleft tright : bt (Color ∧ A))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 → RBtreeInvariant (node key ⟪ c , value ⟫ tleft tright)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 → RBtreeInvariant tleft
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 RBtreeLeftDown leaf leaf (rb-single k1 v) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 RBtreeLeftDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-red x bde rbti) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 RBtreeLeftDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-black x bde rbti) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 RBtreeLeftDown leaf (node key ⟪ Red , value ⟫ t1 t2 ) (rb-right-black x bde rbti)= rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-black x bde ti) = ti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-red x bde ti)= ti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 RBtreeLeftDown (node key ⟪ Red , value ⟫ t t₁) leaf (rb-left-black x bde ti) = ti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = til
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-red x x1 bde1 til bde2 tir) = til
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 RBtreeLeftDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = til
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 RBtreeLeftDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = til
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 RBtreeLeftDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = til
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
246
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 RBtreeRightDown : {n : Level} {A : Set n} { key : ℕ} {value : A} {c : Color}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 → (tleft tright : bt (Color ∧ A))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 → RBtreeInvariant (node key ⟪ c , value ⟫ tleft tright)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 → RBtreeInvariant tright
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 RBtreeRightDown leaf leaf (rb-single k1 v1 ) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 RBtreeRightDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-red x bde rbti) = rbti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 RBtreeRightDown leaf (node key ⟪ Black , value ⟫ t1 t2 ) (rb-right-black x bde rbti) = rbti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 RBtreeRightDown leaf (node key ⟪ Red , value ⟫ t1 t2 ) (rb-right-black x bde rbti)= rbti
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-black x bde ti) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) leaf (rb-left-red x bde ti) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 RBtreeRightDown (node key ⟪ Red , value ⟫ t t₁) leaf (rb-left-black x bde ti) = rb-leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = tir
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-red x x1 bde1 til bde2 tir) = tir
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 RBtreeRightDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Black , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = tir
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 RBtreeRightDown (node key ⟪ Black , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = tir
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 RBtreeRightDown (node key ⟪ Red , value ⟫ t t₁) (node key₁ ⟪ Red , value1 ⟫ t1 t2) (rb-node-black x x1 bde1 til bde2 tir) = tir
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
263
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 findRBT : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt (Color ∧ A) )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 → (stack : List (bt (Color ∧ A)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 → treeInvariant tree ∧ stackInvariant key tree tree0 stack
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 → RBtreeInvariant tree
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 → (next : (tree1 : bt (Color ∧ A) ) → (stack : List (bt (Color ∧ A)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 → RBtreeInvariant tree1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 → bt-depth tree1 < bt-depth tree → t )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 → (exit : (tree1 : bt (Color ∧ A)) → (stack : List (bt (Color ∧ A)))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 → RBtreeInvariant tree1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 findRBT key leaf tree0 stack ti rb0 next exit = exit leaf stack ti rb0 (case1 refl)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 findRBT key n@(node key₁ value left right) tree0 stack ti rb0 next exit with <-cmp key key₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 findRBT key (node key₁ value left right) tree0 stack ti rb0 next exit | tri< a ¬b ¬c
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 = next left (left ∷ stack) ⟪ treeLeftDown left right (_∧_.proj1 ti) , s-left a (_∧_.proj2 ti) ⟫ (RBtreeLeftDown left right rb0) depth-1<
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 findRBT key n tree0 stack ti rb0 _ exit | tri≈ ¬a refl ¬c = exit n stack ti rb0 (case2 refl)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 findRBT key (node key₁ value left right) tree0 stack ti rb0 next exit | tri> ¬a ¬b c
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 = next right (right ∷ stack) ⟪ treeRightDown left right (_∧_.proj1 ti), s-right c (_∧_.proj2 ti) ⟫ (RBtreeRightDown left right rb0) depth-2<
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
283
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 child-replaced : {n : Level} {A : Set n} (key : ℕ) (tree : bt A) → bt A
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 child-replaced key leaf = leaf
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 child-replaced key (node key₁ value left right) with <-cmp key key₁
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 ... | tri< a ¬b ¬c = left
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 ... | tri≈ ¬a b ¬c = node key₁ value left right
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 ... | tri> ¬a ¬b c = right
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 data replacedRBTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt (Color ∧ A) ) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 rbr-leaf : {ca cb : Color} → replacedRBTree key value leaf (node key ⟪ cb , value ⟫ leaf leaf)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 rbr-node : {value₁ : A} → {ca cb : Color } → {t t₁ : bt (Color ∧ A)}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 → replacedRBTree key value (node key ⟪ ca , value₁ ⟫ t t₁) (node key ⟪ cb , value ⟫ t t₁)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 rbr-right : {k : ℕ } {v1 : A} → {ca cb : Color} → {t t1 t2 : bt (Color ∧ A)}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 → k < key → replacedRBTree key value t2 t → replacedRBTree key value (node k ⟪ ca , v1 ⟫ t1 t2) (node k ⟪ cb , v1 ⟫ t1 t)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 rbr-left : {k : ℕ } {v1 : A} → {ca cb : Color} → {t t1 t2 : bt (Color ∧ A)}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 → k < key → replacedRBTree key value t1 t → replacedRBTree key value (node k ⟪ ca , v1 ⟫ t1 t2) (node k ⟪ cb , v1 ⟫ t t2)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
300
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 data ParentGrand {n : Level} {A : Set n} (self : bt A) : (parent uncle grand : bt A) → Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 s2-s1p2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 → parent ≡ node kp vp self n1 → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 s2-1sp2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 → parent ≡ node kp vp n1 self → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 s2-s12p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 → parent ≡ node kp vp self n1 → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 s2-1s2p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 → parent ≡ node kp vp n1 self → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
310
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 record PG {n : Level } (A : Set n) (self : bt A) (stack : List (bt A)) : Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 field
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 parent grand uncle : bt A
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 pg : ParentGrand self parent uncle grand
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 rest : List (bt A)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 stack=gp : stack ≡ ( self ∷ parent ∷ grand ∷ rest )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
317
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 record RBI {n : Level} {A : Set n} (key : ℕ) (value : A) (orig repl : bt (Color ∧ A) ) (stack : List (bt (Color ∧ A))) : Set n where
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 field
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 od d rd : ℕ
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 tree rot : bt (Color ∧ A)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 origti : treeInvariant orig
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 origrb : RBtreeInvariant orig
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 treerb : RBtreeInvariant tree
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 replrb : RBtreeInvariant repl
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 d=rd : ( d ≡ rd ) ∨ ((suc d ≡ rd ) ∧ (color tree ≡ Red))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 si : stackInvariant key tree orig stack
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 rotated : rotatedTree tree rot
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 ri : replacedRBTree key value (child-replaced key rot ) repl
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
330
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
331
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 {-
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 rbi-case1 : {n : Level} {A : Set n} → {key : ℕ} → {value : A} → (parent repl : bt (Color ∧ A) )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 → RBtreeInvariant parent
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 → RBtreeInvariant repl
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 → {left right : bt (Color ∧ A)} → parent ≡ node key ⟪ Black , value ⟫ left right
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 → (color right ≡ Red → RBtreeInvariant (node key ⟪ Black , value ⟫ left repl ) )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 ∧ (color left ≡ Red → RBtreeInvariant (node key ⟪ Black , value ⟫ repl right ) )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 rbi-case1 {n} {A} {key} (node key1 ⟪ Black , value1 ⟫ l r) leaf rbip rbir left right x = {!!}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 rbi-case1 {n} {A} {key} parent (node key₁ value₁ tree1 tree2) rbi rb2 x = {!!}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
341
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 -}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 blackdepth≡ : {n : Level } {A : Set n} → {C : Color} {key : ℕ} {value : A} → (tree1 tree2 : bt (Color ∧ A))
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 → RBtreeInvariant tree1
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 → RBtreeInvariant tree2
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 → RBtreeInvariant (node key ⟪ C , value ⟫ tree1 tree2)
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 → black-depth tree1 ≡ black-depth tree2
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
348
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 blackdepth≡ leaf leaf ri1 ri2 rip = refl
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 blackdepth≡ leaf (node key value t2 t3) ri1 ri2 rip = {!!} --rip kara mitibiki daseru RBinvariant kara toreruka
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 blackdepth≡ (node key value t1 t3) leaf ri1 ri2 rip = {!!}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 blackdepth≡ (node key value t1 t3) (node key₁ value₁ t2 t4) ri1 ri2 rip = {!!}
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
353
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 rbi-case1 : {n : Level} {A : Set n} → {key : ℕ} → {value : A} → (parent repl : bt (Color ∧ A) )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 → RBtreeInvariant parent
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 → RBtreeInvariant repl
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 → (left right : bt (Color ∧ A)) → parent ≡ node key ⟪ Black , value ⟫ left right
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 → RBtreeInvariant left
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 → RBtreeInvariant right
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 → (color right ≡ Red → RBtreeInvariant (node key ⟪ Black , value ⟫ left repl ) ) ∧ (color left ≡ Red → RBtreeInvariant (node key ⟪ Black , value ⟫ repl right ) )
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 rbi-case1 {n} {A} {key} (node key1 ⟪ Black , value1 ⟫ l r) leaf rbip rbir (node key3 ⟪ Red , val3 ⟫ la ra) (node key4 ⟪ Red , val4 ⟫ lb rb) pa li ri
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 = ⟪ {!!} rb-left-black {!!} {!!} li , (λ x → rb-right-black {!!} {!!} ri) ⟫
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
363
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 --⟪ rb-left-black {!!} {!!} (RBtreeLeftDown left right rbip ) , {!!} ⟫
Moririn < Moririn@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 --rbi-case1 {n} {A} {key} parent (node key₁ value₁ tree1 tree2) rbi rb2 x = {!!}