[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / utils / Util.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Util]{Highly random utility functions}
5
6 \begin{code}
7 -- IF_NOT_GHC is meant to make this module useful outside the context of GHC
8 #define IF_NOT_GHC(a)
9
10 module Util (
11         -- The Eager monad
12         Eager, thenEager, returnEager, mapEager, appEager, runEager,
13
14         -- general list processing
15         IF_NOT_GHC(forall COMMA exists COMMA)
16         zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
17         zipLazy, stretchZipEqual,
18         mapAndUnzip, mapAndUnzip3,
19         nOfThem, lengthExceeds, isSingleton,
20         startsWith, endsWith, snocView,
21         isIn, isn'tIn,
22
23         -- association lists
24         assoc, assocUsing, assocDefault, assocDefaultUsing,
25
26         -- duplicate handling
27         hasNoDups, equivClasses, runs, removeDups,
28
29         -- sorting
30         IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
31         sortLt,
32         IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
33         IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
34
35         -- transitive closures
36         transitiveClosure,
37
38         -- accumulating
39         mapAccumL, mapAccumR, mapAccumB,
40
41         -- comparisons
42         thenCmp, cmpList,
43         FastString,
44
45         -- pairs
46         IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
47         IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
48         unzipWith,
49
50         -- tracing (abstract away from lib home)
51         trace,
52
53         -- error handling
54         panic, panic#, assertPanic
55
56     ) where
57
58 #include "HsVersions.h"
59
60 import FastString       ( FastString )
61 import List             ( zipWith4 )
62 import GlaExts          ( trace )
63
64 infixr 9 `thenCmp`
65 \end{code}
66
67 %************************************************************************
68 %*                                                                      *
69 \subsection{The Eager monad}
70 %*                                                                      *
71 %************************************************************************
72
73 The @Eager@ monad is just an encoding of continuation-passing style,
74 used to allow you to express "do this and then that", mainly to avoid
75 space leaks. It's done with a type synonym to save bureaucracy.
76
77 \begin{code}
78 type Eager ans a = (a -> ans) -> ans
79
80 runEager :: Eager a a -> a
81 runEager m = m (\x -> x)
82
83 appEager :: Eager ans a -> (a -> ans) -> ans
84 appEager m cont = m cont
85
86 thenEager :: Eager ans a -> (a -> Eager ans b) -> Eager ans b
87 thenEager m k cont = m (\r -> k r cont)
88
89 returnEager :: a -> Eager ans a
90 returnEager v cont = cont v
91
92 mapEager :: (a -> Eager ans b) -> [a] -> Eager ans [b]
93 mapEager f [] = returnEager []
94 mapEager f (x:xs) = f x                 `thenEager` \ y ->
95                     mapEager f xs       `thenEager` \ ys ->
96                     returnEager (y:ys)
97 \end{code}
98
99 %************************************************************************
100 %*                                                                      *
101 \subsection[Utils-lists]{General list processing}
102 %*                                                                      *
103 %************************************************************************
104
105 Quantifiers are not standard in Haskell. The following fill in the gap.
106
107 \begin{code}
108 forall :: (a -> Bool) -> [a] -> Bool
109 forall pred []     = True
110 forall pred (x:xs) = pred x && forall pred xs
111
112 exists :: (a -> Bool) -> [a] -> Bool
113 exists pred []     = False
114 exists pred (x:xs) = pred x || exists pred xs
115 \end{code}
116
117 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
118 are of equal length.  Alastair Reid thinks this should only happen if
119 DEBUGging on; hey, why not?
120
121 \begin{code}
122 zipEqual        :: String -> [a] -> [b] -> [(a,b)]
123 zipWithEqual    :: String -> (a->b->c) -> [a]->[b]->[c]
124 zipWith3Equal   :: String -> (a->b->c->d) -> [a]->[b]->[c]->[d]
125 zipWith4Equal   :: String -> (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
126
127 #ifndef DEBUG
128 zipEqual      _ = zip
129 zipWithEqual  _ = zipWith
130 zipWith3Equal _ = zipWith3
131 zipWith4Equal _ = zipWith4
132 #else
133 zipEqual msg []     []     = []
134 zipEqual msg (a:as) (b:bs) = (a,b) : zipEqual msg as bs
135 zipEqual msg as     bs     = panic ("zipEqual: unequal lists:"++msg)
136
137 zipWithEqual msg z (a:as) (b:bs)=  z a b : zipWithEqual msg z as bs
138 zipWithEqual msg _ [] []        =  []
139 zipWithEqual msg _ _ _          =  panic ("zipWithEqual: unequal lists:"++msg)
140
141 zipWith3Equal msg z (a:as) (b:bs) (c:cs)
142                                 =  z a b c : zipWith3Equal msg z as bs cs
143 zipWith3Equal msg _ [] []  []   =  []
144 zipWith3Equal msg _ _  _   _    =  panic ("zipWith3Equal: unequal lists:"++msg)
145
146 zipWith4Equal msg z (a:as) (b:bs) (c:cs) (d:ds)
147                                 =  z a b c d : zipWith4Equal msg z as bs cs ds
148 zipWith4Equal msg _ [] [] [] [] =  []
149 zipWith4Equal msg _ _  _  _  _  =  panic ("zipWith4Equal: unequal lists:"++msg)
150 #endif
151 \end{code}
152
153 \begin{code}
154 -- zipLazy is lazy in the second list (observe the ~)
155
156 zipLazy :: [a] -> [b] -> [(a,b)]
157 zipLazy [] ys = []
158 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
159 \end{code}
160
161
162 \begin{code}
163 stretchZipEqual :: (a -> b -> Maybe a) -> [a] -> [b] -> [a]
164 -- (stretchZipEqual f xs ys) stretches ys to "fit" the places where f returns a Just
165
166 stretchZipEqual f [] [] = []
167 stretchZipEqual f (x:xs) (y:ys) = case f x y of
168                                     Just x' -> x' : stretchZipEqual f xs ys
169                                     Nothing -> x  :  stretchZipEqual f xs (y:ys)
170 \end{code}
171
172
173 \begin{code}
174 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
175
176 mapAndUnzip f [] = ([],[])
177 mapAndUnzip f (x:xs)
178   = let
179         (r1,  r2)  = f x
180         (rs1, rs2) = mapAndUnzip f xs
181     in
182     (r1:rs1, r2:rs2)
183
184 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
185
186 mapAndUnzip3 f [] = ([],[],[])
187 mapAndUnzip3 f (x:xs)
188   = let
189         (r1,  r2,  r3)  = f x
190         (rs1, rs2, rs3) = mapAndUnzip3 f xs
191     in
192     (r1:rs1, r2:rs2, r3:rs3)
193 \end{code}
194
195 \begin{code}
196 nOfThem :: Int -> a -> [a]
197 nOfThem n thing = take n (repeat thing)
198
199 lengthExceeds :: [a] -> Int -> Bool
200
201 []      `lengthExceeds` n =  0 > n
202 (x:xs)  `lengthExceeds` n = (1 > n) || (xs `lengthExceeds` (n - 1))
203
204 isSingleton :: [a] -> Bool
205
206 isSingleton [x] = True
207 isSingleton  _  = False
208
209 startsWith, endsWith :: String -> String -> Maybe String
210
211 startsWith []     str = Just str
212 startsWith (c:cs) (s:ss)
213   = if c /= s then Nothing else startsWith cs ss
214 startsWith  _     []  = Nothing
215
216 endsWith cs ss
217   = case (startsWith (reverse cs) (reverse ss)) of
218       Nothing -> Nothing
219       Just rs -> Just (reverse rs)
220 \end{code}
221
222 \begin{code}
223 snocView :: [a] -> ([a], a)     -- Split off the last element
224 snocView xs = go xs []
225             where
226               go [x]    acc = (reverse acc, x)
227               go (x:xs) acc = go xs (x:acc)
228 \end{code}
229
230 Debugging/specialising versions of \tr{elem} and \tr{notElem}
231
232 \begin{code}
233 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
234
235 # ifndef DEBUG
236 isIn    msg x ys = elem__    x ys
237 isn'tIn msg x ys = notElem__ x ys
238
239 --these are here to be SPECIALIZEd (automagically)
240 elem__ _ []     = False
241 elem__ x (y:ys) = x==y || elem__ x ys
242
243 notElem__ x []     =  True
244 notElem__ x (y:ys) =  x /= y && notElem__ x ys
245
246 # else {- DEBUG -}
247 isIn msg x ys
248   = elem ILIT(0) x ys
249   where
250     elem i _ []     = False
251     elem i x (y:ys)
252       | i _GE_ ILIT(100) = panic ("Over-long elem in: " ++ msg)
253       | otherwise        = x == y || elem (i _ADD_ ILIT(1)) x ys
254
255 isn'tIn msg x ys
256   = notElem ILIT(0) x ys
257   where
258     notElem i x [] =  True
259     notElem i x (y:ys)
260       | i _GE_ ILIT(100) = panic ("Over-long notElem in: " ++ msg)
261       | otherwise        =  x /= y && notElem (i _ADD_ ILIT(1)) x ys
262
263 # endif {- DEBUG -}
264
265 \end{code}
266
267 %************************************************************************
268 %*                                                                      *
269 \subsection[Utils-assoc]{Association lists}
270 %*                                                                      *
271 %************************************************************************
272
273 See also @assocMaybe@ and @mkLookupFun@ in module @Maybes@.
274
275 \begin{code}
276 assoc             :: (Eq a) => String -> [(a, b)] -> a -> b
277 assocDefault      :: (Eq a) => b -> [(a, b)] -> a -> b
278 assocUsing        :: (a -> a -> Bool) -> String -> [(a, b)] -> a -> b
279 assocDefaultUsing :: (a -> a -> Bool) -> b -> [(a, b)] -> a -> b
280
281 assocDefaultUsing eq deflt ((k,v) : rest) key
282   | k `eq` key = v
283   | otherwise  = assocDefaultUsing eq deflt rest key
284
285 assocDefaultUsing eq deflt [] key = deflt
286
287 assoc crash_msg         list key = assocDefaultUsing (==) (panic ("Failed in assoc: " ++ crash_msg)) list key
288 assocDefault deflt      list key = assocDefaultUsing (==) deflt list key
289 assocUsing eq crash_msg list key = assocDefaultUsing eq (panic ("Failed in assoc: " ++ crash_msg)) list key
290 \end{code}
291
292 %************************************************************************
293 %*                                                                      *
294 \subsection[Utils-dups]{Duplicate-handling}
295 %*                                                                      *
296 %************************************************************************
297
298 \begin{code}
299 hasNoDups :: (Eq a) => [a] -> Bool
300
301 hasNoDups xs = f [] xs
302   where
303     f seen_so_far []     = True
304     f seen_so_far (x:xs) = if x `is_elem` seen_so_far then
305                                 False
306                            else
307                                 f (x:seen_so_far) xs
308
309     is_elem = isIn "hasNoDups"
310 \end{code}
311
312 \begin{code}
313 equivClasses :: (a -> a -> Ordering)    -- Comparison
314              -> [a]
315              -> [[a]]
316
317 equivClasses cmp stuff@[]     = []
318 equivClasses cmp stuff@[item] = [stuff]
319 equivClasses cmp items
320   = runs eq (sortLt lt items)
321   where
322     eq a b = case cmp a b of { EQ -> True; _ -> False }
323     lt a b = case cmp a b of { LT -> True; _ -> False }
324 \end{code}
325
326 The first cases in @equivClasses@ above are just to cut to the point
327 more quickly...
328
329 @runs@ groups a list into a list of lists, each sublist being a run of
330 identical elements of the input list. It is passed a predicate @p@ which
331 tells when two elements are equal.
332
333 \begin{code}
334 runs :: (a -> a -> Bool)        -- Equality
335      -> [a]
336      -> [[a]]
337
338 runs p []     = []
339 runs p (x:xs) = case (span (p x) xs) of
340                   (first, rest) -> (x:first) : (runs p rest)
341 \end{code}
342
343 \begin{code}
344 removeDups :: (a -> a -> Ordering)      -- Comparison function
345            -> [a]
346            -> ([a],     -- List with no duplicates
347                [[a]])   -- List of duplicate groups.  One representative from
348                         -- each group appears in the first result
349
350 removeDups cmp []  = ([], [])
351 removeDups cmp [x] = ([x],[])
352 removeDups cmp xs
353   = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
354     (xs', dups) }
355   where
356     collect_dups dups_so_far [x]         = (dups_so_far,      x)
357     collect_dups dups_so_far dups@(x:xs) = (dups:dups_so_far, x)
358 \end{code}
359
360
361 %************************************************************************
362 %*                                                                      *
363 \subsection[Utils-sorting]{Sorting}
364 %*                                                                      *
365 %************************************************************************
366
367 %************************************************************************
368 %*                                                                      *
369 \subsubsection[Utils-quicksorting]{Quicksorts}
370 %*                                                                      *
371 %************************************************************************
372
373 \begin{code}
374 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
375 quicksort :: (a -> a -> Bool)           -- Less-than predicate
376           -> [a]                        -- Input list
377           -> [a]                        -- Result list in increasing order
378
379 quicksort lt []      = []
380 quicksort lt [x]     = [x]
381 quicksort lt (x:xs)  = split x [] [] xs
382   where
383     split x lo hi []                 = quicksort lt lo ++ (x : quicksort lt hi)
384     split x lo hi (y:ys) | y `lt` x  = split x (y:lo) hi ys
385                          | True      = split x lo (y:hi) ys
386 \end{code}
387
388 Quicksort variant from Lennart's Haskell-library contribution.  This
389 is a {\em stable} sort.
390
391 \begin{code}
392 stableSortLt = sortLt   -- synonym; when we want to highlight stable-ness
393
394 sortLt :: (a -> a -> Bool)              -- Less-than predicate
395        -> [a]                           -- Input list
396        -> [a]                           -- Result list
397
398 sortLt lt l = qsort lt   l []
399
400 -- qsort is stable and does not concatenate.
401 qsort :: (a -> a -> Bool)       -- Less-than predicate
402       -> [a]                    -- xs, Input list
403       -> [a]                    -- r,  Concatenate this list to the sorted input list
404       -> [a]                    -- Result = sort xs ++ r
405
406 qsort lt []     r = r
407 qsort lt [x]    r = x:r
408 qsort lt (x:xs) r = qpart lt x xs [] [] r
409
410 -- qpart partitions and sorts the sublists
411 -- rlt contains things less than x,
412 -- rge contains the ones greater than or equal to x.
413 -- Both have equal elements reversed with respect to the original list.
414
415 qpart lt x [] rlt rge r =
416     -- rlt and rge are in reverse order and must be sorted with an
417     -- anti-stable sorting
418     rqsort lt rlt (x : rqsort lt rge r)
419
420 qpart lt x (y:ys) rlt rge r =
421     if lt y x then
422         -- y < x
423         qpart lt x ys (y:rlt) rge r
424     else
425         -- y >= x
426         qpart lt x ys rlt (y:rge) r
427
428 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
429 rqsort lt []     r = r
430 rqsort lt [x]    r = x:r
431 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
432
433 rqpart lt x [] rle rgt r =
434     qsort lt rle (x : qsort lt rgt r)
435
436 rqpart lt x (y:ys) rle rgt r =
437     if lt x y then
438         -- y > x
439         rqpart lt x ys rle (y:rgt) r
440     else
441         -- y <= x
442         rqpart lt x ys (y:rle) rgt r
443 \end{code}
444
445 %************************************************************************
446 %*                                                                      *
447 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
448 %*                                                                      *
449 %************************************************************************
450
451 \begin{code}
452 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
453
454 mergesort cmp xs = merge_lists (split_into_runs [] xs)
455   where
456     a `le` b = case cmp a b of { LT -> True;  EQ -> True; GT -> False }
457     a `ge` b = case cmp a b of { LT -> False; EQ -> True; GT -> True  }
458
459     split_into_runs []        []                = []
460     split_into_runs run       []                = [run]
461     split_into_runs []        (x:xs)            = split_into_runs [x] xs
462     split_into_runs [r]       (x:xs) | x `ge` r = split_into_runs [r,x] xs
463     split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
464                                      | True     = rl : (split_into_runs [x] xs)
465
466     merge_lists []       = []
467     merge_lists (x:xs)   = merge x (merge_lists xs)
468
469     merge [] ys = ys
470     merge xs [] = xs
471     merge xl@(x:xs) yl@(y:ys)
472       = case cmp x y of
473           EQ  -> x : y : (merge xs ys)
474           LT  -> x : (merge xs yl)
475           GT -> y : (merge xl ys)
476 \end{code}
477
478 %************************************************************************
479 %*                                                                      *
480 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
481 %*                                                                      *
482 %************************************************************************
483
484 \begin{display}
485 Date: Mon, 3 May 93 20:45:23 +0200
486 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
487 To: partain@dcs.gla.ac.uk
488 Subject: natural merge sort beats quick sort [ and it is prettier ]
489
490 Here is a piece of Haskell code that I'm rather fond of. See it as an
491 attempt to get rid of the ridiculous quick-sort routine. group is
492 quite useful by itself I think it was John's idea originally though I
493 believe the lazy version is due to me [surprisingly complicated].
494 gamma [used to be called] is called gamma because I got inspired by
495 the Gamma calculus. It is not very close to the calculus but does
496 behave less sequentially than both foldr and foldl. One could imagine
497 a version of gamma that took a unit element as well thereby avoiding
498 the problem with empty lists.
499
500 I've tried this code against
501
502    1) insertion sort - as provided by haskell
503    2) the normal implementation of quick sort
504    3) a deforested version of quick sort due to Jan Sparud
505    4) a super-optimized-quick-sort of Lennart's
506
507 If the list is partially sorted both merge sort and in particular
508 natural merge sort wins. If the list is random [ average length of
509 rising subsequences = approx 2 ] mergesort still wins and natural
510 merge sort is marginally beaten by Lennart's soqs. The space
511 consumption of merge sort is a bit worse than Lennart's quick sort
512 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
513 fpca article ] isn't used because of group.
514
515 have fun
516 Carsten
517 \end{display}
518
519 \begin{code}
520 group :: (a -> a -> Bool) -> [a] -> [[a]]
521
522 {-
523 Date: Mon, 12 Feb 1996 15:09:41 +0000
524 From: Andy Gill <andy@dcs.gla.ac.uk>
525
526 Here is a `better' definition of group.
527 -}
528 group p []     = []
529 group p (x:xs) = group' xs x x (x :)
530   where
531     group' []     _     _     s  = [s []]
532     group' (x:xs) x_min x_max s 
533         | not (x `p` x_max) = group' xs x_min x (s . (x :)) 
534         | x `p` x_min       = group' xs x x_max ((x :) . s) 
535         | otherwise         = s [] : group' xs x x (x :) 
536
537 -- This one works forwards *and* backwards, as well as also being
538 -- faster that the one in Util.lhs.
539
540 {- ORIG:
541 group p [] = [[]]
542 group p (x:xs) =
543    let ((h1:t1):tt1) = group p xs
544        (t,tt) = if null xs then ([],[]) else
545                 if x `p` h1 then (h1:t1,tt1) else
546                    ([], (h1:t1):tt1)
547    in ((x:t):tt)
548 -}
549
550 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
551 generalMerge p xs [] = xs
552 generalMerge p [] ys = ys
553 generalMerge p (x:xs) (y:ys) | x `p` y   = x : generalMerge p xs (y:ys)
554                              | otherwise = y : generalMerge p (x:xs) ys
555
556 -- gamma is now called balancedFold
557
558 balancedFold :: (a -> a -> a) -> [a] -> a
559 balancedFold f [] = error "can't reduce an empty list using balancedFold"
560 balancedFold f [x] = x
561 balancedFold f l  = balancedFold f (balancedFold' f l)
562
563 balancedFold' :: (a -> a -> a) -> [a] -> [a]
564 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
565 balancedFold' f xs = xs
566
567 generalMergeSort p [] = []
568 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
569
570 generalNaturalMergeSort p [] = []
571 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
572
573 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
574
575 mergeSort = generalMergeSort (<=)
576 naturalMergeSort = generalNaturalMergeSort (<=)
577
578 mergeSortLe le = generalMergeSort le
579 naturalMergeSortLe le = generalNaturalMergeSort le
580 \end{code}
581
582 %************************************************************************
583 %*                                                                      *
584 \subsection[Utils-transitive-closure]{Transitive closure}
585 %*                                                                      *
586 %************************************************************************
587
588 This algorithm for transitive closure is straightforward, albeit quadratic.
589
590 \begin{code}
591 transitiveClosure :: (a -> [a])         -- Successor function
592                   -> (a -> a -> Bool)   -- Equality predicate
593                   -> [a]
594                   -> [a]                -- The transitive closure
595
596 transitiveClosure succ eq xs
597  = go [] xs
598  where
599    go done []                      = done
600    go done (x:xs) | x `is_in` done = go done xs
601                   | otherwise      = go (x:done) (succ x ++ xs)
602
603    x `is_in` []                 = False
604    x `is_in` (y:ys) | eq x y    = True
605                     | otherwise = x `is_in` ys
606 \end{code}
607
608 %************************************************************************
609 %*                                                                      *
610 \subsection[Utils-accum]{Accumulating}
611 %*                                                                      *
612 %************************************************************************
613
614 @mapAccumL@ behaves like a combination
615 of  @map@ and @foldl@;
616 it applies a function to each element of a list, passing an accumulating
617 parameter from left to right, and returning a final value of this
618 accumulator together with the new list.
619
620 \begin{code}
621 mapAccumL :: (acc -> x -> (acc, y))     -- Function of elt of input list
622                                         -- and accumulator, returning new
623                                         -- accumulator and elt of result list
624             -> acc              -- Initial accumulator
625             -> [x]              -- Input list
626             -> (acc, [y])               -- Final accumulator and result list
627
628 mapAccumL f b []     = (b, [])
629 mapAccumL f b (x:xs) = (b'', x':xs') where
630                                           (b', x') = f b x
631                                           (b'', xs') = mapAccumL f b' xs
632 \end{code}
633
634 @mapAccumR@ does the same, but working from right to left instead.  Its type is
635 the same as @mapAccumL@, though.
636
637 \begin{code}
638 mapAccumR :: (acc -> x -> (acc, y))     -- Function of elt of input list
639                                         -- and accumulator, returning new
640                                         -- accumulator and elt of result list
641             -> acc              -- Initial accumulator
642             -> [x]              -- Input list
643             -> (acc, [y])               -- Final accumulator and result list
644
645 mapAccumR f b []     = (b, [])
646 mapAccumR f b (x:xs) = (b'', x':xs') where
647                                           (b'', x') = f b' x
648                                           (b', xs') = mapAccumR f b xs
649 \end{code}
650
651 Here is the bi-directional version, that works from both left and right.
652
653 \begin{code}
654 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
655                                 -- Function of elt of input list
656                                 -- and accumulator, returning new
657                                 -- accumulator and elt of result list
658           -> accl                       -- Initial accumulator from left
659           -> accr                       -- Initial accumulator from right
660           -> [x]                        -- Input list
661           -> (accl, accr, [y])  -- Final accumulators and result list
662
663 mapAccumB f a b []     = (a,b,[])
664 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
665    where
666         (a',b'',y)  = f a b' x
667         (a'',b',ys) = mapAccumB f a' b xs
668 \end{code}
669
670 %************************************************************************
671 %*                                                                      *
672 \subsection[Utils-comparison]{Comparisons}
673 %*                                                                      *
674 %************************************************************************
675
676 \begin{code}
677 thenCmp :: Ordering -> Ordering -> Ordering
678 {-# INLINE thenCmp #-}
679 thenCmp EQ   any = any
680 thenCmp other any = other
681
682 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
683     -- `cmpList' uses a user-specified comparer
684
685 cmpList cmp []     [] = EQ
686 cmpList cmp []     _  = LT
687 cmpList cmp _      [] = GT
688 cmpList cmp (a:as) (b:bs)
689   = case cmp a b of { EQ -> cmpList cmp as bs; xxx -> xxx }
690 \end{code}
691
692 \begin{code}
693 cmpString :: String -> String -> Ordering
694
695 cmpString []     []     = EQ
696 cmpString (x:xs) (y:ys) = if      x == y then cmpString xs ys
697                           else if x  < y then LT
698                           else                GT
699 cmpString []     ys     = LT
700 cmpString xs     []     = GT
701
702 cmpString _ _ = panic "cmpString"
703 \end{code}
704
705
706 y
707 %************************************************************************
708 %*                                                                      *
709 \subsection[Utils-pairs]{Pairs}
710 %*                                                                      *
711 %************************************************************************
712
713 The following are curried versions of @fst@ and @snd@.
714
715 \begin{code}
716 cfst :: a -> b -> a     -- stranal-sem only (Note)
717 cfst x y = x
718 \end{code}
719
720 The following provide us higher order functions that, when applied
721 to a function, operate on pairs.
722
723 \begin{code}
724 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
725 applyToPair (f,g) (x,y) = (f x, g y)
726
727 applyToFst :: (a -> c) -> (a,b)-> (c,b)
728 applyToFst f (x,y) = (f x,y)
729
730 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
731 applyToSnd f (x,y) = (x,f y)
732
733 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
734 foldPair fg ab [] = ab
735 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
736                        where (u,v) = foldPair fg ab abs
737 \end{code}
738
739 \begin{code}
740 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
741 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
742 \end{code}
743
744
745 %************************************************************************
746 %*                                                                      *
747 \subsection[Utils-errors]{Error handling}
748 %*                                                                      *
749 %************************************************************************
750
751 \begin{code}
752 panic x = error ("panic! (the `impossible' happened):\n\t"
753               ++ x ++ "\n\n"
754               ++ "Please report it as a compiler bug "
755               ++ "to glasgow-haskell-bugs@dcs.gla.ac.uk.\n\n" )
756
757 -- #-versions because panic can't return an unboxed int, and that's
758 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
759 -- No, man -- Too Beautiful! (Will)
760
761 panic# :: String -> FAST_INT
762 panic# s = case (panic s) of () -> ILIT(0)
763
764 assertPanic :: String -> Int -> a
765 assertPanic file line = panic ("ASSERT failed! file " ++ file ++ ", line " ++ show line)
766 \end{code}