6f134bfa85fdb3ef3d70a3a5710a22869f4eccec
[ghc-hetmet.git] / ghc / lib / misc / 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_ Ordering
13 # define LT_ LT
14 # define EQ_ EQ
15 # define GT_ GT
16 # define _LT LT
17 # define _EQ EQ
18 # define _GT GT
19 # define GT__ _
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
25 # define COMMA ,
26 #endif
27
28 #ifndef __GLASGOW_HASKELL__
29 # undef TAG_
30 # undef LT_
31 # undef EQ_
32 # undef GT_
33 # undef tagCmp_
34 #endif
35
36 module Util (
37         -- Haskell-version support
38 #ifndef __GLASGOW_HASKELL__
39         tagCmp_,
40         TAG_(..),
41 #endif
42         -- general list processing
43         IF_NOT_GHC(forall COMMA exists COMMA)
44         zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
45         zipLazy,
46         mapAndUnzip, mapAndUnzip3,
47         nOfThem, lengthExceeds, isSingleton,
48         startsWith, endsWith,
49 #if defined(COMPILING_GHC)
50         isIn, isn'tIn,
51 #endif
52
53         -- association lists
54         assoc,
55
56         -- duplicate handling
57         hasNoDups, equivClasses, runs, removeDups,
58
59         -- sorting
60         IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
61         sortLt,
62         IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
63         IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
64
65         -- transitive closures
66         transitiveClosure,
67
68         -- accumulating
69         mapAccumL, mapAccumR, mapAccumB,
70
71         -- comparisons
72 #if defined(COMPILING_GHC)
73         thenCmp, cmpList,
74         cmpPString,
75 #else
76         cmpString,
77 #endif
78
79         -- pairs
80         IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
81         IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
82         unzipWith
83
84         -- error handling
85 #if defined(COMPILING_GHC)
86         , panic, panic#, pprPanic, pprPanic#, pprError, pprTrace
87         , assertPanic
88 #endif {- COMPILING_GHC -}
89
90         , unvectorize        
91
92     ) where
93
94 import List(zipWith4)
95 import PackedString ( unpackCStringIO )
96 import Addr
97
98 infixr 9 `thenCmp`
99 \end{code}
100
101 %************************************************************************
102 %*                                                                      *
103 \subsection[Utils-version-support]{Functions to help pre-1.2 versions of (non-Glasgow) Haskell}
104 %*                                                                      *
105 %************************************************************************
106
107 This is our own idea:
108 \begin{code}
109 #ifndef __GLASGOW_HASKELL__
110 data TAG_ = LT_ | EQ_ | GT_
111
112 tagCmp_ :: Ord a => a -> a -> TAG_
113 tagCmp_ a b = if a == b then EQ_ else if a < b then LT_ else GT_
114 #endif
115 \end{code}
116
117 %************************************************************************
118 %*                                                                      *
119 \subsection[Utils-lists]{General list processing}
120 %*                                                                      *
121 %************************************************************************
122
123 Quantifiers are not standard in Haskell. The following fill in the gap.
124
125 \begin{code}
126 forall :: (a -> Bool) -> [a] -> Bool
127 forall pred []     = True
128 forall pred (x:xs) = pred x && forall pred xs
129
130 exists :: (a -> Bool) -> [a] -> Bool
131 exists pred []     = False
132 exists pred (x:xs) = pred x || exists pred xs
133 \end{code}
134
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]
139
140 \begin{code}
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]
145
146 #if (!defined(DEBUG)) && defined(COMPILING_GHC)
147 zipEqual      _ = zip
148 zipWithEqual  _ = zipWith
149 zipWith3Equal _ = zipWith3
150 zipWith4Equal _ = zipWith4
151 #else
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)
155
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)
159
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)
164
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)
169 #endif
170 \end{code}
171
172 \begin{code}
173 -- zipLazy is lazy in the second list (observe the ~)
174
175 zipLazy :: [a] -> [b] -> [(a,b)]
176 zipLazy [] ys = []
177 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
178 \end{code}
179
180 \begin{code}
181 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
182
183 mapAndUnzip f [] = ([],[])
184 mapAndUnzip f (x:xs)
185   = let
186         (r1,  r2)  = f x
187         (rs1, rs2) = mapAndUnzip f xs
188     in
189     (r1:rs1, r2:rs2)
190
191 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
192
193 mapAndUnzip3 f [] = ([],[],[])
194 mapAndUnzip3 f (x:xs)
195   = let
196         (r1,  r2,  r3)  = f x
197         (rs1, rs2, rs3) = mapAndUnzip3 f xs
198     in
199     (r1:rs1, r2:rs2, r3:rs3)
200 \end{code}
201
202 \begin{code}
203 nOfThem :: Int -> a -> [a]
204 nOfThem = replicate -- deprecated.
205
206 lengthExceeds :: [a] -> Int -> Bool
207
208 []      `lengthExceeds` n =  0 > n
209 (x:xs)  `lengthExceeds` n = (1 > n) || (xs `lengthExceeds` (n - 1))
210
211 isSingleton :: [a] -> Bool
212
213 isSingleton [x] = True
214 isSingleton  _  = False
215
216 startsWith, endsWith :: String -> String -> Maybe String
217
218 startsWith []     str = Just str
219 startsWith (c:cs) (s:ss)
220   = if c /= s then Nothing else startsWith cs ss
221 startsWith  _     []  = Nothing
222
223 endsWith cs ss
224   = case (startsWith (reverse cs) (reverse ss)) of
225       Nothing -> Nothing
226       Just rs -> Just (reverse rs)
227 \end{code}
228
229 Debugging/specialising versions of \tr{elem} and \tr{notElem}
230 \begin{code}
231 #if defined(COMPILING_GHC)
232 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
233
234 # ifndef DEBUG
235 isIn    msg x ys = elem__    x ys
236 isn'tIn msg x ys = notElem__ x ys
237
238 --these are here to be SPECIALIZEd (automagically)
239 elem__ _ []     = False
240 elem__ x (y:ys) = x==y || elem__ x ys
241
242 notElem__ x []     =  True
243 notElem__ x (y:ys) =  x /= y && notElem__ x ys
244
245 # else {- DEBUG -}
246 isIn msg x ys
247   = elem ILIT(0) x ys
248   where
249     elem i _ []     = False
250     elem i x (y:ys)
251       | i _GE_ ILIT(100) = panic ("Over-long elem in: " ++ msg)
252       | otherwise        = x == y || elem (i _ADD_ ILIT(1)) x ys
253
254 isn'tIn msg x ys
255   = notElem ILIT(0) x ys
256   where
257     notElem i x [] =  True
258     notElem i x (y:ys)
259       | i _GE_ ILIT(100) = panic ("Over-long notElem in: " ++ msg)
260       | otherwise        =  x /= y && notElem (i _ADD_ ILIT(1)) x ys
261
262 # endif {- DEBUG -}
263
264 #endif {- COMPILING_GHC -}
265 \end{code}
266
267 %************************************************************************
268 %*                                                                      *
269 \subsection[Utils-assoc]{Association lists}
270 %*                                                                      *
271 %************************************************************************
272
273 See also @assocMaybe@ and @mkLookupFun@ in module @Maybes@.
274
275 \begin{code}
276 assoc :: (Eq a) => String -> [(a, b)] -> a -> b
277
278 assoc crash_msg lst key
279   = if (null res)
280     then panic ("Failed in assoc: " ++ crash_msg)
281     else head res
282   where res = [ val | (key', val) <- lst, key == key']
283 \end{code}
284
285 %************************************************************************
286 %*                                                                      *
287 \subsection[Utils-dups]{Duplicate-handling}
288 %*                                                                      *
289 %************************************************************************
290
291 \begin{code}
292 hasNoDups :: (Eq a) => [a] -> Bool
293
294 hasNoDups xs = f [] xs
295   where
296     f seen_so_far []     = True
297     f seen_so_far (x:xs) = if x `is_elem` seen_so_far then
298                                 False
299                            else
300                                 f (x:seen_so_far) xs
301
302 #if defined(COMPILING_GHC)
303     is_elem = isIn "hasNoDups"
304 #else
305     is_elem = elem
306 #endif
307 \end{code}
308
309 \begin{code}
310 equivClasses :: (a -> a -> Ordering)    -- Comparison
311              -> [a]
312              -> [[a]]
313
314 equivClasses cmp stuff@[]     = []
315 equivClasses cmp stuff@[item] = [stuff]
316 equivClasses cmp items
317   = runs eq (sortLt lt items)
318   where
319     eq a b = case cmp a b of { EQ -> True; _ -> False }
320     lt a b = case cmp a b of { LT -> True; _ -> False }
321 \end{code}
322
323 The first cases in @equivClasses@ above are just to cut to the point
324 more quickly...
325
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.
329
330 \begin{code}
331 runs :: (a -> a -> Bool)        -- Equality
332      -> [a]
333      -> [[a]]
334
335 runs p []     = []
336 runs p (x:xs) = case (span (p x) xs) of
337                   (first, rest) -> (x:first) : (runs p rest)
338 \end{code}
339
340 \begin{code}
341 removeDups :: (a -> a -> Ordering)      -- Comparison function
342            -> [a]
343            -> ([a],     -- List with no duplicates
344                [[a]])   -- List of duplicate groups.  One representative from
345                         -- each group appears in the first result
346
347 removeDups cmp []  = ([], [])
348 removeDups cmp [x] = ([x],[])
349 removeDups cmp xs
350   = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
351     (xs', dups) }
352   where
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)
355 \end{code}
356
357 %************************************************************************
358 %*                                                                      *
359 \subsection[Utils-sorting]{Sorting}
360 %*                                                                      *
361 %************************************************************************
362
363 %************************************************************************
364 %*                                                                      *
365 \subsubsection[Utils-quicksorting]{Quicksorts}
366 %*                                                                      *
367 %************************************************************************
368
369 \begin{code}
370 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
371 quicksort :: (a -> a -> Bool)           -- Less-than predicate
372           -> [a]                        -- Input list
373           -> [a]                        -- Result list in increasing order
374
375 quicksort lt []      = []
376 quicksort lt [x]     = [x]
377 quicksort lt (x:xs)  = split x [] [] xs
378   where
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
382 \end{code}
383
384 Quicksort variant from Lennart's Haskell-library contribution.  This
385 is a {\em stable} sort.
386
387 \begin{code}
388 stableSortLt = sortLt   -- synonym; when we want to highlight stable-ness
389
390 sortLt :: (a -> a -> Bool)              -- Less-than predicate
391        -> [a]                           -- Input list
392        -> [a]                           -- Result list
393
394 sortLt lt l = qsort lt   l []
395
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
401
402 qsort lt []     r = r
403 qsort lt [x]    r = x:r
404 qsort lt (x:xs) r = qpart lt x xs [] [] r
405
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.
410
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)
415
416 qpart lt x (y:ys) rlt rge r =
417     if lt y x then
418         -- y < x
419         qpart lt x ys (y:rlt) rge r
420     else
421         -- y >= x
422         qpart lt x ys rlt (y:rge) r
423
424 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
425 rqsort lt []     r = r
426 rqsort lt [x]    r = x:r
427 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
428
429 rqpart lt x [] rle rgt r =
430     qsort lt rle (x : qsort lt rgt r)
431
432 rqpart lt x (y:ys) rle rgt r =
433     if lt x y then
434         -- y > x
435         rqpart lt x ys rle (y:rgt) r
436     else
437         -- y <= x
438         rqpart lt x ys (y:rle) rgt r
439 \end{code}
440
441 %************************************************************************
442 %*                                                                      *
443 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
444 %*                                                                      *
445 %************************************************************************
446
447 \begin{code}
448 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
449
450 mergesort cmp xs = merge_lists (split_into_runs [] xs)
451   where
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  }
454
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)
461
462     merge_lists []       = []
463     merge_lists (x:xs)   = merge x (merge_lists xs)
464
465     merge [] ys = ys
466     merge xs [] = xs
467     merge xl@(x:xs) yl@(y:ys)
468       = case cmp x y of
469           EQ_  -> x : y : (merge xs ys)
470           LT_  -> x : (merge xs yl)
471           GT__ -> y : (merge xl ys)
472 \end{code}
473
474 %************************************************************************
475 %*                                                                      *
476 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
477 %*                                                                      *
478 %************************************************************************
479
480 \begin{display}
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 ]
485
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.
495
496 I've tried this code against
497
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
502
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.
510
511 have fun
512 Carsten
513 \end{display}
514
515 \begin{code}
516 group :: (a -> a -> Bool) -> [a] -> [[a]]
517
518 {-
519 Date: Mon, 12 Feb 1996 15:09:41 +0000
520 From: Andy Gill <andy@dcs.gla.ac.uk>
521
522 Here is a `better' definition of group.
523 -}
524 group p []     = []
525 group p (x:xs) = group' xs x x (x :)
526   where
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 :) 
532
533 -- This one works forwards *and* backwards, as well as also being
534 -- faster that the one in Util.lhs.
535
536 {- ORIG:
537 group p [] = [[]]
538 group p (x:xs) =
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
542                    ([], (h1:t1):tt1)
543    in ((x:t):tt)
544 -}
545
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
551
552 -- gamma is now called balancedFold
553
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)
558
559 balancedFold' :: (a -> a -> a) -> [a] -> [a]
560 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
561 balancedFold' f xs = xs
562
563 generalMergeSort p [] = []
564 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
565
566 generalNaturalMergeSort p [] = []
567 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
568
569 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
570
571 mergeSort = generalMergeSort (<=)
572 naturalMergeSort = generalNaturalMergeSort (<=)
573
574 mergeSortLe le = generalMergeSort le
575 naturalMergeSortLe le = generalNaturalMergeSort le
576 \end{code}
577
578 %************************************************************************
579 %*                                                                      *
580 \subsection[Utils-transitive-closure]{Transitive closure}
581 %*                                                                      *
582 %************************************************************************
583
584 This algorithm for transitive closure is straightforward, albeit quadratic.
585
586 \begin{code}
587 transitiveClosure :: (a -> [a])         -- Successor function
588                   -> (a -> a -> Bool)   -- Equality predicate
589                   -> [a]
590                   -> [a]                -- The transitive closure
591
592 transitiveClosure succ eq xs
593  = go [] xs
594  where
595    go done []                      = done
596    go done (x:xs) | x `is_in` done = go done xs
597                   | otherwise      = go (x:done) (succ x ++ xs)
598
599    x `is_in` []                 = False
600    x `is_in` (y:ys) | eq x y    = True
601                     | otherwise = x `is_in` ys
602 \end{code}
603
604 %************************************************************************
605 %*                                                                      *
606 \subsection[Utils-accum]{Accumulating}
607 %*                                                                      *
608 %************************************************************************
609
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.
615
616 \begin{code}
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
621             -> [x]              -- Input list
622             -> (acc, [y])               -- Final accumulator and result list
623
624 mapAccumL f b []     = (b, [])
625 mapAccumL f b (x:xs) = (b'', x':xs') where
626                                           (b', x') = f b x
627                                           (b'', xs') = mapAccumL f b' xs
628 \end{code}
629
630 @mapAccumR@ does the same, but working from right to left instead.  Its type is
631 the same as @mapAccumL@, though.
632
633 \begin{code}
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
638             -> [x]              -- Input list
639             -> (acc, [y])               -- Final accumulator and result list
640
641 mapAccumR f b []     = (b, [])
642 mapAccumR f b (x:xs) = (b'', x':xs') where
643                                           (b'', x') = f b' x
644                                           (b', xs') = mapAccumR f b xs
645 \end{code}
646
647 Here is the bi-directional version, that works from both left and right.
648
649 \begin{code}
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
656           -> [x]                        -- Input list
657           -> (accl, accr, [y])  -- Final accumulators and result list
658
659 mapAccumB f a b []     = (a,b,[])
660 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
661    where
662         (a',b'',y)  = f a b' x
663         (a'',b',ys) = mapAccumB f a' b xs
664 \end{code}
665
666 %************************************************************************
667 %*                                                                      *
668 \subsection[Utils-comparison]{Comparisons}
669 %*                                                                      *
670 %************************************************************************
671
672 See also @tagCmp_@ near the versions-compatibility section.
673
674 The Ord3 class will be subsumed into Ord in Haskell 1.3.
675
676 \begin{code}
677 {-
678 class Ord3 a where
679   cmp :: a -> a -> TAG_
680 -}
681
682 thenCmp :: Ordering -> Ordering -> Ordering
683 {-# INLINE thenCmp #-}
684 thenCmp EQ   any  = any
685 thenCmp other any = other
686
687 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
688     -- `cmpList' uses a user-specified comparer
689
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 }
695 \end{code}
696
697 begin{code}
698 instance Ord3 a => Ord3 [a] where
699   cmp []     []     = EQ_
700   cmp (x:xs) []     = GT_
701   cmp []     (y:ys) = LT_
702   cmp (x:xs) (y:ys) = (x `cmp` y) `thenCmp` (xs `cmp` ys)
703
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
709
710 instance Ord3 Int where
711   cmp a b | a < b     = LT_
712           | a > b     = GT_
713           | otherwise = EQ_
714 end{code}
715
716 \begin{code}
717 cmpString :: String -> String -> TAG_
718
719 cmpString []     []     = EQ_
720 cmpString (x:xs) (y:ys) = if      x == y then cmpString xs ys
721                           else if x  < y then LT_
722                           else                GT_
723 cmpString []     ys     = LT_
724 cmpString xs     []     = GT_
725
726 #ifdef COMPILING_GHC
727 cmpString _ _ = panic# "cmpString"
728 #else
729 cmpString _ _ = error "cmpString"
730 #endif
731 \end{code}
732
733 \begin{code}
734 cmpPString :: FAST_STRING -> FAST_STRING -> TAG_
735
736 cmpPString x y = compare x y
737 \end{code}
738
739 %************************************************************************
740 %*                                                                      *
741 \subsection[Utils-pairs]{Pairs}
742 %*                                                                      *
743 %************************************************************************
744
745 The following are curried versions of @fst@ and @snd@.
746
747 \begin{code}
748 cfst :: a -> b -> a     -- stranal-sem only (Note)
749 cfst x y = x
750 \end{code}
751
752 The following provide us higher order functions that, when applied
753 to a function, operate on pairs.
754
755 \begin{code}
756 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
757 applyToPair (f,g) (x,y) = (f x, g y)
758
759 applyToFst :: (a -> c) -> (a,b)-> (c,b)
760 applyToFst f (x,y) = (f x,y)
761
762 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
763 applyToSnd f (x,y) = (x,f y)
764
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
769 \end{code}
770
771 \begin{code}
772 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
773 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
774 \end{code}
775
776 %************************************************************************
777 %*                                                                      *
778 \subsection[Utils-errors]{Error handling}
779 %*                                                                      *
780 %************************************************************************
781
782 \begin{code}
783 #if defined(COMPILING_GHC)
784 panic x = error ("panic! (the `impossible' happened):\n\t"
785               ++ x ++ "\n\n"
786               ++ "Please report it as a compiler bug "
787               ++ "to glasgow-haskell-bugs@dcs.gla.ac.uk.\n\n" )
788
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))
795 #else
796 pprTrace heading pretty_msg = trace (heading++(ppShow 80 pretty_msg))
797 #endif
798
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)
802
803 panic# :: String -> TAG_
804 panic# s = case (panic s) of () -> EQ_
805
806 pprPanic# heading pretty_msg = panic# (heading++(ppShow 80 pretty_msg))
807
808 assertPanic :: String -> Int -> a
809 assertPanic file line = panic ("ASSERT failed! file "++file++", line "++show line)
810
811 #endif {- COMPILING_GHC -}
812 \end{code}
813
814 Turn a NULL-terminated vector of null-terminated strings into a string list
815 (ToDo: create a module of common marshaling functions)
816
817 \begin{code}
818 unvectorize :: Addr -> Int -> IO [String]
819 unvectorize ptr n
820   | str == ``NULL'' = return []
821   | otherwise = do
822         x  <- unpackCStringIO str
823         xs <- unvectorize ptr (n+1)
824         return (x : xs)
825   where
826    str = indexAddrOffAddr ptr n
827
828 \end{code}