annotate src/parallel_execution/stack.agda @ 478:0223c07c3946

fix stack.agda
author ryokka
date Thu, 28 Dec 2017 19:08:04 +0900
parents c3202635c20a
children 9098ec0a9e6b
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
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
5
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
6
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
7 data Nat : Set where
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
8 zero : Nat
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
9 suc : Nat -> Nat
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
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
15 -- equal : {a : Set} -> a -> a -> Bool
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
16 -- equal x y with x ≡y
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
17 -- equal x .x | refl = True
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
18 -- equal _ _ | _ = False
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
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
29
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
30 open Stack
427
07ccd411ad70 fix interface in agda
ryokka
parents: 181
diff changeset
31
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
32 pushStack : {a t si : Set} -> Stack si -> a -> (Stack si -> t) -> t
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
33 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
34
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
35 popStack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> t) -> t
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
36 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
37
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
38 -- get : {a t si : Set} -> Stack si -> (Stack si -> t) -> t
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
39 -- get {a} {t} s0 next = pop s0 (stack s0) (\s1 -> next (record s0 {}))
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
40
427
07ccd411ad70 fix interface in agda
ryokka
parents: 181
diff changeset
41
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
42 data Element (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
43 cons : a -> Maybe (Element a) -> Element a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
44
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
45 datum : {a : Set} -> Element a -> a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
46 datum (cons a _) = a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
47
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
48 next : {a : Set} -> Element a -> Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
49 next (cons _ n) = n
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
50
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
51
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
52 {-
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
53 -- cannot define recrusive record definition. so use linked list with maybe.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
54 record Element {l : Level} (a : Set l) : Set (suc l) where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
55 field
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
56 datum : a -- `data` is reserved by Agda.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
57 next : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
58 -}
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
59
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
60
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
61
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
62 record SingleLinkedStack (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
63 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
64 top : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
65 open SingleLinkedStack
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
66
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
67 pushSingleLinkedStack : {Data t : Set} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
68 pushSingleLinkedStack stack datum next = next stack1
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
69 where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
70 element = cons datum (top stack)
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
71 stack1 = record {top = Just element}
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
72
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
73
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
74 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
75 popSingleLinkedStack stack cs with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
76 ... | Nothing = cs stack Nothing
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
77 ... | Just d = cs stack1 (Just data1)
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
78 where
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
79 data1 = datum d
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
80 stack1 = record { top = (next d) }
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
81
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
82
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
83 emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
84 emptySingleLinkedStack = record {top = Nothing}
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
85
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
86 createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
87 createSingleLinkedStack = record { stack = emptySingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
88 ; push = pushSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
89 ; pop = popSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
90 }
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
91
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
92
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
93
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
94 test01 : {a : Set} -> SingleLinkedStack a -> Maybe a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
95 test01 stack _ with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
96 ... | (Just _) = True
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
97 ... | Nothing = False
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
98
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
99
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
100 test02 : {a : Set} -> SingleLinkedStack a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
101 test02 stack = (popSingleLinkedStack stack) test01
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
102
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
103 test03 : {a : Set} -> a -> Bool
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
104 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
105
477
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
106 testStack01 : {a : Set} -> a -> Bool
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
107 testStack01 v = pushStack createSingleLinkedStack v (
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
108 \s -> popStack s (\s1 d1 -> True))
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
109
c3202635c20a fix Stack.agda
ryokka
parents: 427
diff changeset
110
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
111
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
112 lemma : {A : Set} {a : A} -> test03 a ≡ False
158
3fdb3334c7a9 fix stack.cbc
mir3636
parents: 156
diff changeset
113 lemma = refl
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
114
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
115 id : {A : Set} -> A -> A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
116 id a = a
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
117
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
118
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
119 n-push : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
120 n-push zero s = s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
121 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
122
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
123 n-pop : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
124 n-pop zero s = s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
125 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
126
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
127 open ≡-Reasoning
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
128
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
129 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
130 push-pop-equiv s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
131
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
132 push-and-n-pop : {A : Set} {a : A} (n : Nat) (s : SingleLinkedStack A) -> n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id) ≡ n-pop {A} {a} n s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
133 push-and-n-pop zero s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
134 push-and-n-pop {A} {a} (suc n) s = begin
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
135 n-pop {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
136 ≡⟨ refl ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
137 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
138 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s)) (push-and-n-pop n s) ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
139 popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
140 ≡⟨ refl ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
141 n-pop {A} {a} (suc n) s
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
142
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
143
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
144
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
145 n-push-pop-equiv : {A : Set} {a : A} (n : Nat) (s : SingleLinkedStack A) -> (n-pop {A} {a} n (n-push {A} {a} n s)) ≡ s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
146 n-push-pop-equiv zero s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
147 n-push-pop-equiv {A} {a} (suc n) s = begin
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
148 n-pop {A} {a} (suc n) (n-push (suc n) s)
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
149 ≡⟨ refl ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
150 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
151 ≡⟨ push-and-n-pop n (n-push n s) ⟩
478
0223c07c3946 fix stack.agda
ryokka
parents: 477
diff changeset
152 n-pop {A} {a} n (n-push n s)
180
d8947747ff3b Fix syntax
atton
parents: 179
diff changeset
153 ≡⟨ n-push-pop-equiv n s ⟩
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
154 s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
155
181
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
156
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
157
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
158 n-push-pop-equiv-empty : {A : Set} {a : A} -> (n : Nat) -> n-pop {A} {a} n (n-push {A} {a} n emptySingleLinkedStack) ≡ emptySingleLinkedStack
78b28c8ffff2 Prove equivalence n-push/n-pop to empty stack
atton
parents: 180
diff changeset
159 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack