annotate Paper/src/stackImpl.agda @ 1:a72446879486

Init paper
author soto <soto@cr.ie.u-ryukyu.ac.jp>
date Thu, 12 Jan 2023 20:28:50 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 record Element {l : Level} (a : Set l) : Set l where
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 inductive
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 constructor cons
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 field
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 datum : a -- `data` is reserved by Agda.
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 next : Maybe (Element a)
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open Element
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 record SingleLinkedStack {n : Level } (a : Set n) : Set n where
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 field
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 top : Maybe (Element a)
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 open SingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 pushSingleLinkedStack : {n m : Level } {t : Set m } {Data : Set n} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 pushSingleLinkedStack stack datum next = next stack1
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 where
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 element = cons datum (top stack)
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 stack1 = record {top = Just element}
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 -- push 以下は省略
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 -- Basic stack implementations are specifications of a Stack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 singleLinkedStackSpec : {n m : Level } {t : Set m } {a : Set n} -> StackMethods {n} {m} a {t} (SingleLinkedStack a)
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 singleLinkedStackSpec = record {
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 push = pushSingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 ; pop = popSingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 ; pop2 = pop2SingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 ; get = getSingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 ; get2 = get2SingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 ; clear = clearSingleLinkedStack
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 }
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 createSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> Stack {n} {m} a {t} (SingleLinkedStack a)
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 createSingleLinkedStack = record {
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 stack = emptySingleLinkedStack ;
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 stackMethods = singleLinkedStackSpec
a72446879486 Init paper
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 }