2011-10-16 00:31:25 -04:00
|
|
|
{- monadic stuff
|
|
|
|
-
|
2012-03-14 17:43:34 -04:00
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2011-10-16 00:31:25 -04:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.Monad where
|
|
|
|
|
|
|
|
import Data.Maybe
|
2012-03-17 00:22:05 -04:00
|
|
|
import Control.Monad (liftM)
|
2011-10-16 00:31:25 -04:00
|
|
|
|
|
|
|
{- Return the first value from a list, if any, satisfying the given
|
|
|
|
- predicate -}
|
2012-01-21 02:24:12 -04:00
|
|
|
firstM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
|
2011-10-16 00:31:25 -04:00
|
|
|
firstM _ [] = return Nothing
|
2012-03-14 17:43:34 -04:00
|
|
|
firstM p (x:xs) = ifM (p x) (return $ Just x , firstM p xs)
|
2011-10-16 00:31:25 -04:00
|
|
|
|
2012-10-06 16:16:31 -04:00
|
|
|
{- Runs the action on values from the list until it succeeds, returning
|
|
|
|
- its result. -}
|
|
|
|
getM :: Monad m => (a -> m (Maybe b)) -> [a] -> m (Maybe b)
|
|
|
|
getM _ [] = return Nothing
|
|
|
|
getM p (x:xs) = maybe (getM p xs) (return . Just) =<< p x
|
|
|
|
|
2012-01-03 18:36:31 -04:00
|
|
|
{- Returns true if any value in the list satisfies the predicate,
|
2011-10-16 00:31:25 -04:00
|
|
|
- stopping once one is found. -}
|
2012-01-21 02:24:12 -04:00
|
|
|
anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
|
2011-10-16 00:31:25 -04:00
|
|
|
anyM p = liftM isJust . firstM p
|
2011-12-03 09:10:23 -04:00
|
|
|
|
|
|
|
{- Runs an action on values from a list until it succeeds. -}
|
2012-01-21 02:24:12 -04:00
|
|
|
untilTrue :: Monad m => [a] -> (a -> m Bool) -> m Bool
|
2011-12-03 09:10:23 -04:00
|
|
|
untilTrue = flip anyM
|
2012-01-02 11:01:08 -04:00
|
|
|
|
2012-03-14 17:43:34 -04:00
|
|
|
{- if with a monadic conditional. -}
|
|
|
|
ifM :: Monad m => m Bool -> (m a, m a) -> m a
|
|
|
|
ifM cond (thenclause, elseclause) = do
|
|
|
|
c <- cond
|
|
|
|
if c then thenclause else elseclause
|
|
|
|
|
2012-03-17 00:22:05 -04:00
|
|
|
{- short-circuiting monadic || -}
|
|
|
|
(<||>) :: Monad m => m Bool -> m Bool -> m Bool
|
|
|
|
ma <||> mb = ifM ma ( return True , mb )
|
2012-03-16 12:28:17 -04:00
|
|
|
|
2012-03-17 00:22:05 -04:00
|
|
|
{- short-circuiting monadic && -}
|
|
|
|
(<&&>) :: Monad m => m Bool -> m Bool -> m Bool
|
|
|
|
ma <&&> mb = ifM ma ( mb , return False )
|
2012-03-16 12:28:17 -04:00
|
|
|
|
2012-11-02 11:20:27 -04:00
|
|
|
{- Same fixity as && and || -}
|
2012-11-02 12:48:52 -04:00
|
|
|
infixr 3 <&&>
|
|
|
|
infixr 2 <||>
|
2012-11-02 11:20:27 -04:00
|
|
|
|
2012-01-03 00:29:27 -04:00
|
|
|
{- Runs an action, passing its value to an observer before returning it. -}
|
2012-01-21 02:24:12 -04:00
|
|
|
observe :: Monad m => (a -> m b) -> m a -> m a
|
2012-01-02 11:01:08 -04:00
|
|
|
observe observer a = do
|
|
|
|
r <- a
|
|
|
|
_ <- observer r
|
|
|
|
return r
|
2012-01-02 14:54:23 -04:00
|
|
|
|
2012-01-03 00:29:27 -04:00
|
|
|
{- b `after` a runs first a, then b, and returns the value of a -}
|
2012-01-21 02:24:12 -04:00
|
|
|
after :: Monad m => m b -> m a -> m a
|
2012-01-03 00:29:27 -04:00
|
|
|
after = observe . const
|
2012-04-21 23:32:33 -04:00
|
|
|
|
|
|
|
{- do nothing -}
|
|
|
|
noop :: Monad m => m ()
|
|
|
|
noop = return ()
|