Module

Control.Bind

Package
prelude
Repository
purerl/purescript-prelude

#BindSource

class Bind :: (Type -> Type) -> Constraintclass (Apply m) <= Bind m  where

The Bind type class extends the Apply type class with a "bind" operation (>>=) which composes computations in sequence, using the return value of one computation to determine the next computation.

The >>= operator can also be expressed using do notation, as follows:

x >>= f = do y <- x
f y

where the function argument of f is given the name y.

Instances must satisfy the following laws in addition to the Apply laws:

• Associativity: (x >>= f) >>= g = x >>= (\k -> f k >>= g)
• Apply Superclass: apply f x = f >>= \f’ -> map f’ x

Associativity tells us that we can regroup operations which use do notation so that we can unambiguously write, for example:

do x <- m1
y <- m2 x
m3 x y

Members

• bind :: forall a b. m a -> (a -> m b) -> m b

Instances

• Bind (Function r)
• Bind Array

The bind/>>= function for Array works by applying a function to each element in the array, and flattening the results into a single, new array.

Array's bind/>>= works like a nested for loop. Each bind adds another level of nesting in the loop. For example:

foo :: Array String
foo =
["a", "b"] >>= \eachElementInArray1 ->
["c", "d"] >>= \eachElementInArray2
pure (eachElementInArray1 <> eachElementInArray2)

-- In other words...
foo
-- ... is the same as...
[ ("a" <> "c"), ("a" <> "d"), ("b" <> "c"), ("b" <> "d") ]
-- which simplifies to...
[ "ac", "ad", "bc", "bd" ]

• Bind Proxy

#(>>=)Source

Operator alias for Control.Bind.bind (left-associative / precedence 1)

#bindFlippedSource

bindFlipped :: forall m a b. Bind m => (a -> m b) -> m a -> m b

bindFlipped is bind with its arguments reversed. For example:

print =<< random

#(=<<)Source

Operator alias for Control.Bind.bindFlipped (right-associative / precedence 1)

A class for types whose values can safely be discarded in a do notation block.

An example is the Unit type, since there is only one possible value which can be returned.

Members

• discard :: forall f b. Bind f => f a -> (a -> f b) -> f b

#joinSource

join :: forall a m. Bind m => m (m a) -> m a

Collapse two applications of a monadic type constructor into one.

#composeKleisliSource

composeKleisli :: forall a b c m. Bind m => (a -> m b) -> (b -> m c) -> a -> m c

Forwards Kleisli composition.

For example:

third = tail >=> tail >=> head

#(>=>)Source

Operator alias for Control.Bind.composeKleisli (right-associative / precedence 1)

#composeKleisliFlippedSource

composeKleisliFlipped :: forall a b c m. Bind m => (b -> m c) -> (a -> m b) -> a -> m c

Backwards Kleisli composition.

#(<=<)Source

Operator alias for Control.Bind.composeKleisliFlipped (right-associative / precedence 1)

#ifMSource

ifM :: forall a m. Bind m => m Boolean -> m a -> m a -> m a

Execute a monadic action if a condition holds.

For example:

main = ifM ((< 0.5) <\$> random)
(trace "Tails")

Re-exports from Control.Applicative

#ApplicativeSource

class Applicative :: (Type -> Type) -> Constraintclass (Apply f) <= Applicative f  where

The Applicative type class extends the Apply type class with a pure function, which can be used to create values of type f a from values of type a.

Where Apply provides the ability to lift functions of two or more arguments to functions whose arguments are wrapped using f, and Functor provides the ability to lift functions of one argument, pure can be seen as the function which lifts functions of zero arguments. That is, Applicative functors support a lifting operation for any number of function arguments.

Instances must satisfy the following laws in addition to the Apply laws:

• Identity: (pure identity) <*> v = v
• Composition: pure (<<<) <*> f <*> g <*> h = f <*> (g <*> h)
• Homomorphism: (pure f) <*> (pure x) = pure (f x)
• Interchange: u <*> (pure y) = (pure (_ \$ y)) <*> u

Members

• pure :: forall a. a -> f a

#whenSource

when :: forall m. Applicative m => Boolean -> m Unit -> m Unit

Perform an applicative action when a condition is true.

#unlessSource

unless :: forall m. Applicative m => Boolean -> m Unit -> m Unit

Perform an applicative action unless a condition is true.

#liftA1Source

liftA1 :: forall f a b. Applicative f => (a -> b) -> f a -> f b

liftA1 provides a default implementation of (<\$>) for any Applicative functor, without using (<\$>) as provided by the Functor-Applicative superclass relationship.

liftA1 can therefore be used to write Functor instances as follows:

instance functorF :: Functor F where
map = liftA1

Re-exports from Control.Apply

#ApplySource

class Apply :: (Type -> Type) -> Constraintclass (Functor f) <= Apply f  where

The Apply class provides the (<*>) which is used to apply a function to an argument under a type constructor.

Apply can be used to lift functions of two or more arguments to work on values wrapped with the type constructor f. It might also be understood in terms of the lift2 function:

lift2 :: forall f a b c. Apply f => (a -> b -> c) -> f a -> f b -> f c
lift2 f a b = f <\$> a <*> b

(<*>) is recovered from lift2 as lift2 (\$). That is, (<*>) lifts the function application operator (\$) to arguments wrapped with the type constructor f.

Put differently...

foo =
functionTakingNArguments <\$> computationProducingArg1
<*> computationProducingArg2
<*> ...
<*> computationProducingArgN

Instances must satisfy the following law in addition to the Functor laws:

• Associative composition: (<<<) <\$> f <*> g <*> h = f <*> (g <*> h)

Formally, Apply represents a strong lax semi-monoidal endofunctor.

Members

• apply :: forall a b. f (a -> b) -> f a -> f b

#(<*>)Source

Operator alias for Control.Apply.apply (left-associative / precedence 4)

#(<*)Source

Operator alias for Control.Apply.applyFirst (left-associative / precedence 4)

#(*>)Source

Operator alias for Control.Apply.applySecond (left-associative / precedence 4)

Re-exports from Data.Functor

#FunctorSource

class Functor :: (Type -> Type) -> Constraintclass Functor f  where

A Functor is a type constructor which supports a mapping operation map.

map can be used to turn functions a -> b into functions f a -> f b whose argument and return types use the type constructor f to represent some computational context.

Instances must satisfy the following laws:

• Identity: map identity = identity
• Composition: map (f <<< g) = map f <<< map g

Members

• map :: forall a b. (a -> b) -> f a -> f b

#voidSource

void :: forall f a. Functor f => f a -> f Unit

The void function is used to ignore the type wrapped by a Functor, replacing it with Unit and keeping only the type information provided by the type constructor itself.

void is often useful when using do notation to change the return type of a monadic computation:

main = forE 1 10 \n -> void do
print n
print (n * n)

#(<\$>)Source

Operator alias for Data.Functor.map (left-associative / precedence 4)

#(<\$)Source

Operator alias for Data.Functor.voidRight (left-associative / precedence 4)

#(<#>)Source

Operator alias for Data.Functor.mapFlipped (left-associative / precedence 1)

#(\$>)Source

Operator alias for Data.Functor.voidLeft (left-associative / precedence 4)