annotate src/parallel_execution/stack.agda @ 485:a7548f01f013

proof pop2 function in agda
author ryokka
date Fri, 29 Dec 2017 19:27:39 +0900
parents 8a22cfd174bf
children 8e133a3938c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
1 module stack where
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
2
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
3 open import Relation.Binary.PropositionalEquality
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
4 open import Relation.Binary.Core
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
5 open import Data.Nat
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
6 open import Level renaming (suc to succ ; zero to Zero)
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
7
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
8 ex : 1 + 2 ≡ 3
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
9 ex = refl
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
10
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
11 data Bool : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
12 True : Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
13 False : Bool
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
14
485
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
15 record _∧_ {a b : Set} : Set where
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
16 field
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
17 pi1 : a
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
18 pi2 : b
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
19
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
20 data Maybe (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
21 Nothing : Maybe a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
22 Just : a -> Maybe a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
23
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
24 record Stack {a t : Set} (stackImpl : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
25 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
26 stack : stackImpl
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
27 push : stackImpl -> a -> (stackImpl -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
28 pop : stackImpl -> (stackImpl -> Maybe a -> t) -> t
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
29 pop2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t
483
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
30 get : stackImpl -> (stackImpl -> Maybe a -> t) -> t
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
31 get2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
32 open Stack
427
07ccd411ad70 fix interface in agda
ryokka
parents: 181
diff changeset
33
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
34 pushStack : {a t si : Set} -> Stack si -> a -> (Stack si -> t) -> t
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
35 pushStack {a} {t} s0 d next = push s0 (stack s0) d (\s1 -> next (record s0 {stack = s1} ))
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
36
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
37 popStack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> t) -> t
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
38 popStack {a} {t} s0 next = pop s0 (stack s0) (\s1 d1 -> next (record s0 {stack = s1}) d1 )
427
07ccd411ad70 fix interface in agda
ryokka
parents: 181
diff changeset
39
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
40 pop2Stack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> Maybe a -> t) -> t
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
41 pop2Stack {a} {t} s0 next = pop2 s0 (stack s0) (\s1 d1 d2 -> next (record s0 {stack = s1}) d1 d2)
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
42
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
43 getStack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> t) -> t
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
44 getStack {a} {t} s0 next = get s0 (stack s0) (\s1 d1 -> next (record s0 {stack = s1}) d1 )
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
45
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
46 get2Stack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> Maybe a -> t) -> t
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
47 get2Stack {a} {t} s0 next = get2 s0 (stack s0) (\s1 d1 d2 -> next (record s0 {stack = s1}) d1 d2)
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
48
427
07ccd411ad70 fix interface in agda
ryokka
parents: 181
diff changeset
49
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
50 data Element (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
51 cons : a -> Maybe (Element a) -> Element a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
52
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
53 datum : {a : Set} -> Element a -> a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
54 datum (cons a _) = a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
55
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
56 next : {a : Set} -> Element a -> Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
57 next (cons _ n) = n
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
58
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
59
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
60 {-
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
61 -- cannot define recrusive record definition. so use linked list with maybe.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
62 record Element {l : Level} (a : Set l) : Set (suc l) where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
63 field
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
64 datum : a -- `data` is reserved by Agda.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
65 next : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
66 -}
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
67
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
68
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
69
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
70 record SingleLinkedStack (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
71 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
72 top : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
73 open SingleLinkedStack
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
74
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
75 pushSingleLinkedStack : {Data t : Set} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
76 pushSingleLinkedStack stack datum next = next stack1
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
77 where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
78 element = cons datum (top stack)
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
79 stack1 = record {top = Just element}
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
80
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
81
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
82 popSingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
83 popSingleLinkedStack stack cs with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
84 ... | Nothing = cs stack Nothing
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
85 ... | Just d = cs stack1 (Just data1)
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
86 where
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
87 data1 = datum d
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
88 stack1 = record { top = (next d) }
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
89
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
90 pop2SingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
91 pop2SingleLinkedStack {a} stack cs with (top stack)
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
92 ... | Nothing = cs stack Nothing Nothing
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
93 ... | Just d = pop2SingleLinkedStack' stack cs
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
94 where
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
95 pop2SingleLinkedStack' : {t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
96 pop2SingleLinkedStack' stack cs with (next d)
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
97 ... | Nothing = cs stack Nothing Nothing
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
98 ... | Just d1 = cs (record {top = (next d)}) (Just (datum d)) (Just (datum d1))
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
99
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
100
483
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
101 getSingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
102 getSingleLinkedStack stack cs with (top stack)
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
103 ... | Nothing = cs stack Nothing
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
104 ... | Just d = cs stack (Just data1)
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
105 where
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
106 data1 = datum d
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
107
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
108 get2SingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
109 get2SingleLinkedStack {a} stack cs with (top stack)
483
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
110 ... | Nothing = cs stack Nothing Nothing
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
111 ... | Just d = get2SingleLinkedStack' stack cs
483
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
112 where
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
113 get2SingleLinkedStack' : {t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
114 get2SingleLinkedStack' stack cs with (next d)
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
115 ... | Nothing = cs stack Nothing Nothing
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
116 ... | Just d1 = cs stack (Just (datum d)) (Just (datum d1))
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
117
483
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
118
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
119
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
120 emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
121 emptySingleLinkedStack = record {top = Nothing}
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
122
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
123 createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
124 createSingleLinkedStack = record { stack = emptySingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
125 ; push = pushSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
126 ; pop = popSingleLinkedStack
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
127 ; pop2 = pop2SingleLinkedStack
483
9098ec0a9e6b add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents: 478
diff changeset
128 ; get = getSingleLinkedStack
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
129 ; get2 = get2SingleLinkedStack
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
130 }
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
131
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
132
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
133 test01 : {a : Set} -> SingleLinkedStack a -> Maybe a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
134 test01 stack _ with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
135 ... | (Just _) = True
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
136 ... | Nothing = False
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
137
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
138
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
139 test02 : {a : Set} -> SingleLinkedStack a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
140 test02 stack = (popSingleLinkedStack stack) test01
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
141
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
142 test03 : {a : Set} -> a -> Bool
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
143 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
144
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
145 testStack01 : {a : Set} -> a -> Bool
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
146 testStack01 v = pushStack createSingleLinkedStack v (
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
147 \s -> popStack s (\s1 d1 -> True))
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
148
485
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
149 testStack02 : (Stack (SingleLinkedStack ℕ) -> Bool) -> Bool
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
150 testStack02 cs = pushStack createSingleLinkedStack 1 (
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
151 \s -> pushStack s 2 cs)
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
152
485
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
153
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
154 testStack031 : (d1 d2 : ℕ ) -> Bool
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
155 testStack031 1 2 = True
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
156 testStack031 _ _ = False
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
157
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
158 testStack032 : (d1 d2 : Maybe ℕ) -> Bool
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
159 testStack032 (Just d1) (Just d2) = testStack031 d1 d2
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
160 testStack032 _ _ = False
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
161
485
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
162 testStack03 : Stack (SingleLinkedStack ℕ) -> ((Maybe ℕ) -> (Maybe ℕ) -> Bool ) -> Bool
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
163 testStack03 s cs = pop2Stack s (
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
164 \s d1 d2 -> cs d1 d2 )
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
165
485
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
166 testStack04 : Bool
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
167 testStack04 = testStack02 (\s -> testStack03 s testStack032)
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
168
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
169 testStack05 : Set
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
170 testStack05 = testStack04 ≡ True
a7548f01f013 proof pop2 function in agda
ryokka
parents: 484
diff changeset
171
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
172
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
173 lemma : {A : Set} {a : A} -> test03 a ≡ False
158
3fdb3334c7a9 fix stack.cbc
mir3636
parents: 156
diff changeset
174 lemma = refl
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
175
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
176 id : {A : Set} -> A -> A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
177 id a = a
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
178
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
179
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
180 n-push : {A : Set} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
181 n-push zero s = s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
182 n-push {A} {a} (suc n) s = pushSingleLinkedStack (n-push {A} {a} n s) a (\s -> s)
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
183
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
184 n-pop : {A : Set} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
185 n-pop zero s = s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
186 n-pop {A} {a} (suc n) s = popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
187
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
188 open ≡-Reasoning
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
189
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
190 push-pop-equiv : {A : Set} {a : A} (s : SingleLinkedStack A) -> popSingleLinkedStack (pushSingleLinkedStack s a (\s -> s)) (\s _ -> s) ≡ s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
191 push-pop-equiv s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
192
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
193 push-and-n-pop : {A : Set} {a : A} (n : ℕ) (s : SingleLinkedStack A) -> n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id) ≡ n-pop {A} {a} n s
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
194 push-and-n-pop zero s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
195 push-and-n-pop {A} {a} (suc n) s = begin
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
196 n-pop {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
197 ≡⟨ refl ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
198 popSingleLinkedStack (n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
199 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s)) (push-and-n-pop n s) ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
200 popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
201 ≡⟨ refl ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
202 n-pop {A} {a} (suc n) s
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
203
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
204
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
205
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
206 n-push-pop-equiv : {A : Set} {a : A} (n : ℕ) (s : SingleLinkedStack A) -> (n-pop {A} {a} n (n-push {A} {a} n s)) ≡ s
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
207 n-push-pop-equiv zero s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
208 n-push-pop-equiv {A} {a} (suc n) s = begin
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
209 n-pop {A} {a} (suc n) (n-push (suc n) s)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
210 ≡⟨ refl ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
211 n-pop {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s))
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
212 ≡⟨ push-and-n-pop n (n-push n s) ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
213 n-pop {A} {a} n (n-push n s)
180
d8947747ff3b Fix syntax
atton
parents: 179
diff changeset
214 ≡⟨ n-push-pop-equiv n s ⟩
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
215 s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
216
181
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
217
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
218
484
8a22cfd174bf pop2 and get2 in agda
ryokka
parents: 483
diff changeset
219 n-push-pop-equiv-empty : {A : Set} {a : A} -> (n : ℕ) -> n-pop {A} {a} n (n-push {A} {a} n emptySingleLinkedStack) ≡ emptySingleLinkedStack
181
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
220 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack