annotate paper/src/stack.agda.replaced @ 2:c7acb9211784

add code, figure. and paper fix content
author ryokka
date Mon, 27 Jan 2020 20:41:36 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
1 open import Level renaming (suc to succ ; zero to Zero )
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
2 module stack where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
3
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
4 open import Relation.Binary.PropositionalEquality
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
5 open import Relation.Binary.Core
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
6 open import Data.Nat
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
7
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
8 ex : 1 + 2 @$\equiv$@ 3
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
9 ex = refl
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
10
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
11 data Bool {n : Level } : Set n where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
12 True : Bool
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
13 False : Bool
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
14
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
15 record _@$\wedge$@_ {n : Level } (a : Set n) (b : Set n): Set n where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
16 field
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
17 pi1 : a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
18 pi2 : b
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
19
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
20 data Maybe {n : Level } (a : Set n) : Set n where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
21 Nothing : Maybe a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
22 Just : a @$\rightarrow$@ Maybe a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
23
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
24 record StackMethods {n m : Level } (a : Set n ) {t : Set m }(stackImpl : Set n ) : Set (m Level.@$\sqcup$@ n) where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
25 field
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
26 push : stackImpl @$\rightarrow$@ a @$\rightarrow$@ (stackImpl @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
27 pop : stackImpl @$\rightarrow$@ (stackImpl @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
28 pop2 : stackImpl @$\rightarrow$@ (stackImpl @$\rightarrow$@ Maybe a @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
29 get : stackImpl @$\rightarrow$@ (stackImpl @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
30 get2 : stackImpl @$\rightarrow$@ (stackImpl @$\rightarrow$@ Maybe a @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
31 clear : stackImpl @$\rightarrow$@ (stackImpl @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
32 open StackMethods
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
33
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
34 record Stack {n m : Level } (a : Set n ) {t : Set m } (si : Set n ) : Set (m Level.@$\sqcup$@ n) where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
35 field
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
36 stack : si
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
37 stackMethods : StackMethods {n} {m} a {t} si
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
38 pushStack : a @$\rightarrow$@ (Stack a si @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
39 pushStack d next = push (stackMethods ) (stack ) d (\s1 @$\rightarrow$@ next (record {stack = s1 ; stackMethods = stackMethods } ))
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
40 popStack : (Stack a si @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
41 popStack next = pop (stackMethods ) (stack ) (\s1 d1 @$\rightarrow$@ next (record {stack = s1 ; stackMethods = stackMethods }) d1 )
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
42 pop2Stack : (Stack a si @$\rightarrow$@ Maybe a @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
43 pop2Stack next = pop2 (stackMethods ) (stack ) (\s1 d1 d2 @$\rightarrow$@ next (record {stack = s1 ; stackMethods = stackMethods }) d1 d2)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
44 getStack : (Stack a si @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
45 getStack next = get (stackMethods ) (stack ) (\s1 d1 @$\rightarrow$@ next (record {stack = s1 ; stackMethods = stackMethods }) d1 )
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
46 get2Stack : (Stack a si @$\rightarrow$@ Maybe a @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
47 get2Stack next = get2 (stackMethods ) (stack ) (\s1 d1 d2 @$\rightarrow$@ next (record {stack = s1 ; stackMethods = stackMethods }) d1 d2)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
48 clearStack : (Stack a si @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
49 clearStack next = clear (stackMethods ) (stack ) (\s1 @$\rightarrow$@ next (record {stack = s1 ; stackMethods = stackMethods } ))
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
50
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
51 open Stack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
52
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
53 {--
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
54 data Element {n : Level } (a : Set n) : Set n where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
55 cons : a @$\rightarrow$@ Maybe (Element a) @$\rightarrow$@ Element a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
56
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
57
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
58 datum : {n : Level } {a : Set n} @$\rightarrow$@ Element a @$\rightarrow$@ a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
59 datum (cons a _) = a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
60
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
61 next : {n : Level } {a : Set n} @$\rightarrow$@ Element a @$\rightarrow$@ Maybe (Element a)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
62 next (cons _ n) = n
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
63 --}
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
64
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
65
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
66 -- cannot define recrusive record definition. so use linked list with maybe.
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
67 record Element {l : Level} (a : Set l) : Set l where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
68 inductive
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
69 constructor cons
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
70 field
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
71 datum : a -- `data` is reserved by Agda.
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
72 next : Maybe (Element a)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
73
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
74 open Element
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
75
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
76
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
77 record SingleLinkedStack {n : Level } (a : Set n) : Set n where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
78 field
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
79 top : Maybe (Element a)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
80 open SingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
81
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
82 pushSingleLinkedStack : {n m : Level } {t : Set m } {Data : Set n} @$\rightarrow$@ SingleLinkedStack Data @$\rightarrow$@ Data @$\rightarrow$@ (Code : SingleLinkedStack Data @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
83 pushSingleLinkedStack stack datum next = next stack1
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
84 where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
85 element = cons datum (top stack)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
86 stack1 = record {top = Just element}
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
87
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
88
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
89 popSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (Code : SingleLinkedStack a @$\rightarrow$@ (Maybe a) @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
90 popSingleLinkedStack stack cs with (top stack)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
91 ... | Nothing = cs stack Nothing
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
92 ... | Just d = cs stack1 (Just data1)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
93 where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
94 data1 = datum d
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
95 stack1 = record { top = (next d) }
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
96
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
97 pop2SingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (Code : SingleLinkedStack a @$\rightarrow$@ (Maybe a) @$\rightarrow$@ (Maybe a) @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
98 pop2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
99 ... | Nothing = cs stack Nothing Nothing
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
100 ... | Just d = pop2SingleLinkedStack' {n} {m} stack cs
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
101 where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
102 pop2SingleLinkedStack' : {n m : Level } {t : Set m } @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (Code : SingleLinkedStack a @$\rightarrow$@ (Maybe a) @$\rightarrow$@ (Maybe a) @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
103 pop2SingleLinkedStack' stack cs with (next d)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
104 ... | Nothing = cs stack Nothing Nothing
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
105 ... | Just d1 = cs (record {top = (next d1)}) (Just (datum d)) (Just (datum d1))
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
106
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
107
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
108 getSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (Code : SingleLinkedStack a @$\rightarrow$@ (Maybe a) @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
109 getSingleLinkedStack stack cs with (top stack)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
110 ... | Nothing = cs stack Nothing
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
111 ... | Just d = cs stack (Just data1)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
112 where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
113 data1 = datum d
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
114
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
115 get2SingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (Code : SingleLinkedStack a @$\rightarrow$@ (Maybe a) @$\rightarrow$@ (Maybe a) @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
116 get2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
117 ... | Nothing = cs stack Nothing Nothing
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
118 ... | Just d = get2SingleLinkedStack' {n} {m} stack cs
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
119 where
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
120 get2SingleLinkedStack' : {n m : Level} {t : Set m } @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (Code : SingleLinkedStack a @$\rightarrow$@ (Maybe a) @$\rightarrow$@ (Maybe a) @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
121 get2SingleLinkedStack' stack cs with (next d)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
122 ... | Nothing = cs stack Nothing Nothing
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
123 ... | Just d1 = cs stack (Just (datum d)) (Just (datum d1))
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
124
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
125 clearSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ (SingleLinkedStack a @$\rightarrow$@ t) @$\rightarrow$@ t
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
126 clearSingleLinkedStack stack next = next (record {top = Nothing})
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
127
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
128
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
129 emptySingleLinkedStack : {n : Level } {a : Set n} @$\rightarrow$@ SingleLinkedStack a
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
130 emptySingleLinkedStack = record {top = Nothing}
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
131
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
132 -----
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
133 -- Basic stack implementations are specifications of a Stack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
134 --
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
135 singleLinkedStackSpec : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ StackMethods {n} {m} a {t} (SingleLinkedStack a)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
136 singleLinkedStackSpec = record {
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
137 push = pushSingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
138 ; pop = popSingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
139 ; pop2 = pop2SingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
140 ; get = getSingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
141 ; get2 = get2SingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
142 ; clear = clearSingleLinkedStack
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
143 }
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
144
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
145 createSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} @$\rightarrow$@ Stack {n} {m} a {t} (SingleLinkedStack a)
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
146 createSingleLinkedStack = record {
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
147 stack = emptySingleLinkedStack ;
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
148 stackMethods = singleLinkedStackSpec
c7acb9211784 add code, figure. and paper fix content
ryokka
parents:
diff changeset
149 }