annotate src/parallel_execution/stack.agda @ 165:bf26f1105862

Generalize lemma
author atton
date Thu, 17 Nov 2016 18:34:39 +0000
parents b0c6e0392b00
children b3be97ba0782
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
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
5 data Bool : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
6 True : Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
7 False : Bool
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
8
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
9 data Maybe (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
10 Nothing : Maybe a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
11 Just : a -> Maybe a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
12
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
13 record Stack {a t : Set} (stackImpl : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
14 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
15 stack : stackImpl
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
16 push : stackImpl -> a -> (stackImpl -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
17 pop : stackImpl -> (stackImpl -> Maybe a -> t) -> t
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
18
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
19 data Element (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
20 cons : a -> Maybe (Element a) -> Element a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
21
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
22 datum : {a : Set} -> Element a -> a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
23 datum (cons a _) = a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
24
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
25 next : {a : Set} -> Element a -> Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
26 next (cons _ n) = n
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
27
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
28
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
29 {-
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
30 -- cannot define recrusive record definition. so use linked list with maybe.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
31 record Element {l : Level} (a : Set l) : Set (suc l) where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
32 field
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
33 datum : a -- `data` is reserved by Agda.
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
34 next : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
35 -}
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
36
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
37
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
38
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
39 record SingleLinkedStack (a : Set) : Set where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
40 field
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
41 top : Maybe (Element a)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
42 open SingleLinkedStack
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
43
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
44 pushSingleLinkedStack : {Data t : Set} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
45 pushSingleLinkedStack stack datum next = next stack1
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
46 where
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
47 element = cons datum (top stack)
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
48 stack1 = record {top = Just element}
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
49
155
19cc626943e4 stack.agda
atton
parents: 154
diff changeset
50
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
51 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
52 popSingleLinkedStack stack cs with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
53 ... | Nothing = cs stack Nothing
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
54 ... | Just d = cs stack1 (Just data1)
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
55 where
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
56 data1 = datum d
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
57 stack1 = record { top = (next d) }
154
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
58
efef5d4df54f Add normal level and agda code
atton
parents:
diff changeset
59
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
60 emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
61 emptySingleLinkedStack = record {top = Nothing}
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
62
164
b0c6e0392b00 Add comment to stack.agda
atton
parents: 161
diff changeset
63 createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
64 createSingleLinkedStack = record { stack = emptySingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
65 ; push = pushSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
66 ; pop = popSingleLinkedStack
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
67 }
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
68
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
69
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
70
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
71 test01 : {a : Set} -> SingleLinkedStack a -> Maybe a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
72 test01 stack _ with (top stack)
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
73 ... | (Just _) = True
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
74 ... | Nothing = False
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
75
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
76
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
77 test02 : {a : Set} -> SingleLinkedStack a -> Bool
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
78 test02 stack = (popSingleLinkedStack stack) test01
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
79
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
80 test03 : {a : Set} -> a -> Bool
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
81 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02
156
0aa2265660a0 Add stack lemma
atton
parents: 155
diff changeset
82
161
db647f7ed2f6 Prove simple lemma in stack.agda
atton
parents: 158
diff changeset
83
165
bf26f1105862 Generalize lemma
atton
parents: 164
diff changeset
84 lemma : {A : Set} {a : A} -> test03 a ≡ False
158
3fdb3334c7a9 fix stack.cbc
mir3636
parents: 156
diff changeset
85 lemma = refl