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