0
|
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 open import Function
|
|
10
|
|
11
|
|
12 open SingleLinkedStack
|
|
13 open Stack
|
|
14
|
|
15 ----
|
|
16 --
|
|
17 -- proof of properties ( concrete cases )
|
|
18 --
|
|
19
|
5
|
20 test01 : {n : Level } {a : Set n} !$\rightarrow$! SingleLinkedStack a !$\rightarrow$! Maybe a !$\rightarrow$! Bool {n}
|
0
|
21 test01 stack _ with (top stack)
|
|
22 ... | (Just _) = True
|
|
23 ... | Nothing = False
|
|
24
|
|
25
|
5
|
26 test02 : {n : Level } {a : Set n} !$\rightarrow$! SingleLinkedStack a !$\rightarrow$! Bool
|
0
|
27 test02 stack = popSingleLinkedStack stack test01
|
|
28
|
5
|
29 test03 : {n : Level } {a : Set n} !$\rightarrow$! a !$\rightarrow$! Bool
|
0
|
30 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02
|
|
31
|
|
32 -- after a push and a pop, the stack is empty
|
5
|
33 lemma : {n : Level} {A : Set n} {a : A} !$\rightarrow$! test03 a !$\equiv$! False
|
0
|
34 lemma = refl
|
|
35
|
5
|
36 testStack01 : {n m : Level } {a : Set n} !$\rightarrow$! a !$\rightarrow$! Bool {m}
|
0
|
37 testStack01 v = pushStack createSingleLinkedStack v (
|
5
|
38 \s !$\rightarrow$! popStack s (\s1 d1 !$\rightarrow$! True))
|
0
|
39
|
|
40 -- after push 1 and 2, pop2 get 1 and 2
|
|
41
|
5
|
42 testStack02 : {m : Level } !$\rightarrow$! ( Stack !$\mathbb{N}$! (SingleLinkedStack !$\mathbb{N}$!) !$\rightarrow$! Bool {m} ) !$\rightarrow$! Bool {m}
|
0
|
43 testStack02 cs = pushStack createSingleLinkedStack 1 (
|
5
|
44 \s !$\rightarrow$! pushStack s 2 cs)
|
0
|
45
|
|
46
|
5
|
47 testStack031 : (d1 d2 : !$\mathbb{N}$! ) !$\rightarrow$! Bool {Zero}
|
0
|
48 testStack031 2 1 = True
|
|
49 testStack031 _ _ = False
|
|
50
|
5
|
51 testStack032 : (d1 d2 : Maybe !$\mathbb{N}$!) !$\rightarrow$! Bool {Zero}
|
0
|
52 testStack032 (Just d1) (Just d2) = testStack031 d1 d2
|
|
53 testStack032 _ _ = False
|
|
54
|
5
|
55 testStack03 : {m : Level } !$\rightarrow$! Stack !$\mathbb{N}$! (SingleLinkedStack !$\mathbb{N}$!) !$\rightarrow$! ((Maybe !$\mathbb{N}$!) !$\rightarrow$! (Maybe !$\mathbb{N}$!) !$\rightarrow$! Bool {m} ) !$\rightarrow$! Bool {m}
|
0
|
56 testStack03 s cs = pop2Stack s (
|
5
|
57 \s d1 d2 !$\rightarrow$! cs d1 d2 )
|
0
|
58
|
|
59 testStack04 : Bool
|
5
|
60 testStack04 = testStack02 (\s !$\rightarrow$! testStack03 s testStack032)
|
0
|
61
|
5
|
62 testStack05 : testStack04 !$\equiv$! True
|
0
|
63 testStack05 = refl
|
|
64
|
5
|
65 testStack06 : {m : Level } !$\rightarrow$! Maybe (Element !$\mathbb{N}$!)
|
0
|
66 testStack06 = pushStack createSingleLinkedStack 1 (
|
5
|
67 \s !$\rightarrow$! pushStack s 2 (\s !$\rightarrow$! top (stack s)))
|
0
|
68
|
5
|
69 testStack07 : {m : Level } !$\rightarrow$! Maybe (Element !$\mathbb{N}$!)
|
0
|
70 testStack07 = pushSingleLinkedStack emptySingleLinkedStack 1 (
|
5
|
71 \s !$\rightarrow$! pushSingleLinkedStack s 2 (\s !$\rightarrow$! top s))
|
0
|
72
|
|
73 testStack08 = pushSingleLinkedStack emptySingleLinkedStack 1
|
5
|
74 $ \s !$\rightarrow$! pushSingleLinkedStack s 2
|
|
75 $ \s !$\rightarrow$! pushSingleLinkedStack s 3
|
|
76 $ \s !$\rightarrow$! pushSingleLinkedStack s 4
|
|
77 $ \s !$\rightarrow$! pushSingleLinkedStack s 5
|
|
78 $ \s !$\rightarrow$! top s
|
0
|
79
|
|
80 ------
|
|
81 --
|
|
82 -- proof of properties with indefinite state of stack
|
|
83 --
|
|
84 -- this should be proved by properties of the stack inteface, not only by the implementation,
|
|
85 -- and the implementation have to provides the properties.
|
|
86 --
|
5
|
87 -- we cannot write "s !$\equiv$! s3", since level of the Set does not fit , but use stack s !$\equiv$! stack s3 is ok.
|
0
|
88 -- anyway some implementations may result s != s3
|
|
89 --
|
|
90
|
5
|
91 stackInSomeState : {l m : Level } {D : Set l} {t : Set m } (s : SingleLinkedStack D ) !$\rightarrow$! Stack {l} {m} D {t} ( SingleLinkedStack D )
|
0
|
92 stackInSomeState s = record { stack = s ; stackMethods = singleLinkedStackSpec }
|
|
93
|
5
|
94 push!$\rightarrow$!push!$\rightarrow$!pop2 : {l : Level } {D : Set l} (x y : D ) (s : SingleLinkedStack D ) !$\rightarrow$!
|
|
95 pushStack ( stackInSomeState s ) x ( \s1 !$\rightarrow$! pushStack s1 y ( \s2 !$\rightarrow$! pop2Stack s2 ( \s3 y1 x1 !$\rightarrow$! (Just x !$\equiv$! x1 ) !$\wedge$! (Just y !$\equiv$! y1 ) ) ))
|
|
96 push!$\rightarrow$!push!$\rightarrow$!pop2 {l} {D} x y s = record { pi1 = refl ; pi2 = refl }
|
0
|
97
|
|
98
|
5
|
99 -- id : {n : Level} {A : Set n} !$\rightarrow$! A !$\rightarrow$! A
|
0
|
100 -- id a = a
|
|
101
|
|
102 -- push a, n times
|
|
103
|
5
|
104 n-push : {n : Level} {A : Set n} {a : A} !$\rightarrow$! !$\mathbb{N}$! !$\rightarrow$! SingleLinkedStack A !$\rightarrow$! SingleLinkedStack A
|
0
|
105 n-push zero s = s
|
5
|
106 n-push {l} {A} {a} (suc n) s = pushSingleLinkedStack (n-push {l} {A} {a} n s) a (\s !$\rightarrow$! s )
|
0
|
107
|
5
|
108 n-pop : {n : Level}{A : Set n} {a : A} !$\rightarrow$! !$\mathbb{N}$! !$\rightarrow$! SingleLinkedStack A !$\rightarrow$! SingleLinkedStack A
|
0
|
109 n-pop zero s = s
|
5
|
110 n-pop {_} {A} {a} (suc n) s = popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ !$\rightarrow$! s )
|
0
|
111
|
5
|
112 open !$\equiv$!-Reasoning
|
0
|
113
|
5
|
114 push-pop-equiv : {n : Level} {A : Set n} {a : A} (s : SingleLinkedStack A) !$\rightarrow$! (popSingleLinkedStack (pushSingleLinkedStack s a (\s !$\rightarrow$! s)) (\s _ !$\rightarrow$! s) ) !$\equiv$! s
|
0
|
115 push-pop-equiv s = refl
|
|
116
|
5
|
117 push-and-n-pop : {n : Level} {A : Set n} {a : A} (n : !$\mathbb{N}$!) (s : SingleLinkedStack A) !$\rightarrow$! n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id) !$\equiv$! n-pop {_} {A} {a} n s
|
0
|
118 push-and-n-pop zero s = refl
|
|
119 push-and-n-pop {_} {A} {a} (suc n) s = begin
|
|
120 n-pop {_} {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id)
|
5
|
121 !$\equiv$!!$\langle$! refl !$\rangle$!
|
|
122 popSingleLinkedStack (n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ !$\rightarrow$! s)
|
|
123 !$\equiv$!!$\langle$! cong (\s !$\rightarrow$! popSingleLinkedStack s (\s _ !$\rightarrow$! s )) (push-and-n-pop n s) !$\rangle$!
|
|
124 popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ !$\rightarrow$! s)
|
|
125 !$\equiv$!!$\langle$! refl !$\rangle$!
|
0
|
126 n-pop {_} {A} {a} (suc n) s
|
5
|
127 !$\blacksquare$!
|
0
|
128
|
|
129
|
5
|
130 n-push-pop-equiv : {n : Level} {A : Set n} {a : A} (n : !$\mathbb{N}$!) (s : SingleLinkedStack A) !$\rightarrow$! (n-pop {_} {A} {a} n (n-push {_} {A} {a} n s)) !$\equiv$! s
|
0
|
131 n-push-pop-equiv zero s = refl
|
|
132 n-push-pop-equiv {_} {A} {a} (suc n) s = begin
|
|
133 n-pop {_} {A} {a} (suc n) (n-push (suc n) s)
|
5
|
134 !$\equiv$!!$\langle$! refl !$\rangle$!
|
|
135 n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s !$\rightarrow$! s))
|
|
136 !$\equiv$!!$\langle$! push-and-n-pop n (n-push n s) !$\rangle$!
|
0
|
137 n-pop {_} {A} {a} n (n-push n s)
|
5
|
138 !$\equiv$!!$\langle$! n-push-pop-equiv n s !$\rangle$!
|
0
|
139 s
|
5
|
140 !$\blacksquare$!
|
0
|
141
|
|
142
|
5
|
143 n-push-pop-equiv-empty : {n : Level} {A : Set n} {a : A} !$\rightarrow$! (n : !$\mathbb{N}$!) !$\rightarrow$! n-pop {_} {A} {a} n (n-push {_} {A} {a} n emptySingleLinkedStack) !$\equiv$! emptySingleLinkedStack
|
0
|
144 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack
|