154
|
1 module stack where
|
|
2
|
161
|
3 open import Relation.Binary.PropositionalEquality
|
477
|
4 open import Relation.Binary.Core
|
161
|
5
|
478
|
6
|
179
|
7 data Nat : Set where
|
|
8 zero : Nat
|
|
9 suc : Nat -> Nat
|
|
10
|
161
|
11 data Bool : Set where
|
|
12 True : Bool
|
|
13 False : Bool
|
164
|
14
|
477
|
15 -- equal : {a : Set} -> a -> a -> Bool
|
478
|
16 -- equal x y with x ≡y
|
477
|
17 -- equal x .x | refl = True
|
|
18 -- equal _ _ | _ = False
|
|
19
|
161
|
20 data Maybe (a : Set) : Set where
|
|
21 Nothing : Maybe a
|
|
22 Just : a -> Maybe a
|
|
23
|
|
24 record Stack {a t : Set} (stackImpl : Set) : Set where
|
|
25 field
|
|
26 stack : stackImpl
|
|
27 push : stackImpl -> a -> (stackImpl -> t) -> t
|
|
28 pop : stackImpl -> (stackImpl -> Maybe a -> t) -> t
|
478
|
29
|
477
|
30 open Stack
|
427
|
31
|
477
|
32 pushStack : {a t si : Set} -> Stack si -> a -> (Stack si -> t) -> t
|
|
33 pushStack {a} {t} s0 d next = push s0 (stack s0) d (\s1 -> next (record s0 {stack = s1} ))
|
|
34
|
|
35 popStack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> t) -> t
|
|
36 popStack {a} {t} s0 next = pop s0 (stack s0) (\s1 d1 -> next (record s0 {stack = s1}) d1 )
|
427
|
37
|
478
|
38 -- get : {a t si : Set} -> Stack si -> (Stack si -> t) -> t
|
|
39 -- get {a} {t} s0 next = pop s0 (stack s0) (\s1 -> next (record s0 {}))
|
|
40
|
427
|
41
|
161
|
42 data Element (a : Set) : Set where
|
|
43 cons : a -> Maybe (Element a) -> Element a
|
|
44
|
|
45 datum : {a : Set} -> Element a -> a
|
|
46 datum (cons a _) = a
|
|
47
|
|
48 next : {a : Set} -> Element a -> Maybe (Element a)
|
|
49 next (cons _ n) = n
|
|
50
|
|
51
|
164
|
52 {-
|
|
53 -- cannot define recrusive record definition. so use linked list with maybe.
|
161
|
54 record Element {l : Level} (a : Set l) : Set (suc l) where
|
|
55 field
|
164
|
56 datum : a -- `data` is reserved by Agda.
|
161
|
57 next : Maybe (Element a)
|
|
58 -}
|
155
|
59
|
|
60
|
164
|
61
|
161
|
62 record SingleLinkedStack (a : Set) : Set where
|
|
63 field
|
|
64 top : Maybe (Element a)
|
|
65 open SingleLinkedStack
|
155
|
66
|
161
|
67 pushSingleLinkedStack : {Data t : Set} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
|
|
68 pushSingleLinkedStack stack datum next = next stack1
|
|
69 where
|
|
70 element = cons datum (top stack)
|
164
|
71 stack1 = record {top = Just element}
|
161
|
72
|
155
|
73
|
161
|
74 popSingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t
|
|
75 popSingleLinkedStack stack cs with (top stack)
|
|
76 ... | Nothing = cs stack Nothing
|
|
77 ... | Just d = cs stack1 (Just data1)
|
154
|
78 where
|
161
|
79 data1 = datum d
|
|
80 stack1 = record { top = (next d) }
|
154
|
81
|
|
82
|
161
|
83 emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
|
|
84 emptySingleLinkedStack = record {top = Nothing}
|
|
85
|
164
|
86 createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
|
161
|
87 createSingleLinkedStack = record { stack = emptySingleLinkedStack
|
|
88 ; push = pushSingleLinkedStack
|
|
89 ; pop = popSingleLinkedStack
|
|
90 }
|
|
91
|
156
|
92
|
|
93
|
161
|
94 test01 : {a : Set} -> SingleLinkedStack a -> Maybe a -> Bool
|
|
95 test01 stack _ with (top stack)
|
|
96 ... | (Just _) = True
|
|
97 ... | Nothing = False
|
|
98
|
156
|
99
|
161
|
100 test02 : {a : Set} -> SingleLinkedStack a -> Bool
|
|
101 test02 stack = (popSingleLinkedStack stack) test01
|
156
|
102
|
165
|
103 test03 : {a : Set} -> a -> Bool
|
|
104 test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02
|
156
|
105
|
477
|
106 testStack01 : {a : Set} -> a -> Bool
|
|
107 testStack01 v = pushStack createSingleLinkedStack v (
|
|
108 \s -> popStack s (\s1 d1 -> True))
|
|
109
|
|
110
|
161
|
111
|
165
|
112 lemma : {A : Set} {a : A} -> test03 a ≡ False
|
158
|
113 lemma = refl
|
179
|
114
|
|
115 id : {A : Set} -> A -> A
|
|
116 id a = a
|
|
117
|
|
118
|
|
119 n-push : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
|
|
120 n-push zero s = s
|
|
121 n-push {A} {a} (suc n) s = pushSingleLinkedStack (n-push {A} {a} n s) a (\s -> s)
|
|
122
|
|
123 n-pop : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
|
|
124 n-pop zero s = s
|
|
125 n-pop {A} {a} (suc n) s = popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
|
|
126
|
|
127 open ≡-Reasoning
|
|
128
|
|
129 push-pop-equiv : {A : Set} {a : A} (s : SingleLinkedStack A) -> popSingleLinkedStack (pushSingleLinkedStack s a (\s -> s)) (\s _ -> s) ≡ s
|
|
130 push-pop-equiv s = refl
|
|
131
|
|
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
|
|
133 push-and-n-pop zero s = refl
|
|
134 push-and-n-pop {A} {a} (suc n) s = begin
|
478
|
135 n-pop {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id)
|
179
|
136 ≡⟨ refl ⟩
|
478
|
137 popSingleLinkedStack (n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s)
|
179
|
138 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s)) (push-and-n-pop n s) ⟩
|
478
|
139 popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
|
179
|
140 ≡⟨ refl ⟩
|
478
|
141 n-pop {A} {a} (suc n) s
|
179
|
142 ∎
|
|
143
|
|
144
|
|
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
|
|
146 n-push-pop-equiv zero s = refl
|
|
147 n-push-pop-equiv {A} {a} (suc n) s = begin
|
478
|
148 n-pop {A} {a} (suc n) (n-push (suc n) s)
|
179
|
149 ≡⟨ refl ⟩
|
478
|
150 n-pop {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s))
|
179
|
151 ≡⟨ push-and-n-pop n (n-push n s) ⟩
|
478
|
152 n-pop {A} {a} n (n-push n s)
|
180
|
153 ≡⟨ n-push-pop-equiv n s ⟩
|
179
|
154 s
|
|
155 ∎
|
181
|
156
|
|
157
|
|
158 n-push-pop-equiv-empty : {A : Set} {a : A} -> (n : Nat) -> n-pop {A} {a} n (n-push {A} {a} n emptySingleLinkedStack) ≡ emptySingleLinkedStack
|
|
159 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack
|