Mercurial > hg > Members > Moririn
annotate stack.agda @ 574:70b09cbefd45
add queue.agda
author | ryokka |
---|---|
date | Thu, 16 Aug 2018 18:22:08 +0900 |
parents | 988c12d7f887 |
children | 73fc32092b64 |
rev | line source |
---|---|
499 | 1 open import Level renaming (suc to succ ; zero to Zero ) |
496 | 2 module stack where |
154 | 3 |
161 | 4 open import Relation.Binary.PropositionalEquality |
477 | 5 open import Relation.Binary.Core |
484 | 6 open import Data.Nat |
478 | 7 |
484 | 8 ex : 1 + 2 ≡ 3 |
9 ex = refl | |
179 | 10 |
501 | 11 data Bool {n : Level } : Set n where |
161 | 12 True : Bool |
13 False : Bool | |
164 | 14 |
501 | 15 record _∧_ {n : Level } (a : Set n) (b : Set n): Set n where |
485 | 16 field |
17 pi1 : a | |
18 pi2 : b | |
477 | 19 |
554
988c12d7f887
use another nat comparator
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
544
diff
changeset
|
20 data _∨_ {n : Level } (a : Set n) (b : Set n): Set n where |
988c12d7f887
use another nat comparator
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
544
diff
changeset
|
21 pi1 : a -> a ∨ b |
988c12d7f887
use another nat comparator
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
544
diff
changeset
|
22 pi2 : b -> a ∨ b |
988c12d7f887
use another nat comparator
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
544
diff
changeset
|
23 |
496 | 24 data Maybe {n : Level } (a : Set n) : Set n where |
161 | 25 Nothing : Maybe a |
26 Just : a -> Maybe a | |
27 | |
574 | 28 record StackMethods {n m : Level } (a : Set n ) {t : Set m } (stackImpl : Set n ) : Set (m Level.⊔ n) where |
161 | 29 field |
30 push : stackImpl -> a -> (stackImpl -> t) -> t | |
31 pop : stackImpl -> (stackImpl -> Maybe a -> t) -> t | |
484 | 32 pop2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
33 get : stackImpl -> (stackImpl -> Maybe a -> t) -> t |
484 | 34 get2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t |
531 | 35 clear : stackImpl -> (stackImpl -> t) -> t |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
36 open StackMethods |
427 | 37 |
515 | 38 record Stack {n m : Level } (a : Set n ) {t : Set m } (si : Set n ) : Set (m Level.⊔ n) where |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
39 field |
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
40 stack : si |
515 | 41 stackMethods : StackMethods {n} {m} a {t} si |
42 pushStack : a -> (Stack a si -> t) -> t | |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
43 pushStack d next = push (stackMethods ) (stack ) d (\s1 -> next (record {stack = s1 ; stackMethods = stackMethods } )) |
515 | 44 popStack : (Stack a si -> Maybe a -> t) -> t |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
45 popStack next = pop (stackMethods ) (stack ) (\s1 d1 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 ) |
515 | 46 pop2Stack : (Stack a si -> Maybe a -> Maybe a -> t) -> t |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
47 pop2Stack next = pop2 (stackMethods ) (stack ) (\s1 d1 d2 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 d2) |
515 | 48 getStack : (Stack a si -> Maybe a -> t) -> t |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
49 getStack next = get (stackMethods ) (stack ) (\s1 d1 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 ) |
515 | 50 get2Stack : (Stack a si -> Maybe a -> Maybe a -> t) -> t |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
51 get2Stack next = get2 (stackMethods ) (stack ) (\s1 d1 d2 -> next (record {stack = s1 ; stackMethods = stackMethods }) d1 d2) |
531 | 52 clearStack : (Stack a si -> t) -> t |
53 clearStack next = clear (stackMethods ) (stack ) (\s1 -> next (record {stack = s1 ; stackMethods = stackMethods } )) | |
484 | 54 |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
55 open Stack |
427 | 56 |
544 | 57 {-- |
496 | 58 data Element {n : Level } (a : Set n) : Set n where |
161 | 59 cons : a -> Maybe (Element a) -> Element a |
60 | |
544 | 61 |
496 | 62 datum : {n : Level } {a : Set n} -> Element a -> a |
161 | 63 datum (cons a _) = a |
64 | |
496 | 65 next : {n : Level } {a : Set n} -> Element a -> Maybe (Element a) |
161 | 66 next (cons _ n) = n |
544 | 67 --} |
161 | 68 |
69 | |
164 | 70 -- cannot define recrusive record definition. so use linked list with maybe. |
544 | 71 record Element {l : Level} (a : Set l) : Set l where |
72 inductive | |
73 constructor cons | |
161 | 74 field |
164 | 75 datum : a -- `data` is reserved by Agda. |
161 | 76 next : Maybe (Element a) |
155 | 77 |
544 | 78 open Element |
155 | 79 |
164 | 80 |
496 | 81 record SingleLinkedStack {n : Level } (a : Set n) : Set n where |
161 | 82 field |
83 top : Maybe (Element a) | |
84 open SingleLinkedStack | |
155 | 85 |
499 | 86 pushSingleLinkedStack : {n m : Level } {t : Set m } {Data : Set n} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t |
161 | 87 pushSingleLinkedStack stack datum next = next stack1 |
88 where | |
89 element = cons datum (top stack) | |
164 | 90 stack1 = record {top = Just element} |
161 | 91 |
155 | 92 |
499 | 93 popSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t |
161 | 94 popSingleLinkedStack stack cs with (top stack) |
95 ... | Nothing = cs stack Nothing | |
96 ... | Just d = cs stack1 (Just data1) | |
154 | 97 where |
161 | 98 data1 = datum d |
99 stack1 = record { top = (next d) } | |
154 | 100 |
499 | 101 pop2SingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
102 pop2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack) | |
484 | 103 ... | Nothing = cs stack Nothing Nothing |
499 | 104 ... | Just d = pop2SingleLinkedStack' {n} {m} stack cs |
484 | 105 where |
499 | 106 pop2SingleLinkedStack' : {n m : Level } {t : Set m } -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
484 | 107 pop2SingleLinkedStack' stack cs with (next d) |
108 ... | Nothing = cs stack Nothing Nothing | |
511 | 109 ... | Just d1 = cs (record {top = (next d1)}) (Just (datum d)) (Just (datum d1)) |
484 | 110 |
111 | |
499 | 112 getSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
113 getSingleLinkedStack stack cs with (top stack) |
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
114 ... | Nothing = cs stack Nothing |
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
115 ... | Just d = cs stack (Just data1) |
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
116 where |
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
117 data1 = datum d |
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
118 |
499 | 119 get2SingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
120 get2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack) | |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
121 ... | Nothing = cs stack Nothing Nothing |
499 | 122 ... | Just d = get2SingleLinkedStack' {n} {m} stack cs |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
123 where |
499 | 124 get2SingleLinkedStack' : {n m : Level} {t : Set m } -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t |
484 | 125 get2SingleLinkedStack' stack cs with (next d) |
126 ... | Nothing = cs stack Nothing Nothing | |
127 ... | Just d1 = cs stack (Just (datum d)) (Just (datum d1)) | |
128 | |
531 | 129 clearSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (SingleLinkedStack a -> t) -> t |
130 clearSingleLinkedStack stack next = next (record {top = Nothing}) | |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
131 |
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
132 |
496 | 133 emptySingleLinkedStack : {n : Level } {a : Set n} -> SingleLinkedStack a |
161 | 134 emptySingleLinkedStack = record {top = Nothing} |
135 | |
509
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
136 ----- |
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
137 -- Basic stack implementations are specifications of a Stack |
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
138 -- |
515 | 139 singleLinkedStackSpec : {n m : Level } {t : Set m } {a : Set n} -> StackMethods {n} {m} a {t} (SingleLinkedStack a) |
509
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
140 singleLinkedStackSpec = record { |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
141 push = pushSingleLinkedStack |
161 | 142 ; pop = popSingleLinkedStack |
484 | 143 ; pop2 = pop2SingleLinkedStack |
483
9098ec0a9e6b
add getSingleLinkedStack,get2SingleLinkedStack. but get2SingleLinkedStack not working now
ryokka
parents:
478
diff
changeset
|
144 ; get = getSingleLinkedStack |
484 | 145 ; get2 = get2SingleLinkedStack |
531 | 146 ; clear = clearSingleLinkedStack |
503
413ce51da50b
separate methods in stack.agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
502
diff
changeset
|
147 } |
161 | 148 |
515 | 149 createSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> Stack {n} {m} a {t} (SingleLinkedStack a) |
509
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
150 createSingleLinkedStack = record { |
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
151 stack = emptySingleLinkedStack ; |
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
152 stackMethods = singleLinkedStackSpec |
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
153 } |
51f0d5e5d1e5
stack proof on indeterminate stack state
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
504
diff
changeset
|
154 |