list-transformer-1.1.0: List monad transformer
Safe HaskellSafe-Inferred
LanguageHaskell2010

List.Transformer

Description

The ListT type is like a list that lets you interleave effects between each element of the list.

Synopsis

Introduction

The type's definition is very short:

newtype ListT m a = ListT { next :: m (Step m a) }

Every ListT begins with an outermost effect (the 'm', commonly IO). The return value of that effect is either:

data Step m a = Cons a (ListT m a) | Nil
  • Cons: a new list element followed by the rest of the list
  • Nil : an empty list

Example: stdin, stdout

You most commonly use the ListT when you wish to generate each element of the list using IO. For example, you can read lines from standard input:

import List.Transformer

import qualified System.IO

stdin :: ListT IO String
stdin = ListT (do
    eof <- System.IO.isEOF
    if eof
        then return Nil
        else do
            string <- getLine
            return (Cons string stdin) )

You can also loop over a ListT to consume elements one-at-a-time. You "pay as you go" for effects, only running what you actually need:

stdout :: ListT IO String -> IO ()
stdout strings = do
    s <- next strings
    case s of
        Nil                  -> return ()
        Cons string strings' -> do
            putStrLn string
            stdout strings'

Combining stdin and stdout forwards lines one-by-one from standard input to standard output:

main :: IO ()
main = stdout stdin

These lines stream in constant space, never retaining more than one line in memory:

$ runghc aboveExample.hs
Test<Enter>
Test
123<Enter>
123
ABC<Enter>
ABC
<Ctrl-D>
$

Core operations

The most important operations that you should familiarize yourself with are:

  • empty, which gives you an empty ListT with 0 elements
empty :: ListT IO a
pure, return :: a -> ListT IO a
liftIO :: IO a -> ListT IO a
(<|>) :: ListT IO a -> ListT IO a -> ListT IO a
  • (>>=), which powers do notation and MonadComprehensions
(>>=) :: ListT IO a -> (a -> ListT IO b) -> ListT IO b
select :: [a] -> ListT IO a

Monadic combination

Sometimes we can simplify the code by taking advantage of the fact that the Monad instance for ListT behaves like a list comprehension:

stdout :: ListT IO String -> IO ()
stdout strings = runListT (do
    string <- strings
    liftIO (putStrLn string) )

You can read the above code as saying: "for each string in strings, call putStrLn on string."

You can even use list comprehension syntax if you enable the MonadComprehensions language extension:

stdout strings = runListT [ r | str <- strings, r <- liftIO (putStrLn str) ]

There are a few ways we could consider defining a ListT analogue to the mapM function from Prelude, but none are given in this library because they need require only (>>=) and some trivial lifting.

mapM                                :: (a -> IO b)       -> [a]        -> IO [b]
( \f xs -> xs        >>=        f ) :: (a -> ListT IO b) -> ListT IO a -> ListT IO b
( \f xs -> select xs >>= lift . f ) :: (a -> IO b)       -> [a]        -> ListT IO b
( \f xs -> xs        >>= lift . f ) :: (a -> IO b)       -> ListT IO a -> ListT IO b

A critical difference between mapM and ListT's monad is that ListT will stream in constant space, whereas mapM buffers the entire output list before returning a single element.

Exercise: Interaction

To test your understanding, guess what this code does and then test your guess by running the code:

import List.Transformer (ListT, runListT, liftIO, (<|>), select)
import Data.Foldable (asum)
import Data.List (repeat)

strings :: ListT IO String
strings = do
    select (repeat ())
    asum
        [ pure ""
        , pure "Say something:"
        , do
            x <- liftIO getLine
            return ("You said: " <|> x)
        ]

main :: IO ()
main = runListT (do
    string <- pure "Hello, there!" <|> strings
    liftIO (putStrLn string) )

ListT

newtype ListT m a Source #

This is like a list except that you can interleave effects between each list element. For example:

stdin :: ListT IO String
stdin = ListT (do
    eof <- System.IO.isEOF
    if eof
        then return Nil
        else do
            line <- getLine
            return (Cons line stdin) )

The mnemonic is "List Transformer" because this type takes a base Monad, 'm', and returns a new transformed Monad that adds support for list comprehensions

Constructors

ListT 

Fields

Instances

Instances details
MonadTrans ListT Source # 
Instance details

Defined in List.Transformer

Methods

lift :: Monad m => m a -> ListT m a #

MFunctor ListT Source # 
Instance details

Defined in List.Transformer

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> ListT m b -> ListT n b #

MonadError e m => MonadError e (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

throwError :: e -> ListT m a #

catchError :: ListT m a -> (e -> ListT m a) -> ListT m a #

MonadReader i m => MonadReader i (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

ask :: ListT m i #

local :: (i -> i) -> ListT m a -> ListT m a #

reader :: (i -> a) -> ListT m a #

MonadState s m => MonadState s (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

get :: ListT m s #

put :: s -> ListT m () #

state :: (s -> (a, s)) -> ListT m a #

Monad m => MonadFail (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

fail :: String -> ListT m a #

MonadIO m => MonadIO (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

liftIO :: IO a -> ListT m a #

Foldable m => Foldable (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

fold :: Monoid m0 => ListT m m0 -> m0 #

foldMap :: Monoid m0 => (a -> m0) -> ListT m a -> m0 #

foldMap' :: Monoid m0 => (a -> m0) -> ListT m a -> m0 #

foldr :: (a -> b -> b) -> b -> ListT m a -> b #

foldr' :: (a -> b -> b) -> b -> ListT m a -> b #

foldl :: (b -> a -> b) -> b -> ListT m a -> b #

foldl' :: (b -> a -> b) -> b -> ListT m a -> b #

foldr1 :: (a -> a -> a) -> ListT m a -> a #

foldl1 :: (a -> a -> a) -> ListT m a -> a #

toList :: ListT m a -> [a] #

null :: ListT m a -> Bool #

length :: ListT m a -> Int #

elem :: Eq a => a -> ListT m a -> Bool #

maximum :: Ord a => ListT m a -> a #

minimum :: Ord a => ListT m a -> a #

sum :: Num a => ListT m a -> a #

product :: Num a => ListT m a -> a #

(Monad m, Traversable m) => Traversable (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

traverse :: Applicative f => (a -> f b) -> ListT m a -> f (ListT m b) #

sequenceA :: Applicative f => ListT m (f a) -> f (ListT m a) #

mapM :: Monad m0 => (a -> m0 b) -> ListT m a -> m0 (ListT m b) #

sequence :: Monad m0 => ListT m (m0 a) -> m0 (ListT m a) #

Monad m => Alternative (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

empty :: ListT m a #

(<|>) :: ListT m a -> ListT m a -> ListT m a #

some :: ListT m a -> ListT m [a] #

many :: ListT m a -> ListT m [a] #

Monad m => Applicative (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

pure :: a -> ListT m a #

(<*>) :: ListT m (a -> b) -> ListT m a -> ListT m b #

liftA2 :: (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c #

(*>) :: ListT m a -> ListT m b -> ListT m b #

(<*) :: ListT m a -> ListT m b -> ListT m a #

Monad m => Functor (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

fmap :: (a -> b) -> ListT m a -> ListT m b #

(<$) :: a -> ListT m b -> ListT m a #

Monad m => Monad (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

(>>=) :: ListT m a -> (a -> ListT m b) -> ListT m b #

(>>) :: ListT m a -> ListT m b -> ListT m b #

return :: a -> ListT m a #

Monad m => MonadPlus (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

mzero :: ListT m a #

mplus :: ListT m a -> ListT m a -> ListT m a #

(Monad m, Semigroup a, Monoid a) => Monoid (ListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

mempty :: ListT m a #

mappend :: ListT m a -> ListT m a -> ListT m a #

mconcat :: [ListT m a] -> ListT m a #

(Monad m, Semigroup a) => Semigroup (ListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

(<>) :: ListT m a -> ListT m a -> ListT m a #

sconcat :: NonEmpty (ListT m a) -> ListT m a #

stimes :: Integral b => b -> ListT m a -> ListT m a #

(Monad m, Floating a) => Floating (ListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

pi :: ListT m a #

exp :: ListT m a -> ListT m a #

log :: ListT m a -> ListT m a #

sqrt :: ListT m a -> ListT m a #

(**) :: ListT m a -> ListT m a -> ListT m a #

logBase :: ListT m a -> ListT m a -> ListT m a #

sin :: ListT m a -> ListT m a #

cos :: ListT m a -> ListT m a #

tan :: ListT m a -> ListT m a #

asin :: ListT m a -> ListT m a #

acos :: ListT m a -> ListT m a #

atan :: ListT m a -> ListT m a #

sinh :: ListT m a -> ListT m a #

cosh :: ListT m a -> ListT m a #

tanh :: ListT m a -> ListT m a #

asinh :: ListT m a -> ListT m a #

acosh :: ListT m a -> ListT m a #

atanh :: ListT m a -> ListT m a #

log1p :: ListT m a -> ListT m a #

expm1 :: ListT m a -> ListT m a #

log1pexp :: ListT m a -> ListT m a #

log1mexp :: ListT m a -> ListT m a #

(Monad m, Num a) => Num (ListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

(+) :: ListT m a -> ListT m a -> ListT m a #

(-) :: ListT m a -> ListT m a -> ListT m a #

(*) :: ListT m a -> ListT m a -> ListT m a #

negate :: ListT m a -> ListT m a #

abs :: ListT m a -> ListT m a #

signum :: ListT m a -> ListT m a #

fromInteger :: Integer -> ListT m a #

(Monad m, Fractional a) => Fractional (ListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

(/) :: ListT m a -> ListT m a -> ListT m a #

recip :: ListT m a -> ListT m a #

fromRational :: Rational -> ListT m a #

Consuming

This library is designed to stream results in constant space and does not expose an obvious way to collect all the results into memory. As a rule of thumb if you think you need to collect all the results in memory try to instead see if you can consume the results as they are being generated (such as in all the above examples). If you can stream the data from start to finish then your code will use significantly less memory and your program will become more responsive.

runListT :: Monad m => ListT m a -> m () Source #

Use this to drain a ListT, running it to completion and discarding all values. For example:

stdout :: ListT IO String -> IO ()
stdout l = runListT (do
    str <- l
    liftIO (putStrLn str) )

The most common specialized type for runListT will be:

runListT :: ListT IO a -> IO ()

fold :: Monad m => (x -> a -> x) -> x -> (x -> b) -> ListT m a -> m b Source #

Use this to fold a ListT into a single value. This is designed to be used with the foldl library:

import Control.Foldl (purely)
import List.Transformer (fold)

purely fold :: Monad m => Fold a b -> ListT m a -> m b

... but you can also use the fold function directly:

fold (+) 0 id :: Num a => ListT m a -> m a
>>> fold (<>) "" id (select ["a", "b", "c", "d", "e"])
"abcde"

foldM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> ListT m a -> m b Source #

Use this to fold a ListT into a single value. This is designed to be used with the foldl library:

import Control.Foldl (impurely)
import List.Transformer (fold)

impurely fold :: Monad m => FoldM m a b -> ListT m a -> m b

... but you can also use the foldM function directly.

Constructing

empty is the empty list with no effects.

Use pure/return to construct a singleton list with no effects. Use liftIO to turn an effect into a singleton list whose sole element is the effect's result.

Suppose you want to build a ListT with three elements and no effects. You could write:

pure 1 <|> pure 2 <|> pure 3 :: ListT IO Int

... although you would probably prefer to use select instead:

select [1, 2, 3] :: ListT IO Int

select :: (Foldable f, Alternative m) => f a -> m a Source #

Convert any collection that implements Foldable to another collection that implements Alternative

For this library, the most common specialized type for select will be:

select :: [a] -> ListT IO a

unfold :: Monad m => (b -> m (Maybe (a, b))) -> b -> ListT m a Source #

unfold step seed generates a ListT from a step function and an initial seed.

Removing elements

take :: Monad m => Int -> ListT m a -> ListT m a Source #

take n xs takes n elements from the head of xs.

>>> let list xs = do x <- select xs; liftIO (print (show x)); return x
>>> let sum = fold (+) 0 id
>>> sum (take 2 (list [5,4,3,2,1]))
"5"
"4"
9

drop :: Monad m => Int -> ListT m a -> ListT m a Source #

drop n xs drops n elements from the head of xs, but still runs their effects.

>>> let list xs = do x <- select xs; liftIO (print (show x)); return x
>>> let sum = fold (+) 0 id
>>> sum (drop 2 (list [5,4,3,2,1]))
"5"
"4"
"3"
"2"
"1"
6

dropWhile :: Monad m => (a -> Bool) -> ListT m a -> ListT m a Source #

dropWhile pred xs drops elements from the head of xs if they satisfy the predicate, but still runs their effects.

>>> let list xs = do x <- select xs; liftIO (print (show x)); return x
>>> let sum = fold (+) 0 id
>>> sum (dropWhile even (list [2,4,5,7,8]))
"2"
"4"
"5"
"7"
"8"
20

takeWhile :: Monad m => (a -> Bool) -> ListT m a -> ListT m a Source #

takeWhile pred xs takes elements from xs until the predicate pred fails

>>> let list xs = do x <- select xs; liftIO (print (show x)); return x
>>> let sum = fold (+) 0 id
>>> sum (takeWhile even (list [2,4,5,7,8]))
"2"
"4"
"5"
6

To filter elements from a list based on a predicate, use guard. For example, the following function is analogous to filter:

filter :: Monad m => (a -> m Bool) -> ListT m a -> ListT m a
filter pred as = do
    a <- as
    b <- lift (pred a)
    guard b
    return a

Concatenation

Use (<|>) to concatenate two lists.

(<|>) :: ListT IO a -> ListT IO a -> ListT IO a

Use asum to flatten a list of lists.

asum :: [ListT IO a] -> ListT IO a

Use join to flatten a ListT of ListTs.

join :: ListT IO (ListT IO a) -> ListT IO a

Pairwise combination

The (<>) operation joins every combination of an element from one list with an element from the other.

>>> runListT ( (select ["a", "b"] <> select ["1", "2", "3"]) >>= (liftIO . print) )
"a1"
"a2"
"a3"
"b1"
"b2"
"b3"

This is the same combinatorial effect that (>>=) produces.

>>> runListT (do x <- select ["a", "b"]; y <- select ["1", "2", "3"]; liftIO (print (x <> y)))
"a1"
"a2"
"a3"
"b1"
"b2"
"b3"

zip :: Monad m => ListT m a -> ListT m b -> ListT m (a, b) Source #

zip xs ys zips two ListT together, running the effects of each before possibly recursing. Notice in the example below, 4 is output even though it has no corresponding element in the second list.

>>> let list xs = do x <- select xs; liftIO (print (show x)); return x
>>> runListT (zip (list [1,2,3,4,5]) (list [6,7,8]))
"1"
"6"
"2"
"7"
"3"
"8"
"4"

Repetition

Unbounded repetition can be induced using select (repeat ()). For example, here are several functions analogous to cycle:

cycle1 :: Monad m => a -> ListT m a
cycle1 a = do
    select (Data.List.repeat ())
    return a
cycle2 :: Monad m => [a] -> ListT m a
cycle2 as = do
    select (Data.List.repeat ())
    select as
cycle3 :: Monad m => m a -> ListT m a
cycle3 m = do
    select (Data.List.repeat ())
    lift m
cycle4 :: Monad m => [m a] -> ListT m a
cycle4 ms = do
    select (Data.List.repeat ())
    m <- select ms
    lift m
cycle5 :: Monad m => ListT m a -> ListT m a
cycle5 x = do
    select (Data.List.repeat ())
    x
cycle6 :: Monad m => [ListT m a] -> ListT m a
cycle6 lists = do
    select (Data.List.repeat ())
    x <- select lists
    x

In a similar manner, we can use replicate as the initial selection to achieve bounded repetition:

replicate1 :: Monad m => Int -> a -> ListT m a
replicate1 n a = do
    select (Data.List.replicate n ())
    return a
replicate2 :: Monad m => Int -> [a] -> ListT m a
replicate2 n as = do
    select (Data.List.replicate n ())
    select as
replicate3 :: Monad m => Int -> m a -> ListT m a
replicate3 n m = do
    select (Data.List.replicate n ())
    lift m
replicate4 :: Monad m => Int -> [m a] -> ListT m a
replicate4 n ms = do
    select (Data.List.replicate n ())
    m <- select ms
    lift m
replicate5 :: Monad m => Int -> ListT m a -> ListT m a
replicate5 n x = do
    select (Data.List.replicate n ())
    x
replicate6 :: Monad m => Int -> [ListT m a] -> ListT m a
replicate6 n lists = do
    select (Data.List.replicate n ())
    x <- select lists
    x

Step

data Step m a Source #

Pattern match on this type when you loop explicitly over a ListT using next. For example:

stdout :: ListT IO String -> IO ()
stdout l = do
    s <- next l
    case s of
        Nil       -> return ()
        Cons x l' -> do
            putStrLn x
            stdout l'

Constructors

Cons a (ListT m a) 
Nil 

Instances

Instances details
MFunctor Step Source # 
Instance details

Defined in List.Transformer

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Step m b -> Step n b #

Foldable m => Foldable (Step m) Source # 
Instance details

Defined in List.Transformer

Methods

fold :: Monoid m0 => Step m m0 -> m0 #

foldMap :: Monoid m0 => (a -> m0) -> Step m a -> m0 #

foldMap' :: Monoid m0 => (a -> m0) -> Step m a -> m0 #

foldr :: (a -> b -> b) -> b -> Step m a -> b #

foldr' :: (a -> b -> b) -> b -> Step m a -> b #

foldl :: (b -> a -> b) -> b -> Step m a -> b #

foldl' :: (b -> a -> b) -> b -> Step m a -> b #

foldr1 :: (a -> a -> a) -> Step m a -> a #

foldl1 :: (a -> a -> a) -> Step m a -> a #

toList :: Step m a -> [a] #

null :: Step m a -> Bool #

length :: Step m a -> Int #

elem :: Eq a => a -> Step m a -> Bool #

maximum :: Ord a => Step m a -> a #

minimum :: Ord a => Step m a -> a #

sum :: Num a => Step m a -> a #

product :: Num a => Step m a -> a #

(Monad m, Traversable m) => Traversable (Step m) Source # 
Instance details

Defined in List.Transformer

Methods

traverse :: Applicative f => (a -> f b) -> Step m a -> f (Step m b) #

sequenceA :: Applicative f => Step m (f a) -> f (Step m a) #

mapM :: Monad m0 => (a -> m0 b) -> Step m a -> m0 (Step m b) #

sequence :: Monad m0 => Step m (m0 a) -> m0 (Step m a) #

Monad m => Functor (Step m) Source # 
Instance details

Defined in List.Transformer

Methods

fmap :: (a -> b) -> Step m a -> Step m b #

(<$) :: a -> Step m b -> Step m a #

Alternative instances

newtype ZipListT m a Source #

Similar to ZipList in base: a newtype wrapper over ListT that overrides its normal Applicative instance (combine every combination) with one that "zips" outputs together one at a time.

>>> let xs = do x <- select [1,2,3,4]; liftIO (print x)
>>> let ys = do y <- select [5,6]; liftIO (print y)
>>> runListT (xs *> ys)
1
5
6
2
5
6
3
5
6
4
5
6
>>> runListT (getZipListT (ZipListT xs *> ZipListT ys))
1
5
2
6
3

Note that the final "3" is printed even though it isn't paired with anything.

While this can be used to do zipping, it is usually more convenient to just use zip. This is more useful if you are working with a function that expects "an Applicative instance", written to be polymorphic over all Applicatives.

Constructors

ZipListT 

Fields

Instances

Instances details
Foldable m => Foldable (ZipListT m) Source # 
Instance details

Defined in List.Transformer

Methods

fold :: Monoid m0 => ZipListT m m0 -> m0 #

foldMap :: Monoid m0 => (a -> m0) -> ZipListT m a -> m0 #

foldMap' :: Monoid m0 => (a -> m0) -> ZipListT m a -> m0 #

foldr :: (a -> b -> b) -> b -> ZipListT m a -> b #

foldr' :: (a -> b -> b) -> b -> ZipListT m a -> b #

foldl :: (b -> a -> b) -> b -> ZipListT m a -> b #

foldl' :: (b -> a -> b) -> b -> ZipListT m a -> b #

foldr1 :: (a -> a -> a) -> ZipListT m a -> a #

foldl1 :: (a -> a -> a) -> ZipListT m a -> a #

toList :: ZipListT m a -> [a] #

null :: ZipListT m a -> Bool #

length :: ZipListT m a -> Int #

elem :: Eq a => a -> ZipListT m a -> Bool #

maximum :: Ord a => ZipListT m a -> a #

minimum :: Ord a => ZipListT m a -> a #

sum :: Num a => ZipListT m a -> a #

product :: Num a => ZipListT m a -> a #

(Monad m, Traversable m) => Traversable (ZipListT m) Source # 
Instance details

Defined in List.Transformer

Methods

traverse :: Applicative f => (a -> f b) -> ZipListT m a -> f (ZipListT m b) #

sequenceA :: Applicative f => ZipListT m (f a) -> f (ZipListT m a) #

mapM :: Monad m0 => (a -> m0 b) -> ZipListT m a -> m0 (ZipListT m b) #

sequence :: Monad m0 => ZipListT m (m0 a) -> m0 (ZipListT m a) #

Monad m => Alternative (ZipListT m) Source # 
Instance details

Defined in List.Transformer

Methods

empty :: ZipListT m a #

(<|>) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

some :: ZipListT m a -> ZipListT m [a] #

many :: ZipListT m a -> ZipListT m [a] #

Monad m => Applicative (ZipListT m) Source # 
Instance details

Defined in List.Transformer

Methods

pure :: a -> ZipListT m a #

(<*>) :: ZipListT m (a -> b) -> ZipListT m a -> ZipListT m b #

liftA2 :: (a -> b -> c) -> ZipListT m a -> ZipListT m b -> ZipListT m c #

(*>) :: ZipListT m a -> ZipListT m b -> ZipListT m b #

(<*) :: ZipListT m a -> ZipListT m b -> ZipListT m a #

Monad m => Functor (ZipListT m) Source # 
Instance details

Defined in List.Transformer

Methods

fmap :: (a -> b) -> ZipListT m a -> ZipListT m b #

(<$) :: a -> ZipListT m b -> ZipListT m a #

(Monad m, Monoid a) => Monoid (ZipListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

mempty :: ZipListT m a #

mappend :: ZipListT m a -> ZipListT m a -> ZipListT m a #

mconcat :: [ZipListT m a] -> ZipListT m a #

(Monad m, Semigroup a) => Semigroup (ZipListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

(<>) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

sconcat :: NonEmpty (ZipListT m a) -> ZipListT m a #

stimes :: Integral b => b -> ZipListT m a -> ZipListT m a #

(Monad m, Floating a) => Floating (ZipListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

pi :: ZipListT m a #

exp :: ZipListT m a -> ZipListT m a #

log :: ZipListT m a -> ZipListT m a #

sqrt :: ZipListT m a -> ZipListT m a #

(**) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

logBase :: ZipListT m a -> ZipListT m a -> ZipListT m a #

sin :: ZipListT m a -> ZipListT m a #

cos :: ZipListT m a -> ZipListT m a #

tan :: ZipListT m a -> ZipListT m a #

asin :: ZipListT m a -> ZipListT m a #

acos :: ZipListT m a -> ZipListT m a #

atan :: ZipListT m a -> ZipListT m a #

sinh :: ZipListT m a -> ZipListT m a #

cosh :: ZipListT m a -> ZipListT m a #

tanh :: ZipListT m a -> ZipListT m a #

asinh :: ZipListT m a -> ZipListT m a #

acosh :: ZipListT m a -> ZipListT m a #

atanh :: ZipListT m a -> ZipListT m a #

log1p :: ZipListT m a -> ZipListT m a #

expm1 :: ZipListT m a -> ZipListT m a #

log1pexp :: ZipListT m a -> ZipListT m a #

log1mexp :: ZipListT m a -> ZipListT m a #

(Monad m, Num a) => Num (ZipListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

(+) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

(-) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

(*) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

negate :: ZipListT m a -> ZipListT m a #

abs :: ZipListT m a -> ZipListT m a #

signum :: ZipListT m a -> ZipListT m a #

fromInteger :: Integer -> ZipListT m a #

(Monad m, Fractional a) => Fractional (ZipListT m a) Source # 
Instance details

Defined in List.Transformer

Methods

(/) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

recip :: ZipListT m a -> ZipListT m a #

fromRational :: Rational -> ZipListT m a #

Re-exports

class MonadTrans (t :: (Type -> Type) -> Type -> Type) where #

The class of monad transformers. Instances should satisfy the following laws, which state that lift is a monad transformation:

Methods

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

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

Instances

Instances details
MonadTrans ListT Source # 
Instance details

Defined in List.Transformer

Methods

lift :: Monad m => m a -> ListT m a #

MonadTrans ListT 
Instance details

Defined in Control.Monad.Trans.List

Methods

lift :: Monad m => m a -> ListT m a #

MonadTrans MaybeT 
Instance details

Defined in Control.Monad.Trans.Maybe

Methods

lift :: Monad m => m a -> MaybeT m a #

MonadTrans (ErrorT e) 
Instance details

Defined in Control.Monad.Trans.Error

Methods

lift :: Monad m => m a -> ErrorT e m a #

MonadTrans (ExceptT e) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

lift :: Monad m => m a -> ExceptT e m a #

MonadTrans (IdentityT :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Trans.Identity

Methods

lift :: Monad m => m a -> IdentityT m a #

MonadTrans (ReaderT r) 
Instance details

Defined in Control.Monad.Trans.Reader

Methods

lift :: Monad m => m a -> ReaderT r m a #

MonadTrans (StateT s) 
Instance details

Defined in Control.Monad.Trans.State.Lazy

Methods

lift :: Monad m => m a -> StateT s m a #

MonadTrans (StateT s) 
Instance details

Defined in Control.Monad.Trans.State.Strict

Methods

lift :: Monad m => m a -> StateT s m a #

Monoid w => MonadTrans (WriterT w) 
Instance details

Defined in Control.Monad.Trans.Writer.Lazy

Methods

lift :: Monad m => m a -> WriterT w m a #

Monoid w => MonadTrans (WriterT w) 
Instance details

Defined in Control.Monad.Trans.Writer.Strict

Methods

lift :: Monad m => m a -> WriterT w m a #

MonadTrans (ContT r) 
Instance details

Defined in Control.Monad.Trans.Cont

Methods

lift :: Monad m => m a -> ContT r m a #

Monoid w => MonadTrans (RWST r w s) 
Instance details

Defined in Control.Monad.Trans.RWS.Lazy

Methods

lift :: Monad m => m a -> RWST r w s m a #

Monoid w => MonadTrans (RWST r w s) 
Instance details

Defined in Control.Monad.Trans.RWS.Strict

Methods

lift :: Monad m => m a -> RWST r w s m a #

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:

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

Expand
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

Instances

Instances details
MonadIO IO

Since: base-4.9.0.0

Instance details

Defined in Control.Monad.IO.Class

Methods

liftIO :: IO a -> IO a #

MonadIO m => MonadIO (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

liftIO :: IO a -> ListT m a #

MonadIO m => MonadIO (ListT m) 
Instance details

Defined in Control.Monad.Trans.List

Methods

liftIO :: IO a -> ListT m a #

MonadIO m => MonadIO (MaybeT m) 
Instance details

Defined in Control.Monad.Trans.Maybe

Methods

liftIO :: IO a -> MaybeT m a #

(Error e, MonadIO m) => MonadIO (ErrorT e m) 
Instance details

Defined in Control.Monad.Trans.Error

Methods

liftIO :: IO a -> ErrorT e m a #

MonadIO m => MonadIO (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftIO :: IO a -> ExceptT e m a #

MonadIO m => MonadIO (IdentityT m) 
Instance details

Defined in Control.Monad.Trans.Identity

Methods

liftIO :: IO a -> IdentityT m a #

MonadIO m => MonadIO (ReaderT r m) 
Instance details

Defined in Control.Monad.Trans.Reader

Methods

liftIO :: IO a -> ReaderT r m a #

MonadIO m => MonadIO (StateT s m) 
Instance details

Defined in Control.Monad.Trans.State.Lazy

Methods

liftIO :: IO a -> StateT s m a #

MonadIO m => MonadIO (StateT s m) 
Instance details

Defined in Control.Monad.Trans.State.Strict

Methods

liftIO :: IO a -> StateT s m a #

(Monoid w, MonadIO m) => MonadIO (WriterT w m) 
Instance details

Defined in Control.Monad.Trans.Writer.Lazy

Methods

liftIO :: IO a -> WriterT w m a #

(Monoid w, MonadIO m) => MonadIO (WriterT w m) 
Instance details

Defined in Control.Monad.Trans.Writer.Strict

Methods

liftIO :: IO a -> WriterT w m a #

MonadIO m => MonadIO (ContT r m) 
Instance details

Defined in Control.Monad.Trans.Cont

Methods

liftIO :: IO a -> ContT r m a #

(Monoid w, MonadIO m) => MonadIO (RWST r w s m) 
Instance details

Defined in Control.Monad.Trans.RWS.Lazy

Methods

liftIO :: IO a -> RWST r w s m a #

(Monoid w, MonadIO m) => MonadIO (RWST r w s m) 
Instance details

Defined in Control.Monad.Trans.RWS.Strict

Methods

liftIO :: IO a -> RWST r w s m a #

class Applicative f => Alternative (f :: Type -> Type) where #

A monoid on applicative functors.

If defined, some and many should be the least solutions of the equations:

Minimal complete definition

empty, (<|>)

Methods

empty :: f a #

The identity of <|>

(<|>) :: f a -> f a -> f a infixl 3 #

An associative binary operation

some :: f a -> f [a] #

One or more.

many :: f a -> f [a] #

Zero or more.

Instances

Instances details
Alternative ZipList

Since: base-4.11.0.0

Instance details

Defined in Control.Applicative

Methods

empty :: ZipList a #

(<|>) :: ZipList a -> ZipList a -> ZipList a #

some :: ZipList a -> ZipList [a] #

many :: ZipList a -> ZipList [a] #

Alternative P

Since: base-4.5.0.0

Instance details

Defined in Text.ParserCombinators.ReadP

Methods

empty :: P a #

(<|>) :: P a -> P a -> P a #

some :: P a -> P [a] #

many :: P a -> P [a] #

Alternative ReadP

Since: base-4.6.0.0

Instance details

Defined in Text.ParserCombinators.ReadP

Methods

empty :: ReadP a #

(<|>) :: ReadP a -> ReadP a -> ReadP a #

some :: ReadP a -> ReadP [a] #

many :: ReadP a -> ReadP [a] #

Alternative IO

Since: base-4.9.0.0

Instance details

Defined in GHC.Base

Methods

empty :: IO a #

(<|>) :: IO a -> IO a -> IO a #

some :: IO a -> IO [a] #

many :: IO a -> IO [a] #

Alternative Maybe

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

empty :: Maybe a #

(<|>) :: Maybe a -> Maybe a -> Maybe a #

some :: Maybe a -> Maybe [a] #

many :: Maybe a -> Maybe [a] #

Alternative []

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

empty :: [a] #

(<|>) :: [a] -> [a] -> [a] #

some :: [a] -> [[a]] #

many :: [a] -> [[a]] #

MonadPlus m => Alternative (WrappedMonad m)

Since: base-2.1

Instance details

Defined in Control.Applicative

Methods

empty :: WrappedMonad m a #

(<|>) :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a #

some :: WrappedMonad m a -> WrappedMonad m [a] #

many :: WrappedMonad m a -> WrappedMonad m [a] #

ArrowPlus a => Alternative (ArrowMonad a)

Since: base-4.6.0.0

Instance details

Defined in Control.Arrow

Methods

empty :: ArrowMonad a a0 #

(<|>) :: ArrowMonad a a0 -> ArrowMonad a a0 -> ArrowMonad a a0 #

some :: ArrowMonad a a0 -> ArrowMonad a [a0] #

many :: ArrowMonad a a0 -> ArrowMonad a [a0] #

Alternative (Proxy :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in Data.Proxy

Methods

empty :: Proxy a #

(<|>) :: Proxy a -> Proxy a -> Proxy a #

some :: Proxy a -> Proxy [a] #

many :: Proxy a -> Proxy [a] #

Alternative (U1 :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

empty :: U1 a #

(<|>) :: U1 a -> U1 a -> U1 a #

some :: U1 a -> U1 [a] #

many :: U1 a -> U1 [a] #

Monad m => Alternative (ListT m) Source # 
Instance details

Defined in List.Transformer

Methods

empty :: ListT m a #

(<|>) :: ListT m a -> ListT m a -> ListT m a #

some :: ListT m a -> ListT m [a] #

many :: ListT m a -> ListT m [a] #

Monad m => Alternative (ZipListT m) Source # 
Instance details

Defined in List.Transformer

Methods

empty :: ZipListT m a #

(<|>) :: ZipListT m a -> ZipListT m a -> ZipListT m a #

some :: ZipListT m a -> ZipListT m [a] #

many :: ZipListT m a -> ZipListT m [a] #

Applicative m => Alternative (ListT m) 
Instance details

Defined in Control.Monad.Trans.List

Methods

empty :: ListT m a #

(<|>) :: ListT m a -> ListT m a -> ListT m a #

some :: ListT m a -> ListT m [a] #

many :: ListT m a -> ListT m [a] #

(Functor m, Monad m) => Alternative (MaybeT m) 
Instance details

Defined in Control.Monad.Trans.Maybe

Methods

empty :: MaybeT m a #

(<|>) :: MaybeT m a -> MaybeT m a -> MaybeT m a #

some :: MaybeT m a -> MaybeT m [a] #

many :: MaybeT m a -> MaybeT m [a] #

(ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b)

Since: base-2.1

Instance details

Defined in Control.Applicative

Methods

empty :: WrappedArrow a b a0 #

(<|>) :: WrappedArrow a b a0 -> WrappedArrow a b a0 -> WrappedArrow a b a0 #

some :: WrappedArrow a b a0 -> WrappedArrow a b [a0] #

many :: WrappedArrow a b a0 -> WrappedArrow a b [a0] #

Alternative m => Alternative (Kleisli m a)

Since: base-4.14.0.0

Instance details

Defined in Control.Arrow

Methods

empty :: Kleisli m a a0 #

(<|>) :: Kleisli m a a0 -> Kleisli m a a0 -> Kleisli m a a0 #

some :: Kleisli m a a0 -> Kleisli m a [a0] #

many :: Kleisli m a a0 -> Kleisli m a [a0] #

Alternative f => Alternative (Ap f)

Since: base-4.12.0.0

Instance details

Defined in Data.Monoid

Methods

empty :: Ap f a #

(<|>) :: Ap f a -> Ap f a -> Ap f a #

some :: Ap f a -> Ap f [a] #

many :: Ap f a -> Ap f [a] #

Alternative f => Alternative (Alt f)

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

Methods

empty :: Alt f a #

(<|>) :: Alt f a -> Alt f a -> Alt f a #

some :: Alt f a -> Alt f [a] #

many :: Alt f a -> Alt f [a] #

Alternative f => Alternative (Rec1 f)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

empty :: Rec1 f a #

(<|>) :: Rec1 f a -> Rec1 f a -> Rec1 f a #

some :: Rec1 f a -> Rec1 f [a] #

many :: Rec1 f a -> Rec1 f [a] #

(Functor m, Monad m, Error e) => Alternative (ErrorT e m) 
Instance details

Defined in Control.Monad.Trans.Error

Methods

empty :: ErrorT e m a #

(<|>) :: ErrorT e m a -> ErrorT e m a -> ErrorT e m a #

some :: ErrorT e m a -> ErrorT e m [a] #

many :: ErrorT e m a -> ErrorT e m [a] #

(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

empty :: ExceptT e m a #

(<|>) :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a #

some :: ExceptT e m a -> ExceptT e m [a] #

many :: ExceptT e m a -> ExceptT e m [a] #

Alternative m => Alternative (IdentityT m) 
Instance details

Defined in Control.Monad.Trans.Identity

Methods

empty :: IdentityT m a #

(<|>) :: IdentityT m a -> IdentityT m a -> IdentityT m a #

some :: IdentityT m a -> IdentityT m [a] #

many :: IdentityT m a -> IdentityT m [a] #

Alternative m => Alternative (ReaderT r m) 
Instance details

Defined in Control.Monad.Trans.Reader

Methods

empty :: ReaderT r m a #

(<|>) :: ReaderT r m a -> ReaderT r m a -> ReaderT r m a #

some :: ReaderT r m a -> ReaderT r m [a] #

many :: ReaderT r m a -> ReaderT r m [a] #

(Functor m, MonadPlus m) => Alternative (StateT s m) 
Instance details

Defined in Control.Monad.Trans.State.Lazy

Methods

empty :: StateT s m a #

(<|>) :: StateT s m a -> StateT s m a -> StateT s m a #

some :: StateT s m a -> StateT s m [a] #

many :: StateT s m a -> StateT s m [a] #

(Functor m, MonadPlus m) => Alternative (StateT s m) 
Instance details

Defined in Control.Monad.Trans.State.Strict

Methods

empty :: StateT s m a #

(<|>) :: StateT s m a -> StateT s m a -> StateT s m a #

some :: StateT s m a -> StateT s m [a] #

many :: StateT s m a -> StateT s m [a] #

(Monoid w, Alternative m) => Alternative (WriterT w m) 
Instance details

Defined in Control.Monad.Trans.Writer.Lazy

Methods

empty :: WriterT w m a #

(<|>) :: WriterT w m a -> WriterT w m a -> WriterT w m a #

some :: WriterT w m a -> WriterT w m [a] #

many :: WriterT w m a -> WriterT w m [a] #

(Monoid w, Alternative m) => Alternative (WriterT w m) 
Instance details

Defined in Control.Monad.Trans.Writer.Strict

Methods

empty :: WriterT w m a #

(<|>) :: WriterT w m a -> WriterT w m a -> WriterT w m a #

some :: WriterT w m a -> WriterT w m [a] #

many :: WriterT w m a -> WriterT w m [a] #

(Alternative f, Alternative g) => Alternative (f :*: g)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

empty :: (f :*: g) a #

(<|>) :: (f :*: g) a -> (f :*: g) a -> (f :*: g) a #

some :: (f :*: g) a -> (f :*: g) [a] #

many :: (f :*: g) a -> (f :*: g) [a] #

(Alternative f, Applicative g) => Alternative (f :.: g)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

empty :: (f :.: g) a #

(<|>) :: (f :.: g) a -> (f :.: g) a -> (f :.: g) a #

some :: (f :.: g) a -> (f :.: g) [a] #

many :: (f :.: g) a -> (f :.: g) [a] #

Alternative f => Alternative (M1 i c f)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

empty :: M1 i c f a #

(<|>) :: M1 i c f a -> M1 i c f a -> M1 i c f a #

some :: M1 i c f a -> M1 i c f [a] #

many :: M1 i c f a -> M1 i c f [a] #

(Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) 
Instance details

Defined in Control.Monad.Trans.RWS.Lazy

Methods

empty :: RWST r w s m a #

(<|>) :: RWST r w s m a -> RWST r w s m a -> RWST r w s m a #

some :: RWST r w s m a -> RWST r w s m [a] #

many :: RWST r w s m a -> RWST r w s m [a] #

(Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) 
Instance details

Defined in Control.Monad.Trans.RWS.Strict

Methods

empty :: RWST r w s m a #

(<|>) :: RWST r w s m a -> RWST r w s m a -> RWST r w s m a #

some :: RWST r w s m a -> RWST r w s m [a] #

many :: RWST r w s m a -> RWST r w s m [a] #

class MFunctor (t :: (Type -> Type) -> k -> Type) where #

A functor in the category of monads, using hoist as the analog of fmap:

hoist (f . g) = hoist f . hoist g

hoist id = id

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> t m b -> t n b #

Lift a monad morphism from m to n into a monad morphism from (t m) to (t n)

The first argument to hoist must be a monad morphism, even though the type system does not enforce this

Instances

Instances details
MFunctor ListT Source # 
Instance details

Defined in List.Transformer

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> ListT m b -> ListT n b #

MFunctor Step Source # 
Instance details

Defined in List.Transformer

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Step m b -> Step n b #

MFunctor Lift 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Lift m b -> Lift n b #

MFunctor MaybeT 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> MaybeT m b -> MaybeT n b #

MFunctor (Backwards :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Backwards m b -> Backwards n b #

MFunctor (ExceptT e :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> ExceptT e m b -> ExceptT e n b #

MFunctor (IdentityT :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> IdentityT m b -> IdentityT n b #

MFunctor (ReaderT r :: (Type -> Type) -> Type -> TYPE LiftedRep) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> ReaderT r m b -> ReaderT r n b #

MFunctor (StateT s :: (Type -> Type) -> Type -> TYPE LiftedRep) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> StateT s m b -> StateT s n b #

MFunctor (StateT s :: (Type -> Type) -> Type -> TYPE LiftedRep) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> StateT s m b -> StateT s n b #

MFunctor (WriterT w :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> WriterT w m b -> WriterT w n b #

MFunctor (WriterT w :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> WriterT w m b -> WriterT w n b #

MFunctor (Product f :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Product f m b -> Product f n b #

Functor f => MFunctor (Compose f :: (Type -> Type) -> Type -> Type) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Compose f m b -> Compose f n b #

MFunctor (RWST r w s :: (Type -> Type) -> Type -> TYPE LiftedRep) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> RWST r w s m b -> RWST r w s n b #

MFunctor (RWST r w s :: (Type -> Type) -> Type -> TYPE LiftedRep) 
Instance details

Defined in Control.Monad.Morph

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> RWST r w s m b -> RWST r w s n b #