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