@@ -9,7 +9,7 @@ module Dict (empty,singleton,insert
11import Maybe as Maybe
11import Maybe as Maybe
12−import JavaScript as JavaScript
12+import Native.Error as Error
14data NColor = Red | Black
14data NColor = Red | Black
@@ -18,8 +18,6 @@ data RBTree k v = RBNode NColor k v (RBTree k v) (RBTree k v) | RBEmpty
18empty : RBTree k v
18empty : RBTree k v
19empty = RBEmpty
19empty = RBEmpty
21−raise err = throw (JavaScript.castStringToJSString err)
23{-- Helpers for checking invariants
21{-- Helpers for checking invariants
25-- Check that the tree has an equal number of black nodes on each path
23-- Check that the tree has an equal number of black nodes on each path
@@ -104,14 +102,14 @@ min t =
104 case t of
102 case t of
105 RBNode _ k v RBEmpty _ -> (k,v)
103 RBNode _ k v RBEmpty _ -> (k,v)
106 RBNode _ _ _ l _ -> min l
104 RBNode _ _ _ l _ -> min l
107− RBEmpty -> raise "(min RBEmpty) is not defined"
105+ RBEmpty -> Error.raise "(min RBEmpty) is not defined"
111 case t of
109 case t of
112 { RBNode _ k v _ RBEmpty -> (k,v)
110 { RBNode _ k v _ RBEmpty -> (k,v)
113 ; RBNode _ _ _ _ r -> max r
111 ; RBNode _ _ _ _ r -> max r
114− ; RBEmpty -> raise "(max RBEmpty) is not defined"
112+ ; RBEmpty -> Error.raise "(max RBEmpty) is not defined"
@@ -138,7 +136,7 @@ findWithDefault base k t =
139find k t =
137find k t =
140 case t of
138 case t of
141− { RBEmpty -> raise "Key was not found in dictionary!"
139+ { RBEmpty -> Error.raise "Key was not found in dictionary!"
142 ; RBNode _ k' v l r ->
140 ; RBNode _ k' v l r ->
143 case compare k k' of
141 case compare k k' of
144 { LT -> find k l
142 { LT -> find k l
@@ -155,7 +153,7 @@ rotateLeft : RBTree k v -> RBTree k v
155rotateLeft t =
153rotateLeft t =
156 case t of
154 case t of
157 RBNode cy ky vy a (RBNode cz kz vz b c) -> RBNode cy kz vz (RBNode Red ky vy a b) c
155 RBNode cy ky vy a (RBNode cz kz vz b c) -> RBNode cy kz vz (RBNode Red ky vy a b) c
158− _ -> raise "rotateLeft of a node without enough children"
156+ _ -> Error.raise "rotateLeft of a node without enough children"
160-- rotateRight -- the reverse, and
158-- rotateRight -- the reverse, and
161-- makes Y have Z's color, and makes Z Red.
159-- makes Y have Z's color, and makes Z Red.
@@ -163,7 +161,7 @@ rotateRight : RBTree k v -> RBTree k v
163rotateRight t =
161rotateRight t =
164 case t of
162 case t of
165 RBNode cz kz vz (RBNode cy ky vy a b) c -> RBNode cz ky vy a (RBNode Red kz vz b c)
163 RBNode cz kz vz (RBNode cy ky vy a b) c -> RBNode cz ky vy a (RBNode Red kz vz b c)
166− _ -> raise "rotateRight of a node without enough children"
164+ _ -> Error.raise "rotateRight of a node without enough children"
168rotateLeftIfNeeded : RBTree k v -> RBTree k v
166rotateLeftIfNeeded : RBTree k v -> RBTree k v
169rotateLeftIfNeeded t =
167rotateLeftIfNeeded t =
@@ -186,7 +184,7 @@ color_flip t =
186 RBNode (otherColor c1) bk bv
184 RBNode (otherColor c1) bk bv
187 (RBNode (otherColor c2) ak av la ra)
185 (RBNode (otherColor c2) ak av la ra)
188 (RBNode (otherColor c3) ck cv lc rc)
186 (RBNode (otherColor c3) ck cv lc rc)
189− _ -> raise "color_flip called on a RBEmpty or RBNode with a RBEmpty child"
187+ _ -> Error.raise "color_flip called on a RBEmpty or RBNode with a RBEmpty child"
191color_flipIfNeeded : RBTree k v -> RBTree k v
189color_flipIfNeeded : RBTree k v -> RBTree k v
192color_flipIfNeeded t =
190color_flipIfNeeded t =
@@ -217,10 +215,10 @@ insert k v t =
217 in ensureBlackRoot (ins t)
215 in ensureBlackRoot (ins t)
219 if not (invariants_hold t) then
217 if not (invariants_hold t) then
220− raise "invariants broken before insert"
218+ Error.raise "invariants broken before insert"
221 else (let new_t = ensureBlackRoot (ins t) in
219 else (let new_t = ensureBlackRoot (ins t) in
222 if not (invariants_hold new_t) then
220 if not (invariants_hold new_t) then
223− raise "invariants broken after insert"
221+ Error.raise "invariants broken after insert"
224 else new_t)
222 else new_t)
@@ -311,14 +309,14 @@ remove k t =
311 eq t = case t of { RBNode _ k' _ _ _ -> k == k' ; _ -> False }
309 eq t = case t of { RBNode _ k' _ _ _ -> k == k' ; _ -> False }
312 delLT t = case moveRedLeftIfNeeded t of
310 delLT t = case moveRedLeftIfNeeded t of
313 RBNode c k' v l r -> fixUp (RBNode c k' v (del l) r)
311 RBNode c k' v l r -> fixUp (RBNode c k' v (del l) r)
314− RBEmpty -> raise "delLT on RBEmpty"
312+ RBEmpty -> Error.raise "delLT on RBEmpty"
315 delEQ t = case t of -- Replace with successor
313 delEQ t = case t of -- Replace with successor
316 RBNode c _ _ l r -> let (k',v') = min r in
314 RBNode c _ _ l r -> let (k',v') = min r in
317 fixUp (RBNode c k' v' l (deleteMin r))
315 fixUp (RBNode c k' v' l (deleteMin r))
318− RBEmpty -> raise "delEQ called on a RBEmpty"
316+ RBEmpty -> Error.raise "delEQ called on a RBEmpty"
319 delGT t = case t of
317 delGT t = case t of
320 RBNode c k' v l r -> fixUp (RBNode c k' v l (del r))
318 RBNode c k' v l r -> fixUp (RBNode c k' v l (del r))
321− RBEmpty -> raise "delGT called on a RBEmpty"
319+ RBEmpty -> Error.raise "delGT called on a RBEmpty"
322 del t = case t of
320 del t = case t of
323 RBEmpty -> RBEmpty
321 RBEmpty -> RBEmpty
324 RBNode _ k' _ _ _ ->
322 RBNode _ k' _ _ _ ->
@@ -330,10 +328,10 @@ remove k t =
330 in if member k t then ensureBlackRoot (del t) else t
328 in if member k t then ensureBlackRoot (del t) else t
332 if not (invariants_hold t) then
330 if not (invariants_hold t) then
333− raise "invariants broken before remove"
331+ Error.raise "invariants broken before remove"
334 else (let t' = ensureBlackRoot (del t) in
332 else (let t' = ensureBlackRoot (del t) in
335 if invariants_hold t' then t' else
333 if invariants_hold t' then t' else
336− raise "invariants broken after remove")
334+ Error.raise "invariants broken after remove")
339map : (a -> b) -> RBTree k a -> RBTree k b
337map : (a -> b) -> RBTree k a -> RBTree k b