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