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_ Ordering
20 # define tagCmp_ compare
21 # define _tagCmp compare
22 # define FAST_STRING String
23 # define ASSERT(x) {-nothing-}
24 # define IF_NOT_GHC(a) a
28 #ifndef __GLASGOW_HASKELL__
37 -- Haskell-version support
38 #ifndef __GLASGOW_HASKELL__
42 -- general list processing
43 IF_NOT_GHC(forall COMMA exists COMMA)
44 zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
46 mapAndUnzip, mapAndUnzip3,
47 nOfThem, lengthExceeds, isSingleton,
49 #if defined(COMPILING_GHC)
57 hasNoDups, equivClasses, runs, removeDups,
60 IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
62 IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
63 IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
65 -- transitive closures
69 mapAccumL, mapAccumR, mapAccumB,
72 #if defined(COMPILING_GHC)
80 IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
81 IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
85 #if defined(COMPILING_GHC)
86 , panic, panic#, pprPanic, pprPanic#, pprError, pprTrace
88 #endif {- COMPILING_GHC -}
95 import PackedString ( unpackCStringIO )
101 %************************************************************************
103 \subsection[Utils-version-support]{Functions to help pre-1.2 versions of (non-Glasgow) Haskell}
105 %************************************************************************
107 This is our own idea:
109 #ifndef __GLASGOW_HASKELL__
110 data TAG_ = LT_ | EQ_ | GT_
112 tagCmp_ :: Ord a => a -> a -> TAG_
113 tagCmp_ a b = if a == b then EQ_ else if a < b then LT_ else GT_
117 %************************************************************************
119 \subsection[Utils-lists]{General list processing}
121 %************************************************************************
123 Quantifiers are not standard in Haskell. The following fill in the gap.
126 forall :: (a -> Bool) -> [a] -> Bool
127 forall pred [] = True
128 forall pred (x:xs) = pred x && forall pred xs
130 exists :: (a -> Bool) -> [a] -> Bool
131 exists pred [] = False
132 exists pred (x:xs) = pred x || exists pred xs
135 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
136 are of equal length. Alastair Reid thinks this should only happen if
137 DEBUGging on; hey, why not?
138 [In the GHC syslib, we want the paranoid behaviour by default --SOF]
141 zipEqual :: String -> [a] -> [b] -> [(a,b)]
142 zipWithEqual :: String -> (a->b->c) -> [a]->[b]->[c]
143 zipWith3Equal :: String -> (a->b->c->d) -> [a]->[b]->[c]->[d]
144 zipWith4Equal :: String -> (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
146 #if (!defined(DEBUG)) && defined(COMPILING_GHC)
148 zipWithEqual _ = zipWith
149 zipWith3Equal _ = zipWith3
150 zipWith4Equal _ = zipWith4
152 zipEqual msg [] [] = []
153 zipEqual msg (a:as) (b:bs) = (a,b) : zipEqual msg as bs
154 zipEqual msg as bs = panic ("zipEqual: unequal lists:"++msg)
156 zipWithEqual msg z (a:as) (b:bs)= z a b : zipWithEqual msg z as bs
157 zipWithEqual msg _ [] [] = []
158 zipWithEqual msg _ _ _ = panic ("zipWithEqual: unequal lists:"++msg)
160 zipWith3Equal msg z (a:as) (b:bs) (c:cs)
161 = z a b c : zipWith3Equal msg z as bs cs
162 zipWith3Equal msg _ [] [] [] = []
163 zipWith3Equal msg _ _ _ _ = panic ("zipWith3Equal: unequal lists:"++msg)
165 zipWith4Equal msg z (a:as) (b:bs) (c:cs) (d:ds)
166 = z a b c d : zipWith4Equal msg z as bs cs ds
167 zipWith4Equal msg _ [] [] [] [] = []
168 zipWith4Equal msg _ _ _ _ _ = panic ("zipWith4Equal: unequal lists:"++msg)
173 -- zipLazy is lazy in the second list (observe the ~)
175 zipLazy :: [a] -> [b] -> [(a,b)]
177 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
181 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
183 mapAndUnzip f [] = ([],[])
187 (rs1, rs2) = mapAndUnzip f xs
191 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
193 mapAndUnzip3 f [] = ([],[],[])
194 mapAndUnzip3 f (x:xs)
197 (rs1, rs2, rs3) = mapAndUnzip3 f xs
199 (r1:rs1, r2:rs2, r3:rs3)
203 nOfThem :: Int -> a -> [a]
204 nOfThem = replicate -- deprecated.
206 lengthExceeds :: [a] -> Int -> Bool
208 [] `lengthExceeds` n = 0 > n
209 (x:xs) `lengthExceeds` n = (1 > n) || (xs `lengthExceeds` (n - 1))
211 isSingleton :: [a] -> Bool
213 isSingleton [x] = True
214 isSingleton _ = False
216 startsWith, endsWith :: String -> String -> Maybe String
218 startsWith [] str = Just str
219 startsWith (c:cs) (s:ss)
220 = if c /= s then Nothing else startsWith cs ss
221 startsWith _ [] = Nothing
224 = case (startsWith (reverse cs) (reverse ss)) of
226 Just rs -> Just (reverse rs)
229 Debugging/specialising versions of \tr{elem} and \tr{notElem}
231 #if defined(COMPILING_GHC)
232 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
235 isIn msg x ys = elem__ x ys
236 isn'tIn msg x ys = notElem__ x ys
238 --these are here to be SPECIALIZEd (automagically)
240 elem__ x (y:ys) = x==y || elem__ x ys
242 notElem__ x [] = True
243 notElem__ x (y:ys) = x /= y && notElem__ x ys
251 | i _GE_ ILIT(100) = panic ("Over-long elem in: " ++ msg)
252 | otherwise = x == y || elem (i _ADD_ ILIT(1)) x ys
255 = notElem ILIT(0) x ys
257 notElem i x [] = True
259 | i _GE_ ILIT(100) = panic ("Over-long notElem in: " ++ msg)
260 | otherwise = x /= y && notElem (i _ADD_ ILIT(1)) x ys
264 #endif {- COMPILING_GHC -}
267 %************************************************************************
269 \subsection[Utils-assoc]{Association lists}
271 %************************************************************************
273 See also @assocMaybe@ and @mkLookupFun@ in module @Maybes@.
276 assoc :: (Eq a) => String -> [(a, b)] -> a -> b
278 assoc crash_msg lst key
280 then panic ("Failed in assoc: " ++ crash_msg)
282 where res = [ val | (key', val) <- lst, key == key']
285 %************************************************************************
287 \subsection[Utils-dups]{Duplicate-handling}
289 %************************************************************************
292 hasNoDups :: (Eq a) => [a] -> Bool
294 hasNoDups xs = f [] xs
296 f seen_so_far [] = True
297 f seen_so_far (x:xs) = if x `is_elem` seen_so_far then
302 #if defined(COMPILING_GHC)
303 is_elem = isIn "hasNoDups"
310 equivClasses :: (a -> a -> Ordering) -- Comparison
314 equivClasses cmp stuff@[] = []
315 equivClasses cmp stuff@[item] = [stuff]
316 equivClasses cmp items
317 = runs eq (sortLt lt items)
319 eq a b = case cmp a b of { EQ -> True; _ -> False }
320 lt a b = case cmp a b of { LT -> True; _ -> False }
323 The first cases in @equivClasses@ above are just to cut to the point
326 @runs@ groups a list into a list of lists, each sublist being a run of
327 identical elements of the input list. It is passed a predicate @p@ which
328 tells when two elements are equal.
331 runs :: (a -> a -> Bool) -- Equality
336 runs p (x:xs) = case (span (p x) xs) of
337 (first, rest) -> (x:first) : (runs p rest)
341 removeDups :: (a -> a -> Ordering) -- Comparison function
343 -> ([a], -- List with no duplicates
344 [[a]]) -- List of duplicate groups. One representative from
345 -- each group appears in the first result
347 removeDups cmp [] = ([], [])
348 removeDups cmp [x] = ([x],[])
350 = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
353 collect_dups dups_so_far [x] = (dups_so_far, x)
354 collect_dups dups_so_far dups@(x:xs) = (dups:dups_so_far, x)
357 %************************************************************************
359 \subsection[Utils-sorting]{Sorting}
361 %************************************************************************
363 %************************************************************************
365 \subsubsection[Utils-quicksorting]{Quicksorts}
367 %************************************************************************
370 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
371 quicksort :: (a -> a -> Bool) -- Less-than predicate
373 -> [a] -- Result list in increasing order
376 quicksort lt [x] = [x]
377 quicksort lt (x:xs) = split x [] [] xs
379 split x lo hi [] = quicksort lt lo ++ (x : quicksort lt hi)
380 split x lo hi (y:ys) | y `lt` x = split x (y:lo) hi ys
381 | True = split x lo (y:hi) ys
384 Quicksort variant from Lennart's Haskell-library contribution. This
385 is a {\em stable} sort.
388 stableSortLt = sortLt -- synonym; when we want to highlight stable-ness
390 sortLt :: (a -> a -> Bool) -- Less-than predicate
392 -> [a] -- Result list
394 sortLt lt l = qsort lt l []
396 -- qsort is stable and does not concatenate.
397 qsort :: (a -> a -> Bool) -- Less-than predicate
398 -> [a] -- xs, Input list
399 -> [a] -- r, Concatenate this list to the sorted input list
400 -> [a] -- Result = sort xs ++ r
404 qsort lt (x:xs) r = qpart lt x xs [] [] r
406 -- qpart partitions and sorts the sublists
407 -- rlt contains things less than x,
408 -- rge contains the ones greater than or equal to x.
409 -- Both have equal elements reversed with respect to the original list.
411 qpart lt x [] rlt rge r =
412 -- rlt and rge are in reverse order and must be sorted with an
413 -- anti-stable sorting
414 rqsort lt rlt (x : rqsort lt rge r)
416 qpart lt x (y:ys) rlt rge r =
419 qpart lt x ys (y:rlt) rge r
422 qpart lt x ys rlt (y:rge) r
424 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
426 rqsort lt [x] r = x:r
427 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
429 rqpart lt x [] rle rgt r =
430 qsort lt rle (x : qsort lt rgt r)
432 rqpart lt x (y:ys) rle rgt r =
435 rqpart lt x ys rle (y:rgt) r
438 rqpart lt x ys (y:rle) rgt r
441 %************************************************************************
443 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
445 %************************************************************************
448 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
450 mergesort cmp xs = merge_lists (split_into_runs [] xs)
452 a `le` b = case cmp a b of { LT_ -> True; EQ_ -> True; GT__ -> False }
453 a `ge` b = case cmp a b of { LT_ -> False; EQ_ -> True; GT__ -> True }
455 split_into_runs [] [] = []
456 split_into_runs run [] = [run]
457 split_into_runs [] (x:xs) = split_into_runs [x] xs
458 split_into_runs [r] (x:xs) | x `ge` r = split_into_runs [r,x] xs
459 split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
460 | True = rl : (split_into_runs [x] xs)
463 merge_lists (x:xs) = merge x (merge_lists xs)
467 merge xl@(x:xs) yl@(y:ys)
469 EQ_ -> x : y : (merge xs ys)
470 LT_ -> x : (merge xs yl)
471 GT__ -> y : (merge xl ys)
474 %************************************************************************
476 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
478 %************************************************************************
481 Date: Mon, 3 May 93 20:45:23 +0200
482 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
483 To: partain@dcs.gla.ac.uk
484 Subject: natural merge sort beats quick sort [ and it is prettier ]
486 Here is a piece of Haskell code that I'm rather fond of. See it as an
487 attempt to get rid of the ridiculous quick-sort routine. group is
488 quite useful by itself I think it was John's idea originally though I
489 believe the lazy version is due to me [surprisingly complicated].
490 gamma [used to be called] is called gamma because I got inspired by
491 the Gamma calculus. It is not very close to the calculus but does
492 behave less sequentially than both foldr and foldl. One could imagine
493 a version of gamma that took a unit element as well thereby avoiding
494 the problem with empty lists.
496 I've tried this code against
498 1) insertion sort - as provided by haskell
499 2) the normal implementation of quick sort
500 3) a deforested version of quick sort due to Jan Sparud
501 4) a super-optimized-quick-sort of Lennart's
503 If the list is partially sorted both merge sort and in particular
504 natural merge sort wins. If the list is random [ average length of
505 rising subsequences = approx 2 ] mergesort still wins and natural
506 merge sort is marginally beaten by Lennart's soqs. The space
507 consumption of merge sort is a bit worse than Lennart's quick sort
508 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
509 fpca article ] isn't used because of group.
516 group :: (a -> a -> Bool) -> [a] -> [[a]]
519 Date: Mon, 12 Feb 1996 15:09:41 +0000
520 From: Andy Gill <andy@dcs.gla.ac.uk>
522 Here is a `better' definition of group.
525 group p (x:xs) = group' xs x x (x :)
527 group' [] _ _ s = [s []]
528 group' (x:xs) x_min x_max s
529 | not (x `p` x_max) = group' xs x_min x (s . (x :))
530 | x `p` x_min = group' xs x x_max ((x :) . s)
531 | otherwise = s [] : group' xs x x (x :)
533 -- This one works forwards *and* backwards, as well as also being
534 -- faster that the one in Util.lhs.
539 let ((h1:t1):tt1) = group p xs
540 (t,tt) = if null xs then ([],[]) else
541 if x `p` h1 then (h1:t1,tt1) else
546 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
547 generalMerge p xs [] = xs
548 generalMerge p [] ys = ys
549 generalMerge p (x:xs) (y:ys) | x `p` y = x : generalMerge p xs (y:ys)
550 | otherwise = y : generalMerge p (x:xs) ys
552 -- gamma is now called balancedFold
554 balancedFold :: (a -> a -> a) -> [a] -> a
555 balancedFold f [] = error "can't reduce an empty list using balancedFold"
556 balancedFold f [x] = x
557 balancedFold f l = balancedFold f (balancedFold' f l)
559 balancedFold' :: (a -> a -> a) -> [a] -> [a]
560 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
561 balancedFold' f xs = xs
563 generalMergeSort p [] = []
564 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
566 generalNaturalMergeSort p [] = []
567 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
569 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
571 mergeSort = generalMergeSort (<=)
572 naturalMergeSort = generalNaturalMergeSort (<=)
574 mergeSortLe le = generalMergeSort le
575 naturalMergeSortLe le = generalNaturalMergeSort le
578 %************************************************************************
580 \subsection[Utils-transitive-closure]{Transitive closure}
582 %************************************************************************
584 This algorithm for transitive closure is straightforward, albeit quadratic.
587 transitiveClosure :: (a -> [a]) -- Successor function
588 -> (a -> a -> Bool) -- Equality predicate
590 -> [a] -- The transitive closure
592 transitiveClosure succ eq xs
596 go done (x:xs) | x `is_in` done = go done xs
597 | otherwise = go (x:done) (succ x ++ xs)
600 x `is_in` (y:ys) | eq x y = True
601 | otherwise = x `is_in` ys
604 %************************************************************************
606 \subsection[Utils-accum]{Accumulating}
608 %************************************************************************
610 @mapAccumL@ behaves like a combination
611 of @map@ and @foldl@;
612 it applies a function to each element of a list, passing an accumulating
613 parameter from left to right, and returning a final value of this
614 accumulator together with the new list.
617 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
618 -- and accumulator, returning new
619 -- accumulator and elt of result list
620 -> acc -- Initial accumulator
622 -> (acc, [y]) -- Final accumulator and result list
624 mapAccumL f b [] = (b, [])
625 mapAccumL f b (x:xs) = (b'', x':xs') where
627 (b'', xs') = mapAccumL f b' xs
630 @mapAccumR@ does the same, but working from right to left instead. Its type is
631 the same as @mapAccumL@, though.
634 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
635 -- and accumulator, returning new
636 -- accumulator and elt of result list
637 -> acc -- Initial accumulator
639 -> (acc, [y]) -- Final accumulator and result list
641 mapAccumR f b [] = (b, [])
642 mapAccumR f b (x:xs) = (b'', x':xs') where
644 (b', xs') = mapAccumR f b xs
647 Here is the bi-directional version, that works from both left and right.
650 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
651 -- Function of elt of input list
652 -- and accumulator, returning new
653 -- accumulator and elt of result list
654 -> accl -- Initial accumulator from left
655 -> accr -- Initial accumulator from right
657 -> (accl, accr, [y]) -- Final accumulators and result list
659 mapAccumB f a b [] = (a,b,[])
660 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
662 (a',b'',y) = f a b' x
663 (a'',b',ys) = mapAccumB f a' b xs
666 %************************************************************************
668 \subsection[Utils-comparison]{Comparisons}
670 %************************************************************************
672 See also @tagCmp_@ near the versions-compatibility section.
674 The Ord3 class will be subsumed into Ord in Haskell 1.3.
679 cmp :: a -> a -> TAG_
682 thenCmp :: Ordering -> Ordering -> Ordering
683 {-# INLINE thenCmp #-}
685 thenCmp other any = other
687 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
688 -- `cmpList' uses a user-specified comparer
690 cmpList cmp [] [] = EQ
691 cmpList cmp [] _ = LT
692 cmpList cmp _ [] = GT
693 cmpList cmp (a:as) (b:bs)
694 = case cmp a b of { EQ -> cmpList cmp as bs; xxx -> xxx }
698 instance Ord3 a => Ord3 [a] where
702 cmp (x:xs) (y:ys) = (x `cmp` y) `thenCmp` (xs `cmp` ys)
704 instance Ord3 a => Ord3 (Maybe a) where
705 cmp Nothing Nothing = EQ_
706 cmp Nothing (Just y) = LT_
707 cmp (Just x) Nothing = GT_
708 cmp (Just x) (Just y) = x `cmp` y
710 instance Ord3 Int where
711 cmp a b | a < b = LT_
717 cmpString :: String -> String -> TAG_
719 cmpString [] [] = EQ_
720 cmpString (x:xs) (y:ys) = if x == y then cmpString xs ys
721 else if x < y then LT_
723 cmpString [] ys = LT_
724 cmpString xs [] = GT_
727 cmpString _ _ = panic# "cmpString"
729 cmpString _ _ = error "cmpString"
734 cmpPString :: FAST_STRING -> FAST_STRING -> TAG_
736 cmpPString x y = compare x y
739 %************************************************************************
741 \subsection[Utils-pairs]{Pairs}
743 %************************************************************************
745 The following are curried versions of @fst@ and @snd@.
748 cfst :: a -> b -> a -- stranal-sem only (Note)
752 The following provide us higher order functions that, when applied
753 to a function, operate on pairs.
756 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
757 applyToPair (f,g) (x,y) = (f x, g y)
759 applyToFst :: (a -> c) -> (a,b)-> (c,b)
760 applyToFst f (x,y) = (f x,y)
762 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
763 applyToSnd f (x,y) = (x,f y)
765 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
766 foldPair fg ab [] = ab
767 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
768 where (u,v) = foldPair fg ab abs
772 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
773 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
776 %************************************************************************
778 \subsection[Utils-errors]{Error handling}
780 %************************************************************************
783 #if defined(COMPILING_GHC)
784 panic x = error ("panic! (the `impossible' happened):\n\t"
786 ++ "Please report it as a compiler bug "
787 ++ "to glasgow-haskell-bugs@dcs.gla.ac.uk.\n\n" )
789 pprPanic heading pretty_msg = panic (heading++(ppShow 80 pretty_msg))
790 pprError heading pretty_msg = error (heading++(ppShow 80 pretty_msg))
791 #if __GLASGOW_HASKELL__ == 201
792 pprTrace heading pretty_msg = GHCbase.trace (heading++(ppShow 80 pretty_msg))
793 #elsif __GLASGOW_HASKELL__ >= 201
794 pprTrace heading pretty_msg = GHC.trace (heading++(ppShow 80 pretty_msg))
796 pprTrace heading pretty_msg = trace (heading++(ppShow 80 pretty_msg))
799 -- #-versions because panic can't return an unboxed int, and that's
800 -- what TAG_ is with GHC at the moment. Ugh. (Simon)
801 -- No, man -- Too Beautiful! (Will)
803 panic# :: String -> TAG_
804 panic# s = case (panic s) of () -> EQ_
806 pprPanic# heading pretty_msg = panic# (heading++(ppShow 80 pretty_msg))
808 assertPanic :: String -> Int -> a
809 assertPanic file line = panic ("ASSERT failed! file "++file++", line "++show line)
811 #endif {- COMPILING_GHC -}
814 Turn a NULL-terminated vector of null-terminated strings into a string list
815 (ToDo: create a module of common marshaling functions)
818 unvectorize :: Addr -> Int -> IO [String]
820 | str == ``NULL'' = return []
822 x <- unpackCStringIO str
823 xs <- unvectorize ptr (n+1)
826 str = indexAddrOffAddr ptr n