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
- throwM :: (HasCallStack, Exception e) => e -> m a
- class Monad m => MonadReader r (m :: Type -> Type) | m -> r where
- class (forall (m :: Type -> Type). Monad m => Monad (t m)) => 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 #
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:
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 #
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 :: (HasCallStack, Exception e) => e -> m a #
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 #
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 (forall (m :: Type -> Type). Monad m => Monad (t m)) => MonadTrans (t :: (Type -> Type) -> Type -> Type) where #
The class of monad transformers.
For any monad m
, the result t m
should also be a monad,
and lift
should be a monad transformation from m
to t m
,
i.e. it should satisfy the following laws:
Since 0.6.0.0 and for GHC 8.6 and later, the requirement that t m
be a Monad
is enforced by the implication constraint
forall m.
enabled by the
Monad
m => Monad
(t m)QuantifiedConstraints
extension.
Ambiguity error with GHC 9.0 to 9.2.2
These versions of GHC have a bug (https://gitlab.haskell.org/ghc/ghc/-/issues/20582) which causes constraints like
(MonadTrans t, forall m. Monad m => Monad (t m)) => ...
to be reported as ambiguous. For transformers 0.6 and later, this can be fixed by removing the second constraint, which is implied by the first.
lift :: Monad m => m a -> t m a #
Lift a computation from the argument monad to the constructed monad.