-- Standard combinators, specialised
returnM, thenM, thenM_, failM, failWithM,
mappM, mappM_, mapSndM, sequenceM, sequenceM_,
- foldlM, foldrM,
+ foldlM, foldrM, anyM,
mapAndUnzipM, mapAndUnzip3M,
checkM, ifM, zipWithM, zipWithM_,
import Data.IORef ( IORef, newIORef, readIORef, writeIORef )
import System.IO.Unsafe ( unsafeInterleaveIO )
import System.IO ( fixIO )
-
+import MonadUtils
----------------------------------------------------------------------
-- Defining the monad type
unIOEnv (IOEnv m) = m
instance Monad (IOEnv m) where
- (>>=) = thenM
- (>>) = thenM_
- return = returnM
- fail s = failM -- Ignore the string
+ (>>=) = thenM
+ (>>) = thenM_
+ return = returnM
+ fail s = failM -- Ignore the string
+
+instance Applicative (IOEnv m) where
+ pure = returnM
+ IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
instance Functor (IOEnv m) where
- fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
+ fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
returnM :: a -> IOEnv env a
returnM a = IOEnv (\ env -> return a)
mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
checkM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is False
ifM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is True
+anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool
mappM f [] = return []
mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
mappM_ f [] = return ()
mappM_ f (x:xs) = f x >> mappM_ f xs
+anyM f [] = return False
+anyM f (x:xs) = do { b <- f x; if b then return True
+ else anyM f xs }
+
zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
zipWithM f [] bs = return []
zipWithM f as [] = return []