3-- TODO: write merge and split
6data Tree : Nat -> (k : Type) -> Type -> Ord k -> Type where
7 Leaf : k -> v -> Tree Z k v o
8 Branch2 : Tree n k v o -> k -> Tree n k v o -> Tree (S n) k v o
9 Branch3 : Tree n k v o -> k -> Tree n k v o -> k -> Tree n k v o -> Tree (S n) k v o
17branch4 a b c d e f g =
18 Branch2 (Branch2 a b c) d (Branch2 e f g)
27branch5 a b c d e f g h i =
28 Branch2 (Branch2 a b c) d (Branch3 e f g h i)
38branch6 a b c d e f g h i j k =
39 Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
50branch7 a b c d e f g h i j k l m =
51 Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
53merge1 : Tree n k v o -> k -> Tree (S n) k v o -> k -> Tree (S n) k v o -> Tree (S (S n)) k v o
54merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
55merge1 a b (Branch2 c d e) f (Branch3 g h i j k) = branch6 a b c d e f g h i j k
56merge1 a b (Branch3 c d e f g) h (Branch2 i j k) = branch6 a b c d e f g h i j k
57merge1 a b (Branch3 c d e f g) h (Branch3 i j k l m) = branch7 a b c d e f g h i j k l m
59merge2 : Tree (S n) k v o -> k -> Tree n k v o -> k -> Tree (S n) k v o -> Tree (S (S n)) k v o
60merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
61merge2 (Branch2 a b c) d e f (Branch3 g h i j k) = branch6 a b c d e f g h i j k
62merge2 (Branch3 a b c d e) f g h (Branch2 i j k) = branch6 a b c d e f g h i j k
63merge2 (Branch3 a b c d e) f g h (Branch3 i j k l m) = branch7 a b c d e f g h i j k l m
65merge3 : Tree (S n) k v o -> k -> Tree (S n) k v o -> k -> Tree n k v o -> Tree (S (S n)) k v o
66merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
67merge3 (Branch2 a b c) d (Branch3 e f g h i) j k = branch6 a b c d e f g h i j k
68merge3 (Branch3 a b c d e) f (Branch2 g h i) j k = branch6 a b c d e f g h i j k
69merge3 (Branch3 a b c d e) f (Branch3 g h i j k) l m = branch7 a b c d e f g h i j k l m
71treeLookup : k -> Tree n k v o -> Maybe v
72treeLookup k (Leaf k' v) =
77treeLookup k (Branch2 t1 k' t2) =
82treeLookup k (Branch3 t1 k1 t2 k2 t3) =
90treeInsert' : k -> v -> Tree n k v o -> Either (Tree n k v o) (Tree n k v o, k, Tree n k v o)
91treeInsert' k v (Leaf k' v') =
93 LT => Right (Leaf k v, k, Leaf k' v')
95 GT => Right (Leaf k' v', k', Leaf k v)
96treeInsert' k v (Branch2 t1 k' t2) =
98 case treeInsert' k v t1 of
99 Left t1' => Left (Branch2 t1' k' t2)
100 Right (a, b, c) => Left (Branch3 a b c k' t2)
102 case treeInsert' k v t2 of
103 Left t2' => Left (Branch2 t1 k' t2')
104 Right (a, b, c) => Left (Branch3 t1 k' a b c)
105treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
107 case treeInsert' k v t1 of
108 Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
109 Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
112 case treeInsert' k v t2 of
113 Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
114 Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
116 case treeInsert' k v t3 of
117 Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
118 Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
120treeInsert : k -> v -> Tree n k v o -> Either (Tree n k v o) (Tree (S n) k v o)
122 case treeInsert' k v t of
124 Right (a, b, c) => Right (Branch2 a b c)
126delType : Nat -> (k : Type) -> Type -> Ord k -> Type
128delType (S n) k v o = Tree n k v o
130treeDelete : k -> Tree n k v o -> Either (Tree n k v o) (delType n k v o)
131treeDelete k (Leaf k' v) =
136treeDelete {n=S Z} k (Branch2 t1 k' t2) =
138 case treeDelete k t1 of
139 Left t1' => Left (Branch2 t1' k' t2)
142 case treeDelete k t2 of
143 Left t2' => Left (Branch2 t1 k' t2')
145treeDelete {n=S Z} k (Branch3 t1 k1 t2 k2 t3) =
147 case treeDelete k t1 of
148 Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
149 Right () => Left (Branch2 t2 k2 t3)
151 case treeDelete k t2 of
152 Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
153 Right () => Left (Branch2 t1 k1 t3)
155 case treeDelete k t3 of
156 Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
157 Right () => Left (Branch2 t1 k1 t2)
158treeDelete {n=S (S _)} k (Branch2 t1 k' t2) =
160 case treeDelete k t1 of
161 Left t1' => Left (Branch2 t1' k' t2)
164 Branch2 a b c => Right (Branch3 t1' k' a b c)
165 Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
167 case treeDelete k t2 of
168 Left t2' => Left (Branch2 t1 k' t2')
171 Branch2 a b c => Right (Branch3 a b c k' t2')
172 Branch3 a b c d e => Left (branch4 a b c d e k' t2')
173treeDelete {n=(S (S _))} k (Branch3 t1 k1 t2 k2 t3) =
175 case treeDelete k t1 of
176 Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
177 Right t1' => Left (merge1 t1' k1 t2 k2 t3)
179 case treeDelete k t2 of
180 Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
181 Right t2' => Left (merge2 t1 k1 t2' k2 t3)
183 case treeDelete k t3 of
184 Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
185 Right t3' => Left (merge3 t1 k1 t2 k2 t3')
187treeToList : Tree n k v o -> List (k, v)
188treeToList = treeToList' (:: [])
190 treeToList' : ((k, v) -> List (k, v)) -> Tree n k v o -> List (k, v)
191 treeToList' cont (Leaf k v) = cont (k, v)
192 treeToList' cont (Branch2 t1 _ t2) = treeToList' (:: treeToList' cont t2) t1
193 treeToList' cont (Branch3 t1 _ t2 _ t3) = treeToList' (:: treeToList' (:: treeToList' cont t3) t2) t1
196data SortedMap : Type -> Type -> Type where
197 Empty : Ord k => SortedMap k v
198 M : (o : Ord k) => (n:Nat) -> Tree n k v o -> SortedMap k v
201empty : Ord k => SortedMap k v
205lookup : k -> SortedMap k v -> Maybe v
206lookup _ Empty = Nothing
207lookup k (M _ t) = treeLookup k t
210insert : k -> v -> SortedMap k v -> SortedMap k v
211insert k v Empty = M Z (Leaf k v)
213 case treeInsert k v t of
218delete : k -> SortedMap k v -> SortedMap k v
219delete _ Empty = Empty
221 case treeDelete k t of
224delete k (M (S _) t) =
225 case treeDelete k t of
230fromList : Ord k => List (k, v) -> SortedMap k v
231fromList l = foldl (flip (uncurry insert)) empty l
234toList : SortedMap k v -> List (k, v)
236toList (M _ t) = treeToList t
239treeMap : (a -> b) -> Tree n k a o -> Tree n k b o
240treeMap f (Leaf k v) = Leaf k (f v)
241treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
242treeMap f (Branch3 t1 k1 t2 k2 t3)
243 = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
245instance Functor (SortedMap k) where
247 map f (M n t) = M _ (treeMap f t)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list