3-- TODO: write merge and split
5data Tree : Nat -> Type -> Type -> Type where
6 Leaf : k -> v -> Tree Z k v
7 Branch2 : Tree n k v -> k -> Tree n k v -> Tree (S n) k v
8 Branch3 : Tree n k v -> k -> Tree n k v -> k -> Tree n k v -> Tree (S n) k v
16branch4 a b c d e f g =
17 Branch2 (Branch2 a b c) d (Branch2 e f g)
26branch5 a b c d e f g h i =
27 Branch2 (Branch2 a b c) d (Branch3 e f g h i)
37branch6 a b c d e f g h i j k =
38 Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
49branch7 a b c d e f g h i j k l m =
50 Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
52merge1 : Tree n k v -> k -> Tree (S n) k v -> k -> Tree (S n) k v -> Tree (S (S n)) k v
53merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
54merge1 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
55merge1 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
56merge1 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
58merge2 : Tree (S n) k v -> k -> Tree n k v -> k -> Tree (S n) k v -> Tree (S (S n)) k v
59merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
60merge2 (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
61merge2 (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
62merge2 (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
64merge3 : Tree (S n) k v -> k -> Tree (S n) k v -> k -> Tree n k v -> Tree (S (S n)) k v
65merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
66merge3 (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
67merge3 (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
68merge3 (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
70treeLookup : Ord k => k -> Tree n k v -> Maybe v
71treeLookup k (Leaf k' v) =
76treeLookup k (Branch2 t1 k' t2) =
81treeLookup k (Branch3 t1 k1 t2 k2 t3) =
89treeInsert' : Ord k => k -> v -> Tree n k v -> Either (Tree n k v) (Tree n k v, k, Tree n k v)
90treeInsert' k v (Leaf k' v') =
92 LT => Right (Leaf k v, k, Leaf k' v')
94 GT => Right (Leaf k' v', k', Leaf k v)
95treeInsert' k v (Branch2 t1 k' t2) =
97 case treeInsert' k v t1 of
98 Left t1' => Left (Branch2 t1' k' t2)
99 Right (a, b, c) => Left (Branch3 a b c k' t2)
101 case treeInsert' k v t2 of
102 Left t2' => Left (Branch2 t1 k' t2')
103 Right (a, b, c) => Left (Branch3 t1 k' a b c)
104treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
106 case treeInsert' k v t1 of
107 Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
108 Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
111 case treeInsert' k v t2 of
112 Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
113 Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
115 case treeInsert' k v t3 of
116 Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
117 Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
119treeInsert : Ord k => k -> v -> Tree n k v -> Either (Tree n k v) (Tree (S n) k v)
121 case treeInsert' k v t of
123 Right (a, b, c) => Right (Branch2 a b c)
125delType : Nat -> Type -> Type -> Type
127delType (S n) k v = Tree n k v
129treeDelete : Ord k => k -> Tree n k v -> Either (Tree n k v) (delType n k v)
130treeDelete k (Leaf k' v) =
135treeDelete {n=S Z} k (Branch2 t1 k' t2) =
137 case treeDelete k t1 of
138 Left t1' => Left (Branch2 t1' k' t2)
141 case treeDelete k t2 of
142 Left t2' => Left (Branch2 t1 k' t2')
144treeDelete {n=S Z} k (Branch3 t1 k1 t2 k2 t3) =
146 case treeDelete k t1 of
147 Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
148 Right () => Left (Branch2 t2 k2 t3)
150 case treeDelete k t2 of
151 Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
152 Right () => Left (Branch2 t1 k1 t3)
154 case treeDelete k t3 of
155 Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
156 Right () => Left (Branch2 t1 k1 t2)
157treeDelete {n=S (S _)} k (Branch2 t1 k' t2) =
159 case treeDelete k t1 of
160 Left t1' => Left (Branch2 t1' k' t2)
163 Branch2 a b c => Right (Branch3 t1' k' a b c)
164 Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
166 case treeDelete k t2 of
167 Left t2' => Left (Branch2 t1 k' t2')
170 Branch2 a b c => Right (Branch3 a b c k' t2')
171 Branch3 a b c d e => Left (branch4 a b c d e k' t2')
172treeDelete {n=(S (S _))} k (Branch3 t1 k1 t2 k2 t3) =
174 case treeDelete k t1 of
175 Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
176 Right t1' => Left (merge1 t1' k1 t2 k2 t3)
178 case treeDelete k t2 of
179 Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
180 Right t2' => Left (merge2 t1 k1 t2' k2 t3)
182 case treeDelete k t3 of
183 Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
184 Right t3' => Left (merge3 t1 k1 t2 k2 t3')
186treeToList : Tree n k v -> List (k, v)
187treeToList = treeToList' (:: [])
189 treeToList' : ((k, v) -> List (k, v)) -> Tree n k v -> List (k, v)
190 treeToList' cont (Leaf k v) = cont (k, v)
191 treeToList' cont (Branch2 t1 _ t2) = treeToList' (:: treeToList' cont t2) t1
192 treeToList' cont (Branch3 t1 _ t2 _ t3) = treeToList' (:: treeToList' (:: treeToList' cont t3) t2) t1
194data SortedMap : Type -> Type -> Type where
195 Empty : SortedMap k v
196 M : (n:Nat) -> Tree n k v -> SortedMap k v
201lookup : Ord k => k -> SortedMap k v -> Maybe v
202lookup _ Empty = Nothing
203lookup k (M _ t) = treeLookup k t
205insert : Ord k => k -> v -> SortedMap k v -> SortedMap k v
206insert k v Empty = M Z (Leaf k v)
208 case treeInsert k v t of
212delete : Ord k => k -> SortedMap k v -> SortedMap k v
213delete _ Empty = Empty
215 case treeDelete k t of
218delete k (M (S _) t) =
219 case treeDelete k t of
223fromList : Ord k => List (k, v) -> SortedMap k v
224fromList l = foldl (flip (uncurry insert)) empty l
226toList : SortedMap k v -> List (k, v)
228toList (M _ t) = treeToList t
230instance Functor (Tree n k) where
231 map f (Leaf k v) = Leaf k (f v)
232 map f (Branch2 t1 k t2) = Branch2 (map f t1) k (map f t2)
233 map f (Branch3 t1 k1 t2 k2 t3) = Branch3 (map f t1) k1 (map f t2) k2 (map f t3)
235instance Functor (SortedMap k) where
237 map f (M n t) = M _ (map 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