annotate paper/src/stackTest.agda @ 11:831316a767e8

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