annotate stackTest.agda @ 537:fffeaf0b0024

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