[project @ 1996-04-10 18:10:47 by partain]
[ghc-hetmet.git] / ghc / compiler / utils / Util.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[Util]{Highly random utility functions}
5
6 \begin{code}
7 #if defined(COMPILING_GHC)
8 # include "HsVersions.h"
9 # define IF_NOT_GHC(a) {--}
10 #else
11 # define panic error
12 # define TAG_ _CMP_TAG
13 # define LT_ _LT
14 # define EQ_ _EQ
15 # define GT_ _GT
16 # define GT__ _
17 # define tagCmp_ _tagCmp
18 # define FAST_STRING String
19 # define ASSERT(x) {-nothing-}
20 # define IF_NOT_GHC(a) a
21 # define COMMA ,
22 #endif
23
24 #ifndef __GLASGOW_HASKELL__
25 # undef TAG_
26 # undef LT_
27 # undef EQ_
28 # undef GT_
29 # undef tagCmp_
30 #endif
31
32 module Util (
33         -- Haskell-version support
34 #ifndef __GLASGOW_HASKELL__
35         tagCmp_,
36         TAG_(..),
37 #endif
38         -- general list processing
39         IF_NOT_GHC(forall COMMA exists COMMA)
40         zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
41         zipLazy,
42         nOfThem, lengthExceeds, isSingleton,
43         startsWith, endsWith,
44 #if defined(COMPILING_GHC)
45         isIn, isn'tIn,
46 #endif
47
48         -- association lists
49         assoc,
50
51         -- duplicate handling
52         hasNoDups, equivClasses, runs, removeDups,
53
54         -- sorting
55         IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
56         sortLt,
57         IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
58         IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
59
60         -- transitive closures
61         transitiveClosure,
62
63         -- accumulating
64         mapAccumL, mapAccumR, mapAccumB,
65
66         -- comparisons
67         Ord3(..), thenCmp, cmpList,
68         IF_NOT_GHC(cmpString COMMA)
69 #ifdef USE_FAST_STRINGS
70         cmpPString,
71 #else
72         substr,
73 #endif
74         -- pairs
75         IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
76         IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
77         unzipWith
78
79         -- error handling
80 #if defined(COMPILING_GHC)
81         , panic, panic#, pprPanic, pprPanic#, pprError, pprTrace
82 # ifdef DEBUG
83         , assertPanic
84 # endif
85 #endif {- COMPILING_GHC -}
86
87         -- and to make the interface self-sufficient...
88 #if __HASKELL1__ < 3
89 # if defined(COMPILING_GHC)
90         , Maybe(..){-.. for pragmas...-}, PrettyRep, Pretty(..)
91 # else
92         , Maybe
93 # endif
94 #endif
95
96     ) where
97
98 #if defined(COMPILING_GHC)
99
100 CHK_Ubiq() -- debugging consistency check
101
102 import Pretty
103 #endif
104 #if __HASKELL1__ < 3
105 import Maybes           ( Maybe(..) )
106 #endif
107 \end{code}
108
109 %************************************************************************
110 %*                                                                      *
111 \subsection[Utils-version-support]{Functions to help pre-1.2 versions of (non-Glasgow) Haskell}
112 %*                                                                      *
113 %************************************************************************
114
115 This is our own idea:
116 \begin{code}
117 #ifndef __GLASGOW_HASKELL__
118 data TAG_ = LT_ | EQ_ | GT_
119
120 tagCmp_ :: Ord a => a -> a -> TAG_
121 tagCmp_ a b = if a == b then EQ_ else if a < b then LT_ else GT_
122 #endif
123 \end{code}
124
125 %************************************************************************
126 %*                                                                      *
127 \subsection[Utils-lists]{General list processing}
128 %*                                                                      *
129 %************************************************************************
130
131 Quantifiers are not standard in Haskell. The following fill in the gap.
132
133 \begin{code}
134 forall :: (a -> Bool) -> [a] -> Bool
135 forall pred []     = True
136 forall pred (x:xs) = pred x && forall pred xs
137
138 exists :: (a -> Bool) -> [a] -> Bool
139 exists pred []     = False
140 exists pred (x:xs) = pred x || exists pred xs
141 \end{code}
142
143 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
144 are of equal length.  Alastair Reid thinks this should only happen if
145 DEBUGging on; hey, why not?
146
147 \begin{code}
148 zipEqual        :: [a] -> [b] -> [(a,b)]
149 zipWithEqual    :: (a->b->c) -> [a]->[b]->[c]
150 zipWith3Equal   :: (a->b->c->d) -> [a]->[b]->[c]->[d]
151 zipWith4Equal   :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
152
153 #ifndef DEBUG
154 zipEqual      = zip
155 zipWithEqual  = zipWith
156 zipWith3Equal = zipWith3
157 zipWith4Equal = zipWith4
158 #else
159 zipEqual []     []     = []
160 zipEqual (a:as) (b:bs) = (a,b) : zipEqual as bs
161 zipEqual as     bs     = panic "zipEqual: unequal lists"
162
163 zipWithEqual z (a:as) (b:bs)    =  z a b : zipWithEqual z as bs
164 zipWithEqual _ [] []            =  []
165 zipWithEqual _ _ _              =  panic "zipWithEqual: unequal lists"
166
167 zipWith3Equal z (a:as) (b:bs) (c:cs)
168                                 =  z a b c : zipWith3Equal z as bs cs
169 zipWith3Equal _ [] []  []       =  []
170 zipWith3Equal _ _  _   _        =  panic "zipWith3Equal: unequal lists"
171
172 zipWith4Equal z (a:as) (b:bs) (c:cs) (d:ds)
173                                 =  z a b c d : zipWith4Equal z as bs cs ds
174 zipWith4Equal _ [] [] [] []     =  []
175 zipWith4Equal _ _  _  _  _      =  panic "zipWith4Equal: unequal lists"
176 #endif
177 \end{code}
178
179 \begin{code}
180 -- zipLazy is lazy in the second list (observe the ~)
181
182 zipLazy :: [a] -> [b] -> [(a,b)]
183 zipLazy [] ys = []
184 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
185 \end{code}
186
187 \begin{code}
188 nOfThem :: Int -> a -> [a]
189 nOfThem n thing = take n (repeat thing)
190
191 lengthExceeds :: [a] -> Int -> Bool
192
193 []      `lengthExceeds` n =  0 > n
194 (x:xs)  `lengthExceeds` n = (1 > n) || (xs `lengthExceeds` (n - 1))
195
196 isSingleton :: [a] -> Bool
197
198 isSingleton [x] = True
199 isSingleton  _  = False
200
201 startsWith, endsWith :: String -> String -> Maybe String
202
203 startsWith []     str = Just str
204 startsWith (c:cs) (s:ss)
205   = if c /= s then Nothing else startsWith cs ss
206
207 endsWith cs ss
208   = case (startsWith (reverse cs) (reverse ss)) of
209       Nothing -> Nothing
210       Just rs -> Just (reverse rs)
211 \end{code}
212
213 Debugging/specialising versions of \tr{elem} and \tr{notElem}
214 \begin{code}
215 #if defined(COMPILING_GHC)
216 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
217
218 # ifndef DEBUG
219 isIn    msg x ys = elem__    x ys
220 isn'tIn msg x ys = notElem__ x ys
221
222 --these are here to be SPECIALIZEd (automagically)
223 elem__ _ []     = False
224 elem__ x (y:ys) = x==y || elem__ x ys
225
226 notElem__ x []     =  True
227 notElem__ x (y:ys) =  x /= y && notElem__ x ys
228
229 # else {- DEBUG -}
230 isIn msg x ys
231   = elem ILIT(0) x ys
232   where
233     elem i _ []     = False
234     elem i x (y:ys)
235       | i _GE_ ILIT(100) = panic ("Over-long elem in: " ++ msg)
236       | otherwise        = x == y || elem (i _ADD_ ILIT(1)) x ys
237
238 isn'tIn msg x ys
239   = notElem ILIT(0) x ys
240   where
241     notElem i x [] =  True
242     notElem i x (y:ys)
243       | i _GE_ ILIT(100) = panic ("Over-long notElem in: " ++ msg)
244       | otherwise        =  x /= y && notElem (i _ADD_ ILIT(1)) x ys
245
246 # endif {- DEBUG -}
247
248 # ifdef USE_ATTACK_PRAGMAS
249 {-# SPECIALIZE isIn :: String -> Literal -> [Literal] -> Bool #-}
250 {-# SPECIALIZE isIn :: String -> Class -> [Class] -> Bool #-}
251 {-# SPECIALIZE isIn :: String -> Id -> [Id] -> Bool #-}
252 {-# SPECIALIZE isIn :: String -> Int -> [Int] -> Bool #-}
253 {-# SPECIALIZE isIn :: String -> MagicId -> [MagicId] -> Bool #-}
254 {-# SPECIALIZE isIn :: String -> Name -> [Name] -> Bool #-}
255 {-# SPECIALIZE isIn :: String -> TyCon -> [TyCon] -> Bool #-}
256 {-# SPECIALIZE isIn :: String -> TyVar -> [TyVar] -> Bool #-}
257 {-# SPECIALIZE isIn :: String -> TyVarTemplate -> [TyVarTemplate] -> Bool #-}
258 {-# SPECIALIZE isIn :: String -> Unique -> [Unique] -> Bool #-}
259 {-# SPECIALIZE isIn :: String -> _PackedString -> [_PackedString] -> Bool #-}
260 {-# SPECIALIZE isn'tIn :: String -> (Id, Id) -> [(Id, Id)] -> Bool #-}
261 {-# SPECIALIZE isn'tIn :: String -> Int -> [Int] -> Bool #-}
262 {-# SPECIALIZE isn'tIn :: String -> Id -> [Id] -> Bool #-}
263 {-# SPECIALIZE isn'tIn :: String -> MagicId -> [MagicId] -> Bool #-}
264 {-# SPECIALIZE isn'tIn :: String -> TyCon -> [TyCon] -> Bool #-}
265 {-# SPECIALIZE isn'tIn :: String -> TyVar -> [TyVar] -> Bool #-}
266 {-# SPECIALIZE isn'tIn :: String -> TyVarTemplate -> [TyVarTemplate] -> Bool #-}
267 # endif
268
269 #endif {- COMPILING_GHC -}
270 \end{code}
271
272 %************************************************************************
273 %*                                                                      *
274 \subsection[Utils-assoc]{Association lists}
275 %*                                                                      *
276 %************************************************************************
277
278 See also @assocMaybe@ and @mkLookupFun@ in module @Maybes@.
279
280 \begin{code}
281 assoc :: (Eq a) => String -> [(a, b)] -> a -> b
282
283 assoc crash_msg lst key
284   = if (null res)
285     then panic ("Failed in assoc: " ++ crash_msg)
286     else head res
287   where res = [ val | (key', val) <- lst, key == key']
288
289 #if defined(COMPILING_GHC)
290 # ifdef USE_ATTACK_PRAGMAS
291 {-# SPECIALIZE assoc :: String -> [(Id,            a)] -> Id            -> a #-}
292 {-# SPECIALIZE assoc :: String -> [(Class,         a)] -> Class         -> a #-}
293 {-# SPECIALIZE assoc :: String -> [(Name,          a)] -> Name          -> a #-}
294 {-# SPECIALIZE assoc :: String -> [(PrimRep,      a)] -> PrimRep        -> a #-}
295 {-# SPECIALIZE assoc :: String -> [(String,        a)] -> String        -> a #-}
296 {-# SPECIALIZE assoc :: String -> [(TyCon,         a)] -> TyCon         -> a #-}
297 {-# SPECIALIZE assoc :: String -> [(TyVar,         a)] -> TyVar         -> a #-}
298 {-# SPECIALIZE assoc :: String -> [(TyVarTemplate, a)] -> TyVarTemplate -> a #-}
299 {-# SPECIALIZE assoc :: String -> [(Type,          a)] -> Type          -> a #-}
300 {-# SPECIALIZE assoc :: String -> [(_PackedString, a)] -> _PackedString -> a #-}
301 # endif
302 #endif
303 \end{code}
304
305 %************************************************************************
306 %*                                                                      *
307 \subsection[Utils-dups]{Duplicate-handling}
308 %*                                                                      *
309 %************************************************************************
310
311 \begin{code}
312 hasNoDups :: (Eq a) => [a] -> Bool
313
314 hasNoDups xs = f [] xs
315   where
316     f seen_so_far []     = True
317     f seen_so_far (x:xs) = if x `is_elem` seen_so_far then
318                                 False
319                            else
320                                 f (x:seen_so_far) xs
321
322 #if defined(COMPILING_GHC)
323     is_elem = isIn "hasNoDups"
324 #else
325     is_elem = elem
326 #endif
327 #if defined(COMPILING_GHC)
328 # ifdef USE_ATTACK_PRAGMAS
329 {-# SPECIALIZE hasNoDups :: [TyVar] -> Bool #-}
330 # endif
331 #endif
332 \end{code}
333
334 \begin{code}
335 equivClasses :: (a -> a -> TAG_)        -- Comparison
336              -> [a]
337              -> [[a]]
338
339 equivClasses cmp stuff@[]     = []
340 equivClasses cmp stuff@[item] = [stuff]
341 equivClasses cmp items
342   = runs eq (sortLt lt items)
343   where
344     eq a b = case cmp a b of { EQ_ -> True; _ -> False }
345     lt a b = case cmp a b of { LT_ -> True; _ -> False }
346 \end{code}
347
348 The first cases in @equivClasses@ above are just to cut to the point
349 more quickly...
350
351 @runs@ groups a list into a list of lists, each sublist being a run of
352 identical elements of the input list. It is passed a predicate @p@ which
353 tells when two elements are equal.
354
355 \begin{code}
356 runs :: (a -> a -> Bool)        -- Equality
357      -> [a]
358      -> [[a]]
359
360 runs p []     = []
361 runs p (x:xs) = case (span (p x) xs) of
362                   (first, rest) -> (x:first) : (runs p rest)
363 \end{code}
364
365 \begin{code}
366 removeDups :: (a -> a -> TAG_)  -- Comparison function
367            -> [a]
368            -> ([a],     -- List with no duplicates
369                [[a]])   -- List of duplicate groups.  One representative from
370                         -- each group appears in the first result
371
372 removeDups cmp []  = ([], [])
373 removeDups cmp [x] = ([x],[])
374 removeDups cmp xs
375   = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
376     (xs', dups) }
377   where
378     collect_dups dups_so_far [x]         = (dups_so_far,      x)
379     collect_dups dups_so_far dups@(x:xs) = (dups:dups_so_far, x)
380 \end{code}
381
382 %************************************************************************
383 %*                                                                      *
384 \subsection[Utils-sorting]{Sorting}
385 %*                                                                      *
386 %************************************************************************
387
388 %************************************************************************
389 %*                                                                      *
390 \subsubsection[Utils-quicksorting]{Quicksorts}
391 %*                                                                      *
392 %************************************************************************
393
394 \begin{code}
395 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
396 quicksort :: (a -> a -> Bool)           -- Less-than predicate
397           -> [a]                        -- Input list
398           -> [a]                        -- Result list in increasing order
399
400 quicksort lt []      = []
401 quicksort lt [x]     = [x]
402 quicksort lt (x:xs)  = split x [] [] xs
403   where
404     split x lo hi []                 = quicksort lt lo ++ (x : quicksort lt hi)
405     split x lo hi (y:ys) | y `lt` x  = split x (y:lo) hi ys
406                          | True      = split x lo (y:hi) ys
407 \end{code}
408
409 Quicksort variant from Lennart's Haskell-library contribution.  This
410 is a {\em stable} sort.
411
412 \begin{code}
413 stableSortLt = sortLt   -- synonym; when we want to highlight stable-ness
414
415 sortLt :: (a -> a -> Bool)              -- Less-than predicate
416        -> [a]                           -- Input list
417        -> [a]                           -- Result list
418
419 sortLt lt l = qsort lt   l []
420
421 -- qsort is stable and does not concatenate.
422 qsort :: (a -> a -> Bool)       -- Less-than predicate
423       -> [a]                    -- xs, Input list
424       -> [a]                    -- r,  Concatenate this list to the sorted input list
425       -> [a]                    -- Result = sort xs ++ r
426
427 qsort lt []     r = r
428 qsort lt [x]    r = x:r
429 qsort lt (x:xs) r = qpart lt x xs [] [] r
430
431 -- qpart partitions and sorts the sublists
432 -- rlt contains things less than x,
433 -- rge contains the ones greater than or equal to x.
434 -- Both have equal elements reversed with respect to the original list.
435
436 qpart lt x [] rlt rge r =
437     -- rlt and rge are in reverse order and must be sorted with an
438     -- anti-stable sorting
439     rqsort lt rlt (x : rqsort lt rge r)
440
441 qpart lt x (y:ys) rlt rge r =
442     if lt y x then
443         -- y < x
444         qpart lt x ys (y:rlt) rge r
445     else
446         -- y >= x
447         qpart lt x ys rlt (y:rge) r
448
449 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
450 rqsort lt []     r = r
451 rqsort lt [x]    r = x:r
452 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
453
454 rqpart lt x [] rle rgt r =
455     qsort lt rle (x : qsort lt rgt r)
456
457 rqpart lt x (y:ys) rle rgt r =
458     if lt x y then
459         -- y > x
460         rqpart lt x ys rle (y:rgt) r
461     else
462         -- y <= x
463         rqpart lt x ys (y:rle) rgt r
464 \end{code}
465
466 %************************************************************************
467 %*                                                                      *
468 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
469 %*                                                                      *
470 %************************************************************************
471
472 \begin{code}
473 mergesort :: (a -> a -> TAG_) -> [a] -> [a]
474
475 mergesort cmp xs = merge_lists (split_into_runs [] xs)
476   where
477     a `le` b = case cmp a b of { LT_ -> True;  EQ_ -> True; GT__ -> False }
478     a `ge` b = case cmp a b of { LT_ -> False; EQ_ -> True; GT__ -> True  }
479
480     split_into_runs []        []                = []
481     split_into_runs run       []                = [run]
482     split_into_runs []        (x:xs)            = split_into_runs [x] xs
483     split_into_runs [r]       (x:xs) | x `ge` r = split_into_runs [r,x] xs
484     split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
485                                      | True     = rl : (split_into_runs [x] xs)
486
487     merge_lists []       = []
488     merge_lists (x:xs)   = merge x (merge_lists xs)
489
490     merge [] ys = ys
491     merge xs [] = xs
492     merge xl@(x:xs) yl@(y:ys)
493       = case cmp x y of
494           EQ_  -> x : y : (merge xs ys)
495           LT_  -> x : (merge xs yl)
496           GT__ -> y : (merge xl ys)
497 \end{code}
498
499 %************************************************************************
500 %*                                                                      *
501 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
502 %*                                                                      *
503 %************************************************************************
504
505 \begin{display}
506 Date: Mon, 3 May 93 20:45:23 +0200
507 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
508 To: partain@dcs.gla.ac.uk
509 Subject: natural merge sort beats quick sort [ and it is prettier ]
510
511 Here is a piece of Haskell code that I'm rather fond of. See it as an
512 attempt to get rid of the ridiculous quick-sort routine. group is
513 quite useful by itself I think it was John's idea originally though I
514 believe the lazy version is due to me [surprisingly complicated].
515 gamma [used to be called] is called gamma because I got inspired by
516 the Gamma calculus. It is not very close to the calculus but does
517 behave less sequentially than both foldr and foldl. One could imagine
518 a version of gamma that took a unit element as well thereby avoiding
519 the problem with empty lists.
520
521 I've tried this code against
522
523    1) insertion sort - as provided by haskell
524    2) the normal implementation of quick sort
525    3) a deforested version of quick sort due to Jan Sparud
526    4) a super-optimized-quick-sort of Lennart's
527
528 If the list is partially sorted both merge sort and in particular
529 natural merge sort wins. If the list is random [ average length of
530 rising subsequences = approx 2 ] mergesort still wins and natural
531 merge sort is marginally beaten by Lennart's soqs. The space
532 consumption of merge sort is a bit worse than Lennart's quick sort
533 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
534 fpca article ] isn't used because of group.
535
536 have fun
537 Carsten
538 \end{display}
539
540 \begin{code}
541 group :: (a -> a -> Bool) -> [a] -> [[a]]
542
543 {-
544 Date: Mon, 12 Feb 1996 15:09:41 +0000
545 From: Andy Gill <andy@dcs.gla.ac.uk>
546
547 Here is a `better' definition of group.
548 -}
549 group p []     = []
550 group p (x:xs) = group' xs x x (x :)
551   where
552     group' []     _     _     s  = [s []]
553     group' (x:xs) x_min x_max s 
554         | not (x `p` x_max) = group' xs x_min x (s . (x :)) 
555         | x `p` x_min       = group' xs x x_max ((x :) . s) 
556         | otherwise         = s [] : group' xs x x (x :) 
557
558 -- This one works forwards *and* backwards, as well as also being
559 -- faster that the one in Util.lhs.
560
561 {- ORIG:
562 group p [] = [[]]
563 group p (x:xs) =
564    let ((h1:t1):tt1) = group p xs
565        (t,tt) = if null xs then ([],[]) else
566                 if x `p` h1 then (h1:t1,tt1) else
567                    ([], (h1:t1):tt1)
568    in ((x:t):tt)
569 -}
570
571 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
572 generalMerge p xs [] = xs
573 generalMerge p [] ys = ys
574 generalMerge p (x:xs) (y:ys) | x `p` y   = x : generalMerge p xs (y:ys)
575                              | otherwise = y : generalMerge p (x:xs) ys
576
577 -- gamma is now called balancedFold
578
579 balancedFold :: (a -> a -> a) -> [a] -> a
580 balancedFold f [] = error "can't reduce an empty list using balancedFold"
581 balancedFold f [x] = x
582 balancedFold f l  = balancedFold f (balancedFold' f l)
583
584 balancedFold' :: (a -> a -> a) -> [a] -> [a]
585 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
586 balancedFold' f xs = xs
587
588 generalMergeSort p [] = []
589 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
590
591 generalNaturalMergeSort p [] = []
592 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
593
594 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
595
596 mergeSort = generalMergeSort (<=)
597 naturalMergeSort = generalNaturalMergeSort (<=)
598
599 mergeSortLe le = generalMergeSort le
600 naturalMergeSortLe le = generalNaturalMergeSort le
601 \end{code}
602
603 %************************************************************************
604 %*                                                                      *
605 \subsection[Utils-transitive-closure]{Transitive closure}
606 %*                                                                      *
607 %************************************************************************
608
609 This algorithm for transitive closure is straightforward, albeit quadratic.
610
611 \begin{code}
612 transitiveClosure :: (a -> [a])         -- Successor function
613                   -> (a -> a -> Bool)   -- Equality predicate
614                   -> [a]
615                   -> [a]                -- The transitive closure
616
617 transitiveClosure succ eq xs
618  = do [] xs
619  where
620    do done []                      = done
621    do done (x:xs) | x `is_in` done = do done xs
622                   | otherwise      = do (x:done) (succ x ++ xs)
623
624    x `is_in` []                 = False
625    x `is_in` (y:ys) | eq x y    = True
626                     | otherwise = x `is_in` ys
627 \end{code}
628
629 %************************************************************************
630 %*                                                                      *
631 \subsection[Utils-accum]{Accumulating}
632 %*                                                                      *
633 %************************************************************************
634
635 @mapAccumL@ behaves like a combination
636 of  @map@ and @foldl@;
637 it applies a function to each element of a list, passing an accumulating
638 parameter from left to right, and returning a final value of this
639 accumulator together with the new list.
640
641 \begin{code}
642 mapAccumL :: (acc -> x -> (acc, y))     -- Function of elt of input list
643                                         -- and accumulator, returning new
644                                         -- accumulator and elt of result list
645             -> acc              -- Initial accumulator
646             -> [x]              -- Input list
647             -> (acc, [y])               -- Final accumulator and result list
648
649 mapAccumL f b []     = (b, [])
650 mapAccumL f b (x:xs) = (b'', x':xs') where
651                                           (b', x') = f b x
652                                           (b'', xs') = mapAccumL f b' xs
653 \end{code}
654
655 @mapAccumR@ does the same, but working from right to left instead.  Its type is
656 the same as @mapAccumL@, though.
657
658 \begin{code}
659 mapAccumR :: (acc -> x -> (acc, y))     -- Function of elt of input list
660                                         -- and accumulator, returning new
661                                         -- accumulator and elt of result list
662             -> acc              -- Initial accumulator
663             -> [x]              -- Input list
664             -> (acc, [y])               -- Final accumulator and result list
665
666 mapAccumR f b []     = (b, [])
667 mapAccumR f b (x:xs) = (b'', x':xs') where
668                                           (b'', x') = f b' x
669                                           (b', xs') = mapAccumR f b xs
670 \end{code}
671
672 Here is the bi-directional version, that works from both left and right.
673
674 \begin{code}
675 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
676                                 -- Function of elt of input list
677                                 -- and accumulator, returning new
678                                 -- accumulator and elt of result list
679           -> accl                       -- Initial accumulator from left
680           -> accr                       -- Initial accumulator from right
681           -> [x]                        -- Input list
682           -> (accl, accr, [y])  -- Final accumulators and result list
683
684 mapAccumB f a b []     = (a,b,[])
685 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
686    where
687         (a',b'',y)  = f a b' x
688         (a'',b',ys) = mapAccumB f a' b xs
689 \end{code}
690
691 %************************************************************************
692 %*                                                                      *
693 \subsection[Utils-comparison]{Comparisons}
694 %*                                                                      *
695 %************************************************************************
696
697 See also @tagCmp_@ near the versions-compatibility section.
698
699 The Ord3 class will be subsumed into Ord in Haskell 1.3.
700
701 \begin{code}
702 class Ord3 a where
703   cmp :: a -> a -> TAG_
704
705 thenCmp :: TAG_ -> TAG_ -> TAG_
706 {-# INLINE thenCmp #-}
707 thenCmp EQ_   any = any
708 thenCmp other any = other
709
710 cmpList :: (a -> a -> TAG_) -> [a] -> [a] -> TAG_
711     -- `cmpList' uses a user-specified comparer
712
713 cmpList cmp []     [] = EQ_
714 cmpList cmp []     _  = LT_
715 cmpList cmp _      [] = GT_
716 cmpList cmp (a:as) (b:bs)
717   = case cmp a b of { EQ_ -> cmpList cmp as bs; xxx -> xxx }
718 \end{code}
719
720 \begin{code}
721 instance Ord3 a => Ord3 [a] where
722   cmp []     []     = EQ_
723   cmp (x:xs) []     = GT_
724   cmp []     (y:ys) = LT_
725   cmp (x:xs) (y:ys) = (x `cmp` y) `thenCmp` (xs `cmp` ys)
726
727 instance Ord3 a => Ord3 (Maybe a) where
728   cmp Nothing  Nothing  = EQ_
729   cmp Nothing  (Just y) = LT_
730   cmp (Just x) Nothing  = GT_
731   cmp (Just x) (Just y) = x `cmp` y
732
733 instance Ord3 Int where
734   cmp a b | a < b     = LT_
735           | a > b     = GT_
736           | otherwise = EQ_
737 \end{code}
738
739 \begin{code}
740 cmpString :: String -> String -> TAG_
741
742 cmpString []     []     = EQ_
743 cmpString (x:xs) (y:ys) = if      x == y then cmpString xs ys
744                           else if x  < y then LT_
745                           else                GT_
746 cmpString []     ys     = LT_
747 cmpString xs     []     = GT_
748
749 cmpString _ _ = panic# "cmpString"
750 \end{code}
751
752 \begin{code}
753 #ifdef USE_FAST_STRINGS
754 cmpPString :: FAST_STRING -> FAST_STRING -> TAG_
755
756 cmpPString x y
757   = case (_tagCmp x y) of { _LT -> LT_ ; _EQ -> EQ_ ; _GT -> GT_ }
758 #endif
759 \end{code}
760
761 \begin{code}
762 #ifndef USE_FAST_STRINGS
763 substr :: FAST_STRING -> Int -> Int -> FAST_STRING
764
765 substr str beg end
766   = ASSERT (beg >= 0 && beg <= end)
767     take (end - beg + 1) (drop beg str)
768 #endif
769 \end{code}
770
771 %************************************************************************
772 %*                                                                      *
773 \subsection[Utils-pairs]{Pairs}
774 %*                                                                      *
775 %************************************************************************
776
777 The following are curried versions of @fst@ and @snd@.
778
779 \begin{code}
780 cfst :: a -> b -> a     -- stranal-sem only (Note)
781 cfst x y = x
782 \end{code}
783
784 The following provide us higher order functions that, when applied
785 to a function, operate on pairs.
786
787 \begin{code}
788 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
789 applyToPair (f,g) (x,y) = (f x, g y)
790
791 applyToFst :: (a -> c) -> (a,b)-> (c,b)
792 applyToFst f (x,y) = (f x,y)
793
794 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
795 applyToSnd f (x,y) = (x,f y)
796
797 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
798 foldPair fg ab [] = ab
799 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
800                        where (u,v) = foldPair fg ab abs
801 \end{code}
802
803 \begin{code}
804 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
805 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
806 \end{code}
807
808 %************************************************************************
809 %*                                                                      *
810 \subsection[Utils-errors]{Error handling}
811 %*                                                                      *
812 %************************************************************************
813
814 \begin{code}
815 #if defined(COMPILING_GHC)
816 panic x = error ("panic! (the `impossible' happened):\n\t"
817               ++ x ++ "\n\n"
818               ++ "Please report it as a compiler bug "
819               ++ "to glasgow-haskell-bugs@dcs.glasgow.ac.uk.\n\n" )
820
821 pprPanic heading pretty_msg = panic (heading++(ppShow 80 pretty_msg))
822 pprError heading pretty_msg = error (heading++(ppShow 80 pretty_msg))
823 pprTrace heading pretty_msg = trace (heading++(ppShow 80 pretty_msg))
824
825 -- #-versions because panic can't return an unboxed int, and that's
826 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
827 -- No, man -- Too Beautiful! (Will)
828
829 panic# :: String -> TAG_
830 panic# s = case (panic s) of () -> EQ_
831
832 pprPanic# heading pretty_msg = panic# (heading++(ppShow 80 pretty_msg))
833
834 # ifdef DEBUG
835 assertPanic :: String -> Int -> a
836 assertPanic file line = panic ("ASSERT failed! file "++file++", line "++show line)
837 # endif
838 #endif {- COMPILING_GHC -}
839 \end{code}