Copyright | (c) 2022 Composewell Technologies |
---|---|
License | BSD-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Synopsis
- mkZipType :: String -> String -> Bool -> Q [Dec]
- mkCrossType :: String -> String -> Bool -> Q [Dec]
- class Monad m => MonadIO (m :: Type -> Type) where
- class Monad m => MonadThrow (m :: Type -> Type) where
- class Monad m => MonadReader r (m :: Type -> Type) | m -> r where
- class MonadTrans (t :: (Type -> Type) -> Type -> Type) where
- ap :: Monad m => m (a -> b) -> m a -> m b
Imports for Examples
>>>
:m
>>>
import Language.Haskell.TH
>>>
import qualified Streamly.Data.Stream.Prelude as Stream
>>>
import Streamly.Internal.Data.Stream.MkType
Template Haskell Macros
:: String | Name of the type |
-> String | Function to use for (<*>) |
-> Bool |
|
-> Q [Dec] |
Create a type with a zip-like applicative.
>>>
expr <- runQ (mkZipType "ZipStream" "zipApply" False)
>>>
putStrLn $ pprint expr
newtype ZipStream m a = ZipStream (Stream.Stream m a) deriving Foldable mkZipStream :: Stream.Stream m a -> ZipStream m a mkZipStream = ZipStream unZipStream :: ZipStream m a -> Stream.Stream m a unZipStream (ZipStream strm) = strm deriving instance IsList (ZipStream Identity a) deriving instance a ~ GHC.Types.Char => IsString (ZipStream Identity a) deriving instance GHC.Classes.Eq a => Eq (ZipStream Identity a) deriving instance GHC.Classes.Ord a => Ord (ZipStream Identity a) instance Show a => Show (ZipStream Identity a) where {{-# INLINE show #-}; show (ZipStream strm) = show strm} instance Read a => Read (ZipStream Identity a) where {{-# INLINE readPrec #-}; readPrec = fmap ZipStream readPrec} instance Monad m => Functor (ZipStream m) where {{-# INLINE fmap #-}; fmap f (ZipStream strm) = ZipStream (fmap f strm)} instance Monad m => Applicative (ZipStream m) where {{-# INLINE pure #-}; pure = ZipStream . Stream.repeat; {-# INLINE (<*>) #-}; (<*>) (ZipStream strm1) (ZipStream strm2) = ZipStream (zipApply strm1 strm2)}
:: String | Name of the type |
-> String | Function to use for (>>=) |
-> Bool |
|
-> Q [Dec] |
Create a type with specific stream combination properties.
>>>
expr <- runQ (mkCrossType "Parallel" "parBind" True)
>>>
putStrLn $ pprint expr
newtype Parallel m a = Parallel (Stream.Stream m a) mkParallel :: Stream.Stream m a -> Parallel m a mkParallel = Parallel unParallel :: Parallel m a -> Stream.Stream m a unParallel (Parallel strm) = strm instance Monad m => Functor (Parallel m) where {{-# INLINE fmap #-}; fmap f (Parallel strm) = Parallel (fmap f strm)} instance Stream.MonadAsync m => Monad (Parallel m) where {{-# INLINE (>>=) #-}; (>>=) (Parallel strm1) f = let f1 a = unParallel (f a) in Parallel (parBind strm1 f1)} instance Stream.MonadAsync m => Applicative (Parallel m) where {{-# INLINE pure #-}; pure = Parallel . Stream.fromPure; {-# INLINE (<*>) #-}; (<*>) = ap} instance (Monad (Parallel m), MonadIO m) => MonadIO (Parallel m) where {{-# INLINE liftIO #-}; liftIO = Parallel . (Stream.fromEffect . liftIO)} instance (Monad (Parallel m), MonadThrow m) => MonadThrow (Parallel m) where {{-# INLINE throwM #-}; throwM = Parallel . (Stream.fromEffect . throwM)}
Re-exports
class Monad m => MonadIO (m :: Type -> Type) where Source #
Monads in which IO
computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO
monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
liftIO :: IO a -> m a Source #
Lift a computation from the IO
monad.
This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations
(i.e. IO
is the base monad for the stack).
Example
import Control.Monad.Trans.State -- from the "transformers" library printState :: Show s => StateT s IO () printState = do state <- get liftIO $ print state
Had we omitted
, we would have ended up with this error:liftIO
• Couldn't match type ‘IO’ with ‘StateT s IO’ Expected type: StateT s IO () Actual type: IO ()
The important part here is the mismatch between StateT s IO ()
and
.IO
()
Luckily, we know of a function that takes an
and returns an IO
a(m a)
:
,
enabling us to run the program and see the expected results:liftIO
> evalStateT printState "hello" "hello" > evalStateT printState 3 3
Instances
class Monad m => MonadThrow (m :: Type -> Type) where Source #
A class for monads in which exceptions may be thrown.
Instances should obey the following law:
throwM e >> x = throwM e
In other words, throwing an exception short-circuits the rest of the monadic computation.
throwM :: Exception e => e -> m a Source #
Throw an exception. Note that this throws when this action is run in
the monad m
, not when it is applied. It is a generalization of
Control.Exception's throwIO
.
Should satisfy the law:
throwM e >> f = throwM e
Instances
class Monad m => MonadReader r (m :: Type -> Type) | m -> r where Source #
See examples in Control.Monad.Reader.
Note, the partially applied function type (->) r
is a simple reader monad.
See the instance
declaration below.
Retrieves the monad environment.
:: (r -> r) | The function to modify the environment. |
-> m a |
|
-> m a |
Executes a computation in a modified environment.
:: (r -> a) | The selector function to apply to the environment. |
-> m a |
Retrieves a function of the current environment.
Instances
class MonadTrans (t :: (Type -> Type) -> Type -> Type) where Source #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift
is a monad transformation:
lift :: Monad m => m a -> t m a Source #
Lift a computation from the argument monad to the constructed monad.
Instances
MonadTrans AheadT Source # | |
MonadTrans AsyncT Source # | |
MonadTrans WAsyncT Source # | |
MonadTrans ParallelT Source # | |
MonadTrans SerialT Source # | |
MonadTrans WSerialT Source # | |
MonadTrans CrossStream | |
Defined in Streamly.Internal.Data.Stream.Type | |
MonadTrans CrossStreamK | |
Defined in Streamly.Internal.Data.StreamK.Type | |
MonadTrans ListT | |
MonadTrans MaybeT | |
Monoid w => MonadTrans (AccumT w) | |
MonadTrans (ErrorT e) | |
MonadTrans (ExceptT e) | |
MonadTrans (IdentityT :: (Type -> Type) -> Type -> Type) | |
MonadTrans (ReaderT r) | |
MonadTrans (SelectT r) | |
MonadTrans (StateT s) | |
MonadTrans (StateT s) | |
MonadTrans (WriterT w) | |
Monoid w => MonadTrans (WriterT w) | |
Monoid w => MonadTrans (WriterT w) | |
MonadTrans (ContT r) | |
MonadTrans (RWST r w s) | |
Monoid w => MonadTrans (RWST r w s) | |
Monoid w => MonadTrans (RWST r w s) | |