2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Util]{Highly random utility functions}
7 -- IF_NOT_GHC is meant to make this module useful outside the context of GHC
13 Eager, thenEager, returnEager, mapEager, appEager, runEager,
16 -- general list processing
17 zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
18 zipLazy, stretchZipWith,
19 mapAndUnzip, mapAndUnzip3,
21 lengthExceeds, lengthIs, lengthAtLeast, listLengthCmp, atLength,
33 IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
35 IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
36 IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
38 -- transitive closures
42 mapAccumL, mapAccumR, mapAccumB,
45 takeList, dropList, splitAtList,
48 eqListBy, equalLength, compareLength,
49 thenCmp, cmpList, prefixMatch, suffixMatch,
55 IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
56 IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
61 #if __GLASGOW_HASKELL__ <= 408
69 #include "../includes/config.h"
70 #include "HsVersions.h"
72 import List ( zipWith4 )
73 import Maybe ( Maybe(..) )
74 import Panic ( panic )
75 import IOExts ( IORef, newIORef, unsafePerformIO )
77 #if __GLASGOW_HASKELL__ <= 408
78 import Exception ( catchIO, justIoErrors, raiseInThread )
84 %************************************************************************
86 \subsection{The Eager monad}
88 %************************************************************************
90 The @Eager@ monad is just an encoding of continuation-passing style,
91 used to allow you to express "do this and then that", mainly to avoid
92 space leaks. It's done with a type synonym to save bureaucracy.
97 type Eager ans a = (a -> ans) -> ans
99 runEager :: Eager a a -> a
100 runEager m = m (\x -> x)
102 appEager :: Eager ans a -> (a -> ans) -> ans
103 appEager m cont = m cont
105 thenEager :: Eager ans a -> (a -> Eager ans b) -> Eager ans b
106 thenEager m k cont = m (\r -> k r cont)
108 returnEager :: a -> Eager ans a
109 returnEager v cont = cont v
111 mapEager :: (a -> Eager ans b) -> [a] -> Eager ans [b]
112 mapEager f [] = returnEager []
113 mapEager f (x:xs) = f x `thenEager` \ y ->
114 mapEager f xs `thenEager` \ ys ->
119 %************************************************************************
121 \subsection{A for loop}
123 %************************************************************************
126 -- Compose a function with itself n times. (nth rather than twice)
127 nTimes :: Int -> (a -> a) -> (a -> a)
130 nTimes n f = f . nTimes (n-1) f
133 %************************************************************************
135 \subsection{Maybe-ery}
137 %************************************************************************
140 unJust :: String -> Maybe a -> a
141 unJust who (Just x) = x
142 unJust who Nothing = panic ("unJust of Nothing, called by " ++ who)
145 %************************************************************************
147 \subsection[Utils-lists]{General list processing}
149 %************************************************************************
151 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
152 are of equal length. Alastair Reid thinks this should only happen if
153 DEBUGging on; hey, why not?
156 zipEqual :: String -> [a] -> [b] -> [(a,b)]
157 zipWithEqual :: String -> (a->b->c) -> [a]->[b]->[c]
158 zipWith3Equal :: String -> (a->b->c->d) -> [a]->[b]->[c]->[d]
159 zipWith4Equal :: String -> (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
163 zipWithEqual _ = zipWith
164 zipWith3Equal _ = zipWith3
165 zipWith4Equal _ = zipWith4
167 zipEqual msg [] [] = []
168 zipEqual msg (a:as) (b:bs) = (a,b) : zipEqual msg as bs
169 zipEqual msg as bs = panic ("zipEqual: unequal lists:"++msg)
171 zipWithEqual msg z (a:as) (b:bs)= z a b : zipWithEqual msg z as bs
172 zipWithEqual msg _ [] [] = []
173 zipWithEqual msg _ _ _ = panic ("zipWithEqual: unequal lists:"++msg)
175 zipWith3Equal msg z (a:as) (b:bs) (c:cs)
176 = z a b c : zipWith3Equal msg z as bs cs
177 zipWith3Equal msg _ [] [] [] = []
178 zipWith3Equal msg _ _ _ _ = panic ("zipWith3Equal: unequal lists:"++msg)
180 zipWith4Equal msg z (a:as) (b:bs) (c:cs) (d:ds)
181 = z a b c d : zipWith4Equal msg z as bs cs ds
182 zipWith4Equal msg _ [] [] [] [] = []
183 zipWith4Equal msg _ _ _ _ _ = panic ("zipWith4Equal: unequal lists:"++msg)
188 -- zipLazy is lazy in the second list (observe the ~)
190 zipLazy :: [a] -> [b] -> [(a,b)]
192 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
197 stretchZipWith :: (a -> Bool) -> b -> (a->b->c) -> [a] -> [b] -> [c]
198 -- (stretchZipWith p z f xs ys) stretches ys by inserting z in
199 -- the places where p returns *True*
201 stretchZipWith p z f [] ys = []
202 stretchZipWith p z f (x:xs) ys
203 | p x = f x z : stretchZipWith p z f xs ys
204 | otherwise = case ys of
206 (y:ys) -> f x y : stretchZipWith p z f xs ys
211 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
213 mapAndUnzip f [] = ([],[])
217 (rs1, rs2) = mapAndUnzip f xs
221 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
223 mapAndUnzip3 f [] = ([],[],[])
224 mapAndUnzip3 f (x:xs)
227 (rs1, rs2, rs3) = mapAndUnzip3 f xs
229 (r1:rs1, r2:rs2, r3:rs3)
233 nOfThem :: Int -> a -> [a]
234 nOfThem n thing = replicate n thing
236 -- 'atLength atLen atEnd ls n' unravels list 'ls' to position 'n';
239 -- atLength atLenPred atEndPred ls n
240 -- | n < 0 = atLenPred n
241 -- | length ls < n = atEndPred (n - length ls)
242 -- | otherwise = atLenPred (drop n ls)
244 atLength :: ([a] -> b)
249 atLength atLenPred atEndPred ls n
250 | n < 0 = atEndPred n
251 | otherwise = go n ls
253 go n [] = atEndPred n
254 go 0 ls = atLenPred ls
255 go n (_:xs) = go (n-1) xs
258 lengthExceeds :: [a] -> Int -> Bool
259 lengthExceeds = atLength (not.null) (const False)
261 lengthAtLeast :: [a] -> Int -> Bool
262 lengthAtLeast = atLength (not.null) (== 0)
264 lengthIs :: [a] -> Int -> Bool
265 lengthIs = atLength null (==0)
267 listLengthCmp :: [a] -> Int -> Ordering
268 listLengthCmp = atLength atLen atEnd
272 | x > 0 = LT -- not yet seen 'n' elts, so list length is < n.
278 isSingleton :: [a] -> Bool
279 isSingleton [x] = True
280 isSingleton _ = False
291 snocView :: [a] -> ([a], a) -- Split off the last element
292 snocView xs = go xs []
294 go [x] acc = (reverse acc, x)
295 go (x:xs) acc = go xs (x:acc)
298 Debugging/specialising versions of \tr{elem} and \tr{notElem}
301 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
304 isIn msg x ys = elem__ x ys
305 isn'tIn msg x ys = notElem__ x ys
307 --these are here to be SPECIALIZEd (automagically)
309 elem__ x (y:ys) = x==y || elem__ x ys
311 notElem__ x [] = True
312 notElem__ x (y:ys) = x /= y && notElem__ x ys
316 = elem (_ILIT 0) x ys
320 | i ># _ILIT 100 = panic ("Over-long elem in: " ++ msg)
321 | otherwise = x == y || elem (i +# _ILIT(1)) x ys
324 = notElem (_ILIT 0) x ys
326 notElem i x [] = True
328 | i ># _ILIT 100 = panic ("Over-long notElem in: " ++ msg)
329 | otherwise = x /= y && notElem (i +# _ILIT(1)) x ys
335 %************************************************************************
337 \subsection[Utils-sorting]{Sorting}
339 %************************************************************************
341 %************************************************************************
343 \subsubsection[Utils-quicksorting]{Quicksorts}
345 %************************************************************************
350 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
351 quicksort :: (a -> a -> Bool) -- Less-than predicate
353 -> [a] -- Result list in increasing order
356 quicksort lt [x] = [x]
357 quicksort lt (x:xs) = split x [] [] xs
359 split x lo hi [] = quicksort lt lo ++ (x : quicksort lt hi)
360 split x lo hi (y:ys) | y `lt` x = split x (y:lo) hi ys
361 | True = split x lo (y:hi) ys
365 Quicksort variant from Lennart's Haskell-library contribution. This
366 is a {\em stable} sort.
369 stableSortLt = sortLt -- synonym; when we want to highlight stable-ness
371 sortLt :: (a -> a -> Bool) -- Less-than predicate
373 -> [a] -- Result list
375 sortLt lt l = qsort lt l []
377 -- qsort is stable and does not concatenate.
378 qsort :: (a -> a -> Bool) -- Less-than predicate
379 -> [a] -- xs, Input list
380 -> [a] -- r, Concatenate this list to the sorted input list
381 -> [a] -- Result = sort xs ++ r
385 qsort lt (x:xs) r = qpart lt x xs [] [] r
387 -- qpart partitions and sorts the sublists
388 -- rlt contains things less than x,
389 -- rge contains the ones greater than or equal to x.
390 -- Both have equal elements reversed with respect to the original list.
392 qpart lt x [] rlt rge r =
393 -- rlt and rge are in reverse order and must be sorted with an
394 -- anti-stable sorting
395 rqsort lt rlt (x : rqsort lt rge r)
397 qpart lt x (y:ys) rlt rge r =
400 qpart lt x ys (y:rlt) rge r
403 qpart lt x ys rlt (y:rge) r
405 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
407 rqsort lt [x] r = x:r
408 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
410 rqpart lt x [] rle rgt r =
411 qsort lt rle (x : qsort lt rgt r)
413 rqpart lt x (y:ys) rle rgt r =
416 rqpart lt x ys rle (y:rgt) r
419 rqpart lt x ys (y:rle) rgt r
422 %************************************************************************
424 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
426 %************************************************************************
430 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
432 mergesort cmp xs = merge_lists (split_into_runs [] xs)
434 a `le` b = case cmp a b of { LT -> True; EQ -> True; GT -> False }
435 a `ge` b = case cmp a b of { LT -> False; EQ -> True; GT -> True }
437 split_into_runs [] [] = []
438 split_into_runs run [] = [run]
439 split_into_runs [] (x:xs) = split_into_runs [x] xs
440 split_into_runs [r] (x:xs) | x `ge` r = split_into_runs [r,x] xs
441 split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
442 | True = rl : (split_into_runs [x] xs)
445 merge_lists (x:xs) = merge x (merge_lists xs)
449 merge xl@(x:xs) yl@(y:ys)
451 EQ -> x : y : (merge xs ys)
452 LT -> x : (merge xs yl)
453 GT -> y : (merge xl ys)
457 %************************************************************************
459 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
461 %************************************************************************
464 Date: Mon, 3 May 93 20:45:23 +0200
465 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
466 To: partain@dcs.gla.ac.uk
467 Subject: natural merge sort beats quick sort [ and it is prettier ]
469 Here is a piece of Haskell code that I'm rather fond of. See it as an
470 attempt to get rid of the ridiculous quick-sort routine. group is
471 quite useful by itself I think it was John's idea originally though I
472 believe the lazy version is due to me [surprisingly complicated].
473 gamma [used to be called] is called gamma because I got inspired by
474 the Gamma calculus. It is not very close to the calculus but does
475 behave less sequentially than both foldr and foldl. One could imagine
476 a version of gamma that took a unit element as well thereby avoiding
477 the problem with empty lists.
479 I've tried this code against
481 1) insertion sort - as provided by haskell
482 2) the normal implementation of quick sort
483 3) a deforested version of quick sort due to Jan Sparud
484 4) a super-optimized-quick-sort of Lennart's
486 If the list is partially sorted both merge sort and in particular
487 natural merge sort wins. If the list is random [ average length of
488 rising subsequences = approx 2 ] mergesort still wins and natural
489 merge sort is marginally beaten by Lennart's soqs. The space
490 consumption of merge sort is a bit worse than Lennart's quick sort
491 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
492 fpca article ] isn't used because of group.
499 group :: (a -> a -> Bool) -> [a] -> [[a]]
502 Date: Mon, 12 Feb 1996 15:09:41 +0000
503 From: Andy Gill <andy@dcs.gla.ac.uk>
505 Here is a `better' definition of group.
508 group p (x:xs) = group' xs x x (x :)
510 group' [] _ _ s = [s []]
511 group' (x:xs) x_min x_max s
512 | not (x `p` x_max) = group' xs x_min x (s . (x :))
513 | x `p` x_min = group' xs x x_max ((x :) . s)
514 | otherwise = s [] : group' xs x x (x :)
516 -- This one works forwards *and* backwards, as well as also being
517 -- faster that the one in Util.lhs.
522 let ((h1:t1):tt1) = group p xs
523 (t,tt) = if null xs then ([],[]) else
524 if x `p` h1 then (h1:t1,tt1) else
529 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
530 generalMerge p xs [] = xs
531 generalMerge p [] ys = ys
532 generalMerge p (x:xs) (y:ys) | x `p` y = x : generalMerge p xs (y:ys)
533 | otherwise = y : generalMerge p (x:xs) ys
535 -- gamma is now called balancedFold
537 balancedFold :: (a -> a -> a) -> [a] -> a
538 balancedFold f [] = error "can't reduce an empty list using balancedFold"
539 balancedFold f [x] = x
540 balancedFold f l = balancedFold f (balancedFold' f l)
542 balancedFold' :: (a -> a -> a) -> [a] -> [a]
543 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
544 balancedFold' f xs = xs
546 generalMergeSort p [] = []
547 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
549 generalNaturalMergeSort p [] = []
550 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
552 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
554 mergeSort = generalMergeSort (<=)
555 naturalMergeSort = generalNaturalMergeSort (<=)
557 mergeSortLe le = generalMergeSort le
558 naturalMergeSortLe le = generalNaturalMergeSort le
561 %************************************************************************
563 \subsection[Utils-transitive-closure]{Transitive closure}
565 %************************************************************************
567 This algorithm for transitive closure is straightforward, albeit quadratic.
570 transitiveClosure :: (a -> [a]) -- Successor function
571 -> (a -> a -> Bool) -- Equality predicate
573 -> [a] -- The transitive closure
575 transitiveClosure succ eq xs
579 go done (x:xs) | x `is_in` done = go done xs
580 | otherwise = go (x:done) (succ x ++ xs)
583 x `is_in` (y:ys) | eq x y = True
584 | otherwise = x `is_in` ys
587 %************************************************************************
589 \subsection[Utils-accum]{Accumulating}
591 %************************************************************************
593 @mapAccumL@ behaves like a combination
594 of @map@ and @foldl@;
595 it applies a function to each element of a list, passing an accumulating
596 parameter from left to right, and returning a final value of this
597 accumulator together with the new list.
600 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
601 -- and accumulator, returning new
602 -- accumulator and elt of result list
603 -> acc -- Initial accumulator
605 -> (acc, [y]) -- Final accumulator and result list
607 mapAccumL f b [] = (b, [])
608 mapAccumL f b (x:xs) = (b'', x':xs') where
610 (b'', xs') = mapAccumL f b' xs
613 @mapAccumR@ does the same, but working from right to left instead. Its type is
614 the same as @mapAccumL@, though.
617 mapAccumR :: (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 mapAccumR f b [] = (b, [])
625 mapAccumR f b (x:xs) = (b'', x':xs') where
627 (b', xs') = mapAccumR f b xs
630 Here is the bi-directional version, that works from both left and right.
633 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
634 -- Function of elt of input list
635 -- and accumulator, returning new
636 -- accumulator and elt of result list
637 -> accl -- Initial accumulator from left
638 -> accr -- Initial accumulator from right
640 -> (accl, accr, [y]) -- Final accumulators and result list
642 mapAccumB f a b [] = (a,b,[])
643 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
645 (a',b'',y) = f a b' x
646 (a'',b',ys) = mapAccumB f a' b xs
649 A strict version of foldl.
652 foldl' :: (a -> b -> a) -> a -> [b] -> a
653 foldl' f z xs = lgo z xs
656 lgo z (x:xs) = (lgo $! (f z x)) xs
659 A combination of foldl with zip. It works with equal length lists.
662 foldl2 :: (acc -> a -> b -> acc) -> acc -> [a] -> [b] -> acc
664 foldl2 k z (a:as) (b:bs) = foldl2 k (k z a b) as bs
667 Count the number of times a predicate is true
670 count :: (a -> Bool) -> [a] -> Int
672 count p (x:xs) | p x = 1 + count p xs
673 | otherwise = count p xs
676 @splitAt@, @take@, and @drop@ but with length of another
677 list giving the break-off point:
680 takeList :: [b] -> [a] -> [a]
685 (y:ys) -> y : takeList xs ys
687 dropList :: [b] -> [a] -> [a]
689 dropList _ xs@[] = xs
690 dropList (_:xs) (_:ys) = dropList xs ys
693 splitAtList :: [b] -> [a] -> ([a], [a])
694 splitAtList [] xs = ([], xs)
695 splitAtList _ xs@[] = (xs, xs)
696 splitAtList (_:xs) (y:ys) = (y:ys', ys'')
698 (ys', ys'') = splitAtList xs ys
703 %************************************************************************
705 \subsection[Utils-comparison]{Comparisons}
707 %************************************************************************
710 eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
711 eqListBy eq [] [] = True
712 eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
713 eqListBy eq xs ys = False
715 equalLength :: [a] -> [b] -> Bool
716 equalLength [] [] = True
717 equalLength (_:xs) (_:ys) = equalLength xs ys
718 equalLength xs ys = False
720 compareLength :: [a] -> [b] -> Ordering
721 compareLength [] [] = EQ
722 compareLength (_:xs) (_:ys) = compareLength xs ys
723 compareLength [] _ys = LT
724 compareLength _xs [] = GT
726 thenCmp :: Ordering -> Ordering -> Ordering
727 {-# INLINE thenCmp #-}
729 thenCmp other any = other
731 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
732 -- `cmpList' uses a user-specified comparer
734 cmpList cmp [] [] = EQ
735 cmpList cmp [] _ = LT
736 cmpList cmp _ [] = GT
737 cmpList cmp (a:as) (b:bs)
738 = case cmp a b of { EQ -> cmpList cmp as bs; xxx -> xxx }
742 prefixMatch :: Eq a => [a] -> [a] -> Bool
743 prefixMatch [] _str = True
744 prefixMatch _pat [] = False
745 prefixMatch (p:ps) (s:ss) | p == s = prefixMatch ps ss
748 suffixMatch :: Eq a => [a] -> [a] -> Bool
749 suffixMatch pat str = prefixMatch (reverse pat) (reverse str)
752 %************************************************************************
754 \subsection[Utils-pairs]{Pairs}
756 %************************************************************************
758 The following are curried versions of @fst@ and @snd@.
761 cfst :: a -> b -> a -- stranal-sem only (Note)
765 The following provide us higher order functions that, when applied
766 to a function, operate on pairs.
769 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
770 applyToPair (f,g) (x,y) = (f x, g y)
772 applyToFst :: (a -> c) -> (a,b)-> (c,b)
773 applyToFst f (x,y) = (f x,y)
775 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
776 applyToSnd f (x,y) = (x,f y)
778 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
779 foldPair fg ab [] = ab
780 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
781 where (u,v) = foldPair fg ab abs
785 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
786 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
790 seqList :: [a] -> b -> b
792 seqList (x:xs) b = x `seq` seqList xs b
798 global :: a -> IORef a
799 global a = unsafePerformIO (newIORef a)
805 #if __GLASGOW_HASKELL__ <= 408
807 ioErrors = justIoErrors
808 throwTo = raiseInThread