annotate src/parallel_execution/stack.agda @ 180:d8947747ff3b

Fix syntax
author atton
date Tue, 13 Dec 2016 02:08:40 +0000
parents b3be97ba0782
children 78b28c8ffff2
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
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
4
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
5 data Nat : Set where
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
6 zero : Nat
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
7 suc : Nat -> Nat
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
8
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
9 data Bool : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
10 True : Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
11 False : Bool
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
12
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
13 data Maybe (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
14 Nothing : Maybe a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
15 Just : a -> Maybe a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
16
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
17 record Stack {a t : Set} (stackImpl : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
18 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
19 stack : stackImpl
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
20 push : stackImpl -> a -> (stackImpl -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
21 pop : stackImpl -> (stackImpl -> Maybe a -> t) -> t
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
22
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
23 data Element (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
24 cons : a -> Maybe (Element a) -> Element a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
25
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
26 datum : {a : Set} -> Element a -> a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
27 datum (cons a _) = a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
28
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
29 next : {a : Set} -> Element a -> Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
30 next (cons _ n) = n
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
31
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
32
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
33 {-
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
34 -- cannot define recrusive record definition. so use linked list with maybe.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
35 record Element {l : Level} (a : Set l) : Set (suc l) where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
36 field
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
37 datum : a -- `data` is reserved by Agda.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
38 next : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
39 -}
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
40
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
41
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
42
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
43 record SingleLinkedStack (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
44 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
45 top : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
46 open SingleLinkedStack
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
47
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
48 pushSingleLinkedStack : {Data t : Set} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
49 pushSingleLinkedStack stack datum next = next stack1
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
50 where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
51 element = cons datum (top stack)
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
52 stack1 = record {top = Just element}
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
53
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
54
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
55 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
56 popSingleLinkedStack stack cs with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
57 ... | Nothing = cs stack Nothing
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
58 ... | Just d = cs stack1 (Just data1)
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
59 where
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
60 data1 = datum d
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
61 stack1 = record { top = (next d) }
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
62
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
63
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
64 emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
65 emptySingleLinkedStack = record {top = Nothing}
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
66
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
67 createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
68 createSingleLinkedStack = record { stack = emptySingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
69 ; push = pushSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
70 ; pop = popSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
71 }
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
72
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
73
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
74
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
75 test01 : {a : Set} -> SingleLinkedStack a -> Maybe a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
76 test01 stack _ with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
77 ... | (Just _) = True
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
78 ... | Nothing = False
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
79
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
80
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
81 test02 : {a : Set} -> SingleLinkedStack a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
82 test02 stack = (popSingleLinkedStack stack) test01
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
83
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
84 test03 : {a : Set} -> a -> Bool
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
85 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
86
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
87
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
88 lemma : {A : Set} {a : A} -> test03 a ≡ False
158
3fdb3334c7a9 fix stack.cbc
mir3636
parents: 156
diff changeset
89 lemma = refl
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
90
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
91 id : {A : Set} -> A -> A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
92 id a = a
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
93
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
94
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
95 n-push : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
96 n-push zero s = s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
97 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
98
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
99 n-pop : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
100 n-pop zero s = s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
101 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
102
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
103 open ≡-Reasoning
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
104
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
105 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
106 push-pop-equiv s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
107
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
108 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
109 push-and-n-pop zero s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
110 push-and-n-pop {A} {a} (suc n) s = begin
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
111 n-pop (suc (suc n)) (pushSingleLinkedStack s a id)
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
112 ≡⟨ refl ⟩
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
113 popSingleLinkedStack (n-pop (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s)
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
114 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s)) (push-and-n-pop n s) ⟩
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
115 popSingleLinkedStack (n-pop n s) (\s _ -> s)
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
116 ≡⟨ refl ⟩
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
117 n-pop (suc n) s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
118
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
119
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
120
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
121 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
122 n-push-pop-equiv zero s = refl
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
123 n-push-pop-equiv {A} {a} (suc n) s = begin
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
124 n-pop (suc n) (n-push (suc n) s)
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
125 ≡⟨ refl ⟩
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
126 n-pop (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s))
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
127 ≡⟨ push-and-n-pop n (n-push n s) ⟩
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
128 n-pop n (n-push n s)
180
d8947747ff3b Fix syntax
atton
parents: 179
diff changeset
129 ≡⟨ n-push-pop-equiv n s ⟩
179
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
130 s
b3be97ba0782 Prove n-push/n-pop
atton
parents: 165
diff changeset
131