Mercurial > hg > Gears > GearsAgda
annotate stackTest.agda @ 538:5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
author | ryokka |
---|---|
date | Wed, 10 Jan 2018 17:38:24 +0900 |
parents | fffeaf0b0024 |
children | 429ece770187 |
rev | line source |
---|---|
537 | 1 open import Level renaming (suc to succ ; zero to Zero ) |
2 module stackTest where | |
3 | |
4 open import stack | |
5 | |
6 open import Relation.Binary.PropositionalEquality | |
7 open import Relation.Binary.Core | |
8 open import Data.Nat | |
9 | |
10 | |
11 open SingleLinkedStack | |
12 open Stack | |
13 | |
14 ---- | |
15 -- | |
16 -- proof of properties ( concrete cases ) | |
17 -- | |
18 | |
19 test01 : {n : Level } {a : Set n} -> SingleLinkedStack a -> Maybe a -> Bool {n} | |
20 test01 stack _ with (top stack) | |
21 ... | (Just _) = True | |
22 ... | Nothing = False | |
23 | |
24 | |
25 test02 : {n : Level } {a : Set n} -> SingleLinkedStack a -> Bool | |
26 test02 stack = popSingleLinkedStack stack test01 | |
27 | |
28 test03 : {n : Level } {a : Set n} -> a -> Bool | |
29 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02 | |
30 | |
31 -- after a push and a pop, the stack is empty | |
32 lemma : {n : Level} {A : Set n} {a : A} -> test03 a ≡ False | |
33 lemma = refl | |
34 | |
35 testStack01 : {n m : Level } {a : Set n} -> a -> Bool {m} | |
36 testStack01 v = pushStack createSingleLinkedStack v ( | |
37 \s -> popStack s (\s1 d1 -> True)) | |
38 | |
39 -- after push 1 and 2, pop2 get 1 and 2 | |
40 | |
41 testStack02 : {m : Level } -> ( Stack ℕ (SingleLinkedStack ℕ) -> Bool {m} ) -> Bool {m} | |
42 testStack02 cs = pushStack createSingleLinkedStack 1 ( | |
43 \s -> pushStack s 2 cs) | |
44 | |
45 | |
46 testStack031 : (d1 d2 : ℕ ) -> Bool {Zero} | |
47 testStack031 2 1 = True | |
48 testStack031 _ _ = False | |
49 | |
50 testStack032 : (d1 d2 : Maybe ℕ) -> Bool {Zero} | |
51 testStack032 (Just d1) (Just d2) = testStack031 d1 d2 | |
52 testStack032 _ _ = False | |
53 | |
54 testStack03 : {m : Level } -> Stack ℕ (SingleLinkedStack ℕ) -> ((Maybe ℕ) -> (Maybe ℕ) -> Bool {m} ) -> Bool {m} | |
55 testStack03 s cs = pop2Stack s ( | |
56 \s d1 d2 -> cs d1 d2 ) | |
57 | |
58 testStack04 : Bool | |
59 testStack04 = testStack02 (\s -> testStack03 s testStack032) | |
60 | |
61 testStack05 : testStack04 ≡ True | |
62 testStack05 = refl | |
63 | |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
64 testStack06 : {m : Level } -> Maybe (Element ℕ) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
65 testStack06 = pushStack createSingleLinkedStack 1 ( |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
66 \s -> pushStack s 2 (\s -> top (stack s))) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
67 |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
68 testStack07 : {m : Level } -> Maybe (Element ℕ) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
69 testStack07 = pushSingleLinkedStack emptySingleLinkedStack 1 ( |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
70 \s -> pushSingleLinkedStack s 2 (\s -> top s)) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
71 |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
72 |
537 | 73 ------ |
74 -- | |
75 -- proof of properties with indefinite state of stack | |
76 -- | |
77 -- this should be proved by properties of the stack inteface, not only by the implementation, | |
78 -- and the implementation have to provides the properties. | |
79 -- | |
80 -- we cannot write "s ≡ s3", since level of the Set does not fit , but use stack s ≡ stack s3 is ok. | |
81 -- anyway some implementations may result s != s3 | |
82 -- | |
83 | |
84 stackInSomeState : {l m : Level } {D : Set l} {t : Set m } (s : SingleLinkedStack D ) -> Stack {l} {m} D {t} ( SingleLinkedStack D ) | |
85 stackInSomeState s = record { stack = s ; stackMethods = singleLinkedStackSpec } | |
86 | |
87 push->push->pop2 : {l : Level } {D : Set l} (x y : D ) (s : SingleLinkedStack D ) -> | |
88 pushStack ( stackInSomeState s ) x ( \s1 -> pushStack s1 y ( \s2 -> pop2Stack s2 ( \s3 y1 x1 -> (Just x ≡ x1 ) ∧ (Just y ≡ y1 ) ) )) | |
89 push->push->pop2 {l} {D} x y s = record { pi1 = refl ; pi2 = refl } | |
90 | |
91 | |
92 id : {n : Level} {A : Set n} -> A -> A | |
93 id a = a | |
94 | |
95 -- push a, n times | |
96 | |
97 n-push : {n : Level} {A : Set n} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A | |
98 n-push zero s = s | |
99 n-push {l} {A} {a} (suc n) s = pushSingleLinkedStack (n-push {l} {A} {a} n s) a (\s -> s ) | |
100 | |
101 n-pop : {n : Level}{A : Set n} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A | |
102 n-pop zero s = s | |
103 n-pop {_} {A} {a} (suc n) s = popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ -> s ) | |
104 | |
105 open ≡-Reasoning | |
106 | |
107 push-pop-equiv : {n : Level} {A : Set n} {a : A} (s : SingleLinkedStack A) -> (popSingleLinkedStack (pushSingleLinkedStack s a (\s -> s)) (\s _ -> s) ) ≡ s | |
108 push-pop-equiv s = refl | |
109 | |
110 push-and-n-pop : {n : Level} {A : Set n} {a : A} (n : ℕ) (s : SingleLinkedStack A) -> n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id) ≡ n-pop {_} {A} {a} n s | |
111 push-and-n-pop zero s = refl | |
112 push-and-n-pop {_} {A} {a} (suc n) s = begin | |
113 n-pop {_} {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id) | |
114 ≡⟨ refl ⟩ | |
115 popSingleLinkedStack (n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s) | |
116 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s )) (push-and-n-pop n s) ⟩ | |
117 popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ -> s) | |
118 ≡⟨ refl ⟩ | |
119 n-pop {_} {A} {a} (suc n) s | |
120 ∎ | |
121 | |
122 | |
123 n-push-pop-equiv : {n : Level} {A : Set n} {a : A} (n : ℕ) (s : SingleLinkedStack A) -> (n-pop {_} {A} {a} n (n-push {_} {A} {a} n s)) ≡ s | |
124 n-push-pop-equiv zero s = refl | |
125 n-push-pop-equiv {_} {A} {a} (suc n) s = begin | |
126 n-pop {_} {A} {a} (suc n) (n-push (suc n) s) | |
127 ≡⟨ refl ⟩ | |
128 n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s)) | |
129 ≡⟨ push-and-n-pop n (n-push n s) ⟩ | |
130 n-pop {_} {A} {a} n (n-push n s) | |
131 ≡⟨ n-push-pop-equiv n s ⟩ | |
132 s | |
133 ∎ | |
134 | |
135 | |
136 n-push-pop-equiv-empty : {n : Level} {A : Set n} {a : A} -> (n : ℕ) -> n-pop {_} {A} {a} n (n-push {_} {A} {a} n emptySingleLinkedStack) ≡ emptySingleLinkedStack | |
137 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack |