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