-- | Highly random utility functions
module Util (
-- * Flags dependent on the compiler build
- ghciSupported, debugIsOn, ghciTablesNextToCode, picIsOn,
+ ghciSupported, debugIsOn, ghciTablesNextToCode, isDynamicGhcLib,
isWindowsHost, isWindowsTarget, isDarwinTarget,
-- * General list processing
isIn, isn'tIn,
+ -- * Tuples
+ fstOf3, sndOf3, thirdOf3,
+
-- * List operations controlled by another list
takeList, dropList, splitAtList, split,
dropTail,
import Panic
-import Data.IORef ( IORef, newIORef )
+import Data.IORef ( IORef, newIORef, atomicModifyIORef )
import System.IO.Unsafe ( unsafePerformIO )
-import Data.IORef ( readIORef, writeIORef )
import Data.List hiding (group)
import Control.Concurrent.MVar ( MVar, newMVar, newEmptyMVar )
#ifdef DEBUG
-import qualified Data.List as List ( elem, notElem )
import FastTypes
#endif
ghciTablesNextToCode = False
#endif
-picIsOn :: Bool
-#ifdef __PIC__
-picIsOn = True
+isDynamicGhcLib :: Bool
+#ifdef DYNAMIC
+isDynamicGhcLib = True
#else
-picIsOn = False
+isDynamicGhcLib = False
#endif
isWindowsHost :: Bool
nTimes n f = f . nTimes (n-1) f
\end{code}
+\begin{code}
+fstOf3 :: (a,b,c) -> a
+sndOf3 :: (a,b,c) -> b
+thirdOf3 :: (a,b,c) -> c
+fstOf3 (a,_,_) = a
+sndOf3 (_,b,_) = b
+thirdOf3 (_,_,c) = c
+\end{code}
+
%************************************************************************
%* *
\subsection[Utils-lists]{General list processing}
isIn, isn'tIn :: Eq a => String -> a -> [a] -> Bool
# ifndef DEBUG
-isIn _msg x ys = elem__ x ys
-isn'tIn _msg x ys = notElem__ x ys
-
---these are here to be SPECIALIZEd (automagically)
-elem__ :: Eq a => a -> [a] -> Bool
-elem__ _ [] = False
-elem__ x (y:ys) = x == y || elem__ x ys
-
-notElem__ :: Eq a => a -> [a] -> Bool
-notElem__ _ [] = True
-notElem__ x (y:ys) = x /= y && notElem__ x ys
+isIn _msg x ys = x `elem` ys
+isn'tIn _msg x ys = x `notElem` ys
# else /* DEBUG */
isIn msg x ys
- = elem (_ILIT(0)) x ys
+ = elem100 (_ILIT(0)) x ys
where
- elem _ _ [] = False
- elem i x (y:ys)
+ elem100 _ _ [] = False
+ elem100 i x (y:ys)
| i ># _ILIT(100) = trace ("Over-long elem in " ++ msg)
- (x `List.elem` (y:ys))
- | otherwise = x == y || elem (i +# _ILIT(1)) x ys
+ (x `elem` (y:ys))
+ | otherwise = x == y || elem100 (i +# _ILIT(1)) x ys
isn'tIn msg x ys
- = notElem (_ILIT(0)) x ys
+ = notElem100 (_ILIT(0)) x ys
where
- notElem _ _ [] = True
- notElem i x (y:ys)
+ notElem100 _ _ [] = True
+ notElem100 i x (y:ys)
| i ># _ILIT(100) = trace ("Over-long notElem in " ++ msg)
- (x `List.notElem` (y:ys))
- | otherwise = x /= y && notElem (i +# _ILIT(1)) x ys
+ (x `notElem` (y:ys))
+ | otherwise = x /= y && notElem100 (i +# _ILIT(1)) x ys
# endif /* DEBUG */
\end{code}
\begin{code}
consIORef :: IORef [a] -> a -> IO ()
consIORef var x = do
- xs <- readIORef var
- writeIORef var (x:xs)
+ atomicModifyIORef var (\xs -> (x:xs,()))
\end{code}
\begin{code}