annotate redBlackTreeHoare.agda @ 606:61a0491a627b

with Hoare condition
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Nov 2021 16:14:09 +0900
parents 40d01b368e34
children b088fa199d3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
576
40d01b368e34 Temporary Push
ryokka
parents: 575
diff changeset
1 module RedBlackTreeHoare where
575
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
2
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
3
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
4 open import Level hiding (zero)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
5
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
6 open import Data.Nat hiding (compare)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
7 open import Data.Nat.Properties as NatProp
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
8 open import Data.Maybe
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
9 open import Data.Bool
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
10 open import Data.Empty
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
11
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
12 open import Relation.Binary
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
13 open import Relation.Binary.PropositionalEquality
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
14
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
15 open import stack
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
16
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
17 record TreeMethods {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.⊔ n) where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
18 field
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
19 putImpl : treeImpl → a → (treeImpl → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
20 getImpl : treeImpl → (treeImpl → Maybe a → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
21 open TreeMethods
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
22
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
23 record Tree {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.⊔ n) where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
24 field
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
25 tree : treeImpl
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
26 treeMethods : TreeMethods {n} {m} {a} {t} treeImpl
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
27 putTree : a → (Tree treeImpl → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
28 putTree d next = putImpl (treeMethods ) tree d (\t1 → next (record {tree = t1 ; treeMethods = treeMethods} ))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
29 getTree : (Tree treeImpl → Maybe a → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
30 getTree next = getImpl (treeMethods ) tree (\t1 d → next (record {tree = t1 ; treeMethods = treeMethods} ) d )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
31
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
32 open Tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
33
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
34 data Color {n : Level } : Set n where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
35 Red : Color
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
36 Black : Color
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
37
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
38
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
39 record Node {n : Level } (a : Set n) (k : ℕ) : Set n where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
40 inductive
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
41 field
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
42 key : ℕ
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
43 value : a
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
44 right : Maybe (Node a k)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
45 left : Maybe (Node a k)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
46 color : Color {n}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
47 open Node
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
48
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
49 record RedBlackTree {n m : Level } {t : Set m} (a : Set n) (k : ℕ) : Set (m Level.⊔ n) where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
50 field
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
51 root : Maybe (Node a k)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
52 nodeStack : SingleLinkedStack (Node a k)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
53 -- compare : k → k → Tri A B C
576
40d01b368e34 Temporary Push
ryokka
parents: 575
diff changeset
54 -- <-cmp
575
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
55
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
56 open RedBlackTree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
57
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
58 open SingleLinkedStack
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
59
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
60 compTri : ( x y : ℕ ) -> Tri ( x < y ) ( x ≡ y ) ( x > y )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
61 compTri = IsStrictTotalOrder.compare (Relation.Binary.StrictTotalOrder.isStrictTotalOrder <-strictTotalOrder)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
62 where open import Relation.Binary
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
63
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
64 -- put new node at parent node, and rebuild tree to the top
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
65 --
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
66 {-# TERMINATING #-} -- https://agda.readthedocs.io/en/v2.5.3/language/termination-checking.html
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
67 replaceNode : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Node a k → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
68 replaceNode {n} {m} {t} {a} {k} tree s n0 next = popSingleLinkedStack s (
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
69 \s parent → replaceNode1 s parent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
70 module ReplaceNode where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
71 replaceNode1 : SingleLinkedStack (Node a k) → Maybe ( Node a k ) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
72 replaceNode1 s nothing = next ( record tree { root = just (record n0 { color = Black}) } )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
73 replaceNode1 s (just n1) with compTri (key n1) (key n0)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
74 replaceNode1 s (just n1) | tri< lt ¬eq ¬gt = replaceNode {n} {m} {t} {a} {k} tree s ( record n1 { value = value n0 ; left = left n0 ; right = right n0 } ) next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
75 replaceNode1 s (just n1) | tri≈ ¬lt eq ¬gt = replaceNode {n} {m} {t} {a} {k} tree s ( record n1 { left = just n0 } ) next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
76 replaceNode1 s (just n1) | tri> ¬lt ¬eq gt = replaceNode {n} {m} {t} {a} {k} tree s ( record n1 { right = just n0 } ) next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
77
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
78
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
79 rotateRight : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) →
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
80 (RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
81 rotateRight {n} {m} {t} {a} {k} tree s n0 parent rotateNext = getSingleLinkedStack s (\ s n0 → rotateRight1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
82 where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
83 rotateRight1 : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) →
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
84 (RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
85 rotateRight1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext with n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
86 ... | nothing = rotateNext tree s nothing n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
87 ... | just n1 with parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
88 ... | nothing = rotateNext tree s (just n1 ) n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
89 ... | just parent1 with left parent1
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
90 ... | nothing = rotateNext tree s (just n1) nothing
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
91 ... | just leftParent with compTri (key n1) (key leftParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
92 rotateRight1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext | just n1 | just parent1 | just leftParent | tri< a₁ ¬b ¬c = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
93 rotateRight1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext | just n1 | just parent1 | just leftParent | tri≈ ¬a b ¬c = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
94 rotateRight1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext | just n1 | just parent1 | just leftParent | tri> ¬a ¬b c = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
95
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
96
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
97 rotateLeft : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) →
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
98 (RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
99 rotateLeft {n} {m} {t} {a} {k} tree s n0 parent rotateNext = getSingleLinkedStack s (\ s n0 → rotateLeft1 tree s n0 parent rotateNext)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
100 where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
101 rotateLeft1 : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) →
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
102 (RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
103 rotateLeft1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext with n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
104 ... | nothing = rotateNext tree s nothing n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
105 ... | just n1 with parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
106 ... | nothing = rotateNext tree s (just n1) nothing
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
107 ... | just parent1 with right parent1
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
108 ... | nothing = rotateNext tree s (just n1) nothing
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
109 ... | just rightParent with compTri (key n1) (key rightParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
110 rotateLeft1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext | just n1 | just parent1 | just rightParent | tri< a₁ ¬b ¬c = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
111 rotateLeft1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext | just n1 | just parent1 | just rightParent | tri≈ ¬a b ¬c = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
112 rotateLeft1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext | just n1 | just parent1 | just rightParent | tri> ¬a ¬b c = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
113 -- ... | EQ = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
114 -- ... | _ = rotateNext tree s (just n1) parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
115
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
116 {-# TERMINATING #-}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
117 insertCase5 : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Node a k → Node a k → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
118 insertCase5 {n} {m} {t} {a} {k} tree s n0 parent grandParent next = pop2SingleLinkedStack s (\ s parent grandParent → insertCase51 tree s n0 parent grandParent next)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
119 where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
120 insertCase51 : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) → Maybe (Node a k) → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
121 insertCase51 {n} {m} {t} {a} {k} tree s n0 parent grandParent next with n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
122 ... | nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
123 ... | just n1 with parent | grandParent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
124 ... | nothing | _ = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
125 ... | _ | nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
126 ... | just parent1 | just grandParent1 with left parent1 | left grandParent1
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
127 ... | nothing | _ = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
128 ... | _ | nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
129 ... | just leftParent1 | just leftGrandParent1
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
130 with compTri (key n1) (key leftParent1) | compTri (key leftParent1) (key leftGrandParent1)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
131 ... | tri≈ ¬a b ¬c | tri≈ ¬a1 b1 ¬c1 = rotateRight tree s n0 parent (\ tree s n0 parent → insertCase5 tree s n0 parent1 grandParent1 next)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
132 ... | _ | _ = rotateLeft tree s n0 parent (\ tree s n0 parent → insertCase5 tree s n0 parent1 grandParent1 next)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
133 -- ... | EQ | EQ = rotateRight tree s n0 parent (\ tree s n0 parent → insertCase5 tree s n0 parent1 grandParent1 next)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
134 -- ... | _ | _ = rotateLeft tree s n0 parent (\ tree s n0 parent → insertCase5 tree s n0 parent1 grandParent1 next)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
135
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
136 insertCase4 : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Node a k → Node a k → Node a k → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
137 insertCase4 {n} {m} {t} {a} {k} tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
138 with (right parent) | (left grandParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
139 ... | nothing | _ = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
140 ... | _ | nothing = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
141 ... | just rightParent | just leftGrandParent with compTri (key n0) (key rightParent) | compTri (key parent) (key leftGrandParent) -- (key n0) (key rightParent) | (key parent) (key leftGrandParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
142 -- ... | EQ | EQ = popSingleLinkedStack s (\ s n1 → rotateLeft tree s (left n0) (just grandParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
143 -- (\ tree s n0 parent → insertCase5 tree s n0 rightParent grandParent next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
144 -- ... | _ | _ = insertCase41 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
145 ... | tri≈ ¬a b ¬c | tri≈ ¬a1 b1 ¬c1 = popSingleLinkedStack s (\ s n1 → rotateLeft tree s (left n0) (just grandParent) (\ tree s n0 parent → insertCase5 tree s n0 rightParent grandParent next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
146 ... | _ | _ = insertCase41 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
147 where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
148 insertCase41 : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Node a k → Node a k → Node a k → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
149 insertCase41 {n} {m} {t} {a} {k} tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
150 with (left parent) | (right grandParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
151 ... | nothing | _ = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
152 ... | _ | nothing = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
153 ... | just leftParent | just rightGrandParent with compTri (key n0) (key leftParent) | compTri (key parent) (key rightGrandParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
154 ... | tri≈ ¬a b ¬c | tri≈ ¬a1 b1 ¬c1 = popSingleLinkedStack s (\ s n1 → rotateRight tree s (right n0) (just grandParent) (\ tree s n0 parent → insertCase5 tree s n0 leftParent grandParent next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
155 ... | _ | _ = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
156 -- ... | EQ | EQ = popSingleLinkedStack s (\ s n1 → rotateRight tree s (right n0) (just grandParent)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
157 -- (\ tree s n0 parent → insertCase5 tree s n0 leftParent grandParent next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
158 -- ... | _ | _ = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
159
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
160 colorNode : {n : Level } {a : Set n} {k : ℕ} → Node a k → Color → Node a k
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
161 colorNode old c = record old { color = c }
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
162
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
163 {-# TERMINATING #-}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
164 insertNode : {n m : Level } {t : Set m } {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Node a k → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
165 insertNode {n} {m} {t} {a} {k} tree s n0 next = get2SingleLinkedStack s (insertCase1 n0)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
166 where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
167 insertCase1 : Node a k → SingleLinkedStack (Node a k) → Maybe (Node a k) → Maybe (Node a k) → t -- placed here to allow mutual recursion
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
168 -- http://agda.readthedocs.io/en/v2.5.2/language/mutual-recursion.html
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
169 insertCase3 : SingleLinkedStack (Node a k) → Node a k → Node a k → Node a k → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
170 insertCase3 s n0 parent grandParent with left grandParent | right grandParent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
171 ... | nothing | nothing = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
172 ... | nothing | just uncle = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
173 ... | just uncle | _ with compTri ( key uncle ) ( key parent )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
174 insertCase3 s n0 parent grandParent | just uncle | _ | tri≈ ¬a b ¬c = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
175 insertCase3 s n0 parent grandParent | just uncle | _ | tri< a ¬b ¬c with color uncle
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
176 insertCase3 s n0 parent grandParent | just uncle | _ | tri< a ¬b ¬c | Red = pop2SingleLinkedStack s ( \s p0 p1 → insertCase1 (
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
177 record grandParent { color = Red ; left = just ( record parent { color = Black } ) ; right = just ( record uncle { color = Black } ) }) s p0 p1 )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
178 insertCase3 s n0 parent grandParent | just uncle | _ | tri< a ¬b ¬c | Black = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
179 insertCase3 s n0 parent grandParent | just uncle | _ | tri> ¬a ¬b c with color uncle
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
180 insertCase3 s n0 parent grandParent | just uncle | _ | tri> ¬a ¬b c | Red = pop2SingleLinkedStack s ( \s p0 p1 → insertCase1 ( record grandParent { color = Red ; left = just ( record parent { color = Black } ) ; right = just ( record uncle { color = Black } ) }) s p0 p1 )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
181 insertCase3 s n0 parent grandParent | just uncle | _ | tri> ¬a ¬b c | Black = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
182 -- ... | EQ = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
183 -- ... | _ with color uncle
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
184 -- ... | Red = pop2SingleLinkedStack s ( \s p0 p1 → insertCase1 (
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
185 -- record grandParent { color = Red ; left = just ( record parent { color = Black } ) ; right = just ( record uncle { color = Black } ) }) s p0 p1 )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
186 -- ... | Black = insertCase4 tree s n0 parent grandParent next --!!
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
187 insertCase2 : SingleLinkedStack (Node a k) → Node a k → Node a k → Node a k → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
188 insertCase2 s n0 parent grandParent with color parent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
189 ... | Black = replaceNode tree s n0 next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
190 ... | Red = insertCase3 s n0 parent grandParent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
191 insertCase1 n0 s nothing nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
192 insertCase1 n0 s nothing (just grandParent) = next tree
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
193 insertCase1 n0 s (just parent) nothing = replaceNode tree s (colorNode n0 Black) next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
194 insertCase1 n0 s (just parent) (just grandParent) = insertCase2 s n0 parent grandParent
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
195
576
40d01b368e34 Temporary Push
ryokka
parents: 575
diff changeset
196
575
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
197 ----
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
198 -- find node potition to insert or to delete, the path will be in the stack
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
199 --
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
200 findNode : {n m : Level } {a : Set n} {k : ℕ} {t : Set m} → RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → (Node a k) → (Node a k) → (RedBlackTree {n} {m} {t} a k → SingleLinkedStack (Node a k) → Node a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
201 findNode {n} {m} {a} {k} {t} tree s n0 n1 next = pushSingleLinkedStack s n1 (\ s → findNode1 s n1)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
202 module FindNode where
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
203 findNode2 : SingleLinkedStack (Node a k) → (Maybe (Node a k)) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
204 findNode2 s nothing = next tree s n0
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
205 findNode2 s (just n) = findNode tree s n0 n next
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
206 findNode1 : SingleLinkedStack (Node a k) → (Node a k) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
207 findNode1 s n1 with (compTri (key n0) (key n1))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
208 findNode1 s n1 | tri< a ¬b ¬c = popSingleLinkedStack s ( \s _ → next tree s (record n1 { key = key n1 ; value = value n0 } ) )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
209 findNode1 s n1 | tri≈ ¬a b ¬c = findNode2 s (right n1)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
210 findNode1 s n1 | tri> ¬a ¬b c = findNode2 s (left n1)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
211 -- ... | EQ = popSingleLinkedStack s ( \s _ → next tree s (record n1 { key = key n1 ; value = value n0 } ) )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
212 -- ... | GT = findNode2 s (right n1)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
213 -- ... | LT = findNode2 s (left n1)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
214
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
215
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
216
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
217
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
218 leafNode : {n : Level } { a : Set n } → a → (k : ℕ) → (Node a k)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
219 leafNode v k1 = record { key = k1 ; value = v ; right = nothing ; left = nothing ; color = Red }
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
220
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
221 putRedBlackTree : {n m : Level} {t : Set m} {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} a k → {!!} → {!!} → (RedBlackTree {n} {m} {t} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
222 putRedBlackTree {n} {m} {t} {a} {k} tree val k1 next with (root tree)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
223 putRedBlackTree {n} {m} {t} {a} {k} tree val k1 next | nothing = next (record tree {root = just (leafNode {!!} {!!}) })
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
224 putRedBlackTree {n} {m} {t} {a} {k} tree val k1 next | just n2 = clearSingleLinkedStack (nodeStack tree) (λ s → findNode tree s (leafNode {!!} {!!}) n2 (λ tree1 s n1 → insertNode tree1 s n1 next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
225 -- putRedBlackTree {n} {m} {t} {a} {k} tree value k1 next with (root tree)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
226 -- ... | nothing = next (record tree {root = just (leafNode k1 value) })
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
227 -- ... | just n2 = clearSingleLinkedStack (nodeStack tree) (\ s → findNode tree s (leafNode k1 value) n2 (\ tree1 s n1 → insertNode tree1 s n1 next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
228
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
229
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
230 -- getRedBlackTree : {n m : Level } {t : Set m} {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} {A} a k → k → (RedBlackTree {n} {m} {t} {A} a k → (Maybe (Node a k)) → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
231 -- getRedBlackTree {_} {_} {t} {a} {k} tree k1 cs = checkNode (root tree)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
232 -- module GetRedBlackTree where -- http://agda.readthedocs.io/en/v2.5.2/language/let-and-where.html
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
233 -- search : Node a k → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
234 -- checkNode : Maybe (Node a k) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
235 -- checkNode nothing = cs tree nothing
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
236 -- checkNode (just n) = search n
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
237 -- search n with compTri k1 (key n)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
238 -- search n | tri< a ¬b ¬c = checkNode (left n)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
239 -- search n | tri≈ ¬a b ¬c = cs tree (just n)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
240 -- search n | tri> ¬a ¬b c = checkNode (right n)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
241
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
242
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
243
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
244 -- compareT : {A B C : Set } → ℕ → ℕ → Tri A B C
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
245 -- compareT x y with IsStrictTotalOrder.compare (Relation.Binary.StrictTotalOrder.isStrictTotalOrder <-strictTotalOrder) x y
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
246 -- compareT x y | tri< a ¬b ¬c = tri< {!!} {!!} {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
247 -- compareT x y | tri≈ ¬a b ¬c = {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
248 -- compareT x y | tri> ¬a ¬b c = {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
249 -- -- ... | tri≈ a b c = {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
250 -- -- ... | tri< a b c = {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
251 -- -- ... | tri> a b c = {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
252
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
253 -- compare2 : (x y : ℕ ) → CompareResult {Level.zero}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
254 -- compare2 zero zero = EQ
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
255 -- compare2 (suc _) zero = GT
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
256 -- compare2 zero (suc _) = LT
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
257 -- compare2 (suc x) (suc y) = compare2 x y
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
258
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
259 -- -- putUnblanceTree : {n m : Level } {a : Set n} {k : ℕ} {t : Set m} → RedBlackTree {n} {m} {t} {A} a k → k → a → (RedBlackTree {n} {m} {t} {A} a k → t) → t
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
260 -- -- putUnblanceTree {n} {m} {A} {a} {k} {t} tree k1 value next with (root tree)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
261 -- -- ... | nothing = next (record tree {root = just (leafNode k1 value) })
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
262 -- -- ... | just n2 = clearSingleLinkedStack (nodeStack tree) (λ s → findNode tree s (leafNode k1 value) n2 (λ tree1 s n1 → replaceNode tree1 s n1 next))
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
263
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
264 -- -- checkT : {m : Level } (n : Maybe (Node ℕ ℕ)) → ℕ → Bool
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
265 -- -- checkT nothing _ = false
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
266 -- -- checkT (just n) x with compTri (value n) x
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
267 -- -- ... | tri≈ _ _ _ = true
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
268 -- -- ... | _ = false
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
269
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
270 -- -- checkEQ : {m : Level } ( x : ℕ ) -> ( n : Node ℕ ℕ ) -> (value n ) ≡ x -> checkT {m} (just n) x ≡ true
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
271 -- -- checkEQ x n refl with compTri (value n) x
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
272 -- -- ... | tri≈ _ refl _ = refl
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
273 -- -- ... | tri> _ neq gt = ⊥-elim (neq refl)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
274 -- -- ... | tri< lt neq _ = ⊥-elim (neq refl)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
275
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
276
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
277 createEmptyRedBlackTreeℕ : {n m : Level} {t : Set m} (a : Set n) (b : ℕ)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
278 → RedBlackTree {n} {m} {t} a b
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
279 createEmptyRedBlackTreeℕ a b = record {
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
280 root = nothing
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
281 ; nodeStack = emptySingleLinkedStack
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
282 -- ; nodeComp = λ x x₁ → {!!}
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
283
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
284 }
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
285
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
286 -- ( x y : ℕ ) -> Tri ( x < y ) ( x ≡ y ) ( x > y )
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
287
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
288 -- test = (λ x → (createEmptyRedBlackTreeℕ x x)
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
289
576
40d01b368e34 Temporary Push
ryokka
parents: 575
diff changeset
290 -- ts = createEmptyRedBlackTreeℕ {ℕ} {?} {!!} 0
575
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
291
73fc32092b64 push local rbtree
ryokka
parents:
diff changeset
292 -- tes = putRedBlackTree {_} {_} {_} (createEmptyRedBlackTreeℕ {_} {_} {_} 3 3) 2 2 (λ t → t)