1 module CLasH.Utils where
6 import qualified Data.Accessor.Monad.Trans.State as MonadState
7 import qualified Data.Map as Map
8 import qualified Control.Monad as Monad
9 import qualified Control.Monad.Trans.State as State
11 -- Make a caching version of a stateful computatation.
12 makeCached :: (Monad m, Ord k) =>
13 k -- ^ The key to use for the cache
14 -> Accessor s (Map.Map k v) -- ^ The accessor to get at the cache
15 -> State.StateT s m v -- ^ How to compute the value to cache?
16 -> State.StateT s m v -- ^ The resulting value, from the cache or freshly
18 makeCached key accessor create = do
19 cache <- MonadState.get accessor
20 case Map.lookup key cache of
21 -- Found in cache, just return
22 Just value -> return value
23 -- Not found, compute it and put it in the cache
26 MonadState.modify accessor (Map.insert key value)
29 unzipM :: (Monad m) =>
32 unzipM = Monad.liftM unzip
34 catMaybesM :: (Monad m) =>
37 catMaybesM = Monad.liftM Maybe.catMaybes
39 concatM :: (Monad m) =>
42 concatM = Monad.liftM concat
44 isJustM :: (Monad m) => m (Maybe a) -> m Bool
45 isJustM = Monad.liftM Maybe.isJust
47 andM, orM :: (Monad m) => m [Bool] -> m Bool
48 andM = Monad.liftM and
51 mapAccumLM :: (Monad m) => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
52 mapAccumLM _ s [] = return (s, [])
53 mapAccumLM f s (x:xs) = do
55 (s'', ys) <- mapAccumLM f s' xs