2 % (c) The University of Glasgow 1992-2002
4 \section[Util]{Highly random utility functions}
9 -- general list processing
10 zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
11 zipLazy, stretchZipWith,
12 mapAndUnzip, mapAndUnzip3,
14 lengthExceeds, lengthIs, lengthAtLeast, listLengthCmp, atLength,
24 sortLt, naturalMergeSortLe,
26 -- transitive closures
30 mapAccumL, mapAccumR, mapAccumB,
33 takeList, dropList, splitAtList,
36 eqListBy, equalLength, compareLength,
37 thenCmp, cmpList, prefixMatch, suffixMatch, maybePrefixMatch,
53 #include "../includes/config.h"
54 #include "HsVersions.h"
56 import Panic ( panic, trace )
59 #if __GLASGOW_HASKELL__ <= 408
60 import EXCEPTION ( catchIO, justIoErrors, raiseInThread )
62 import DATA_IOREF ( IORef, newIORef )
63 import UNSAFE_IO ( unsafePerformIO )
65 import qualified List ( elem, notElem )
68 import List ( zipWith4 )
71 import Char ( isUpper, isAlphaNum, isSpace )
76 %************************************************************************
78 \subsection{The Eager monad}
80 %************************************************************************
82 The @Eager@ monad is just an encoding of continuation-passing style,
83 used to allow you to express "do this and then that", mainly to avoid
84 space leaks. It's done with a type synonym to save bureaucracy.
89 type Eager ans a = (a -> ans) -> ans
91 runEager :: Eager a a -> a
92 runEager m = m (\x -> x)
94 appEager :: Eager ans a -> (a -> ans) -> ans
95 appEager m cont = m cont
97 thenEager :: Eager ans a -> (a -> Eager ans b) -> Eager ans b
98 thenEager m k cont = m (\r -> k r cont)
100 returnEager :: a -> Eager ans a
101 returnEager v cont = cont v
103 mapEager :: (a -> Eager ans b) -> [a] -> Eager ans [b]
104 mapEager f [] = returnEager []
105 mapEager f (x:xs) = f x `thenEager` \ y ->
106 mapEager f xs `thenEager` \ ys ->
111 %************************************************************************
113 \subsection{A for loop}
115 %************************************************************************
118 -- Compose a function with itself n times. (nth rather than twice)
119 nTimes :: Int -> (a -> a) -> (a -> a)
122 nTimes n f = f . nTimes (n-1) f
125 %************************************************************************
127 \subsection[Utils-lists]{General list processing}
129 %************************************************************************
131 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
132 are of equal length. Alastair Reid thinks this should only happen if
133 DEBUGging on; hey, why not?
136 zipEqual :: String -> [a] -> [b] -> [(a,b)]
137 zipWithEqual :: String -> (a->b->c) -> [a]->[b]->[c]
138 zipWith3Equal :: String -> (a->b->c->d) -> [a]->[b]->[c]->[d]
139 zipWith4Equal :: String -> (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
143 zipWithEqual _ = zipWith
144 zipWith3Equal _ = zipWith3
145 zipWith4Equal _ = zipWith4
147 zipEqual msg [] [] = []
148 zipEqual msg (a:as) (b:bs) = (a,b) : zipEqual msg as bs
149 zipEqual msg as bs = panic ("zipEqual: unequal lists:"++msg)
151 zipWithEqual msg z (a:as) (b:bs)= z a b : zipWithEqual msg z as bs
152 zipWithEqual msg _ [] [] = []
153 zipWithEqual msg _ _ _ = panic ("zipWithEqual: unequal lists:"++msg)
155 zipWith3Equal msg z (a:as) (b:bs) (c:cs)
156 = z a b c : zipWith3Equal msg z as bs cs
157 zipWith3Equal msg _ [] [] [] = []
158 zipWith3Equal msg _ _ _ _ = panic ("zipWith3Equal: unequal lists:"++msg)
160 zipWith4Equal msg z (a:as) (b:bs) (c:cs) (d:ds)
161 = z a b c d : zipWith4Equal msg z as bs cs ds
162 zipWith4Equal msg _ [] [] [] [] = []
163 zipWith4Equal msg _ _ _ _ _ = panic ("zipWith4Equal: unequal lists:"++msg)
168 -- zipLazy is lazy in the second list (observe the ~)
170 zipLazy :: [a] -> [b] -> [(a,b)]
172 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
177 stretchZipWith :: (a -> Bool) -> b -> (a->b->c) -> [a] -> [b] -> [c]
178 -- (stretchZipWith p z f xs ys) stretches ys by inserting z in
179 -- the places where p returns *True*
181 stretchZipWith p z f [] ys = []
182 stretchZipWith p z f (x:xs) ys
183 | p x = f x z : stretchZipWith p z f xs ys
184 | otherwise = case ys of
186 (y:ys) -> f x y : stretchZipWith p z f xs ys
191 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
193 mapAndUnzip f [] = ([],[])
197 (rs1, rs2) = mapAndUnzip f xs
201 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
203 mapAndUnzip3 f [] = ([],[],[])
204 mapAndUnzip3 f (x:xs)
207 (rs1, rs2, rs3) = mapAndUnzip3 f xs
209 (r1:rs1, r2:rs2, r3:rs3)
213 nOfThem :: Int -> a -> [a]
214 nOfThem n thing = replicate n thing
216 -- 'atLength atLen atEnd ls n' unravels list 'ls' to position 'n';
219 -- atLength atLenPred atEndPred ls n
220 -- | n < 0 = atLenPred n
221 -- | length ls < n = atEndPred (n - length ls)
222 -- | otherwise = atLenPred (drop n ls)
224 atLength :: ([a] -> b)
229 atLength atLenPred atEndPred ls n
230 | n < 0 = atEndPred n
231 | otherwise = go n ls
233 go n [] = atEndPred n
234 go 0 ls = atLenPred ls
235 go n (_:xs) = go (n-1) xs
238 lengthExceeds :: [a] -> Int -> Bool
239 -- (lengthExceeds xs n) = (length xs > n)
240 lengthExceeds = atLength notNull (const False)
242 lengthAtLeast :: [a] -> Int -> Bool
243 lengthAtLeast = atLength notNull (== 0)
245 lengthIs :: [a] -> Int -> Bool
246 lengthIs = atLength null (==0)
248 listLengthCmp :: [a] -> Int -> Ordering
249 listLengthCmp = atLength atLen atEnd
253 | x > 0 = LT -- not yet seen 'n' elts, so list length is < n.
259 isSingleton :: [a] -> Bool
260 isSingleton [x] = True
261 isSingleton _ = False
263 notNull :: [a] -> Bool
267 snocView :: [a] -> Maybe ([a],a)
268 -- Split off the last element
269 snocView [] = Nothing
270 snocView xs = go [] xs
272 -- Invariant: second arg is non-empty
273 go acc [x] = Just (reverse acc, x)
274 go acc (x:xs) = go (x:acc) xs
284 Debugging/specialising versions of \tr{elem} and \tr{notElem}
287 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
290 isIn msg x ys = elem__ x ys
291 isn'tIn msg x ys = notElem__ x ys
293 --these are here to be SPECIALIZEd (automagically)
295 elem__ x (y:ys) = x==y || elem__ x ys
297 notElem__ x [] = True
298 notElem__ x (y:ys) = x /= y && notElem__ x ys
302 = elem (_ILIT 0) x ys
306 | i ># _ILIT 100 = trace ("Over-long elem in " ++ msg) $
308 | otherwise = x == y || elem (i +# _ILIT(1)) x ys
311 = notElem (_ILIT 0) x ys
313 notElem i x [] = True
315 | i ># _ILIT 100 = trace ("Over-long notElem in " ++ msg) $
316 x `List.notElem` (y:ys)
317 | otherwise = x /= y && notElem (i +# _ILIT(1)) x ys
321 %************************************************************************
323 \subsection[Utils-sorting]{Sorting}
325 %************************************************************************
327 %************************************************************************
329 \subsubsection[Utils-quicksorting]{Quicksorts}
331 %************************************************************************
336 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
337 quicksort :: (a -> a -> Bool) -- Less-than predicate
339 -> [a] -- Result list in increasing order
342 quicksort lt [x] = [x]
343 quicksort lt (x:xs) = split x [] [] xs
345 split x lo hi [] = quicksort lt lo ++ (x : quicksort lt hi)
346 split x lo hi (y:ys) | y `lt` x = split x (y:lo) hi ys
347 | True = split x lo (y:hi) ys
351 Quicksort variant from Lennart's Haskell-library contribution. This
352 is a {\em stable} sort.
355 sortLt :: (a -> a -> Bool) -- Less-than predicate
357 -> [a] -- Result list
359 sortLt lt l = qsort lt l []
361 -- qsort is stable and does not concatenate.
362 qsort :: (a -> a -> Bool) -- Less-than predicate
363 -> [a] -- xs, Input list
364 -> [a] -- r, Concatenate this list to the sorted input list
365 -> [a] -- Result = sort xs ++ r
369 qsort lt (x:xs) r = qpart lt x xs [] [] r
371 -- qpart partitions and sorts the sublists
372 -- rlt contains things less than x,
373 -- rge contains the ones greater than or equal to x.
374 -- Both have equal elements reversed with respect to the original list.
376 qpart lt x [] rlt rge r =
377 -- rlt and rge are in reverse order and must be sorted with an
378 -- anti-stable sorting
379 rqsort lt rlt (x : rqsort lt rge r)
381 qpart lt x (y:ys) rlt rge r =
384 qpart lt x ys (y:rlt) rge r
387 qpart lt x ys rlt (y:rge) r
389 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
391 rqsort lt [x] r = x:r
392 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
394 rqpart lt x [] rle rgt r =
395 qsort lt rle (x : qsort lt rgt r)
397 rqpart lt x (y:ys) rle rgt r =
400 rqpart lt x ys rle (y:rgt) r
403 rqpart lt x ys (y:rle) rgt r
406 %************************************************************************
408 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
410 %************************************************************************
414 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
416 mergesort cmp xs = merge_lists (split_into_runs [] xs)
418 a `le` b = case cmp a b of { LT -> True; EQ -> True; GT -> False }
419 a `ge` b = case cmp a b of { LT -> False; EQ -> True; GT -> True }
421 split_into_runs [] [] = []
422 split_into_runs run [] = [run]
423 split_into_runs [] (x:xs) = split_into_runs [x] xs
424 split_into_runs [r] (x:xs) | x `ge` r = split_into_runs [r,x] xs
425 split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
426 | True = rl : (split_into_runs [x] xs)
429 merge_lists (x:xs) = merge x (merge_lists xs)
433 merge xl@(x:xs) yl@(y:ys)
435 EQ -> x : y : (merge xs ys)
436 LT -> x : (merge xs yl)
437 GT -> y : (merge xl ys)
441 %************************************************************************
443 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
445 %************************************************************************
448 Date: Mon, 3 May 93 20:45:23 +0200
449 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
450 To: partain@dcs.gla.ac.uk
451 Subject: natural merge sort beats quick sort [ and it is prettier ]
453 Here is a piece of Haskell code that I'm rather fond of. See it as an
454 attempt to get rid of the ridiculous quick-sort routine. group is
455 quite useful by itself I think it was John's idea originally though I
456 believe the lazy version is due to me [surprisingly complicated].
457 gamma [used to be called] is called gamma because I got inspired by
458 the Gamma calculus. It is not very close to the calculus but does
459 behave less sequentially than both foldr and foldl. One could imagine
460 a version of gamma that took a unit element as well thereby avoiding
461 the problem with empty lists.
463 I've tried this code against
465 1) insertion sort - as provided by haskell
466 2) the normal implementation of quick sort
467 3) a deforested version of quick sort due to Jan Sparud
468 4) a super-optimized-quick-sort of Lennart's
470 If the list is partially sorted both merge sort and in particular
471 natural merge sort wins. If the list is random [ average length of
472 rising subsequences = approx 2 ] mergesort still wins and natural
473 merge sort is marginally beaten by Lennart's soqs. The space
474 consumption of merge sort is a bit worse than Lennart's quick sort
475 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
476 fpca article ] isn't used because of group.
483 group :: (a -> a -> Bool) -> [a] -> [[a]]
486 Date: Mon, 12 Feb 1996 15:09:41 +0000
487 From: Andy Gill <andy@dcs.gla.ac.uk>
489 Here is a `better' definition of group.
492 group p (x:xs) = group' xs x x (x :)
494 group' [] _ _ s = [s []]
495 group' (x:xs) x_min x_max s
496 | not (x `p` x_max) = group' xs x_min x (s . (x :))
497 | x `p` x_min = group' xs x x_max ((x :) . s)
498 | otherwise = s [] : group' xs x x (x :)
500 -- This one works forwards *and* backwards, as well as also being
501 -- faster that the one in Util.lhs.
506 let ((h1:t1):tt1) = group p xs
507 (t,tt) = if null xs then ([],[]) else
508 if x `p` h1 then (h1:t1,tt1) else
513 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
514 generalMerge p xs [] = xs
515 generalMerge p [] ys = ys
516 generalMerge p (x:xs) (y:ys) | x `p` y = x : generalMerge p xs (y:ys)
517 | otherwise = y : generalMerge p (x:xs) ys
519 -- gamma is now called balancedFold
521 balancedFold :: (a -> a -> a) -> [a] -> a
522 balancedFold f [] = error "can't reduce an empty list using balancedFold"
523 balancedFold f [x] = x
524 balancedFold f l = balancedFold f (balancedFold' f l)
526 balancedFold' :: (a -> a -> a) -> [a] -> [a]
527 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
528 balancedFold' f xs = xs
530 generalNaturalMergeSort p [] = []
531 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
534 generalMergeSort p [] = []
535 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
537 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
539 mergeSort = generalMergeSort (<=)
540 naturalMergeSort = generalNaturalMergeSort (<=)
542 mergeSortLe le = generalMergeSort le
545 naturalMergeSortLe le = generalNaturalMergeSort le
548 %************************************************************************
550 \subsection[Utils-transitive-closure]{Transitive closure}
552 %************************************************************************
554 This algorithm for transitive closure is straightforward, albeit quadratic.
557 transitiveClosure :: (a -> [a]) -- Successor function
558 -> (a -> a -> Bool) -- Equality predicate
560 -> [a] -- The transitive closure
562 transitiveClosure succ eq xs
566 go done (x:xs) | x `is_in` done = go done xs
567 | otherwise = go (x:done) (succ x ++ xs)
570 x `is_in` (y:ys) | eq x y = True
571 | otherwise = x `is_in` ys
574 %************************************************************************
576 \subsection[Utils-accum]{Accumulating}
578 %************************************************************************
580 @mapAccumL@ behaves like a combination
581 of @map@ and @foldl@;
582 it applies a function to each element of a list, passing an accumulating
583 parameter from left to right, and returning a final value of this
584 accumulator together with the new list.
587 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
588 -- and accumulator, returning new
589 -- accumulator and elt of result list
590 -> acc -- Initial accumulator
592 -> (acc, [y]) -- Final accumulator and result list
594 mapAccumL f b [] = (b, [])
595 mapAccumL f b (x:xs) = (b'', x':xs') where
597 (b'', xs') = mapAccumL f b' xs
600 @mapAccumR@ does the same, but working from right to left instead. Its type is
601 the same as @mapAccumL@, though.
604 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
605 -- and accumulator, returning new
606 -- accumulator and elt of result list
607 -> acc -- Initial accumulator
609 -> (acc, [y]) -- Final accumulator and result list
611 mapAccumR f b [] = (b, [])
612 mapAccumR f b (x:xs) = (b'', x':xs') where
614 (b', xs') = mapAccumR f b xs
617 Here is the bi-directional version, that works from both left and right.
620 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
621 -- Function of elt of input list
622 -- and accumulator, returning new
623 -- accumulator and elt of result list
624 -> accl -- Initial accumulator from left
625 -> accr -- Initial accumulator from right
627 -> (accl, accr, [y]) -- Final accumulators and result list
629 mapAccumB f a b [] = (a,b,[])
630 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
632 (a',b'',y) = f a b' x
633 (a'',b',ys) = mapAccumB f a' b xs
636 A strict version of foldl.
639 foldl' :: (a -> b -> a) -> a -> [b] -> a
640 foldl' f z xs = lgo z xs
643 lgo z (x:xs) = (lgo $! (f z x)) xs
646 A combination of foldl with zip. It works with equal length lists.
649 foldl2 :: (acc -> a -> b -> acc) -> acc -> [a] -> [b] -> acc
651 foldl2 k z (a:as) (b:bs) = foldl2 k (k z a b) as bs
654 Count the number of times a predicate is true
657 count :: (a -> Bool) -> [a] -> Int
659 count p (x:xs) | p x = 1 + count p xs
660 | otherwise = count p xs
663 @splitAt@, @take@, and @drop@ but with length of another
664 list giving the break-off point:
667 takeList :: [b] -> [a] -> [a]
672 (y:ys) -> y : takeList xs ys
674 dropList :: [b] -> [a] -> [a]
676 dropList _ xs@[] = xs
677 dropList (_:xs) (_:ys) = dropList xs ys
680 splitAtList :: [b] -> [a] -> ([a], [a])
681 splitAtList [] xs = ([], xs)
682 splitAtList _ xs@[] = (xs, xs)
683 splitAtList (_:xs) (y:ys) = (y:ys', ys'')
685 (ys', ys'') = splitAtList xs ys
690 %************************************************************************
692 \subsection[Utils-comparison]{Comparisons}
694 %************************************************************************
697 eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
698 eqListBy eq [] [] = True
699 eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
700 eqListBy eq xs ys = False
702 equalLength :: [a] -> [b] -> Bool
703 equalLength [] [] = True
704 equalLength (_:xs) (_:ys) = equalLength xs ys
705 equalLength xs ys = False
707 compareLength :: [a] -> [b] -> Ordering
708 compareLength [] [] = EQ
709 compareLength (_:xs) (_:ys) = compareLength xs ys
710 compareLength [] _ys = LT
711 compareLength _xs [] = GT
713 thenCmp :: Ordering -> Ordering -> Ordering
714 {-# INLINE thenCmp #-}
716 thenCmp other any = other
718 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
719 -- `cmpList' uses a user-specified comparer
721 cmpList cmp [] [] = EQ
722 cmpList cmp [] _ = LT
723 cmpList cmp _ [] = GT
724 cmpList cmp (a:as) (b:bs)
725 = case cmp a b of { EQ -> cmpList cmp as bs; xxx -> xxx }
729 prefixMatch :: Eq a => [a] -> [a] -> Bool
730 prefixMatch [] _str = True
731 prefixMatch _pat [] = False
732 prefixMatch (p:ps) (s:ss) | p == s = prefixMatch ps ss
735 maybePrefixMatch :: String -> String -> Maybe String
736 maybePrefixMatch [] rest = Just rest
737 maybePrefixMatch (_:_) [] = Nothing
738 maybePrefixMatch (p:pat) (r:rest)
739 | p == r = maybePrefixMatch pat rest
740 | otherwise = Nothing
742 suffixMatch :: Eq a => [a] -> [a] -> Bool
743 suffixMatch pat str = prefixMatch (reverse pat) (reverse str)
746 %************************************************************************
748 \subsection[Utils-pairs]{Pairs}
750 %************************************************************************
752 The following are curried versions of @fst@ and @snd@.
756 cfst :: a -> b -> a -- stranal-sem only (Note)
761 The following provide us higher order functions that, when applied
762 to a function, operate on pairs.
766 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
767 applyToPair (f,g) (x,y) = (f x, g y)
769 applyToFst :: (a -> c) -> (a,b)-> (c,b)
770 applyToFst f (x,y) = (f x,y)
772 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
773 applyToSnd f (x,y) = (x,f y)
778 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
779 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
783 seqList :: [a] -> b -> b
785 seqList (x:xs) b = x `seq` seqList xs b
791 global :: a -> IORef a
792 global a = unsafePerformIO (newIORef a)
798 looksLikeModuleName [] = False
799 looksLikeModuleName (c:cs) = isUpper c && go cs
801 go ('.':cs) = looksLikeModuleName cs
802 go (c:cs) = (isAlphaNum c || c == '_') && go cs
805 Akin to @Prelude.words@, but sensitive to dquoted entities treating
806 them as single words.
809 toArgs :: String -> [String]
812 case break (\ ch -> isSpace ch || ch == '"') (dropWhile isSpace s) of -- "
814 (\ ws -> if null w then ws else w : ws) $
818 | x /= '"' -> toArgs xs
821 ((str,rs):_) -> stripQuotes str : toArgs rs
824 -- strip away dquotes; assume first and last chars contain quotes.
825 stripQuotes :: String -> String
826 stripQuotes ('"':xs) = init xs