[project @ 2001-03-01 15:58:28 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / Util.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Util]{Highly random utility functions}
5
6 \begin{code}
7 -- IF_NOT_GHC is meant to make this module useful outside the context of GHC
8 #define IF_NOT_GHC(a)
9
10 module Util (
11 #if NOT_USED
12         -- The Eager monad
13         Eager, thenEager, returnEager, mapEager, appEager, runEager,
14 #endif
15
16         -- general list processing
17         zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal,
18         zipLazy, stretchZipWith,
19         mapAndUnzip, mapAndUnzip3,
20         nOfThem, lengthExceeds, isSingleton, only,
21         snocView,
22         isIn, isn'tIn,
23
24         -- for-loop
25         nTimes,
26
27         -- maybe-ish
28         unJust,
29
30         -- sorting
31         IF_NOT_GHC(quicksort COMMA stableSortLt COMMA mergesort COMMA)
32         sortLt,
33         IF_NOT_GHC(mergeSort COMMA) naturalMergeSortLe, -- from Carsten
34         IF_NOT_GHC(naturalMergeSort COMMA mergeSortLe COMMA)
35
36         -- transitive closures
37         transitiveClosure,
38
39         -- accumulating
40         mapAccumL, mapAccumR, mapAccumB, 
41         foldl2, count,
42
43         -- comparisons
44         thenCmp, cmpList, prefixMatch, suffixMatch,
45
46         -- strictness
47         foldl', seqList,
48
49         -- pairs
50         IF_NOT_GHC(cfst COMMA applyToPair COMMA applyToFst COMMA)
51         IF_NOT_GHC(applyToSnd COMMA foldPair COMMA)
52         unzipWith
53
54         , global
55         , myGetProcessID
56
57 #if __GLASGOW_HASKELL__ <= 408
58         , catchJust
59         , ioErrors
60         , throwTo
61 #endif
62
63     ) where
64
65 #include "../includes/config.h"
66 #include "HsVersions.h"
67
68 import List             ( zipWith4 )
69 import Maybe            ( Maybe(..) )
70 import Panic            ( panic )
71 import IOExts           ( IORef, newIORef, unsafePerformIO )
72 import FastTypes
73 #if __GLASGOW_HASKELL__ <= 408
74 import Exception        ( catchIO, justIoErrors, raiseInThread )
75 #endif
76 #ifndef mingw32_TARGET_OS
77 import Posix
78 #endif
79 infixr 9 `thenCmp`
80 \end{code}
81
82 %************************************************************************
83 %*                                                                      *
84 \subsection{The Eager monad}
85 %*                                                                      *
86 %************************************************************************
87
88 The @Eager@ monad is just an encoding of continuation-passing style,
89 used to allow you to express "do this and then that", mainly to avoid
90 space leaks. It's done with a type synonym to save bureaucracy.
91
92 \begin{code}
93 #if NOT_USED
94
95 type Eager ans a = (a -> ans) -> ans
96
97 runEager :: Eager a a -> a
98 runEager m = m (\x -> x)
99
100 appEager :: Eager ans a -> (a -> ans) -> ans
101 appEager m cont = m cont
102
103 thenEager :: Eager ans a -> (a -> Eager ans b) -> Eager ans b
104 thenEager m k cont = m (\r -> k r cont)
105
106 returnEager :: a -> Eager ans a
107 returnEager v cont = cont v
108
109 mapEager :: (a -> Eager ans b) -> [a] -> Eager ans [b]
110 mapEager f [] = returnEager []
111 mapEager f (x:xs) = f x                 `thenEager` \ y ->
112                     mapEager f xs       `thenEager` \ ys ->
113                     returnEager (y:ys)
114 #endif
115 \end{code}
116
117 %************************************************************************
118 %*                                                                      *
119 \subsection{A for loop}
120 %*                                                                      *
121 %************************************************************************
122
123 \begin{code}
124 -- Compose a function with itself n times.  (nth rather than twice)
125 nTimes :: Int -> (a -> a) -> (a -> a)
126 nTimes 0 _ = id
127 nTimes 1 f = f
128 nTimes n f = f . nTimes (n-1) f
129 \end{code}
130
131 %************************************************************************
132 %*                                                                      *
133 \subsection{Maybe-ery}
134 %*                                                                      *
135 %************************************************************************
136
137 \begin{code}
138 unJust :: String -> Maybe a -> a
139 unJust who (Just x) = x
140 unJust who Nothing  = panic ("unJust of Nothing, called by " ++ who)
141 \end{code}
142
143 %************************************************************************
144 %*                                                                      *
145 \subsection[Utils-lists]{General list processing}
146 %*                                                                      *
147 %************************************************************************
148
149 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
150 are of equal length.  Alastair Reid thinks this should only happen if
151 DEBUGging on; hey, why not?
152
153 \begin{code}
154 zipEqual        :: String -> [a] -> [b] -> [(a,b)]
155 zipWithEqual    :: String -> (a->b->c) -> [a]->[b]->[c]
156 zipWith3Equal   :: String -> (a->b->c->d) -> [a]->[b]->[c]->[d]
157 zipWith4Equal   :: String -> (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
158
159 #ifndef DEBUG
160 zipEqual      _ = zip
161 zipWithEqual  _ = zipWith
162 zipWith3Equal _ = zipWith3
163 zipWith4Equal _ = zipWith4
164 #else
165 zipEqual msg []     []     = []
166 zipEqual msg (a:as) (b:bs) = (a,b) : zipEqual msg as bs
167 zipEqual msg as     bs     = panic ("zipEqual: unequal lists:"++msg)
168
169 zipWithEqual msg z (a:as) (b:bs)=  z a b : zipWithEqual msg z as bs
170 zipWithEqual msg _ [] []        =  []
171 zipWithEqual msg _ _ _          =  panic ("zipWithEqual: unequal lists:"++msg)
172
173 zipWith3Equal msg z (a:as) (b:bs) (c:cs)
174                                 =  z a b c : zipWith3Equal msg z as bs cs
175 zipWith3Equal msg _ [] []  []   =  []
176 zipWith3Equal msg _ _  _   _    =  panic ("zipWith3Equal: unequal lists:"++msg)
177
178 zipWith4Equal msg z (a:as) (b:bs) (c:cs) (d:ds)
179                                 =  z a b c d : zipWith4Equal msg z as bs cs ds
180 zipWith4Equal msg _ [] [] [] [] =  []
181 zipWith4Equal msg _ _  _  _  _  =  panic ("zipWith4Equal: unequal lists:"++msg)
182 #endif
183 \end{code}
184
185 \begin{code}
186 -- zipLazy is lazy in the second list (observe the ~)
187
188 zipLazy :: [a] -> [b] -> [(a,b)]
189 zipLazy [] ys = []
190 zipLazy (x:xs) ~(y:ys) = (x,y) : zipLazy xs ys
191 \end{code}
192
193
194 \begin{code}
195 stretchZipWith :: (a -> Bool) -> b -> (a->b->c) -> [a] -> [b] -> [c]
196 -- (stretchZipWith p z f xs ys) stretches ys by inserting z in 
197 -- the places where p returns *True*
198
199 stretchZipWith p z f [] ys = []
200 stretchZipWith p z f (x:xs) ys
201   | p x       = f x z : stretchZipWith p z f xs ys
202   | otherwise = case ys of
203                   []     -> []
204                   (y:ys) -> f x y : stretchZipWith p z f xs ys
205 \end{code}
206
207
208 \begin{code}
209 mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
210
211 mapAndUnzip f [] = ([],[])
212 mapAndUnzip f (x:xs)
213   = let
214         (r1,  r2)  = f x
215         (rs1, rs2) = mapAndUnzip f xs
216     in
217     (r1:rs1, r2:rs2)
218
219 mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
220
221 mapAndUnzip3 f [] = ([],[],[])
222 mapAndUnzip3 f (x:xs)
223   = let
224         (r1,  r2,  r3)  = f x
225         (rs1, rs2, rs3) = mapAndUnzip3 f xs
226     in
227     (r1:rs1, r2:rs2, r3:rs3)
228 \end{code}
229
230 \begin{code}
231 nOfThem :: Int -> a -> [a]
232 nOfThem n thing = replicate n thing
233
234 lengthExceeds :: [a] -> Int -> Bool
235 -- (lengthExceeds xs n) is True if   length xs > n
236 (x:xs)  `lengthExceeds` n = n < 1 || xs `lengthExceeds` (n - 1)
237 []      `lengthExceeds` n = n < 0
238
239 isSingleton :: [a] -> Bool
240 isSingleton [x] = True
241 isSingleton  _  = False
242
243 only :: [a] -> a
244 #ifdef DEBUG
245 only [a] = a
246 #else
247 only (a:_) = a
248 #endif
249 \end{code}
250
251 \begin{code}
252 snocView :: [a] -> ([a], a)     -- Split off the last element
253 snocView xs = go xs []
254             where
255               go [x]    acc = (reverse acc, x)
256               go (x:xs) acc = go xs (x:acc)
257 \end{code}
258
259 Debugging/specialising versions of \tr{elem} and \tr{notElem}
260
261 \begin{code}
262 isIn, isn'tIn :: (Eq a) => String -> a -> [a] -> Bool
263
264 # ifndef DEBUG
265 isIn    msg x ys = elem__    x ys
266 isn'tIn msg x ys = notElem__ x ys
267
268 --these are here to be SPECIALIZEd (automagically)
269 elem__ _ []     = False
270 elem__ x (y:ys) = x==y || elem__ x ys
271
272 notElem__ x []     =  True
273 notElem__ x (y:ys) =  x /= y && notElem__ x ys
274
275 # else {- DEBUG -}
276 isIn msg x ys
277   = elem (_ILIT 0) x ys
278   where
279     elem i _ []     = False
280     elem i x (y:ys)
281       | i ># _ILIT 100 = panic ("Over-long elem in: " ++ msg)
282       | otherwise        = x == y || elem (i +# _ILIT(1)) x ys
283
284 isn'tIn msg x ys
285   = notElem (_ILIT 0) x ys
286   where
287     notElem i x [] =  True
288     notElem i x (y:ys)
289       | i ># _ILIT 100 = panic ("Over-long notElem in: " ++ msg)
290       | otherwise        =  x /= y && notElem (i +# _ILIT(1)) x ys
291
292 # endif {- DEBUG -}
293
294 \end{code}
295
296 %************************************************************************
297 %*                                                                      *
298 \subsection[Utils-sorting]{Sorting}
299 %*                                                                      *
300 %************************************************************************
301
302 %************************************************************************
303 %*                                                                      *
304 \subsubsection[Utils-quicksorting]{Quicksorts}
305 %*                                                                      *
306 %************************************************************************
307
308 \begin{code}
309 #if NOT_USED
310
311 -- tail-recursive, etc., "quicker sort" [as per Meira thesis]
312 quicksort :: (a -> a -> Bool)           -- Less-than predicate
313           -> [a]                        -- Input list
314           -> [a]                        -- Result list in increasing order
315
316 quicksort lt []      = []
317 quicksort lt [x]     = [x]
318 quicksort lt (x:xs)  = split x [] [] xs
319   where
320     split x lo hi []                 = quicksort lt lo ++ (x : quicksort lt hi)
321     split x lo hi (y:ys) | y `lt` x  = split x (y:lo) hi ys
322                          | True      = split x lo (y:hi) ys
323 #endif
324 \end{code}
325
326 Quicksort variant from Lennart's Haskell-library contribution.  This
327 is a {\em stable} sort.
328
329 \begin{code}
330 stableSortLt = sortLt   -- synonym; when we want to highlight stable-ness
331
332 sortLt :: (a -> a -> Bool)              -- Less-than predicate
333        -> [a]                           -- Input list
334        -> [a]                           -- Result list
335
336 sortLt lt l = qsort lt   l []
337
338 -- qsort is stable and does not concatenate.
339 qsort :: (a -> a -> Bool)       -- Less-than predicate
340       -> [a]                    -- xs, Input list
341       -> [a]                    -- r,  Concatenate this list to the sorted input list
342       -> [a]                    -- Result = sort xs ++ r
343
344 qsort lt []     r = r
345 qsort lt [x]    r = x:r
346 qsort lt (x:xs) r = qpart lt x xs [] [] r
347
348 -- qpart partitions and sorts the sublists
349 -- rlt contains things less than x,
350 -- rge contains the ones greater than or equal to x.
351 -- Both have equal elements reversed with respect to the original list.
352
353 qpart lt x [] rlt rge r =
354     -- rlt and rge are in reverse order and must be sorted with an
355     -- anti-stable sorting
356     rqsort lt rlt (x : rqsort lt rge r)
357
358 qpart lt x (y:ys) rlt rge r =
359     if lt y x then
360         -- y < x
361         qpart lt x ys (y:rlt) rge r
362     else
363         -- y >= x
364         qpart lt x ys rlt (y:rge) r
365
366 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
367 rqsort lt []     r = r
368 rqsort lt [x]    r = x:r
369 rqsort lt (x:xs) r = rqpart lt x xs [] [] r
370
371 rqpart lt x [] rle rgt r =
372     qsort lt rle (x : qsort lt rgt r)
373
374 rqpart lt x (y:ys) rle rgt r =
375     if lt x y then
376         -- y > x
377         rqpart lt x ys rle (y:rgt) r
378     else
379         -- y <= x
380         rqpart lt x ys (y:rle) rgt r
381 \end{code}
382
383 %************************************************************************
384 %*                                                                      *
385 \subsubsection[Utils-dull-mergesort]{A rather dull mergesort}
386 %*                                                                      *
387 %************************************************************************
388
389 \begin{code}
390 #if NOT_USED
391 mergesort :: (a -> a -> Ordering) -> [a] -> [a]
392
393 mergesort cmp xs = merge_lists (split_into_runs [] xs)
394   where
395     a `le` b = case cmp a b of { LT -> True;  EQ -> True; GT -> False }
396     a `ge` b = case cmp a b of { LT -> False; EQ -> True; GT -> True  }
397
398     split_into_runs []        []                = []
399     split_into_runs run       []                = [run]
400     split_into_runs []        (x:xs)            = split_into_runs [x] xs
401     split_into_runs [r]       (x:xs) | x `ge` r = split_into_runs [r,x] xs
402     split_into_runs rl@(r:rs) (x:xs) | x `le` r = split_into_runs (x:rl) xs
403                                      | True     = rl : (split_into_runs [x] xs)
404
405     merge_lists []       = []
406     merge_lists (x:xs)   = merge x (merge_lists xs)
407
408     merge [] ys = ys
409     merge xs [] = xs
410     merge xl@(x:xs) yl@(y:ys)
411       = case cmp x y of
412           EQ  -> x : y : (merge xs ys)
413           LT  -> x : (merge xs yl)
414           GT -> y : (merge xl ys)
415 #endif
416 \end{code}
417
418 %************************************************************************
419 %*                                                                      *
420 \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten}
421 %*                                                                      *
422 %************************************************************************
423
424 \begin{display}
425 Date: Mon, 3 May 93 20:45:23 +0200
426 From: Carsten Kehler Holst <kehler@cs.chalmers.se>
427 To: partain@dcs.gla.ac.uk
428 Subject: natural merge sort beats quick sort [ and it is prettier ]
429
430 Here is a piece of Haskell code that I'm rather fond of. See it as an
431 attempt to get rid of the ridiculous quick-sort routine. group is
432 quite useful by itself I think it was John's idea originally though I
433 believe the lazy version is due to me [surprisingly complicated].
434 gamma [used to be called] is called gamma because I got inspired by
435 the Gamma calculus. It is not very close to the calculus but does
436 behave less sequentially than both foldr and foldl. One could imagine
437 a version of gamma that took a unit element as well thereby avoiding
438 the problem with empty lists.
439
440 I've tried this code against
441
442    1) insertion sort - as provided by haskell
443    2) the normal implementation of quick sort
444    3) a deforested version of quick sort due to Jan Sparud
445    4) a super-optimized-quick-sort of Lennart's
446
447 If the list is partially sorted both merge sort and in particular
448 natural merge sort wins. If the list is random [ average length of
449 rising subsequences = approx 2 ] mergesort still wins and natural
450 merge sort is marginally beaten by Lennart's soqs. The space
451 consumption of merge sort is a bit worse than Lennart's quick sort
452 approx a factor of 2. And a lot worse if Sparud's bug-fix [see his
453 fpca article ] isn't used because of group.
454
455 have fun
456 Carsten
457 \end{display}
458
459 \begin{code}
460 group :: (a -> a -> Bool) -> [a] -> [[a]]
461
462 {-
463 Date: Mon, 12 Feb 1996 15:09:41 +0000
464 From: Andy Gill <andy@dcs.gla.ac.uk>
465
466 Here is a `better' definition of group.
467 -}
468 group p []     = []
469 group p (x:xs) = group' xs x x (x :)
470   where
471     group' []     _     _     s  = [s []]
472     group' (x:xs) x_min x_max s 
473         | not (x `p` x_max) = group' xs x_min x (s . (x :)) 
474         | x `p` x_min       = group' xs x x_max ((x :) . s) 
475         | otherwise         = s [] : group' xs x x (x :) 
476
477 -- This one works forwards *and* backwards, as well as also being
478 -- faster that the one in Util.lhs.
479
480 {- ORIG:
481 group p [] = [[]]
482 group p (x:xs) =
483    let ((h1:t1):tt1) = group p xs
484        (t,tt) = if null xs then ([],[]) else
485                 if x `p` h1 then (h1:t1,tt1) else
486                    ([], (h1:t1):tt1)
487    in ((x:t):tt)
488 -}
489
490 generalMerge :: (a -> a -> Bool) -> [a] -> [a] -> [a]
491 generalMerge p xs [] = xs
492 generalMerge p [] ys = ys
493 generalMerge p (x:xs) (y:ys) | x `p` y   = x : generalMerge p xs (y:ys)
494                              | otherwise = y : generalMerge p (x:xs) ys
495
496 -- gamma is now called balancedFold
497
498 balancedFold :: (a -> a -> a) -> [a] -> a
499 balancedFold f [] = error "can't reduce an empty list using balancedFold"
500 balancedFold f [x] = x
501 balancedFold f l  = balancedFold f (balancedFold' f l)
502
503 balancedFold' :: (a -> a -> a) -> [a] -> [a]
504 balancedFold' f (x:y:xs) = f x y : balancedFold' f xs
505 balancedFold' f xs = xs
506
507 generalMergeSort p [] = []
508 generalMergeSort p xs = (balancedFold (generalMerge p) . map (: [])) xs
509
510 generalNaturalMergeSort p [] = []
511 generalNaturalMergeSort p xs = (balancedFold (generalMerge p) . group p) xs
512
513 mergeSort, naturalMergeSort :: Ord a => [a] -> [a]
514
515 mergeSort = generalMergeSort (<=)
516 naturalMergeSort = generalNaturalMergeSort (<=)
517
518 mergeSortLe le = generalMergeSort le
519 naturalMergeSortLe le = generalNaturalMergeSort le
520 \end{code}
521
522 %************************************************************************
523 %*                                                                      *
524 \subsection[Utils-transitive-closure]{Transitive closure}
525 %*                                                                      *
526 %************************************************************************
527
528 This algorithm for transitive closure is straightforward, albeit quadratic.
529
530 \begin{code}
531 transitiveClosure :: (a -> [a])         -- Successor function
532                   -> (a -> a -> Bool)   -- Equality predicate
533                   -> [a]
534                   -> [a]                -- The transitive closure
535
536 transitiveClosure succ eq xs
537  = go [] xs
538  where
539    go done []                      = done
540    go done (x:xs) | x `is_in` done = go done xs
541                   | otherwise      = go (x:done) (succ x ++ xs)
542
543    x `is_in` []                 = False
544    x `is_in` (y:ys) | eq x y    = True
545                     | otherwise = x `is_in` ys
546 \end{code}
547
548 %************************************************************************
549 %*                                                                      *
550 \subsection[Utils-accum]{Accumulating}
551 %*                                                                      *
552 %************************************************************************
553
554 @mapAccumL@ behaves like a combination
555 of  @map@ and @foldl@;
556 it applies a function to each element of a list, passing an accumulating
557 parameter from left to right, and returning a final value of this
558 accumulator together with the new list.
559
560 \begin{code}
561 mapAccumL :: (acc -> x -> (acc, y))     -- Function of elt of input list
562                                         -- and accumulator, returning new
563                                         -- accumulator and elt of result list
564             -> acc              -- Initial accumulator
565             -> [x]              -- Input list
566             -> (acc, [y])               -- Final accumulator and result list
567
568 mapAccumL f b []     = (b, [])
569 mapAccumL f b (x:xs) = (b'', x':xs') where
570                                           (b', x') = f b x
571                                           (b'', xs') = mapAccumL f b' xs
572 \end{code}
573
574 @mapAccumR@ does the same, but working from right to left instead.  Its type is
575 the same as @mapAccumL@, though.
576
577 \begin{code}
578 mapAccumR :: (acc -> x -> (acc, y))     -- Function of elt of input list
579                                         -- and accumulator, returning new
580                                         -- accumulator and elt of result list
581             -> acc              -- Initial accumulator
582             -> [x]              -- Input list
583             -> (acc, [y])               -- Final accumulator and result list
584
585 mapAccumR f b []     = (b, [])
586 mapAccumR f b (x:xs) = (b'', x':xs') where
587                                           (b'', x') = f b' x
588                                           (b', xs') = mapAccumR f b xs
589 \end{code}
590
591 Here is the bi-directional version, that works from both left and right.
592
593 \begin{code}
594 mapAccumB :: (accl -> accr -> x -> (accl, accr,y))
595                                 -- Function of elt of input list
596                                 -- and accumulator, returning new
597                                 -- accumulator and elt of result list
598           -> accl                       -- Initial accumulator from left
599           -> accr                       -- Initial accumulator from right
600           -> [x]                        -- Input list
601           -> (accl, accr, [y])  -- Final accumulators and result list
602
603 mapAccumB f a b []     = (a,b,[])
604 mapAccumB f a b (x:xs) = (a'',b'',y:ys)
605    where
606         (a',b'',y)  = f a b' x
607         (a'',b',ys) = mapAccumB f a' b xs
608 \end{code}
609
610 A strict version of foldl.
611
612 \begin{code}
613 foldl'        :: (a -> b -> a) -> a -> [b] -> a
614 foldl' f z xs = lgo z xs
615              where
616                 lgo z []     =  z
617                 lgo z (x:xs) = (lgo $! (f z x)) xs
618 \end{code}
619
620 A combination of foldl with zip.  It works with equal length lists.
621
622 \begin{code}
623 foldl2 :: (acc -> a -> b -> acc) -> acc -> [a] -> [b] -> acc
624 foldl2 k z [] [] = z
625 foldl2 k z (a:as) (b:bs) = foldl2 k (k z a b) as bs
626 \end{code}
627
628 Count the number of times a predicate is true
629
630 \begin{code}
631 count :: (a -> Bool) -> [a] -> Int
632 count p [] = 0
633 count p (x:xs) | p x       = 1 + count p xs
634                | otherwise = count p xs
635 \end{code}
636
637
638 %************************************************************************
639 %*                                                                      *
640 \subsection[Utils-comparison]{Comparisons}
641 %*                                                                      *
642 %************************************************************************
643
644 \begin{code}
645 thenCmp :: Ordering -> Ordering -> Ordering
646 {-# INLINE thenCmp #-}
647 thenCmp EQ   any = any
648 thenCmp other any = other
649
650 cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
651     -- `cmpList' uses a user-specified comparer
652
653 cmpList cmp []     [] = EQ
654 cmpList cmp []     _  = LT
655 cmpList cmp _      [] = GT
656 cmpList cmp (a:as) (b:bs)
657   = case cmp a b of { EQ -> cmpList cmp as bs; xxx -> xxx }
658 \end{code}
659
660 \begin{code}
661 prefixMatch :: Eq a => [a] -> [a] -> Bool
662 prefixMatch [] _str = True
663 prefixMatch _pat [] = False
664 prefixMatch (p:ps) (s:ss) | p == s    = prefixMatch ps ss
665                           | otherwise = False
666
667 suffixMatch :: Eq a => [a] -> [a] -> Bool
668 suffixMatch pat str = prefixMatch (reverse pat) (reverse str)
669 \end{code}
670
671 %************************************************************************
672 %*                                                                      *
673 \subsection[Utils-pairs]{Pairs}
674 %*                                                                      *
675 %************************************************************************
676
677 The following are curried versions of @fst@ and @snd@.
678
679 \begin{code}
680 cfst :: a -> b -> a     -- stranal-sem only (Note)
681 cfst x y = x
682 \end{code}
683
684 The following provide us higher order functions that, when applied
685 to a function, operate on pairs.
686
687 \begin{code}
688 applyToPair :: ((a -> c),(b -> d)) -> (a,b) -> (c,d)
689 applyToPair (f,g) (x,y) = (f x, g y)
690
691 applyToFst :: (a -> c) -> (a,b)-> (c,b)
692 applyToFst f (x,y) = (f x,y)
693
694 applyToSnd :: (b -> d) -> (a,b) -> (a,d)
695 applyToSnd f (x,y) = (x,f y)
696
697 foldPair :: (a->a->a,b->b->b) -> (a,b) -> [(a,b)] -> (a,b)
698 foldPair fg ab [] = ab
699 foldPair fg@(f,g) ab ((a,b):abs) = (f a u,g b v)
700                        where (u,v) = foldPair fg ab abs
701 \end{code}
702
703 \begin{code}
704 unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
705 unzipWith f pairs = map ( \ (a, b) -> f a b ) pairs
706 \end{code}
707
708 \begin{code}
709 seqList :: [a] -> b -> b
710 seqList [] b = b
711 seqList (x:xs) b = x `seq` seqList xs b
712 \end{code}
713
714 Global variables:
715
716 \begin{code}
717 global :: a -> IORef a
718 global a = unsafePerformIO (newIORef a)
719 \end{code}
720
721 Compatibility stuff:
722
723 \begin{code}
724 #if __GLASGOW_HASKELL__ <= 408
725 catchJust = catchIO
726 ioErrors  = justIoErrors
727 throwTo   = raiseInThread
728 #endif
729
730 #ifdef mingw32_TARGET_OS
731 foreign import "_getpid" myGetProcessID :: IO Int 
732 #else
733 myGetProcessID :: IO Int
734 myGetProcessID = Posix.getProcessID
735 #endif
736 \end{code}