Mercurial > hg > Members > Moririn
annotate redBlackTreeTest.agda @ 550:2476f7123dc3
root = Nothing case passed on putTest1
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 17 Jan 2018 17:31:41 +0900 |
parents | bc3208d510cd |
children | 8bc39f95c961 |
rev | line source |
---|---|
537 | 1 module redBlackTreeTest where |
2 | |
3 open import RedBlackTree | |
4 open import stack | |
5 open import Level hiding (zero) | |
6 | |
7 open import Data.Nat | |
8 | |
9 open Tree | |
10 open Node | |
11 open RedBlackTree.RedBlackTree | |
12 open Stack | |
13 | |
14 -- tests | |
15 | |
543 | 16 putTree1 : {n m : Level } {a k : Set n} {t : Set m} -> RedBlackTree {n} {m} {t} a k -> k -> a -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
17 putTree1 {n} {m} {a} {k} {t} tree k1 value next with (root tree) | |
537 | 18 ... | Nothing = next (record tree {root = Just (leafNode k1 value) }) |
543 | 19 ... | Just n2 = clearSingleLinkedStack (nodeStack tree) (\ s -> findNode tree s (leafNode k1 value) n2 (\ tree1 s n1 -> replaceNode tree1 s n1 next)) |
537 | 20 |
21 open import Relation.Binary.PropositionalEquality | |
22 open import Relation.Binary.Core | |
23 open import Function | |
24 | |
25 | |
26 check1 : {m : Level } (n : Maybe (Node ℕ ℕ)) -> ℕ -> Bool {m} | |
27 check1 Nothing _ = False | |
28 check1 (Just n) x with Data.Nat.compare (value n) x | |
29 ... | equal _ = True | |
30 ... | _ = False | |
31 | |
549 | 32 check2 : {m : Level } (n : Maybe (Node ℕ ℕ)) -> ℕ -> Bool {m} |
33 check2 Nothing _ = False | |
34 check2 (Just n) x with compare2 (value n) x | |
35 ... | EQ = True | |
36 ... | _ = False | |
37 | |
38 test1 : putTree1 {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ {Set Level.zero} ) 1 1 ( \t -> getRedBlackTree t 1 ( \t x -> check2 x 1 ≡ True )) | |
537 | 39 test1 = refl |
40 | |
41 test2 : putTree1 {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ {Set Level.zero} ) 1 1 ( | |
42 \t -> putTree1 t 2 2 ( | |
43 \t -> getRedBlackTree t 1 ( | |
549 | 44 \t x -> check2 x 1 ≡ True ))) |
537 | 45 test2 = refl |
46 | |
545 | 47 open ≡-Reasoning |
542 | 48 test3 : putTree1 {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ {Set Level.zero}) 1 1 |
49 $ \t -> putTree1 t 2 2 | |
50 $ \t -> putTree1 t 3 3 | |
544 | 51 $ \t -> putTree1 t 4 4 |
545 | 52 $ \t -> getRedBlackTree t 1 |
549 | 53 $ \t x -> check2 x 1 ≡ True |
545 | 54 test3 = begin |
549 | 55 check2 (Just (record {key = 1 ; value = 1 ; color = Black ; left = Nothing ; right = Just (leafNode 2 2)})) 1 |
545 | 56 ≡⟨ refl ⟩ |
57 True | |
58 ∎ | |
542 | 59 |
60 test31 = putTree1 {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ ) 1 1 | |
61 $ \t -> putTree1 t 2 2 | |
62 $ \t -> putTree1 t 3 3 | |
63 $ \t -> putTree1 t 4 4 | |
544 | 64 $ \t -> getRedBlackTree t 4 |
65 $ \t x -> x | |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
66 |
540 | 67 -- test5 : Maybe (Node ℕ ℕ) |
541 | 68 test5 = putTree1 {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ ) 4 4 |
69 $ \t -> putTree1 t 6 6 | |
543 | 70 $ \t0 -> clearSingleLinkedStack (nodeStack t0) |
541 | 71 $ \s -> findNode1 t0 s (leafNode 3 3) ( root t0 ) |
540 | 72 $ \t1 s n1 -> replaceNode t1 s n1 |
539 | 73 $ \t -> getRedBlackTree t 3 |
541 | 74 -- $ \t x -> SingleLinkedStack.top (stack s) |
540 | 75 -- $ \t x -> n1 |
541 | 76 $ \t x -> root t |
540 | 77 where |
543 | 78 findNode1 : {n m : Level } {a k : Set n} {t : Set m} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> (Node a k) -> (Maybe (Node a k)) -> (RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Node a k -> t) -> t |
540 | 79 findNode1 t s n1 Nothing next = next t s n1 |
80 findNode1 t s n1 ( Just n2 ) next = findNode t s n1 n2 next | |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
81 |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
82 -- test51 : putTree1 {_} {_} {ℕ} {ℕ} {_} {Maybe (Node ℕ ℕ)} (createEmptyRedBlackTreeℕ ℕ {Set Level.zero} ) 1 1 $ \t -> |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
83 -- putTree1 t 2 2 $ \t -> putTree1 t 3 3 $ \t -> root t ≡ Just (record { key = 1; value = 1; left = Just (record { key = 2 ; value = 2 } ); right = Nothing} ) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
84 -- test51 = refl |
539 | 85 |
86 test6 : Maybe (Node ℕ ℕ) | |
87 test6 = root (createEmptyRedBlackTreeℕ {_} ℕ {Maybe (Node ℕ ℕ)}) | |
88 | |
89 | |
90 test7 : Maybe (Node ℕ ℕ) | |
543 | 91 test7 = clearSingleLinkedStack (nodeStack tree2) (\ s -> replaceNode tree2 s n2 (\ t -> root t)) |
539 | 92 where |
93 tree2 = createEmptyRedBlackTreeℕ {_} ℕ {Maybe (Node ℕ ℕ)} | |
94 k1 = 1 | |
95 n2 = leafNode 0 0 | |
96 value1 = 1 | |
97 | |
98 test8 : Maybe (Node ℕ ℕ) | |
99 test8 = putTree1 {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ) 1 1 | |
100 $ \t -> putTree1 t 2 2 (\ t -> root t) | |
544 | 101 |
102 | |
549 | 103 test9 : putRedBlackTree {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ {Set Level.zero} ) 1 1 ( \t -> getRedBlackTree t 1 ( \t x -> check2 x 1 ≡ True )) |
544 | 104 test9 = refl |
105 | |
106 test10 : putRedBlackTree {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ {Set Level.zero} ) 1 1 ( | |
107 \t -> putRedBlackTree t 2 2 ( | |
108 \t -> getRedBlackTree t 1 ( | |
549 | 109 \t x -> check2 x 1 ≡ True ))) |
544 | 110 test10 = refl |
111 | |
112 test11 = putRedBlackTree {_} {_} {ℕ} {ℕ} (createEmptyRedBlackTreeℕ ℕ) 1 1 | |
113 $ \t -> putRedBlackTree t 2 2 | |
114 $ \t -> putRedBlackTree t 3 3 | |
115 $ \t -> getRedBlackTree t 2 | |
116 $ \t x -> root t | |
545 | 117 |
118 | |
119 redBlackInSomeState :{ m : Level } (a : Set Level.zero) (n : Maybe (Node a ℕ)) {t : Set m} -> RedBlackTree {Level.zero} {m} {t} a ℕ | |
549 | 120 redBlackInSomeState {m} a n {t} = record { root = n ; nodeStack = emptySingleLinkedStack ; compare = compare2 } |
545 | 121 |
549 | 122 -- compare2 : (x y : ℕ ) -> CompareResult {Level.zero} |
123 -- compare2 zero zero = EQ | |
124 -- compare2 (suc _) zero = GT | |
125 -- compare2 zero (suc _) = LT | |
126 -- compare2 (suc x) (suc y) = compare2 x y | |
546 | 127 |
548 | 128 putTest1Lemma2 : (k : ℕ) -> compare2 k k ≡ EQ |
129 putTest1Lemma2 zero = refl | |
130 putTest1Lemma2 (suc k) = putTest1Lemma2 k | |
546 | 131 |
548 | 132 putTest1Lemma1 : (x y : ℕ) -> compareℕ x y ≡ compare2 x y |
133 putTest1Lemma1 zero zero = refl | |
134 putTest1Lemma1 (suc m) zero = refl | |
135 putTest1Lemma1 zero (suc n) = refl | |
136 putTest1Lemma1 (suc m) (suc n) with Data.Nat.compare m n | |
137 putTest1Lemma1 (suc .m) (suc .(Data.Nat.suc m + k)) | less m k = lemma1 m | |
138 where | |
139 lemma1 : (m : ℕ) -> LT ≡ compare2 m (ℕ.suc (m + k)) | |
140 lemma1 zero = refl | |
141 lemma1 (suc y) = lemma1 y | |
142 putTest1Lemma1 (suc .m) (suc .m) | equal m = lemma1 m | |
143 where | |
144 lemma1 : (m : ℕ) -> EQ ≡ compare2 m m | |
145 lemma1 zero = refl | |
146 lemma1 (suc y) = lemma1 y | |
147 putTest1Lemma1 (suc .(Data.Nat.suc m + k)) (suc .m) | greater m k = lemma1 m | |
148 where | |
149 lemma1 : (m : ℕ) -> GT ≡ compare2 (ℕ.suc (m + k)) m | |
150 lemma1 zero = refl | |
151 lemma1 (suc y) = lemma1 y | |
152 | |
153 putTest1Lemma3 : (k : ℕ) -> compareℕ k k ≡ EQ | |
154 putTest1Lemma3 k = trans (putTest1Lemma1 k k) ( putTest1Lemma2 k ) | |
546 | 155 |
550
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
156 compareLemma1 : (x y : ℕ) -> compare2 x y ≡ EQ -> x ≡ y |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
157 compareLemma1 zero zero refl = refl |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
158 compareLemma1 zero (suc _) () |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
159 compareLemma1 (suc _) zero () |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
160 compareLemma1 (suc x) (suc y) eq = cong ( \z -> ℕ.suc z ) ( compareLemma1 x y ( trans lemma2 eq ) ) |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
161 where |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
162 lemma2 : compare2 (ℕ.suc x) (ℕ.suc y) ≡ compare2 x y |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
163 lemma2 = refl |
549 | 164 |
546 | 165 |
545 | 166 putTest1 :{ m : Level } (n : Maybe (Node ℕ ℕ)) |
167 -> (k : ℕ) (x : ℕ) | |
168 -> putTree1 {_} {_} {ℕ} {ℕ} (redBlackInSomeState {_} ℕ n {Set Level.zero}) k x | |
549 | 169 (\ t -> getRedBlackTree t k (\ t x1 -> check2 x1 x ≡ True)) |
546 | 170 putTest1 n k x with n |
171 ... | Just n1 = {!!} | |
549 | 172 ... | Nothing = lemma1 |
173 where | |
174 lemma1 : getRedBlackTree {_} {_} {ℕ} {ℕ} {Set Level.zero} ( record { root = Just ( record { | |
175 key = k ; value = x ; right = Nothing ; left = Nothing ; color = Red | |
176 } ) ; nodeStack = record { top = Nothing } ; compare = λ x₁ y → compare2 x₁ y } ) k | |
177 ( \ t x1 -> check2 x1 x ≡ True) | |
550
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
178 lemma1 with compare2 k k | putTest1Lemma2 k |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
179 ... | EQ | refl with compare2 x x | putTest1Lemma2 x |
2476f7123dc3
root = Nothing case passed on putTest1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
549
diff
changeset
|
180 ... | EQ | refl = refl |