HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulestreamly-0.10.1Haskell2010

Streamly.Internal.Data.Stream.MkType

  • 4 classes
  • 3 values
  • Packagestreamly-0.10.1
  • Exports7
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMkType.hs

Imports for Examples

0 declarations
Example4 expressions
:mimport Language.Haskell.THimport qualified Streamly.Data.Stream.Prelude as Streamimport Streamly.Internal.Data.Stream.MkType

Template Haskell Macros

2 declarations
valuemkZipType
  1. :: String

    Name of the type

  2. -> String

    Function to use for (<*>)

  3. -> Bool

    True if (<*>) requires MonadAsync constraint (concurrent)

  4. -> Q [Dec]
#

Create a type with a zip-like applicative.

Example2 expressions
expr <- runQ (mkZipType "ZipStream" "zipApply" False)putStrLn $ pprint exprnewtype ZipStream m a    = ZipStream (Stream.Stream m a)    deriving FoldablemkZipStream :: Stream.Stream m a -> ZipStream m amkZipStream = ZipStreamunZipStream :: ZipStream m a -> Stream.Stream m aunZipStream (ZipStream strm) = strmderiving 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)}
valuemkCrossType
  1. :: String

    Name of the type

  2. -> String

    Function to use for (>>=)

  3. -> Bool

    True if (>>=) requires MonadAsync constraint (concurrent)

  4. -> Q [Dec]
#

Create a type with specific stream combination properties.

Example2 expressions
expr <- runQ (mkCrossType "Parallel" "parBind" True)putStrLn $ pprint exprnewtype Parallel m a = Parallel (Stream.Stream m a)mkParallel :: Stream.Stream m a -> Parallel m amkParallel = ParallelunParallel :: Parallel m a -> Stream.Stream m aunParallel (Parallel strm) = strminstance 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

5 declarations
classclass 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:

Methods

  • liftIO :: IO a -> m a

    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 liftIO, we would have ended up with this error:

    • 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 IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

    > evalStateT printState "hello"
    "hello"
    
    > evalStateT printState 3
    3
    
Instances28MonadIO, …
classclass 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.

Methods

  • 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
Instances27MonadThrow, …
classclass 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.

Methods

  • ask :: m r

    Retrieves the monad environment.

  • local :: (r -> r) -> m a -> m a

    Executes a computation in a modified environment.

  • reader :: (r -> a) -> m a

    Retrieves a function of the current environment.

Instances23MonadReader, …
classclass (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. Monad m => Monad (t m) enabled by the 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.

Methods

  • lift :: Monad m => m a -> t m a

    Lift a computation from the argument monad to the constructed monad.

Instances20MonadTrans, …
valueap :: Monad m => m (a -> b) -> m a -> m b
#

In many situations, the liftM operations can be replaced by uses of ap, which promotes function application.

return f `ap` x1 `ap` ... `ap` xn

is equivalent to

liftM<n> f x1 x2 ... xn
Examples
Example1 expression
pure (\x y z -> x + y * z) `ap` Just 1 `ap` Just 5 `ap` Just 10Just 51