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,
25 sortLt, naturalMergeSortLe,
27 -- transitive closures
31 mapAccumL, mapAccumR, mapAccumB,
34 takeList, dropList, splitAtList,
37 eqListBy, equalLength, compareLength,
38 thenCmp, cmpList, prefixMatch, suffixMatch,
49 #include "../includes/config.h"
50 #include "HsVersions.h"
52 import Panic ( panic, trace )
55 #if __GLASGOW_HASKELL__ <= 408
56 import EXCEPTION ( catchIO, justIoErrors, raiseInThread )
58 import DATA_IOREF ( IORef, newIORef )
59 import UNSAFE_IO ( unsafePerformIO )
61 import qualified List ( elem, notElem )
64 import List ( zipWith4 )
70 %************************************************************************
72 \subsection{The Eager monad}
74 %************************************************************************
76 The @Eager@ monad is just an encoding of continuation-passing style,
77 used to allow you to express "do this and then that", mainly to avoid
78 space leaks. It's done with a type synonym to save bureaucracy.
83 type Eager ans a = (a -> ans) -> ans
85 runEager :: Eager a a -> a
86 runEager m = m (\x -> x)
88 appEager :: Eager ans a -> (a -> ans) -> ans
89 appEager m cont = m cont
91 thenEager :: Eager ans a -> (a -> Eager ans b) -> Eager ans b
92 thenEager m k cont = m (\r -> k r cont)
94 returnEager :: a -> Eager ans a
95 returnEager v cont = cont v
97 mapEager :: (a -> Eager ans b) -> [a] -> Eager ans [b]
98 mapEager f [] = returnEager []
99 mapEager f (x:xs) = f x `thenEager` \ y ->
100 mapEager f xs `thenEager` \ ys ->
105 %************************************************************************
107 \subsection{A for loop}
109 %************************************************************************
112 -- Compose a function with itself n times. (nth rather than twice)
113 nTimes :: Int -> (a -> a) -> (a -> a)
116 nTimes n f = f . nTimes (n-1) f
119 %************************************************************************
121 \subsection[Utils-lists]{General list processing}
123 %************************************************************************
125 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
126 are of equal length. Alastair Reid thinks this should only happen if
127 DEBUGging on; hey, why not?
130 zipEqual :: String -> [a] -> [b] -> [(a,b)]
131 zipWithEqual :: String -> (a->b->c) -> [a]->[b]->[c]
132 zipWith3Equal :: String -> (a->b->c->d) -> [a]->[b]->[c]->[d]
133 zipWith4Equal :: String -> (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
137 zipWithEqual _ = zipWith
138 zipWith3Equal _ = zipWith3
139 zipWith4Equal _ = zipWith4
141 zipEqual msg [] [] = []
142 zipEqual msg (a:as) (b:bs) = (a,b) : zipEqual msg as bs
143 zipEqual msg as bs = panic ("zipEqual: unequal lists:"++msg)
145 zipWithEqual msg z (a:as) (b:bs)= z a b : zipWithEqual msg z as bs
146 zipWithEqual msg _ [] [] = []
147 zipWithEqual msg _ _ _ = panic ("zipWithEqual: unequal lists:"++msg)
149 zipWith3Equal msg z (a:as) (b:bs) (c:cs)
150 = z a b c : zipWith3Equal msg z as bs cs
151 zipWith3Equal msg _ [] [] [] = []
152 zipWith3Equal msg _ _ _ _ = panic ("zipWith3Equal: unequal lists:"++msg)
154 zipWith4Equal msg z (a:as) (b:bs) (c:cs) (d:ds)
155 = z a b c d : zipWith4Equal msg z as bs cs ds
156 zipWith4Equal msg _ [] [] [] [] = []
157 zipWith4Equal msg _ _ _ _ _ = panic ("zipWith4Equal: unequal lists:"++msg)
162 -- zipLazy is lazy in the second list (observe the ~)
164 zipLazy :: [a] -> [b] -> [(a,b)]
166 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
171 stretchZipWith :: (a -> Bool) -> b -> (a->b->c) -> [a] -> [b] -> [c]
172 -- (stretchZipWith p z f xs ys) stretches ys by inserting z in
173 -- the places where p returns *True*
175 stretchZipWith p z f [] ys = []
176 stretchZipWith p z f (x:xs) ys
177 | p x = f x z : stretchZipWith p z f xs ys
178 | otherwise = case ys of
180 (y:ys) -> f x y : stretchZipWith p z f xs ys
185 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
187 mapAndUnzip f [] = ([],[])
191 (rs1, rs2) = mapAndUnzip f xs
195 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
197 mapAndUnzip3 f [] = ([],[],[])
198 mapAndUnzip3 f (x:xs)
201 (rs1, rs2, rs3) = mapAndUnzip3 f xs
203 (r1:rs1, r2:rs2, r3:rs3)
207 nOfThem :: Int -> a -> [a]
208 nOfThem n thing = replicate n thing
210 -- 'atLength atLen atEnd ls n' unravels list 'ls' to position 'n';
213 -- atLength atLenPred atEndPred ls n
214 -- | n < 0 = atLenPred n
215 -- | length ls < n = atEndPred (n - length ls)
216 -- | otherwise = atLenPred (drop n ls)
218 atLength :: ([a] -> b)
223 atLength atLenPred atEndPred ls n
224 | n < 0 = atEndPred n
225 | otherwise = go n ls
227 go n [] = atEndPred n
228 go 0 ls = atLenPred ls
229 go n (_:xs) = go (n-1) xs
232 lengthExceeds :: [a] -> Int -> Bool
233 -- (lengthExceeds xs n) = (length xs > n)
234 lengthExceeds = atLength notNull (const False)
236 lengthAtLeast :: [a] -> Int -> Bool
237 lengthAtLeast = atLength notNull (== 0)
239 lengthIs :: [a] -> Int -> Bool
240 lengthIs = atLength null (==0)
242 listLengthCmp :: [a] -> Int -> Ordering
243 listLengthCmp = atLength atLen atEnd
247 | x > 0 = LT -- not yet seen 'n' elts, so list length is < n.
253 isSingleton :: [a] -> Bool
254 isSingleton [x] = True
255 isSingleton _ = False
257 notNull :: [a] -> Bool
270 snocView :: [a] -> ([a], a) -- Split off the last element
271 snocView xs = go xs []
273 go [x] acc = (reverse acc, x)
274 go (x:xs) acc = go xs (x:acc)
277 Debugging/specialising versions of \tr{elem} and \tr{notElem}
280 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
283 isIn msg x ys = elem__ x ys
284 isn'tIn msg x ys = notElem__ x ys
286 --these are here to be SPECIALIZEd (automagically)
288 elem__ x (y:ys) = x==y || elem__ x ys
290 notElem__ x [] = True
291 notElem__ x (y:ys) = x /= y && notElem__ x ys
295 = elem (_ILIT 0) x ys
299 | i ># _ILIT 100 = trace ("Over-long elem in " ++ msg) $
301 | otherwise = x == y || elem (i +# _ILIT(1)) x ys
304 = notElem (_ILIT 0) x ys
306 notElem i x [] = True
308 | i ># _ILIT 100 = trace ("Over-long notElem in " ++ msg) $
309 x `List.notElem` (y:ys)
310 | otherwise = x /= y && notElem (i +# _ILIT(1)) x ys
314 %************************************************************************
316 \subsection[Utils-sorting]{Sorting}
318 %************************************************************************
320 %************************************************************************
322 \subsubsection[Utils-quicksorting]{Quicksorts}
324 %************************************************************************
329 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
330 quicksort :: (a -> a -> Bool) -- Less-than predicate
332 -> [a] -- Result list in increasing order
335 quicksort lt [x] = [x]
336 quicksort lt (x:xs) = split x [] [] xs
338 split x lo hi [] = quicksort lt lo ++ (x : quicksort lt hi)
339 split x lo hi (y:ys) | y `lt` x = split x (y:lo) hi ys
340 | True = split x lo (y:hi) ys
344 Quicksort variant from Lennart's Haskell-library contribution. This
345 is a {\em stable} sort.
348 sortLt :: (a -> a -> Bool) -- Less-than predicate
350 -> [a] -- Result list
352 sortLt lt l = qsort lt l []
354 -- qsort is stable and does not concatenate.
355 qsort :: (a -> a -> Bool) -- Less-than predicate
356 -> [a] -- xs, Input list
357 -> [a] -- r, Concatenate this list to the sorted input list
358 -> [a] -- Result = sort xs ++ r
362 qsort lt (x:xs) r = qpart lt x xs [] [] r
364 -- qpart partitions and sorts the sublists
365 -- rlt contains things less than x,
366 -- rge contains the ones greater than or equal to x.
367 -- Both have equal elements reversed with respect to the original list.
369 qpart lt x [] rlt rge r =
370 -- rlt and rge are in reverse order and must be sorted with an
371 -- anti-stable sorting
372 rqsort lt rlt (x : rqsort lt rge r)
374 qpart lt x (y:ys) rlt rge r =
377 qpart lt x ys (y:rlt) rge r
380 qpart lt x ys rlt (y:rge) r
382 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
384 rqsort lt [x] r = x:r
385 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
387 rqpart lt x [] rle rgt r =
388 qsort lt rle (x : qsort lt rgt r)
390 rqpart lt x (y:ys) rle rgt r =
393 rqpart lt x ys rle (y:rgt) r
396 rqpart lt x ys (y:rle) rgt r
399 %************************************************************************
401 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
403 %************************************************************************
407 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
409 mergesort cmp xs = merge_lists (split_into_runs [] xs)
411 a `le` b = case cmp a b of { LT -> True; EQ -> True; GT -> False }
412 a `ge` b = case cmp a b of { LT -> False; EQ -> True; GT -> True }
414 split_into_runs [] [] = []
415 split_into_runs run [] = [run]
416 split_into_runs [] (x:xs) = split_into_runs [x] xs
417 split_into_runs [r] (x:xs) | x `ge` r = split_into_runs [r,x] xs
418 split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
419 | True = rl : (split_into_runs [x] xs)
422 merge_lists (x:xs) = merge x (merge_lists xs)
426 merge xl@(x:xs) yl@(y:ys)
428 EQ -> x : y : (merge xs ys)
429 LT -> x : (merge xs yl)
430 GT -> y : (merge xl ys)
434 %************************************************************************
436 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
438 %************************************************************************
441 Date: Mon, 3 May 93 20:45:23 +0200
442 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
443 To: partain@dcs.gla.ac.uk
444 Subject: natural merge sort beats quick sort [ and it is prettier ]
446 Here is a piece of Haskell code that I'm rather fond of. See it as an
447 attempt to get rid of the ridiculous quick-sort routine. group is
448 quite useful by itself I think it was John's idea originally though I
449 believe the lazy version is due to me [surprisingly complicated].
450 gamma [used to be called] is called gamma because I got inspired by
451 the Gamma calculus. It is not very close to the calculus but does
452 behave less sequentially than both foldr and foldl. One could imagine
453 a version of gamma that took a unit element as well thereby avoiding
454 the problem with empty lists.
456 I've tried this code against
458 1) insertion sort - as provided by haskell
459 2) the normal implementation of quick sort
460 3) a deforested version of quick sort due to Jan Sparud
461 4) a super-optimized-quick-sort of Lennart's
463 If the list is partially sorted both merge sort and in particular
464 natural merge sort wins. If the list is random [ average length of
465 rising subsequences = approx 2 ] mergesort still wins and natural
466 merge sort is marginally beaten by Lennart's soqs. The space
467 consumption of merge sort is a bit worse than Lennart's quick sort
468 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
469 fpca article ] isn't used because of group.
476 group :: (a -> a -> Bool) -> [a] -> [[a]]
479 Date: Mon, 12 Feb 1996 15:09:41 +0000
480 From: Andy Gill <andy@dcs.gla.ac.uk>
482 Here is a `better' definition of group.
485 group p (x:xs) = group' xs x x (x :)
487 group' [] _ _ s = [s []]
488 group' (x:xs) x_min x_max s
489 | not (x `p` x_max) = group' xs x_min x (s . (x :))
490 | x `p` x_min = group' xs x x_max ((x :) . s)
491 | otherwise = s [] : group' xs x x (x :)
493 -- This one works forwards *and* backwards, as well as also being
494 -- faster that the one in Util.lhs.
499 let ((h1:t1):tt1) = group p xs
500 (t,tt) = if null xs then ([],[]) else
501 if x `p` h1 then (h1:t1,tt1) else
506 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
507 generalMerge p xs [] = xs
508 generalMerge p [] ys = ys
509 generalMerge p (x:xs) (y:ys) | x `p` y = x : generalMerge p xs (y:ys)
510 | otherwise = y : generalMerge p (x:xs) ys
512 -- gamma is now called balancedFold
514 balancedFold :: (a -> a -> a) -> [a] -> a
515 balancedFold f [] = error "can't reduce an empty list using balancedFold"
516 balancedFold f [x] = x
517 balancedFold f l = balancedFold f (balancedFold' f l)
519 balancedFold' :: (a -> a -> a) -> [a] -> [a]
520 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
521 balancedFold' f xs = xs
523 generalMergeSort p [] = []
524 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
526 generalNaturalMergeSort p [] = []
527 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
530 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
532 mergeSort = generalMergeSort (<=)
533 naturalMergeSort = generalNaturalMergeSort (<=)
535 mergeSortLe le = generalMergeSort le
538 naturalMergeSortLe le = generalNaturalMergeSort le
541 %************************************************************************
543 \subsection[Utils-transitive-closure]{Transitive closure}
545 %************************************************************************
547 This algorithm for transitive closure is straightforward, albeit quadratic.
550 transitiveClosure :: (a -> [a]) -- Successor function
551 -> (a -> a -> Bool) -- Equality predicate
553 -> [a] -- The transitive closure
555 transitiveClosure succ eq xs
559 go done (x:xs) | x `is_in` done = go done xs
560 | otherwise = go (x:done) (succ x ++ xs)
563 x `is_in` (y:ys) | eq x y = True
564 | otherwise = x `is_in` ys
567 %************************************************************************
569 \subsection[Utils-accum]{Accumulating}
571 %************************************************************************
573 @mapAccumL@ behaves like a combination
574 of @map@ and @foldl@;
575 it applies a function to each element of a list, passing an accumulating
576 parameter from left to right, and returning a final value of this
577 accumulator together with the new list.
580 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
581 -- and accumulator, returning new
582 -- accumulator and elt of result list
583 -> acc -- Initial accumulator
585 -> (acc, [y]) -- Final accumulator and result list
587 mapAccumL f b [] = (b, [])
588 mapAccumL f b (x:xs) = (b'', x':xs') where
590 (b'', xs') = mapAccumL f b' xs
593 @mapAccumR@ does the same, but working from right to left instead. Its type is
594 the same as @mapAccumL@, though.
597 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
598 -- and accumulator, returning new
599 -- accumulator and elt of result list
600 -> acc -- Initial accumulator
602 -> (acc, [y]) -- Final accumulator and result list
604 mapAccumR f b [] = (b, [])
605 mapAccumR f b (x:xs) = (b'', x':xs') where
607 (b', xs') = mapAccumR f b xs
610 Here is the bi-directional version, that works from both left and right.
613 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
614 -- Function of elt of input list
615 -- and accumulator, returning new
616 -- accumulator and elt of result list
617 -> accl -- Initial accumulator from left
618 -> accr -- Initial accumulator from right
620 -> (accl, accr, [y]) -- Final accumulators and result list
622 mapAccumB f a b [] = (a,b,[])
623 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
625 (a',b'',y) = f a b' x
626 (a'',b',ys) = mapAccumB f a' b xs
629 A strict version of foldl.
632 foldl' :: (a -> b -> a) -> a -> [b] -> a
633 foldl' f z xs = lgo z xs
636 lgo z (x:xs) = (lgo $! (f z x)) xs
639 A combination of foldl with zip. It works with equal length lists.
642 foldl2 :: (acc -> a -> b -> acc) -> acc -> [a] -> [b] -> acc
644 foldl2 k z (a:as) (b:bs) = foldl2 k (k z a b) as bs
647 Count the number of times a predicate is true
650 count :: (a -> Bool) -> [a] -> Int
652 count p (x:xs) | p x = 1 + count p xs
653 | otherwise = count p xs
656 @splitAt@, @take@, and @drop@ but with length of another
657 list giving the break-off point:
660 takeList :: [b] -> [a] -> [a]
665 (y:ys) -> y : takeList xs ys
667 dropList :: [b] -> [a] -> [a]
669 dropList _ xs@[] = xs
670 dropList (_:xs) (_:ys) = dropList xs ys
673 splitAtList :: [b] -> [a] -> ([a], [a])
674 splitAtList [] xs = ([], xs)
675 splitAtList _ xs@[] = (xs, xs)
676 splitAtList (_:xs) (y:ys) = (y:ys', ys'')
678 (ys', ys'') = splitAtList xs ys
683 %************************************************************************
685 \subsection[Utils-comparison]{Comparisons}
687 %************************************************************************
690 eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
691 eqListBy eq [] [] = True
692 eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
693 eqListBy eq xs ys = False
695 equalLength :: [a] -> [b] -> Bool
696 equalLength [] [] = True
697 equalLength (_:xs) (_:ys) = equalLength xs ys
698 equalLength xs ys = False
700 compareLength :: [a] -> [b] -> Ordering
701 compareLength [] [] = EQ
702 compareLength (_:xs) (_:ys) = compareLength xs ys
703 compareLength [] _ys = LT
704 compareLength _xs [] = GT
706 thenCmp :: Ordering -> Ordering -> Ordering
707 {-# INLINE thenCmp #-}
709 thenCmp other any = other
711 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
712 -- `cmpList' uses a user-specified comparer
714 cmpList cmp [] [] = EQ
715 cmpList cmp [] _ = LT
716 cmpList cmp _ [] = GT
717 cmpList cmp (a:as) (b:bs)
718 = case cmp a b of { EQ -> cmpList cmp as bs; xxx -> xxx }
722 prefixMatch :: Eq a => [a] -> [a] -> Bool
723 prefixMatch [] _str = True
724 prefixMatch _pat [] = False
725 prefixMatch (p:ps) (s:ss) | p == s = prefixMatch ps ss
728 suffixMatch :: Eq a => [a] -> [a] -> Bool
729 suffixMatch pat str = prefixMatch (reverse pat) (reverse str)
732 %************************************************************************
734 \subsection[Utils-pairs]{Pairs}
736 %************************************************************************
738 The following are curried versions of @fst@ and @snd@.
742 cfst :: a -> b -> a -- stranal-sem only (Note)
747 The following provide us higher order functions that, when applied
748 to a function, operate on pairs.
752 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
753 applyToPair (f,g) (x,y) = (f x, g y)
755 applyToFst :: (a -> c) -> (a,b)-> (c,b)
756 applyToFst f (x,y) = (f x,y)
758 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
759 applyToSnd f (x,y) = (x,f y)
762 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
763 foldPair fg ab [] = ab
764 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
765 where (u,v) = foldPair fg ab abs
769 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
770 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
774 seqList :: [a] -> b -> b
776 seqList (x:xs) b = x `seq` seqList xs b
782 global :: a -> IORef a
783 global a = unsafePerformIO (newIORef a)