annotate queue.agda @ 815:e22ebb0f00a3

add replaceRBTNode
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 24 Jan 2024 10:36:44 +0900
parents 70b09cbefd45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
574
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
1 open import Level renaming (suc to succ ; zero to Zero )
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
2 module Queue where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
3
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
4 open import Relation.Binary.PropositionalEquality
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
5 open import Relation.Binary.Core
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
6 open import Data.Nat
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
7
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
8 data Maybe {n : Level } (a : Set n) : Set n where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
9 Nothing : Maybe a
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
10 Just : a -> Maybe a
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
11
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
12 data Bool {n : Level }: Set n where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
13 True : Bool
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
14 False : Bool
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
15
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
16
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
17 record QueueMethods {n m : Level} (a : Set n) {t : Set m} (queueImpl : Set n) : Set (m Level.⊔ n) where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
18 field
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
19 put : queueImpl -> a -> (queueImpl -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
20 take : queueImpl -> (queueImpl -> Maybe a -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
21 clear : queueImpl -> (queueImpl -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
22 open QueueMethods
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
23
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
24 record Queue {n m : Level} (a : Set n) {t : Set m} (qu : Set n) : Set (m Level.⊔ n) where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
25 field
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
26 queue : qu
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
27 queueMethods : QueueMethods {n} {m} a {t} qu
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
28 putQueue : a -> (Queue a qu -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
29 putQueue a next = put (queueMethods) (queue) a (\q1 -> next record {queue = q1 ; queueMethods = queueMethods})
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
30 takeQueue : (Queue a qu -> Maybe a -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
31 takeQueue next = take (queueMethods) (queue) (\q1 d1 -> next record {queue = q1 ; queueMethods = queueMethods} d1)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
32 clearQueue : (Queue a qu -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
33 clearQueue next = clear (queueMethods) (queue) (\q1 -> next record {queue = q1 ; queueMethods = queueMethods})
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
34 open Queue
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
35
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
36
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
37
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
38 record Element {n : Level} (a : Set n) : Set n where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
39 inductive
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
40 constructor cons
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
41 field
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
42 datum : a -- `data` is reserved by Agda.
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
43 next : Maybe (Element a)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
44 open Element
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
45
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
46
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
47 record SingleLinkedQueue {n : Level} (a : Set n) : Set n where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
48 field
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
49 top : Maybe (Element a)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
50 last : Maybe (Element a)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
51 open SingleLinkedQueue
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
52
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
53
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
54 {-# TERMINATING #-}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
55 reverseElement : {n : Level} {a : Set n} -> Element a -> Maybe (Element a) -> Element a
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
56 reverseElement el Nothing with next el
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
57 ... | Just el1 = reverseElement el1 (Just rev)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
58 where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
59 rev = cons (datum el) Nothing
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
60 ... | Nothing = el
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
61 reverseElement el (Just el0) with next el
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
62 ... | Just el1 = reverseElement el1 (Just (cons (datum el) (Just el0)))
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
63 ... | Nothing = (cons (datum el) (Just el0))
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
64
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
65
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
66 {-# TERMINATING #-}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
67 putSingleLinkedQueue : {n m : Level} {t : Set m} {a : Set n} -> SingleLinkedQueue a -> a -> (Code : SingleLinkedQueue a -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
68 putSingleLinkedQueue queue a cs with top queue
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
69 ... | Just te = cs queue1
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
70 where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
71 re = reverseElement te Nothing
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
72 ce = cons a (Just re)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
73 commit = reverseElement ce Nothing
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
74 queue1 = record queue {top = Just commit}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
75 ... | Nothing = cs queue1
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
76 where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
77 cel = record {datum = a ; next = Nothing}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
78 queue1 = record {top = Just cel ; last = Just cel}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
79
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
80 {-# TERMINATING #-}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
81 takeSingleLinkedQueue : {n m : Level} {t : Set m} {a : Set n} -> SingleLinkedQueue a -> (Code : SingleLinkedQueue a -> (Maybe a) -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
82 takeSingleLinkedQueue queue cs with (top queue)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
83 ... | Just te = cs record {top = (next te) ; last = Just (lastElement te)} (Just (datum te))
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
84 where
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
85 lastElement : {n : Level} {a : Set n} -> Element a -> Element a
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
86 lastElement el with next el
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
87 ... | Just el1 = lastElement el1
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
88 ... | Nothing = el
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
89 ... | Nothing = cs queue Nothing
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
90
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
91 clearSingleLinkedQueue : {n m : Level} {t : Set m} {a : Set n} -> SingleLinkedQueue a -> (SingleLinkedQueue a -> t) -> t
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
92 clearSingleLinkedQueue queue cs = cs (record {top = Nothing ; last = Nothing})
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
93
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
94
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
95 emptySingleLinkedQueue : {n : Level} {a : Set n} -> SingleLinkedQueue a
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
96 emptySingleLinkedQueue = record {top = Nothing ; last = Nothing}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
97
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
98 singleLinkedQueueSpec : {n m : Level } {t : Set m } {a : Set n} -> QueueMethods {n} {m} a {t} (SingleLinkedQueue a)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
99 singleLinkedQueueSpec = record {
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
100 put = putSingleLinkedQueue
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
101 ; take = takeSingleLinkedQueue
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
102 ; clear = clearSingleLinkedQueue
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
103 }
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
104
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
105
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
106 createSingleLinkedQueue : {n m : Level} {t : Set m} {a : Set n} -> Queue {n} {m} a {t} (SingleLinkedQueue a)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
107 createSingleLinkedQueue = record {
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
108 queue = emptySingleLinkedQueue ;
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
109 queueMethods = singleLinkedQueueSpec
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
110 }
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
111
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
112
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
113 check1 = putSingleLinkedQueue emptySingleLinkedQueue 1 (\q1 -> putSingleLinkedQueue q1 2 (\q2 -> putSingleLinkedQueue q2 3 (\q3 -> putSingleLinkedQueue q3 4 (\q4 -> putSingleLinkedQueue q4 5 (\q5 -> q5)))))
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
114
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
115
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
116 check2 = putSingleLinkedQueue emptySingleLinkedQueue 1 (\q1 -> putSingleLinkedQueue q1 2 (\q2 -> putSingleLinkedQueue q2 3 (\q3 -> putSingleLinkedQueue q3 4 (\q4 -> takeSingleLinkedQueue q4 (\q d -> q)))))
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
117
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
118
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
119 test01 : {n : Level } {a : Set n} -> SingleLinkedQueue a -> Maybe a -> Bool {n}
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
120 test01 queue _ with (top queue)
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
121 ... | (Just _) = True
70b09cbefd45 add queue.agda
ryokka
parents:
diff changeset
122 ... | Nothing = False