| Copyright | (C) 2012-2016 University of Twente |
|---|---|
| License | BSD2 (see the file LICENSE) |
| Maintainer | Christiaan Baaij <christiaan.baaij@gmail.com> |
| Safe Haskell | None |
| Language | Haskell2010 |
Clash.Util
Description
Assortment of utility function used in the Clash library
Synopsis
- class MonadUnique m where
- curLoc :: Q Exp
- makeCached :: (MonadState s m, Hashable k, Eq k) => k -> Lens' s (HashMap k v) -> m v -> m v
- makeCachedT3 :: (MonadTrans t2, MonadTrans t1, MonadTrans t, Eq k, Hashable k, MonadState s m, Monad (t2 m), Monad (t1 (t2 m)), Monad (t (t1 (t2 m)))) => k -> Lens' s (HashMap k v) -> t (t1 (t2 m)) v -> t (t1 (t2 m)) v
- makeCachedT3S :: (MonadTrans t2, MonadTrans t1, MonadTrans t, Eq k, Hashable k, MonadState s m, Monad (t2 m), Monad (t1 (t2 m)), Monad (t (t1 (t2 m))), NFData v) => k -> Lens' s (HashMap k v) -> t (t1 (t2 m)) v -> t (t1 (t2 m)) v
- liftState :: MonadState s m => Lens' s s' -> State s' a -> m a
- firstM :: Functor f => (a -> f c) -> (a, b) -> f (c, b)
- secondM :: Functor f => (b -> f c) -> (a, b) -> f (a, c)
- combineM :: Applicative f => (a -> f b) -> (c -> f d) -> (a, c) -> f (b, d)
- traceIf :: Bool -> String -> a -> a
- partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])
- mapAccumLM :: Monad m => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
- dot :: (c -> d) -> (a -> b -> c) -> a -> b -> d
- ifThenElse :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b
- (<:>) :: Applicative f => f a -> f [a] -> f [a]
- indexMaybe :: [a] -> Int -> Maybe a
- indexNote :: String -> [a] -> Int -> a
- splitAtList :: [b] -> [a] -> ([a], [a])
- clashLibVersion :: Version
- flogBase :: Integer -> Integer -> Maybe Int
- clogBase :: Integer -> Integer -> Maybe Int
- class Functor f => Applicative (f :: * -> *) where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- first :: Arrow a => a b c -> a (b, d) (c, d)
- second :: Arrow a => a b c -> a (d, b) (d, c)
- (***) :: Arrow a => a b c -> a b' c' -> a (b, b') (c, c')
- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
- makeLenses :: Name -> DecsQ
Documentation
class MonadUnique m where #
A class that can generate unique numbers
Minimal complete definition
Instances
| MonadUnique (RewriteMonad extra) # | |
Defined in Clash.Rewrite.Types Methods getUniqueM :: RewriteMonad extra Int # | |
| Monad m => MonadUnique (StateT Int m) # | |
Defined in Clash.Util Methods getUniqueM :: StateT Int m Int # | |
Create a TH expression that returns the a formatted string containing the
name of the module curLoc is spliced into, and the line where it was spliced.
Arguments
| :: (MonadState s m, Hashable k, Eq k) | |
| => k | The key the action is associated with |
| -> Lens' s (HashMap k v) | The Lens to the HashMap that is the cache |
| -> m v | The action to cache |
| -> m v |
Cache the result of a monadic action
Arguments
| :: (MonadTrans t2, MonadTrans t1, MonadTrans t, Eq k, Hashable k, MonadState s m, Monad (t2 m), Monad (t1 (t2 m)), Monad (t (t1 (t2 m)))) | |
| => k | The key the action is associated with |
| -> Lens' s (HashMap k v) | The Lens to the HashMap that is the cache |
| -> t (t1 (t2 m)) v | The action to cache |
| -> t (t1 (t2 m)) v |
Cache the result of a monadic action in a State 3 transformer layers down
makeCachedT3S :: (MonadTrans t2, MonadTrans t1, MonadTrans t, Eq k, Hashable k, MonadState s m, Monad (t2 m), Monad (t1 (t2 m)), Monad (t (t1 (t2 m))), NFData v) => k -> Lens' s (HashMap k v) -> t (t1 (t2 m)) v -> t (t1 (t2 m)) v #
Spine-strict cache variant of mkCachedT3
Arguments
| :: MonadState s m | |
| => Lens' s s' | Lens to the State in the higher-layer monad |
| -> State s' a | The State-action to perform |
| -> m a |
Run a State-action using the State that is stored in a higher-layer Monad
combineM :: Applicative f => (a -> f b) -> (c -> f d) -> (a, c) -> f (b, d) #
partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a]) #
Monadic version of partition
mapAccumLM :: Monad m => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y]) #
Monadic version of mapAccumL
dot :: (c -> d) -> (a -> b -> c) -> a -> b -> d #
Composition of a unary function with a binary function
ifThenElse :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b #
if-then-else as a function on an argument
(<:>) :: Applicative f => f a -> f [a] -> f [a] infixr 5 #
Applicative version of 'GHC.Types.(:)'
indexMaybe :: [a] -> Int -> Maybe a #
Safe indexing, returns a Nothing if the index does not exist
indexNote :: String -> [a] -> Int -> a #
Unsafe indexing, return a custom error message when indexing fails
splitAtList :: [b] -> [a] -> ([a], [a]) #
Split the second list at the length of the first list
class Functor f => Applicative (f :: * -> *) where #
A functor with application, providing operations to
A minimal complete definition must include implementations of pure
and of either <*> or liftA2. If it defines both, then they must behave
the same as their default definitions:
(<*>) =liftA2id
liftA2f x y = f<$>x<*>y
Further, any definition must satisfy the following:
- identity
pureid<*>v = v- composition
pure(.)<*>u<*>v<*>w = u<*>(v<*>w)- homomorphism
puref<*>purex =pure(f x)- interchange
u
<*>purey =pure($y)<*>u
The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:
As a consequence of these laws, the Functor instance for f will satisfy
It may be useful to note that supposing
forall x y. p (q x y) = f x . g y
it follows from the above that
liftA2p (liftA2q u v) =liftA2f u .liftA2g v
If f is also a Monad, it should satisfy
(which implies that pure and <*> satisfy the applicative functor laws).
Methods
Lift a value.
(<*>) :: f (a -> b) -> f a -> f b infixl 4 #
Sequential application.
A few functors support an implementation of <*> that is more
efficient than the default one.
Instances
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #
An infix synonym for fmap.
The name of this operator is an allusion to $.
Note the similarities between their types:
($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b
Whereas $ is function application, <$> is function
application lifted over a Functor.
Examples
Convert from a to a Maybe Int using Maybe Stringshow:
>>>show <$> NothingNothing>>>show <$> Just 3Just "3"
Convert from an to an Either Int IntEither IntString using show:
>>>show <$> Left 17Left 17>>>show <$> Right 17Right "17"
Double each element of a list:
>>>(*2) <$> [1,2,3][2,4,6]
Apply even to the second element of a pair:
>>>even <$> (2,2)(2,True)
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 #
Left-to-right Kleisli composition of monads.
first :: Arrow a => a b c -> a (b, d) (c, d) #
Send the first component of the input through the argument arrow, and copy the rest unchanged to the output.
second :: Arrow a => a b c -> a (d, b) (d, c) #
A mirror image of first.
The default definition may be overridden with a more efficient version if desired.
(***) :: Arrow a => a b c -> a b' c' -> a (b, b') (c, c') infixr 3 #
Split the input between the two argument arrows and combine their output. Note that this is in general not a functor.
The default definition may be overridden with a more efficient version if desired.
makeLenses :: Name -> DecsQ #
Build lenses (and traversals) with a sensible default configuration.
e.g.
data FooBar
= Foo { _x, _y :: Int }
| Bar { _x :: Int }
makeLenses ''FooBar
will create
x ::Lens'FooBarIntx f (Foo a b) = (\a' -> Foo a' b) <$> f a x f (Bar a) = Bar <$> f a y ::Traversal'FooBarInty f (Foo a b) = (\b' -> Foo a b') <$> f b y _ c@(Bar _) = pure c
makeLenses=makeLensesWithlensRules