Mercurial > hg > Gears > GearsAgda
annotate src/parallel_execution/stack.agda @ 504:0bec9490c199
stack.agda comment
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 01 Jan 2018 19:17:01 +0900 |
parents | 413ce51da50b |
children | 51f0d5e5d1e5 |
rev | line source |
---|---|
499 | 1 open import Level renaming (suc to succ ; zero to Zero ) |
496 | 2 module stack where |
154 | 3 |
161 | 4 open import Relation.Binary.PropositionalEquality |
477 | 5 open import Relation.Binary.Core |
484 | 6 open import Data.Nat |
478 | 7 |
484 | 8 ex : 1 + 2 ≡ 3 |
9 ex = refl | |
179 | 10 |
501 | 11 data Bool {n : Level } : Set n where |
161 | 12 True : Bool |
13 False : Bool | |
164 | 14 |
501 | 15 record _∧_ {n : Level } (a : Set n) (b : Set n): Set n where |
485 | 16 field |
17 pi1 : a | |
18 pi2 : b | |
477 | 19 |
496 | 20 data Maybe {n : Level } (a : Set n) : Set n where |
161 | 21 Nothing : Maybe a |
22 Just : a -> Maybe a | |
23 | |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
24 record StackMethods {n m : Level } {a : Set n } {t : Set m }(stackImpl : Set n ) : Set (m Level.⊔ n) where |
161 | 25 field |
26 push : stackImpl -> a -> (stackImpl -> t) -> t | |
27 pop : stackImpl -> (stackImpl -> Maybe a -> t) -> t | |
484 | 28 pop2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
29 get : stackImpl -> (stackImpl -> Maybe a -> t) -> t |
484 | 30 get2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
31 open StackMethods |
427 | 32 |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
33 record Stack {n m : Level } {a : Set n } {t : Set m } (si : Set n ) : Set (m Level.⊔ n) where |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
34 field |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
35 stack : si |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
36 stackMethods : StackMethods {n} {m} {a} {t} si |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
37 pushStack : a -> (Stack si -> t) -> t |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
38 pushStack d next = push (stackMethods ) (stack ) d (\s1 -> next (record {stack = s1 ; stackMethods = stackMethods } )) |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
39 popStack : (Stack si -> Maybe a -> t) -> t |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
40 popStack next = pop (stackMethods ) (stack ) (\s1 d1 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 ) |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
41 pop2Stack : (Stack si -> Maybe a -> Maybe a -> t) -> t |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
42 pop2Stack next = pop2 (stackMethods ) (stack ) (\s1 d1 d2 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 d2) |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
43 getStack : (Stack si -> Maybe a -> t) -> t |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
44 getStack next = get (stackMethods ) (stack ) (\s1 d1 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 ) |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
45 get2Stack : (Stack si -> Maybe a -> Maybe a -> t) -> t |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
46 get2Stack next = get2 (stackMethods ) (stack ) (\s1 d1 d2 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 d2) |
484 | 47 |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
48 open Stack |
427 | 49 |
496 | 50 data Element {n : Level } (a : Set n) : Set n where |
161 | 51 cons : a -> Maybe (Element a) -> Element a |
52 | |
496 | 53 datum : {n : Level } {a : Set n} -> Element a -> a |
161 | 54 datum (cons a _) = a |
55 | |
496 | 56 next : {n : Level } {a : Set n} -> Element a -> Maybe (Element a) |
161 | 57 next (cons _ n) = n |
58 | |
59 | |
164 | 60 {- |
61 -- cannot define recrusive record definition. so use linked list with maybe. | |
496 | 62 record Element {l : Level} (a : Set n l) : Set n (suc l) where |
161 | 63 field |
164 | 64 datum : a -- `data` is reserved by Agda. |
161 | 65 next : Maybe (Element a) |
66 -} | |
155 | 67 |
68 | |
164 | 69 |
496 | 70 record SingleLinkedStack {n : Level } (a : Set n) : Set n where |
161 | 71 field |
72 top : Maybe (Element a) | |
73 open SingleLinkedStack | |
155 | 74 |
499 | 75 pushSingleLinkedStack : {n m : Level } {t : Set m } {Data : Set n} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t |
161 | 76 pushSingleLinkedStack stack datum next = next stack1 |
77 where | |
78 element = cons datum (top stack) | |
164 | 79 stack1 = record {top = Just element} |
161 | 80 |
155 | 81 |
499 | 82 popSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t |
161 | 83 popSingleLinkedStack stack cs with (top stack) |
84 ... | Nothing = cs stack Nothing | |
85 ... | Just d = cs stack1 (Just data1) | |
154 | 86 where |
161 | 87 data1 = datum d |
88 stack1 = record { top = (next d) } | |
154 | 89 |
499 | 90 pop2SingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
91 pop2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack) | |
484 | 92 ... | Nothing = cs stack Nothing Nothing |
499 | 93 ... | Just d = pop2SingleLinkedStack' {n} {m} stack cs |
484 | 94 where |
499 | 95 pop2SingleLinkedStack' : {n m : Level } {t : Set m } -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
484 | 96 pop2SingleLinkedStack' stack cs with (next d) |
97 ... | Nothing = cs stack Nothing Nothing | |
98 ... | Just d1 = cs (record {top = (next d)}) (Just (datum d)) (Just (datum d1)) | |
99 | |
100 | |
499 | 101 getSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t |
483
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 |
499 | 108 get2SingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
109 get2SingleLinkedStack {n} {m} {t} {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 |
499 | 111 ... | Just d = get2SingleLinkedStack' {n} {m} stack cs |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
112 where |
499 | 113 get2SingleLinkedStack' : {n m : Level} {t : Set m } -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
484 | 114 get2SingleLinkedStack' stack cs with (next d) |
115 ... | Nothing = cs stack Nothing Nothing | |
116 ... | Just d1 = cs stack (Just (datum d)) (Just (datum d1)) | |
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 |
496 | 120 emptySingleLinkedStack : {n : Level } {a : Set n} -> SingleLinkedStack a |
161 | 121 emptySingleLinkedStack = record {top = Nothing} |
122 | |
499 | 123 createSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> Stack {n} {m} {a} {t} (SingleLinkedStack a) |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
124 createSingleLinkedStack = record { |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
125 stack = emptySingleLinkedStack ; |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
126 stackMethods = record { |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
127 push = pushSingleLinkedStack |
161 | 128 ; pop = popSingleLinkedStack |
484 | 129 ; pop2 = pop2SingleLinkedStack |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
130 ; get = getSingleLinkedStack |
484 | 131 ; get2 = get2SingleLinkedStack |
161 | 132 } |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
133 } |
161 | 134 |
156 | 135 |
499 | 136 test01 : {n : Level } {a : Set n} -> SingleLinkedStack a -> Maybe a -> Bool {n} |
161 | 137 test01 stack _ with (top stack) |
138 ... | (Just _) = True | |
139 ... | Nothing = False | |
140 | |
156 | 141 |
496 | 142 test02 : {n : Level } {a : Set n} -> SingleLinkedStack a -> Bool |
143 test02 stack = popSingleLinkedStack stack test01 | |
156 | 144 |
496 | 145 test03 : {n : Level } {a : Set n} -> a -> Bool |
165 | 146 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02 |
156 | 147 |
499 | 148 -- after a push and a pop, the stack is empty |
149 lemma : {n : Level} {A : Set n} {a : A} -> test03 a ≡ False | |
150 lemma = refl | |
151 | |
501 | 152 testStack01 : {n m : Level } {a : Set n} -> a -> Bool {m} |
477 | 153 testStack01 v = pushStack createSingleLinkedStack v ( |
154 \s -> popStack s (\s1 d1 -> True)) | |
155 | |
500 | 156 -- after push 1 and 2, pop2 get 1 and 2 |
157 | |
501 | 158 testStack02 : {m : Level } -> ( Stack (SingleLinkedStack ℕ) -> Bool {m} ) -> Bool {m} |
484 | 159 testStack02 cs = pushStack createSingleLinkedStack 1 ( |
160 \s -> pushStack s 2 cs) | |
477 | 161 |
485 | 162 |
499 | 163 testStack031 : (d1 d2 : ℕ ) -> Bool {Zero} |
500 | 164 testStack031 2 1 = True |
485 | 165 testStack031 _ _ = False |
166 | |
499 | 167 testStack032 : (d1 d2 : Maybe ℕ) -> Bool {Zero} |
485 | 168 testStack032 (Just d1) (Just d2) = testStack031 d1 d2 |
169 testStack032 _ _ = False | |
484 | 170 |
501 | 171 testStack03 : {m : Level } -> Stack (SingleLinkedStack ℕ) -> ((Maybe ℕ) -> (Maybe ℕ) -> Bool {m} ) -> Bool {m} |
485 | 172 testStack03 s cs = pop2Stack s ( |
173 \s d1 d2 -> cs d1 d2 ) | |
484 | 174 |
485 | 175 testStack04 : Bool |
176 testStack04 = testStack02 (\s -> testStack03 s testStack032) | |
177 | |
500 | 178 testStack05 : testStack04 ≡ True |
179 testStack05 = refl | |
179 | 180 |
501 | 181 ------ |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
182 -- |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
183 -- this should be proved by properties of the stack inteface, not only by the implementation, |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
184 -- and the implementation have to provides the properties. |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
185 -- |
504 | 186 -- we cannot write "s ≡ s3", since level of the Set does not fit , but we cant use stack s ≡ stack s3 |
187 -- | |
501 | 188 -- push->push->pop2 : {l : Level } {D : Set l} (x y : D ) (s : Stack (SingleLinkedStack D) ) -> |
502 | 189 -- pushStack s x ( \s1 -> pushStack s1 y ( \s2 -> pop2Stack s2 ( \s3 y1 x1 -> ((stack s ≡ stack s3 ) ∧ ( (Just x ≡ x1 ) ∧ (Just y ≡ y1 ) ) )))) |
501 | 190 -- push->push->pop2 {l} {D} x y s = {!!} |
502 | 191 -- where |
192 -- t0 : (s3 : Stack {_} {succ l} {D} {Set l} (SingleLinkedStack D)) (x1 y1 : Maybe D) -> (stack s ≡ stack s3 ) -> (Just x ≡ x1 ) -> (Just y ≡ y1 ) | |
193 -- -> ((stack s ≡ stack s3 ) ∧ ( (Just x ≡ x1 ) ∧ (Just y ≡ y1 ) )) | |
194 -- t0 s3 x1 y1 refl refl refl = record { pi1 = refl ; pi2 = record { pi1 = refl ; pi2 = refl } } | |
195 -- t1 : (s2 : Stack (SingleLinkedStack D)) -> pop2Stack s2 ( \s3 y1 x1 -> ((stack s ≡ stack s3 ) ∧ ( (Just x ≡ x1 ) ∧ (Just y ≡ y1 ) ) )) | |
196 -- t1 s2 = {!!} | |
197 -- t2 : (s1 : Stack (SingleLinkedStack D)) (x1 y1 : Maybe D) -> | |
198 -- pushStack s1 y ( \s2 -> pop2Stack s2 ( \s3 y1 x1 -> ((stack s ≡ stack s3 ) ∧ ( (Just x ≡ x1 ) ∧ (Just y ≡ y1 ) ) ) )) | |
199 -- t2 s1 = {!!} | |
501 | 200 |
201 | |
496 | 202 id : {n : Level} {A : Set n} -> A -> A |
179 | 203 id a = a |
204 | |
499 | 205 -- push a, n times |
179 | 206 |
496 | 207 n-push : {n : Level} {A : Set n} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A |
179 | 208 n-push zero s = s |
499 | 209 n-push {l} {A} {a} (suc n) s = pushSingleLinkedStack (n-push {l} {A} {a} n s) a (\s -> s ) |
179 | 210 |
499 | 211 n-pop : {n : Level}{A : Set n} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A |
179 | 212 n-pop zero s = s |
499 | 213 n-pop {_} {A} {a} (suc n) s = popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ -> s ) |
179 | 214 |
215 open ≡-Reasoning | |
216 | |
499 | 217 push-pop-equiv : {n : Level} {A : Set n} {a : A} (s : SingleLinkedStack A) -> (popSingleLinkedStack (pushSingleLinkedStack s a (\s -> s)) (\s _ -> s) ) ≡ s |
179 | 218 push-pop-equiv s = refl |
219 | |
499 | 220 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 |
179 | 221 push-and-n-pop zero s = refl |
496 | 222 push-and-n-pop {_} {A} {a} (suc n) s = begin |
499 | 223 n-pop {_} {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id) |
179 | 224 ≡⟨ refl ⟩ |
499 | 225 popSingleLinkedStack (n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s) |
226 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s )) (push-and-n-pop n s) ⟩ | |
227 popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ -> s) | |
179 | 228 ≡⟨ refl ⟩ |
499 | 229 n-pop {_} {A} {a} (suc n) s |
179 | 230 ∎ |
231 | |
232 | |
499 | 233 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 |
179 | 234 n-push-pop-equiv zero s = refl |
499 | 235 n-push-pop-equiv {_} {A} {a} (suc n) s = begin |
236 n-pop {_} {A} {a} (suc n) (n-push (suc n) s) | |
179 | 237 ≡⟨ refl ⟩ |
499 | 238 n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s)) |
179 | 239 ≡⟨ push-and-n-pop n (n-push n s) ⟩ |
499 | 240 n-pop {_} {A} {a} n (n-push n s) |
180 | 241 ≡⟨ n-push-pop-equiv n s ⟩ |
499 | 242 s |
179 | 243 ∎ |
181 | 244 |
245 | |
496 | 246 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 |
181 | 247 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack |