2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Util]{Highly random utility functions}
7 #if defined(COMPILING_GHC)
8 # include "HsVersions.h"
9 # define IF_NOT_GHC(a) {--}
12 # define TAG_ _CMP_TAG
17 # define tagCmp_ _tagCmp
18 # define FAST_STRING String
19 # define ASSERT(x) {-nothing-}
20 # define IF_NOT_GHC(a) a
24 #ifndef __GLASGOW_HASKELL__
33 -- Haskell-version support
34 #ifndef __GLASGOW_HASKELL__
38 -- general list processing
39 IF_NOT_GHC(forall COMMA exists COMMA)
40 zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
43 nOfThem, lengthExceeds, isSingleton,
45 #if defined(COMPILING_GHC)
53 hasNoDups, equivClasses, runs, removeDups,
56 IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
58 IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
59 IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
61 -- transitive closures
65 mapAccumL, mapAccumR, mapAccumB,
68 Ord3(..), thenCmp, cmpList,
69 IF_NOT_GHC(cmpString COMMA)
70 #ifdef USE_FAST_STRINGS
76 IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
77 IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
81 #if defined(COMPILING_GHC)
82 , panic, panic#, pprPanic, pprPanic#, pprError, pprTrace
86 #endif {- COMPILING_GHC -}
88 -- and to make the interface self-sufficient...
90 # if defined(COMPILING_GHC)
91 , Maybe(..){-.. for pragmas...-}, PrettyRep, Pretty(..)
99 #if defined(COMPILING_GHC)
101 CHK_Ubiq() -- debugging consistency check
106 import Maybes ( Maybe(..) )
110 %************************************************************************
112 \subsection[Utils-version-support]{Functions to help pre-1.2 versions of (non-Glasgow) Haskell}
114 %************************************************************************
116 This is our own idea:
118 #ifndef __GLASGOW_HASKELL__
119 data TAG_ = LT_ | EQ_ | GT_
121 tagCmp_ :: Ord a => a -> a -> TAG_
122 tagCmp_ a b = if a == b then EQ_ else if a < b then LT_ else GT_
126 %************************************************************************
128 \subsection[Utils-lists]{General list processing}
130 %************************************************************************
132 Quantifiers are not standard in Haskell. The following fill in the gap.
135 forall :: (a -> Bool) -> [a] -> Bool
136 forall pred [] = True
137 forall pred (x:xs) = pred x && forall pred xs
139 exists :: (a -> Bool) -> [a] -> Bool
140 exists pred [] = False
141 exists pred (x:xs) = pred x || exists pred xs
144 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
145 are of equal length. Alastair Reid thinks this should only happen if
146 DEBUGging on; hey, why not?
149 zipEqual :: [a] -> [b] -> [(a,b)]
150 zipWithEqual :: (a->b->c) -> [a]->[b]->[c]
151 zipWith3Equal :: (a->b->c->d) -> [a]->[b]->[c]->[d]
152 zipWith4Equal :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
156 zipWithEqual = zipWith
157 zipWith3Equal = zipWith3
158 zipWith4Equal = zipWith4
161 zipEqual (a:as) (b:bs) = (a,b) : zipEqual as bs
162 zipEqual as bs = panic "zipEqual: unequal lists"
164 zipWithEqual z (a:as) (b:bs) = z a b : zipWithEqual z as bs
165 zipWithEqual _ [] [] = []
166 zipWithEqual _ _ _ = panic "zipWithEqual: unequal lists"
168 zipWith3Equal z (a:as) (b:bs) (c:cs)
169 = z a b c : zipWith3Equal z as bs cs
170 zipWith3Equal _ [] [] [] = []
171 zipWith3Equal _ _ _ _ = panic "zipWith3Equal: unequal lists"
173 zipWith4Equal z (a:as) (b:bs) (c:cs) (d:ds)
174 = z a b c d : zipWith4Equal z as bs cs ds
175 zipWith4Equal _ [] [] [] [] = []
176 zipWith4Equal _ _ _ _ _ = panic "zipWith4Equal: unequal lists"
181 -- zipLazy is lazy in the second list (observe the ~)
183 zipLazy :: [a] -> [b] -> [(a,b)]
185 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
189 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
191 mapAndUnzip f [] = ([],[])
195 (rs1, rs2) = mapAndUnzip f xs
201 nOfThem :: Int -> a -> [a]
202 nOfThem n thing = take n (repeat thing)
204 lengthExceeds :: [a] -> Int -> Bool
206 [] `lengthExceeds` n = 0 > n
207 (x:xs) `lengthExceeds` n = (1 > n) || (xs `lengthExceeds` (n - 1))
209 isSingleton :: [a] -> Bool
211 isSingleton [x] = True
212 isSingleton _ = False
214 startsWith, endsWith :: String -> String -> Maybe String
216 startsWith [] str = Just str
217 startsWith (c:cs) (s:ss)
218 = if c /= s then Nothing else startsWith cs ss
221 = case (startsWith (reverse cs) (reverse ss)) of
223 Just rs -> Just (reverse rs)
226 Debugging/specialising versions of \tr{elem} and \tr{notElem}
228 #if defined(COMPILING_GHC)
229 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
232 isIn msg x ys = elem__ x ys
233 isn'tIn msg x ys = notElem__ x ys
235 --these are here to be SPECIALIZEd (automagically)
237 elem__ x (y:ys) = x==y || elem__ x ys
239 notElem__ x [] = True
240 notElem__ x (y:ys) = x /= y && notElem__ x ys
248 | i _GE_ ILIT(100) = panic ("Over-long elem in: " ++ msg)
249 | otherwise = x == y || elem (i _ADD_ ILIT(1)) x ys
252 = notElem ILIT(0) x ys
254 notElem i x [] = True
256 | i _GE_ ILIT(100) = panic ("Over-long notElem in: " ++ msg)
257 | otherwise = x /= y && notElem (i _ADD_ ILIT(1)) x ys
261 # ifdef USE_ATTACK_PRAGMAS
262 {-# SPECIALIZE isIn :: String -> Literal -> [Literal] -> Bool #-}
263 {-# SPECIALIZE isIn :: String -> Class -> [Class] -> Bool #-}
264 {-# SPECIALIZE isIn :: String -> Id -> [Id] -> Bool #-}
265 {-# SPECIALIZE isIn :: String -> Int -> [Int] -> Bool #-}
266 {-# SPECIALIZE isIn :: String -> MagicId -> [MagicId] -> Bool #-}
267 {-# SPECIALIZE isIn :: String -> Name -> [Name] -> Bool #-}
268 {-# SPECIALIZE isIn :: String -> TyCon -> [TyCon] -> Bool #-}
269 {-# SPECIALIZE isIn :: String -> TyVar -> [TyVar] -> Bool #-}
270 {-# SPECIALIZE isIn :: String -> TyVarTemplate -> [TyVarTemplate] -> Bool #-}
271 {-# SPECIALIZE isIn :: String -> Unique -> [Unique] -> Bool #-}
272 {-# SPECIALIZE isIn :: String -> _PackedString -> [_PackedString] -> Bool #-}
273 {-# SPECIALIZE isn'tIn :: String -> (Id, Id) -> [(Id, Id)] -> Bool #-}
274 {-# SPECIALIZE isn'tIn :: String -> Int -> [Int] -> Bool #-}
275 {-# SPECIALIZE isn'tIn :: String -> Id -> [Id] -> Bool #-}
276 {-# SPECIALIZE isn'tIn :: String -> MagicId -> [MagicId] -> Bool #-}
277 {-# SPECIALIZE isn'tIn :: String -> TyCon -> [TyCon] -> Bool #-}
278 {-# SPECIALIZE isn'tIn :: String -> TyVar -> [TyVar] -> Bool #-}
279 {-# SPECIALIZE isn'tIn :: String -> TyVarTemplate -> [TyVarTemplate] -> Bool #-}
282 #endif {- COMPILING_GHC -}
285 %************************************************************************
287 \subsection[Utils-assoc]{Association lists}
289 %************************************************************************
291 See also @assocMaybe@ and @mkLookupFun@ in module @Maybes@.
294 assoc :: (Eq a) => String -> [(a, b)] -> a -> b
296 assoc crash_msg lst key
298 then panic ("Failed in assoc: " ++ crash_msg)
300 where res = [ val | (key', val) <- lst, key == key']
302 #if defined(COMPILING_GHC)
303 # ifdef USE_ATTACK_PRAGMAS
304 {-# SPECIALIZE assoc :: String -> [(Id, a)] -> Id -> a #-}
305 {-# SPECIALIZE assoc :: String -> [(Class, a)] -> Class -> a #-}
306 {-# SPECIALIZE assoc :: String -> [(Name, a)] -> Name -> a #-}
307 {-# SPECIALIZE assoc :: String -> [(PrimRep, a)] -> PrimRep -> a #-}
308 {-# SPECIALIZE assoc :: String -> [(String, a)] -> String -> a #-}
309 {-# SPECIALIZE assoc :: String -> [(TyCon, a)] -> TyCon -> a #-}
310 {-# SPECIALIZE assoc :: String -> [(TyVar, a)] -> TyVar -> a #-}
311 {-# SPECIALIZE assoc :: String -> [(TyVarTemplate, a)] -> TyVarTemplate -> a #-}
312 {-# SPECIALIZE assoc :: String -> [(Type, a)] -> Type -> a #-}
313 {-# SPECIALIZE assoc :: String -> [(_PackedString, a)] -> _PackedString -> a #-}
318 %************************************************************************
320 \subsection[Utils-dups]{Duplicate-handling}
322 %************************************************************************
325 hasNoDups :: (Eq a) => [a] -> Bool
327 hasNoDups xs = f [] xs
329 f seen_so_far [] = True
330 f seen_so_far (x:xs) = if x `is_elem` seen_so_far then
335 #if defined(COMPILING_GHC)
336 is_elem = isIn "hasNoDups"
340 #if defined(COMPILING_GHC)
341 # ifdef USE_ATTACK_PRAGMAS
342 {-# SPECIALIZE hasNoDups :: [TyVar] -> Bool #-}
348 equivClasses :: (a -> a -> TAG_) -- Comparison
352 equivClasses cmp stuff@[] = []
353 equivClasses cmp stuff@[item] = [stuff]
354 equivClasses cmp items
355 = runs eq (sortLt lt items)
357 eq a b = case cmp a b of { EQ_ -> True; _ -> False }
358 lt a b = case cmp a b of { LT_ -> True; _ -> False }
361 The first cases in @equivClasses@ above are just to cut to the point
364 @runs@ groups a list into a list of lists, each sublist being a run of
365 identical elements of the input list. It is passed a predicate @p@ which
366 tells when two elements are equal.
369 runs :: (a -> a -> Bool) -- Equality
374 runs p (x:xs) = case (span (p x) xs) of
375 (first, rest) -> (x:first) : (runs p rest)
379 removeDups :: (a -> a -> TAG_) -- Comparison function
381 -> ([a], -- List with no duplicates
382 [[a]]) -- List of duplicate groups. One representative from
383 -- each group appears in the first result
385 removeDups cmp [] = ([], [])
386 removeDups cmp [x] = ([x],[])
388 = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
391 collect_dups dups_so_far [x] = (dups_so_far, x)
392 collect_dups dups_so_far dups@(x:xs) = (dups:dups_so_far, x)
395 %************************************************************************
397 \subsection[Utils-sorting]{Sorting}
399 %************************************************************************
401 %************************************************************************
403 \subsubsection[Utils-quicksorting]{Quicksorts}
405 %************************************************************************
408 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
409 quicksort :: (a -> a -> Bool) -- Less-than predicate
411 -> [a] -- Result list in increasing order
414 quicksort lt [x] = [x]
415 quicksort lt (x:xs) = split x [] [] xs
417 split x lo hi [] = quicksort lt lo ++ (x : quicksort lt hi)
418 split x lo hi (y:ys) | y `lt` x = split x (y:lo) hi ys
419 | True = split x lo (y:hi) ys
422 Quicksort variant from Lennart's Haskell-library contribution. This
423 is a {\em stable} sort.
426 stableSortLt = sortLt -- synonym; when we want to highlight stable-ness
428 sortLt :: (a -> a -> Bool) -- Less-than predicate
430 -> [a] -- Result list
432 sortLt lt l = qsort lt l []
434 -- qsort is stable and does not concatenate.
435 qsort :: (a -> a -> Bool) -- Less-than predicate
436 -> [a] -- xs, Input list
437 -> [a] -- r, Concatenate this list to the sorted input list
438 -> [a] -- Result = sort xs ++ r
442 qsort lt (x:xs) r = qpart lt x xs [] [] r
444 -- qpart partitions and sorts the sublists
445 -- rlt contains things less than x,
446 -- rge contains the ones greater than or equal to x.
447 -- Both have equal elements reversed with respect to the original list.
449 qpart lt x [] rlt rge r =
450 -- rlt and rge are in reverse order and must be sorted with an
451 -- anti-stable sorting
452 rqsort lt rlt (x : rqsort lt rge r)
454 qpart lt x (y:ys) rlt rge r =
457 qpart lt x ys (y:rlt) rge r
460 qpart lt x ys rlt (y:rge) r
462 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
464 rqsort lt [x] r = x:r
465 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
467 rqpart lt x [] rle rgt r =
468 qsort lt rle (x : qsort lt rgt r)
470 rqpart lt x (y:ys) rle rgt r =
473 rqpart lt x ys rle (y:rgt) r
476 rqpart lt x ys (y:rle) rgt r
479 %************************************************************************
481 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
483 %************************************************************************
486 mergesort :: (a -> a -> TAG_) -> [a] -> [a]
488 mergesort cmp xs = merge_lists (split_into_runs [] xs)
490 a `le` b = case cmp a b of { LT_ -> True; EQ_ -> True; GT__ -> False }
491 a `ge` b = case cmp a b of { LT_ -> False; EQ_ -> True; GT__ -> True }
493 split_into_runs [] [] = []
494 split_into_runs run [] = [run]
495 split_into_runs [] (x:xs) = split_into_runs [x] xs
496 split_into_runs [r] (x:xs) | x `ge` r = split_into_runs [r,x] xs
497 split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
498 | True = rl : (split_into_runs [x] xs)
501 merge_lists (x:xs) = merge x (merge_lists xs)
505 merge xl@(x:xs) yl@(y:ys)
507 EQ_ -> x : y : (merge xs ys)
508 LT_ -> x : (merge xs yl)
509 GT__ -> y : (merge xl ys)
512 %************************************************************************
514 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
516 %************************************************************************
519 Date: Mon, 3 May 93 20:45:23 +0200
520 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
521 To: partain@dcs.gla.ac.uk
522 Subject: natural merge sort beats quick sort [ and it is prettier ]
524 Here is a piece of Haskell code that I'm rather fond of. See it as an
525 attempt to get rid of the ridiculous quick-sort routine. group is
526 quite useful by itself I think it was John's idea originally though I
527 believe the lazy version is due to me [surprisingly complicated].
528 gamma [used to be called] is called gamma because I got inspired by
529 the Gamma calculus. It is not very close to the calculus but does
530 behave less sequentially than both foldr and foldl. One could imagine
531 a version of gamma that took a unit element as well thereby avoiding
532 the problem with empty lists.
534 I've tried this code against
536 1) insertion sort - as provided by haskell
537 2) the normal implementation of quick sort
538 3) a deforested version of quick sort due to Jan Sparud
539 4) a super-optimized-quick-sort of Lennart's
541 If the list is partially sorted both merge sort and in particular
542 natural merge sort wins. If the list is random [ average length of
543 rising subsequences = approx 2 ] mergesort still wins and natural
544 merge sort is marginally beaten by Lennart's soqs. The space
545 consumption of merge sort is a bit worse than Lennart's quick sort
546 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
547 fpca article ] isn't used because of group.
554 group :: (a -> a -> Bool) -> [a] -> [[a]]
557 Date: Mon, 12 Feb 1996 15:09:41 +0000
558 From: Andy Gill <andy@dcs.gla.ac.uk>
560 Here is a `better' definition of group.
563 group p (x:xs) = group' xs x x (x :)
565 group' [] _ _ s = [s []]
566 group' (x:xs) x_min x_max s
567 | not (x `p` x_max) = group' xs x_min x (s . (x :))
568 | x `p` x_min = group' xs x x_max ((x :) . s)
569 | otherwise = s [] : group' xs x x (x :)
571 -- This one works forwards *and* backwards, as well as also being
572 -- faster that the one in Util.lhs.
577 let ((h1:t1):tt1) = group p xs
578 (t,tt) = if null xs then ([],[]) else
579 if x `p` h1 then (h1:t1,tt1) else
584 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
585 generalMerge p xs [] = xs
586 generalMerge p [] ys = ys
587 generalMerge p (x:xs) (y:ys) | x `p` y = x : generalMerge p xs (y:ys)
588 | otherwise = y : generalMerge p (x:xs) ys
590 -- gamma is now called balancedFold
592 balancedFold :: (a -> a -> a) -> [a] -> a
593 balancedFold f [] = error "can't reduce an empty list using balancedFold"
594 balancedFold f [x] = x
595 balancedFold f l = balancedFold f (balancedFold' f l)
597 balancedFold' :: (a -> a -> a) -> [a] -> [a]
598 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
599 balancedFold' f xs = xs
601 generalMergeSort p [] = []
602 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
604 generalNaturalMergeSort p [] = []
605 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
607 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
609 mergeSort = generalMergeSort (<=)
610 naturalMergeSort = generalNaturalMergeSort (<=)
612 mergeSortLe le = generalMergeSort le
613 naturalMergeSortLe le = generalNaturalMergeSort le
616 %************************************************************************
618 \subsection[Utils-transitive-closure]{Transitive closure}
620 %************************************************************************
622 This algorithm for transitive closure is straightforward, albeit quadratic.
625 transitiveClosure :: (a -> [a]) -- Successor function
626 -> (a -> a -> Bool) -- Equality predicate
628 -> [a] -- The transitive closure
630 transitiveClosure succ eq xs
634 do done (x:xs) | x `is_in` done = do done xs
635 | otherwise = do (x:done) (succ x ++ xs)
638 x `is_in` (y:ys) | eq x y = True
639 | otherwise = x `is_in` ys
642 %************************************************************************
644 \subsection[Utils-accum]{Accumulating}
646 %************************************************************************
648 @mapAccumL@ behaves like a combination
649 of @map@ and @foldl@;
650 it applies a function to each element of a list, passing an accumulating
651 parameter from left to right, and returning a final value of this
652 accumulator together with the new list.
655 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
656 -- and accumulator, returning new
657 -- accumulator and elt of result list
658 -> acc -- Initial accumulator
660 -> (acc, [y]) -- Final accumulator and result list
662 mapAccumL f b [] = (b, [])
663 mapAccumL f b (x:xs) = (b'', x':xs') where
665 (b'', xs') = mapAccumL f b' xs
668 @mapAccumR@ does the same, but working from right to left instead. Its type is
669 the same as @mapAccumL@, though.
672 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
673 -- and accumulator, returning new
674 -- accumulator and elt of result list
675 -> acc -- Initial accumulator
677 -> (acc, [y]) -- Final accumulator and result list
679 mapAccumR f b [] = (b, [])
680 mapAccumR f b (x:xs) = (b'', x':xs') where
682 (b', xs') = mapAccumR f b xs
685 Here is the bi-directional version, that works from both left and right.
688 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
689 -- Function of elt of input list
690 -- and accumulator, returning new
691 -- accumulator and elt of result list
692 -> accl -- Initial accumulator from left
693 -> accr -- Initial accumulator from right
695 -> (accl, accr, [y]) -- Final accumulators and result list
697 mapAccumB f a b [] = (a,b,[])
698 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
700 (a',b'',y) = f a b' x
701 (a'',b',ys) = mapAccumB f a' b xs
704 %************************************************************************
706 \subsection[Utils-comparison]{Comparisons}
708 %************************************************************************
710 See also @tagCmp_@ near the versions-compatibility section.
712 The Ord3 class will be subsumed into Ord in Haskell 1.3.
716 cmp :: a -> a -> TAG_
718 thenCmp :: TAG_ -> TAG_ -> TAG_
719 {-# INLINE thenCmp #-}
720 thenCmp EQ_ any = any
721 thenCmp other any = other
723 cmpList :: (a -> a -> TAG_) -> [a] -> [a] -> TAG_
724 -- `cmpList' uses a user-specified comparer
726 cmpList cmp [] [] = EQ_
727 cmpList cmp [] _ = LT_
728 cmpList cmp _ [] = GT_
729 cmpList cmp (a:as) (b:bs)
730 = case cmp a b of { EQ_ -> cmpList cmp as bs; xxx -> xxx }
734 instance Ord3 a => Ord3 [a] where
738 cmp (x:xs) (y:ys) = (x `cmp` y) `thenCmp` (xs `cmp` ys)
740 instance Ord3 a => Ord3 (Maybe a) where
741 cmp Nothing Nothing = EQ_
742 cmp Nothing (Just y) = LT_
743 cmp (Just x) Nothing = GT_
744 cmp (Just x) (Just y) = x `cmp` y
746 instance Ord3 Int where
747 cmp a b | a < b = LT_
753 cmpString :: String -> String -> TAG_
755 cmpString [] [] = EQ_
756 cmpString (x:xs) (y:ys) = if x == y then cmpString xs ys
757 else if x < y then LT_
759 cmpString [] ys = LT_
760 cmpString xs [] = GT_
762 cmpString _ _ = panic# "cmpString"
766 #ifdef USE_FAST_STRINGS
767 cmpPString :: FAST_STRING -> FAST_STRING -> TAG_
770 = case (_tagCmp x y) of { _LT -> LT_ ; _EQ -> EQ_ ; _GT -> GT_ }
775 #ifndef USE_FAST_STRINGS
776 substr :: FAST_STRING -> Int -> Int -> FAST_STRING
779 = ASSERT (beg >= 0 && beg <= end)
780 take (end - beg + 1) (drop beg str)
784 %************************************************************************
786 \subsection[Utils-pairs]{Pairs}
788 %************************************************************************
790 The following are curried versions of @fst@ and @snd@.
793 cfst :: a -> b -> a -- stranal-sem only (Note)
797 The following provide us higher order functions that, when applied
798 to a function, operate on pairs.
801 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
802 applyToPair (f,g) (x,y) = (f x, g y)
804 applyToFst :: (a -> c) -> (a,b)-> (c,b)
805 applyToFst f (x,y) = (f x,y)
807 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
808 applyToSnd f (x,y) = (x,f y)
810 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
811 foldPair fg ab [] = ab
812 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
813 where (u,v) = foldPair fg ab abs
817 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
818 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
821 %************************************************************************
823 \subsection[Utils-errors]{Error handling}
825 %************************************************************************
828 #if defined(COMPILING_GHC)
829 panic x = error ("panic! (the `impossible' happened):\n\t"
831 ++ "Please report it as a compiler bug "
832 ++ "to glasgow-haskell-bugs@dcs.glasgow.ac.uk.\n\n" )
834 pprPanic heading pretty_msg = panic (heading++(ppShow 80 pretty_msg))
835 pprError heading pretty_msg = error (heading++(ppShow 80 pretty_msg))
836 pprTrace heading pretty_msg = trace (heading++(ppShow 80 pretty_msg))
838 -- #-versions because panic can't return an unboxed int, and that's
839 -- what TAG_ is with GHC at the moment. Ugh. (Simon)
840 -- No, man -- Too Beautiful! (Will)
842 panic# :: String -> TAG_
843 panic# s = case (panic s) of () -> EQ_
845 pprPanic# heading pretty_msg = panic# (heading++(ppShow 80 pretty_msg))
848 assertPanic :: String -> Int -> a
849 assertPanic file line = panic ("ASSERT failed! file "++file++", line "++show line)
851 #endif {- COMPILING_GHC -}