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