Module

Contract.Prelude

A custom Prelude that re-exports Purescript's prelude and further expands.

#mconcat

mconcat :: forall (f :: Type -> Type) (m :: Type). Foldable f => Monoid m => f m -> m

Re-exports from Ctl.Internal.Helpers

#liftMWith

liftMWith :: forall (e :: Type) (m :: Type -> Type) (a :: Type) (b :: Type). MonadError e m => e -> (a -> b) -> Maybe a -> m b

Given an error and a Maybe value, lift the context via liftEither with a handler on Right.

#liftM

liftM :: forall (e :: Type) (m :: Type -> Type) (a :: Type). MonadThrow e m => e -> Maybe a -> m a

Given an error and a Maybe value, lift the context via liftEither.

#liftEither

liftEither :: forall (a :: Type) (e :: Type) (m :: Type -> Type). MonadThrow e m => Either e a -> m a

#fromRightEff

fromRightEff :: forall (a :: Type) (e :: Type). Show e => Either e a -> Effect a

#fromJustEff

fromJustEff :: forall (a :: Type). String -> Maybe a -> Effect a

Throws provided error on Nothing

#filterMapWithKeyM

filterMapWithKeyM :: forall (m :: Type -> Type) (k :: Type) (v :: Type). Ord k => Monad m => (k -> v -> m Boolean) -> Map k v -> m (Map k v)

Filters a map on a Monadic context over a lifted predicate on both the map's key and value

#filterMapM

filterMapM :: forall (m :: Type -> Type) (k :: Type) (v :: Type). Ord k => Monad m => (v -> m Boolean) -> Map k v -> m (Map k v)

Filters a map on a Monadic context over a lifted predicate on the map's value

#appendLastMaybe

appendLastMaybe :: forall (a :: Type). Maybe a -> Maybe a -> Maybe a

Combine two Maybes taking the Last Maybe

#appendFirstMaybe

appendFirstMaybe :: forall (a :: Type). Maybe a -> Maybe a -> Maybe a

Combine two Maybes taking the First Maybe

#(<\>)

Operator alias for Ctl.Internal.Helpers.appendFirstMaybe (right-associative / precedence 5)

#(<<>>)

Operator alias for Ctl.Internal.Helpers.maybeArrayMerge (right-associative / precedence 5)

#(</>)

Operator alias for Ctl.Internal.Helpers.appendLastMaybe (right-associative / precedence 5)

Re-exports from Data.Either

#Either

data Either a b

The Either type is used to represent a choice between two types of value.

A common use case for Either is error handling, where Left is used to carry an error value and Right is used to carry a success value.

Constructors

Instances

  • Functor (Either a)

    The Functor instance allows functions to transform the contents of a Right with the <$> operator:

    f <$> Right x == Right (f x)
    

    Left values are untouched:

    f <$> Left y == Left y
    
  • Generic (Either a b) _
  • Invariant (Either a)
  • Apply (Either e)

    The Apply instance allows functions contained within a Right to transform a value contained within a Right using the (<*>) operator:

    Right f <*> Right x == Right (f x)
    

    Left values are left untouched:

    Left f <*> Right x == Left f
    Right f <*> Left y == Left y
    

    Combining Functor's <$> with Apply's <*> can be used to transform a pure function to take Either-typed arguments so f :: a -> b -> c becomes f :: Either l a -> Either l b -> Either l c:

    f <$> Right x <*> Right y == Right (f x y)
    

    The Left-preserving behaviour of both operators means the result of an expression like the above but where any one of the values is Left means the whole result becomes Left also, taking the first Left value found:

    f <$> Left x <*> Right y == Left x
    f <$> Right x <*> Left y == Left y
    f <$> Left x <*> Left y == Left x
    
  • Applicative (Either e)

    The Applicative instance enables lifting of values into Either with the pure function:

    pure x :: Either _ _ == Right x
    

    Combining Functor's <$> with Apply's <*> and Applicative's pure can be used to pass a mixture of Either and non-Either typed values to a function that does not usually expect them, by using pure for any value that is not already Either typed:

    f <$> Right x <*> pure y == Right (f x y)
    

    Even though pure = Right it is recommended to use pure in situations like this as it allows the choice of Applicative to be changed later without having to go through and replace Right with a new constructor.

  • Alt (Either e)

    The Alt instance allows for a choice to be made between two Either values with the <|> operator, where the first Right encountered is taken.

    Right x <|> Right y == Right x
    Left x <|> Right y == Right y
    Left x <|> Left y == Left y
    
  • Bind (Either e)

    The Bind instance allows sequencing of Either values and functions that return an Either by using the >>= operator:

    Left x >>= f = Left x
    Right x >>= f = f x
    

    Either's "do notation" can be understood to work like this:

    x :: forall e a. Either e a
    x = --
    
    y :: forall e b. Either e b
    y = --
    
    foo :: forall e a. (a -> b -> c) -> Either e c
    foo f = do
      x' <- x
      y' <- y
      pure (f x' y')
    

    ...which is equivalent to...

    x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
    

    ...and is the same as writing...

    foo :: forall e a. (a -> b -> c) -> Either e c
    foo f = case x of
      Left e ->
        Left e
      Right x -> case y of
        Left e ->
          Left e
        Right y ->
          Right (f x y)
    
  • Monad (Either e)

    The Monad instance guarantees that there are both Applicative and Bind instances for Either.

  • Extend (Either e)

    The Extend instance allows sequencing of Either values and functions that accept an Either and return a non-Either result using the <<= operator.

    f <<= Left x = Left x
    f <<= Right x = Right (f (Right x))
    
  • (Show a, Show b) => Show (Either a b)

    The Show instance allows Either values to be rendered as a string with show whenever there is an Show instance for both type the Either can contain.

  • (Eq a, Eq b) => Eq (Either a b)

    The Eq instance allows Either values to be checked for equality with == and inequality with /= whenever there is an Eq instance for both types the Either can contain.

  • (Eq a) => Eq1 (Either a)
  • (Ord a, Ord b) => Ord (Either a b)

    The Ord instance allows Either values to be compared with compare, >, >=, < and <= whenever there is an Ord instance for both types the Either can contain.

    Any Left value is considered to be less than a Right value.

  • (Ord a) => Ord1 (Either a)
  • (Bounded a, Bounded b) => Bounded (Either a b)
  • (Semigroup b) => Semigroup (Either a b)

#note'

note' :: forall a b. (Unit -> a) -> Maybe b -> Either a b

Similar to note, but for use in cases where the default value may be expensive to compute.

note' (\_ -> "default") Nothing = Left "default"
note' (\_ -> "default") (Just 1) = Right 1

#note

note :: forall a b. a -> Maybe b -> Either a b

Takes a default and a Maybe value, if the value is a Just, turn it into a Right, if the value is a Nothing use the provided default as a Left

note "default" Nothing = Left "default"
note "default" (Just 1) = Right 1

#isRight

isRight :: forall a b. Either a b -> Boolean

Returns true when the Either value was constructed with Right.

#isLeft

isLeft :: forall a b. Either a b -> Boolean

Returns true when the Either value was constructed with Left.

#hush

hush :: forall a b. Either a b -> Maybe b

Turns an Either into a Maybe, by throwing potential Left values away and converting them into Nothing. Right values get turned into Justs.

hush (Left "ParseError") = Nothing
hush (Right 42) = Just 42

#fromRight'

fromRight' :: forall a b. (Unit -> b) -> Either a b -> b

Similar to fromRight but for use in cases where the default value may be expensive to compute. As PureScript is not lazy, the standard fromRight has to evaluate the default value before returning the result, whereas here the value is only computed when the Either is known to be Left.

#fromRight

fromRight :: forall a b. b -> Either a b -> b

A function that extracts the value from the Right data constructor. The first argument is a default value, which will be returned in the case where a Left is passed to fromRight.

#fromLeft'

fromLeft' :: forall a b. (Unit -> a) -> Either a b -> a

Similar to fromLeft but for use in cases where the default value may be expensive to compute. As PureScript is not lazy, the standard fromLeft has to evaluate the default value before returning the result, whereas here the value is only computed when the Either is known to be Right.

#fromLeft

fromLeft :: forall a b. a -> Either a b -> a

A function that extracts the value from the Left data constructor. The first argument is a default value, which will be returned in the case where a Right is passed to fromLeft.

#either

either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c

Takes two functions and an Either value, if the value is a Left the inner value is applied to the first function, if the value is a Right the inner value is applied to the second function.

either f g (Left x) == f x
either f g (Right y) == g y

#choose

choose :: forall m a b. Alt m => m a -> m b -> m (Either a b)

Combine two alternatives.

Re-exports from Data.Enum

#Cardinality

newtype Cardinality :: forall k. k -> Typenewtype Cardinality a

A type for the size of finite enumerations.

Constructors

Instances

#BoundedEnum

class (Bounded a, Enum a) <= BoundedEnum a  where

Type class for finite enumerations.

This should not be considered a part of a numeric hierarchy, as in Haskell. Rather, this is a type class for small, ordered sum types with statically-determined cardinality and the ability to easily compute successor and predecessor elements like DayOfWeek.

Laws:

  • succ bottom >>= succ >>= succ ... succ [cardinality - 1 times] == top
  • pred top >>= pred >>= pred ... pred [cardinality - 1 times] == bottom
  • forall a > bottom: pred a >>= succ == Just a
  • forall a < top: succ a >>= pred == Just a
  • forall a > bottom: fromEnum <$> pred a = pred (fromEnum a)
  • forall a < top: fromEnum <$> succ a = succ (fromEnum a)
  • e1 `compare` e2 == fromEnum e1 `compare` fromEnum e2
  • toEnum (fromEnum a) = Just a

Members

Instances

#Enum

class (Ord a) <= Enum a  where

Type class for enumerations.

Laws:

  • Successor: all (a < _) (succ a)
  • Predecessor: all (_ < a) (pred a)
  • Succ retracts pred: pred >=> succ >=> pred = pred
  • Pred retracts succ: succ >=> pred >=> succ = succ
  • Non-skipping succ: b <= a || any (_ <= b) (succ a)
  • Non-skipping pred: a <= b || any (b <= _) (pred a)

The retraction laws can intuitively be understood as saying that succ is the opposite of pred; if you apply succ and then pred to something, you should end up with what you started with (although of course this doesn't apply if you tried to succ the last value in an enumeration and therefore got Nothing out).

The non-skipping laws can intuitively be understood as saying that succ shouldn't skip over any elements of your type. For example, without the non-skipping laws, it would be permissible to write an Enum Int instance where succ x = Just (x+2), and similarly pred x = Just (x-2).

Members

Instances

#upFromIncluding

upFromIncluding :: forall a u. Enum a => Unfoldable1 u => a -> u a

Produces all successors of an Enum value, including the start value.

upFromIncluding bottom will return all values in an Enum.

#upFrom

upFrom :: forall a u. Enum a => Unfoldable u => a -> u a

Produces all successors of an Enum value, excluding the start value.

#toEnumWithDefaults

toEnumWithDefaults :: forall a. BoundedEnum a => a -> a -> Int -> a

Like toEnum but returns the first argument if x is less than fromEnum bottom and the second argument if x is greater than fromEnum top.

toEnumWithDefaults False True (-1) -- False
toEnumWithDefaults False True 0    -- False
toEnumWithDefaults False True 1    -- True
toEnumWithDefaults False True 2    -- True

#enumFromTo

enumFromTo :: forall a u. Enum a => Unfoldable1 u => a -> a -> u a

Returns a contiguous sequence of elements from the first value to the second value (inclusive).

enumFromTo 0 3 = [0, 1, 2, 3]
enumFromTo 'c' 'a' = ['c', 'b', 'a']

The example shows Array return values, but the result can be any type with an Unfoldable1 instance.

#enumFromThenTo

enumFromThenTo :: forall f a. Unfoldable f => Functor f => BoundedEnum a => a -> a -> a -> f a

Returns a sequence of elements from the first value, taking steps according to the difference between the first and second value, up to (but not exceeding) the third value.

enumFromThenTo 0 2 6 = [0, 2, 4, 6]
enumFromThenTo 0 3 5 = [0, 3]

Note that there is no BoundedEnum instance for integers, they're just being used here for illustrative purposes to help clarify the behaviour.

The example shows Array return values, but the result can be any type with an Unfoldable1 instance.

#downFromIncluding

downFromIncluding :: forall a u. Enum a => Unfoldable1 u => a -> u a

Produces all predecessors of an Enum value, including the start value.

downFromIncluding top will return all values in an Enum, in reverse order.

#downFrom

downFrom :: forall a u. Enum a => Unfoldable u => a -> u a

Produces all predecessors of an Enum value, excluding the start value.

#defaultToEnum

defaultToEnum :: forall a. Bounded a => Enum a => Int -> Maybe a

Provides a default implementation for toEnum.

  • Assumes fromEnum bottom = 0.
  • Cannot be used in conjuction with defaultSucc.

Runs in O(n) where n is fromEnum a.

#defaultSucc

defaultSucc :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a

Provides a default implementation for succ, given a function that maps integers to values in the Enum, and a function that maps values in the Enum back to integers. The integer mapping must agree in both directions for this to implement a law-abiding succ.

If a BoundedEnum instance exists for a, the toEnum and fromEnum functions can be used here:

succ = defaultSucc toEnum fromEnum

#defaultPred

defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a

Provides a default implementation for pred, given a function that maps integers to values in the Enum, and a function that maps values in the Enum back to integers. The integer mapping must agree in both directions for this to implement a law-abiding pred.

If a BoundedEnum instance exists for a, the toEnum and fromEnum functions can be used here:

pred = defaultPred toEnum fromEnum

#defaultFromEnum

defaultFromEnum :: forall a. Enum a => a -> Int

Provides a default implementation for fromEnum.

  • Assumes toEnum 0 = Just bottom.
  • Cannot be used in conjuction with defaultPred.

Runs in O(n) where n is fromEnum a.

#defaultCardinality

defaultCardinality :: forall a. Bounded a => Enum a => Cardinality a

Provides a default implementation for cardinality.

Runs in O(n) where n is fromEnum top

Re-exports from Data.Foldable

#Foldable

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

Foldable represents data structures which can be folded.

  • foldr folds a structure from the right
  • foldl folds a structure from the left
  • foldMap folds a structure by accumulating values in a Monoid

Default implementations are provided by the following functions:

  • foldrDefault
  • foldlDefault
  • foldMapDefaultR
  • foldMapDefaultL

Note: some combinations of the default implementations are unsafe to use together - causing a non-terminating mutually recursive cycle. These combinations are documented per function.

Members

  • foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
  • foldl :: forall a b. (b -> a -> b) -> b -> f a -> b
  • foldMap :: forall a m. Monoid m => (a -> m) -> f a -> m

Instances

#surroundMap

surroundMap :: forall f a m. Foldable f => Semigroup m => m -> (a -> m) -> f a -> m

foldMap but with each element surrounded by some fixed value.

For example:

> surroundMap "*" show []
= "*"

> surroundMap "*" show [1]
= "*1*"

> surroundMap "*" show [1, 2]
= "*1*2*"

> surroundMap "*" show [1, 2, 3]
= "*1*2*3*"

#surround

surround :: forall f m. Foldable f => Semigroup m => m -> f m -> m

fold but with each element surrounded by some fixed value.

For example:

> surround "*" []
= "*"

> surround "*" ["1"]
= "*1*"

> surround "*" ["1", "2"]
= "*1*2*"

> surround "*" ["1", "2", "3"]
= "*1*2*3*"

#sum

sum :: forall a f. Foldable f => Semiring a => f a -> a

Find the sum of the numeric values in a data structure.

#product

product :: forall a f. Foldable f => Semiring a => f a -> a

Find the product of the numeric values in a data structure.

#or

or :: forall a f. Foldable f => HeytingAlgebra a => f a -> a

The disjunction of all the values in a data structure. When specialized to Boolean, this function will test whether any of the values in a data structure is true.

#oneOfMap

oneOfMap :: forall f g a b. Foldable f => Plus g => (a -> g b) -> f a -> g b

Folds a structure into some Plus.

#oneOf

oneOf :: forall f g a. Foldable f => Plus g => f (g a) -> g a

Combines a collection of elements using the Alt operation.

#null

null :: forall a f. Foldable f => f a -> Boolean

Test whether the structure is empty. Optimized for structures that are similar to cons-lists, because there is no general way to do better.

#notElem

notElem :: forall a f. Foldable f => Eq a => a -> f a -> Boolean

Test whether a value is not an element of a data structure.

#minimumBy

minimumBy :: forall a f. Foldable f => (a -> a -> Ordering) -> f a -> Maybe a

Find the smallest element of a structure, according to a given comparison function. The comparison function should represent a total ordering (see the Ord type class laws); if it does not, the behaviour is undefined.

#minimum

minimum :: forall a f. Ord a => Foldable f => f a -> Maybe a

Find the smallest element of a structure, according to its Ord instance.

#maximumBy

maximumBy :: forall a f. Foldable f => (a -> a -> Ordering) -> f a -> Maybe a

Find the largest element of a structure, according to a given comparison function. The comparison function should represent a total ordering (see the Ord type class laws); if it does not, the behaviour is undefined.

#maximum

maximum :: forall a f. Ord a => Foldable f => f a -> Maybe a

Find the largest element of a structure, according to its Ord instance.

#lookup

lookup :: forall a b f. Foldable f => Eq a => a -> f (Tuple a b) -> Maybe b

Lookup a value in a data structure of Tuples, generalizing association lists.

#length

length :: forall a b f. Foldable f => Semiring b => f a -> b

Returns the size/length of a finite structure. Optimized for structures that are similar to cons-lists, because there is no general way to do better.

#intercalate

intercalate :: forall f m. Foldable f => Monoid m => m -> f m -> m

Fold a data structure, accumulating values in some Monoid, combining adjacent elements using the specified separator.

For example:

> intercalate ", " ["Lorem", "ipsum", "dolor"]
= "Lorem, ipsum, dolor"

> intercalate "*" ["a", "b", "c"]
= "a*b*c"

> intercalate [1] [[2, 3], [4, 5], [6, 7]]
= [2, 3, 1, 4, 5, 1, 6, 7]

#indexr

indexr :: forall a f. Foldable f => Int -> f a -> Maybe a

Try to get nth element from the right in a data structure

#indexl

indexl :: forall a f. Foldable f => Int -> f a -> Maybe a

Try to get nth element from the left in a data structure

#foldrDefault

foldrDefault :: forall f a b. Foldable f => (a -> b -> b) -> b -> f a -> b

A default implementation of foldr using foldMap.

Note: when defining a Foldable instance, this function is unsafe to use in combination with foldMapDefaultR.

#foldlDefault

foldlDefault :: forall f a b. Foldable f => (b -> a -> b) -> b -> f a -> b

A default implementation of foldl using foldMap.

Note: when defining a Foldable instance, this function is unsafe to use in combination with foldMapDefaultL.

#foldMapDefaultR

foldMapDefaultR :: forall f a m. Foldable f => Monoid m => (a -> m) -> f a -> m

A default implementation of foldMap using foldr.

Note: when defining a Foldable instance, this function is unsafe to use in combination with foldrDefault.

#foldMapDefaultL

foldMapDefaultL :: forall f a m. Foldable f => Monoid m => (a -> m) -> f a -> m

A default implementation of foldMap using foldl.

Note: when defining a Foldable instance, this function is unsafe to use in combination with foldlDefault.

#foldM

foldM :: forall f m a b. Foldable f => Monad m => (b -> a -> m b) -> b -> f a -> m b

Similar to 'foldl', but the result is encapsulated in a monad.

Note: this function is not generally stack-safe, e.g., for monads which build up thunks a la Eff.

#fold

fold :: forall f m. Foldable f => Monoid m => f m -> m

Fold a data structure, accumulating values in some Monoid.

#findMap

findMap :: forall a b f. Foldable f => (a -> Maybe b) -> f a -> Maybe b

Try to find an element in a data structure which satisfies a predicate mapping.

#find

find :: forall a f. Foldable f => (a -> Boolean) -> f a -> Maybe a

Try to find an element in a data structure which satisfies a predicate.

#elem

elem :: forall a f. Foldable f => Eq a => a -> f a -> Boolean

Test whether a value is an element of a data structure.

#any

any :: forall a b f. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b

any f is the same as or <<< map f; map a function over the structure, and then get the disjunction of the results.

#and

and :: forall a f. Foldable f => HeytingAlgebra a => f a -> a

The conjunction of all the values in a data structure. When specialized to Boolean, this function will test whether all of the values in a data structure are true.

#all

all :: forall a b f. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b

all f is the same as and <<< map f; map a function over the structure, and then get the conjunction of the results.

Re-exports from Data.Generic.Rep

#Generic

class Generic a rep | a -> rep

The Generic class asserts the existence of a type function from types to their representations using the type constructors defined in this module.

Re-exports from Data.Log.Level

#LogLevel

Re-exports from Data.Maybe

#Maybe

data Maybe a

The Maybe type is used to represent optional values and can be seen as something like a type-safe null, where Nothing is null and Just x is the non-null value x.

Constructors

Instances

  • Functor Maybe

    The Functor instance allows functions to transform the contents of a Just with the <$> operator:

    f <$> Just x == Just (f x)
    

    Nothing values are left untouched:

    f <$> Nothing == Nothing
    
  • Apply Maybe

    The Apply instance allows functions contained within a Just to transform a value contained within a Just using the apply operator:

    Just f <*> Just x == Just (f x)
    

    Nothing values are left untouched:

    Just f <*> Nothing == Nothing
    Nothing <*> Just x == Nothing
    

    Combining Functor's <$> with Apply's <*> can be used transform a pure function to take Maybe-typed arguments so f :: a -> b -> c becomes f :: Maybe a -> Maybe b -> Maybe c:

    f <$> Just x <*> Just y == Just (f x y)
    

    The Nothing-preserving behaviour of both operators means the result of an expression like the above but where any one of the values is Nothing means the whole result becomes Nothing also:

    f <$> Nothing <*> Just y == Nothing
    f <$> Just x <*> Nothing == Nothing
    f <$> Nothing <*> Nothing == Nothing
    
  • Applicative Maybe

    The Applicative instance enables lifting of values into Maybe with the pure function:

    pure x :: Maybe _ == Just x
    

    Combining Functor's <$> with Apply's <*> and Applicative's pure can be used to pass a mixture of Maybe and non-Maybe typed values to a function that does not usually expect them, by using pure for any value that is not already Maybe typed:

    f <$> Just x <*> pure y == Just (f x y)
    

    Even though pure = Just it is recommended to use pure in situations like this as it allows the choice of Applicative to be changed later without having to go through and replace Just with a new constructor.

  • Alt Maybe

    The Alt instance allows for a choice to be made between two Maybe values with the <|> operator, where the first Just encountered is taken.

    Just x <|> Just y == Just x
    Nothing <|> Just y == Just y
    Nothing <|> Nothing == Nothing
    
  • Plus Maybe

    The Plus instance provides a default Maybe value:

    empty :: Maybe _ == Nothing
    
  • Alternative Maybe

    The Alternative instance guarantees that there are both Applicative and Plus instances for Maybe.

  • Bind Maybe

    The Bind instance allows sequencing of Maybe values and functions that return a Maybe by using the >>= operator:

    Just x >>= f = f x
    Nothing >>= f = Nothing
    
  • Monad Maybe

    The Monad instance guarantees that there are both Applicative and Bind instances for Maybe. This also enables the do syntactic sugar:

    do
      x' <- x
      y' <- y
      pure (f x' y')
    

    Which is equivalent to:

    x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
    

    Which is equivalent to:

    case x of
      Nothing -> Nothing
      Just x' -> case y of
        Nothing -> Nothing
        Just y' -> Just (f x' y')
    
  • Extend Maybe

    The Extend instance allows sequencing of Maybe values and functions that accept a Maybe a and return a non-Maybe result using the <<= operator.

    f <<= Nothing = Nothing
    f <<= x = Just (f x)
    
  • Invariant Maybe
  • (Semigroup a) => Semigroup (Maybe a)

    The Semigroup instance enables use of the operator <> on Maybe values whenever there is a Semigroup instance for the type the Maybe contains. The exact behaviour of <> depends on the "inner" Semigroup instance, but generally captures the notion of appending or combining things.

    Just x <> Just y = Just (x <> y)
    Just x <> Nothing = Just x
    Nothing <> Just y = Just y
    Nothing <> Nothing = Nothing
    
  • (Semigroup a) => Monoid (Maybe a)
  • (Semiring a) => Semiring (Maybe a)
  • (Eq a) => Eq (Maybe a)

    The Eq instance allows Maybe values to be checked for equality with == and inequality with /= whenever there is an Eq instance for the type the Maybe contains.

  • Eq1 Maybe
  • (Ord a) => Ord (Maybe a)

    The Ord instance allows Maybe values to be compared with compare, >, >=, < and <= whenever there is an Ord instance for the type the Maybe contains.

    Nothing is considered to be less than any Just value.

  • Ord1 Maybe
  • (Bounded a) => Bounded (Maybe a)
  • (Show a) => Show (Maybe a)

    The Show instance allows Maybe values to be rendered as a string with show whenever there is an Show instance for the type the Maybe contains.

  • Generic (Maybe a) _

#optional

optional :: forall f a. Alt f => Applicative f => f a -> f (Maybe a)

One or none.

optional empty = pure Nothing

The behaviour of optional (pure x) depends on whether the Alt instance satisfy the left catch law (pure a <|> b = pure a).

Either e does:

optional (Right x) = Right (Just x)

But Array does not:

optional [x] = [Just x, Nothing]

#maybe'

maybe' :: forall a b. (Unit -> b) -> (a -> b) -> Maybe a -> b

Similar to maybe but for use in cases where the default value may be expensive to compute. As PureScript is not lazy, the standard maybe has to evaluate the default value before returning the result, whereas here the value is only computed when the Maybe is known to be Nothing.

maybe' (\_ -> x) f Nothing == x
maybe' (\_ -> x) f (Just y) == f y

#maybe

maybe :: forall a b. b -> (a -> b) -> Maybe a -> b

Takes a default value, a function, and a Maybe value. If the Maybe value is Nothing the default value is returned, otherwise the function is applied to the value inside the Just and the result is returned.

maybe x f Nothing == x
maybe x f (Just y) == f y

#isNothing

isNothing :: forall a. Maybe a -> Boolean

Returns true when the Maybe value is Nothing.

#isJust

isJust :: forall a. Maybe a -> Boolean

Returns true when the Maybe value was constructed with Just.

#fromMaybe'

fromMaybe' :: forall a. (Unit -> a) -> Maybe a -> a

Similar to fromMaybe but for use in cases where the default value may be expensive to compute. As PureScript is not lazy, the standard fromMaybe has to evaluate the default value before returning the result, whereas here the value is only computed when the Maybe is known to be Nothing.

fromMaybe' (\_ -> x) Nothing == x
fromMaybe' (\_ -> x) (Just y) == y

#fromMaybe

fromMaybe :: forall a. a -> Maybe a -> a

Takes a default value, and a Maybe value. If the Maybe value is Nothing the default value is returned, otherwise the value inside the Just is returned.

fromMaybe x Nothing == x
fromMaybe x (Just y) == y

#fromJust

fromJust :: forall a. Partial => Maybe a -> a

A partial function that extracts the value from the Just data constructor. Passing Nothing to fromJust will throw an error at runtime.

Re-exports from Data.Newtype

#Newtype

class (Coercible t a) <= Newtype t a | t -> a

A type class for newtypes to enable convenient wrapping and unwrapping, and the use of the other functions in this module.

The compiler can derive instances of Newtype automatically:

newtype EmailAddress = EmailAddress String

derive instance newtypeEmailAddress :: Newtype EmailAddress _

Note that deriving for Newtype instances requires that the type be defined as newtype rather than data declaration (even if the data structurally fits the rules of a newtype), and the use of a wildcard for the wrapped type.

Instances

#wrap

wrap :: forall t a. Newtype t a => a -> t

#unwrap

unwrap :: forall t a. Newtype t a => t -> a

#over

over :: forall t a s b. Newtype t a => Newtype s b => (a -> t) -> (a -> b) -> t -> s

Lifts a function operate over newtypes. This can be used to lift a function to manipulate the contents of a single newtype, somewhat like map does for a Functor:

newtype Label = Label String
derive instance newtypeLabel :: Newtype Label _

toUpperLabel :: Label -> Label
toUpperLabel = over Label String.toUpper

But the result newtype is polymorphic, meaning the result can be returned as an alternative newtype:

newtype UppercaseLabel = UppercaseLabel String
derive instance newtypeUppercaseLabel :: Newtype UppercaseLabel _

toUpperLabel' :: Label -> UppercaseLabel
toUpperLabel' = over Label String.toUpper

Re-exports from Data.Show.Generic

#genericShow

genericShow :: forall a rep. Generic a rep => GenericShow rep => a -> String

A Generic implementation of the show member from the Show type class.

Re-exports from Data.Traversable

#Accum

type Accum s a = { accum :: s, value :: a }

#Traversable

class Traversable :: (Type -> Type) -> Constraintclass (Functor t, Foldable t) <= Traversable t  where

Traversable represents data structures which can be traversed, accumulating results and effects in some Applicative functor.

  • traverse runs an action for every element in a data structure, and accumulates the results.
  • sequence runs the actions contained in a data structure, and accumulates the results.
import Data.Traversable
import Data.Maybe
import Data.Int (fromNumber)

sequence [Just 1, Just 2, Just 3] == Just [1,2,3]
sequence [Nothing, Just 2, Just 3] == Nothing

traverse fromNumber [1.0, 2.0, 3.0] == Just [1,2,3]
traverse fromNumber [1.5, 2.0, 3.0] == Nothing

traverse logShow [1,2,3]
-- prints:
   1
   2
   3

traverse (\x -> [x, 0]) [1,2,3] == [[1,2,3],[1,2,0],[1,0,3],[1,0,0],[0,2,3],[0,2,0],[0,0,3],[0,0,0]]

The traverse and sequence functions should be compatible in the following sense:

  • traverse f xs = sequence (f <$> xs)
  • sequence = traverse identity

Traversable instances should also be compatible with the corresponding Foldable instances, in the following sense:

  • foldMap f = runConst <<< traverse (Const <<< f)

Default implementations are provided by the following functions:

  • traverseDefault
  • sequenceDefault

Members

Instances

#traverse_

traverse_ :: forall a b f m. Applicative m => Foldable f => (a -> m b) -> f a -> m Unit

Traverse a data structure, performing some effects encoded by an Applicative functor at each value, ignoring the final result.

For example:

traverse_ print [1, 2, 3]

#traverseDefault

traverseDefault :: forall t a b m. Traversable t => Applicative m => (a -> m b) -> t a -> m (t b)

A default implementation of traverse using sequence and map.

#sequence_

sequence_ :: forall a f m. Applicative m => Foldable f => f (m a) -> m Unit

Perform all of the effects in some data structure in the order given by the Foldable instance, ignoring the final result.

For example:

sequence_ [ trace "Hello, ", trace " world!" ]

#sequenceDefault

sequenceDefault :: forall t a m. Traversable t => Applicative m => t (m a) -> m (t a)

A default implementation of sequence using traverse.

#scanr

scanr :: forall a b f. Traversable f => (a -> b -> b) -> b -> f a -> f b

Fold a data structure from the right, keeping all intermediate results instead of only the final result. Note that the initial value does not appear in the result (unlike Haskell's Prelude.scanr).

scanr (+) 0 [1,2,3] = [6,5,3]
scanr (flip (-)) 10 [1,2,3] = [4,5,7]

#scanl

scanl :: forall a b f. Traversable f => (b -> a -> b) -> b -> f a -> f b

Fold a data structure from the left, keeping all intermediate results instead of only the final result. Note that the initial value does not appear in the result (unlike Haskell's Prelude.scanl).

scanl (+) 0  [1,2,3] = [1,3,6]
scanl (-) 10 [1,2,3] = [9,7,4]

#mapAccumR

mapAccumR :: forall a b s f. Traversable f => (s -> a -> Accum s b) -> s -> f a -> Accum s (f b)

Fold a data structure from the right, keeping all intermediate results instead of only the final result.

Unlike scanr, mapAccumR allows the type of accumulator to differ from the element type of the final data structure.

#mapAccumL

mapAccumL :: forall a b s f. Traversable f => (s -> a -> Accum s b) -> s -> f a -> Accum s (f b)

Fold a data structure from the left, keeping all intermediate results instead of only the final result.

Unlike scanl, mapAccumL allows the type of accumulator to differ from the element type of the final data structure.

#for_

for_ :: forall a b f m. Applicative m => Foldable f => f a -> (a -> m b) -> m Unit

A version of traverse_ with its arguments flipped.

This can be useful when running an action written using do notation for every element in a data structure:

For example:

for_ [1, 2, 3] \n -> do
  print n
  trace "squared is"
  print (n * n)

#for

for :: forall a b m t. Applicative m => Traversable t => t a -> (a -> m b) -> m (t b)

A version of traverse with its arguments flipped.

This can be useful when running an action written using do notation for every element in a data structure:

For example:

for [1, 2, 3] \n -> do
  print n
  return (n * n)

Re-exports from Data.Tuple

#Tuple

data Tuple a b

A simple product type for wrapping a pair of component values.

Constructors

Instances

#uncurry

uncurry :: forall a b c. (a -> b -> c) -> Tuple a b -> c

Turn a function of two arguments into a function that expects a tuple.

#swap

swap :: forall a b. Tuple a b -> Tuple b a

Exchange the first and second components of a tuple.

#snd

snd :: forall a b. Tuple a b -> b

Returns the second component of a tuple.

#fst

fst :: forall a b. Tuple a b -> a

Returns the first component of a tuple.

#curry

curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c

Turn a function that expects a tuple into a function of two arguments.

Re-exports from Data.Tuple.Nested

#Tuple9

type Tuple9 a b c d e f g h i = T10 a b c d e f g h i Unit

#Tuple8

type Tuple8 a b c d e f g h = T9 a b c d e f g h Unit

#Tuple7

type Tuple7 a b c d e f g = T8 a b c d e f g Unit

#Tuple6

type Tuple6 a b c d e f = T7 a b c d e f Unit

#Tuple5

type Tuple5 a b c d e = T6 a b c d e Unit

#Tuple4

type Tuple4 a b c d = T5 a b c d Unit

#Tuple3

type Tuple3 a b c = T4 a b c Unit

#Tuple2

type Tuple2 a b = T3 a b Unit

#Tuple10

type Tuple10 a b c d e f g h i j = T11 a b c d e f g h i j Unit

#Tuple1

type Tuple1 a = T2 a Unit

#T9

type T9 a b c d e f g h z = Tuple a (T8 b c d e f g h z)

#T8

type T8 a b c d e f g z = Tuple a (T7 b c d e f g z)

#T7

type T7 a b c d e f z = Tuple a (T6 b c d e f z)

#T6

type T6 a b c d e z = Tuple a (T5 b c d e z)

#T5

type T5 a b c d z = Tuple a (T4 b c d z)

#T4

type T4 a b c z = Tuple a (T3 b c z)

#T3

type T3 a b z = Tuple a (T2 b z)

#T2

type T2 a z = Tuple a z

#T11

type T11 a b c d e f g h i j z = Tuple a (T10 b c d e f g h i j z)

#T10

type T10 a b c d e f g h i z = Tuple a (T9 b c d e f g h i z)

#uncurry9

uncurry9 :: forall a b c d e f g h i r z. (a -> b -> c -> d -> e -> f -> g -> h -> i -> r) -> T10 a b c d e f g h i z -> r

Given a function of 9 arguments, returns a function that accepts a 9-tuple.

#uncurry8

uncurry8 :: forall a b c d e f g h r z. (a -> b -> c -> d -> e -> f -> g -> h -> r) -> T9 a b c d e f g h z -> r

Given a function of 8 arguments, returns a function that accepts an 8-tuple.

#uncurry7

uncurry7 :: forall a b c d e f g r z. (a -> b -> c -> d -> e -> f -> g -> r) -> T8 a b c d e f g z -> r

Given a function of 7 arguments, returns a function that accepts a 7-tuple.

#uncurry6

uncurry6 :: forall a b c d e f r z. (a -> b -> c -> d -> e -> f -> r) -> T7 a b c d e f z -> r

Given a function of 6 arguments, returns a function that accepts a 6-tuple.

#uncurry5

uncurry5 :: forall a b c d e r z. (a -> b -> c -> d -> e -> r) -> T6 a b c d e z -> r

Given a function of 5 arguments, returns a function that accepts a 5-tuple.

#uncurry4

uncurry4 :: forall a b c d r z. (a -> b -> c -> d -> r) -> T5 a b c d z -> r

Given a function of 4 arguments, returns a function that accepts a 4-tuple.

#uncurry3

uncurry3 :: forall a b c r z. (a -> b -> c -> r) -> T4 a b c z -> r

Given a function of 3 arguments, returns a function that accepts a 3-tuple.

#uncurry2

uncurry2 :: forall a b r z. (a -> b -> r) -> T3 a b z -> r

Given a function of 2 arguments, returns a function that accepts a 2-tuple.

#uncurry10

uncurry10 :: forall a b c d e f g h i j r z. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> r) -> T11 a b c d e f g h i j z -> r

Given a function of 10 arguments, returns a function that accepts a 10-tuple.

#uncurry1

uncurry1 :: forall a r z. (a -> r) -> T2 a z -> r

Given a function of 1 argument, returns a function that accepts a singleton tuple.

#tuple9

tuple9 :: forall a b c d e f g h i. a -> b -> c -> d -> e -> f -> g -> h -> i -> Tuple9 a b c d e f g h i

Given 9 values, creates a nested 9-tuple.

#tuple8

tuple8 :: forall a b c d e f g h. a -> b -> c -> d -> e -> f -> g -> h -> Tuple8 a b c d e f g h

Given 8 values, creates a nested 8-tuple.

#tuple7

tuple7 :: forall a b c d e f g. a -> b -> c -> d -> e -> f -> g -> Tuple7 a b c d e f g

Given 7 values, creates a nested 7-tuple.

#tuple6

tuple6 :: forall a b c d e f. a -> b -> c -> d -> e -> f -> Tuple6 a b c d e f

Given 6 values, creates a nested 6-tuple.

#tuple5

tuple5 :: forall a b c d e. a -> b -> c -> d -> e -> Tuple5 a b c d e

Given 5 values, creates a nested 5-tuple.

#tuple4

tuple4 :: forall a b c d. a -> b -> c -> d -> Tuple4 a b c d

Given 4 values, creates a nested 4-tuple.

#tuple3

tuple3 :: forall a b c. a -> b -> c -> Tuple3 a b c

Given 3 values, creates a nested 3-tuple.

#tuple2

tuple2 :: forall a b. a -> b -> Tuple2 a b

Given 2 values, creates a 2-tuple.

#tuple10

tuple10 :: forall a b c d e f g h i j. a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> Tuple10 a b c d e f g h i j

Given 10 values, creates a nested 10-tuple.

#tuple1

tuple1 :: forall a. a -> Tuple1 a

Creates a singleton tuple.

#over9

over9 :: forall a b c d e f g h i r z. (i -> r) -> T10 a b c d e f g h i z -> T10 a b c d e f g h r z

Given at least a 9-tuple, modifies the ninth value.

#over8

over8 :: forall a b c d e f g h r z. (h -> r) -> T9 a b c d e f g h z -> T9 a b c d e f g r z

Given at least an 8-tuple, modifies the eighth value.

#over7

over7 :: forall a b c d e f g r z. (g -> r) -> T8 a b c d e f g z -> T8 a b c d e f r z

Given at least a 7-tuple, modifies the seventh value.

#over6

over6 :: forall a b c d e f r z. (f -> r) -> T7 a b c d e f z -> T7 a b c d e r z

Given at least a 6-tuple, modifies the sixth value.

#over5

over5 :: forall a b c d e r z. (e -> r) -> T6 a b c d e z -> T6 a b c d r z

Given at least a 5-tuple, modifies the fifth value.

#over4

over4 :: forall a b c d r z. (d -> r) -> T5 a b c d z -> T5 a b c r z

Given at least a 4-tuple, modifies the fourth value.

#over3

over3 :: forall a b c r z. (c -> r) -> T4 a b c z -> T4 a b r z

Given at least a 3-tuple, modifies the third value.

#over2

over2 :: forall a b r z. (b -> r) -> T3 a b z -> T3 a r z

Given at least a 2-tuple, modifies the second value.

#over10

over10 :: forall a b c d e f g h i j r z. (j -> r) -> T11 a b c d e f g h i j z -> T11 a b c d e f g h i r z

Given at least a 10-tuple, modifies the tenth value.

#over1

over1 :: forall a r z. (a -> r) -> T2 a z -> T2 r z

Given at least a singleton tuple, modifies the first value.

#get9

get9 :: forall a b c d e f g h i z. T10 a b c d e f g h i z -> i

Given at least a 9-tuple, gets the ninth value.

#get8

get8 :: forall a b c d e f g h z. T9 a b c d e f g h z -> h

Given at least an 8-tuple, gets the eigth value.

#get7

get7 :: forall a b c d e f g z. T8 a b c d e f g z -> g

Given at least a 7-tuple, gets the seventh value.

#get6

get6 :: forall a b c d e f z. T7 a b c d e f z -> f

Given at least a 6-tuple, gets the sixth value.

#get5

get5 :: forall a b c d e z. T6 a b c d e z -> e

Given at least a 5-tuple, gets the fifth value.

#get4

get4 :: forall a b c d z. T5 a b c d z -> d

Given at least a 4-tuple, gets the fourth value.

#get3

get3 :: forall a b c z. T4 a b c z -> c

Given at least a 3-tuple, gets the third value.

#get2

get2 :: forall a b z. T3 a b z -> b

Given at least a 2-tuple, gets the second value.

#get10

get10 :: forall a b c d e f g h i j z. T11 a b c d e f g h i j z -> j

Given at least a 10-tuple, gets the tenth value.

#get1

get1 :: forall a z. T2 a z -> a

Given at least a singleton tuple, gets the first value.

#curry9

curry9 :: forall a b c d e f g h i r z. z -> (T10 a b c d e f g h i z -> r) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> r

Given a function that accepts at least a 9-tuple, returns a function of 9 arguments.

#curry8

curry8 :: forall a b c d e f g h r z. z -> (T9 a b c d e f g h z -> r) -> a -> b -> c -> d -> e -> f -> g -> h -> r

Given a function that accepts at least an 8-tuple, returns a function of 8 arguments.

#curry7

curry7 :: forall a b c d e f g r z. z -> (T8 a b c d e f g z -> r) -> a -> b -> c -> d -> e -> f -> g -> r

Given a function that accepts at least a 7-tuple, returns a function of 7 arguments.

#curry6

curry6 :: forall a b c d e f r z. z -> (T7 a b c d e f z -> r) -> a -> b -> c -> d -> e -> f -> r

Given a function that accepts at least a 6-tuple, returns a function of 6 arguments.

#curry5

curry5 :: forall a b c d e r z. z -> (T6 a b c d e z -> r) -> a -> b -> c -> d -> e -> r

Given a function that accepts at least a 5-tuple, returns a function of 5 arguments.

#curry4

curry4 :: forall a b c d r z. z -> (T5 a b c d z -> r) -> a -> b -> c -> d -> r

Given a function that accepts at least a 4-tuple, returns a function of 4 arguments.

#curry3

curry3 :: forall a b c r z. z -> (T4 a b c z -> r) -> a -> b -> c -> r

Given a function that accepts at least a 3-tuple, returns a function of 3 arguments.

#curry2

curry2 :: forall a b r z. z -> (T3 a b z -> r) -> a -> b -> r

Given a function that accepts at least a 2-tuple, returns a function of 2 arguments.

#curry10

curry10 :: forall a b c d e f g h i j r z. z -> (T11 a b c d e f g h i j z -> r) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> r

Given a function that accepts at least a 10-tuple, returns a function of 10 arguments.

#curry1

curry1 :: forall a r z. z -> (T2 a z -> r) -> a -> r

Given a function that accepts at least a singleton tuple, returns a function of 1 argument.

#(/\)

Operator alias for Data.Tuple.Tuple (right-associative / precedence 6)

Shorthand for constructing n-tuples as nested pairs. a /\ b /\ c /\ d /\ unit becomes Tuple a (Tuple b (Tuple c (Tuple d unit)))

#type (/\)

Operator alias for Data.Tuple.Tuple (right-associative / precedence 6)

Shorthand for constructing n-tuple types as nested pairs. forall a b c d. a /\ b /\ c /\ d /\ Unit becomes forall a b c d. Tuple a (Tuple b (Tuple c (Tuple d Unit)))

Re-exports from Effect

#Effect

data Effect t0

A native effect. The type parameter denotes the return type of running the effect, that is, an Effect Int is a possibly-effectful computation which eventually produces a value of the type Int when it finishes.

Instances

Re-exports from Effect.Aff

#Aff

data Aff t0

An Aff a is an asynchronous computation with effects. The computation may either error with an exception, or produce a result of type a. Aff effects are assembled from primitive Effect effects using makeAff or liftEffect.

Instances

Re-exports from Effect.Aff.Class

#liftAff

liftAff :: forall m. MonadAff m => Aff ~> m

Re-exports from Effect.Class

#liftEffect

liftEffect :: forall m a. MonadEffect m => Effect a -> m a

Re-exports from Effect.Class.Console

#log

log :: forall m. MonadEffect m => String -> m Unit

Re-exports from Prelude

#Void

newtype Void

An uninhabited data type. In other words, one can never create a runtime value of type Void because no such value exists.

Void is useful to eliminate the possibility of a value being created. For example, a value of type Either Void Boolean can never have a Left value created in PureScript.

This should not be confused with the keyword void that commonly appears in C-family languages, such as Java:

public class Foo {
  void doSomething() { System.out.println("hello world!"); }
}

In PureScript, one often uses Unit to achieve similar effects as the void of C-family languages above.

#Unit

data Unit

The Unit type has a single inhabitant, called unit. It represents values with no computational content.

Unit is often used, wrapped in a monadic type constructor, as the return type of a computation where only the effects are important.

When returning a value of type Unit from an FFI function, it is recommended to use undefined, or not return a value at all.

#Ordering

data Ordering

The Ordering data type represents the three possible outcomes of comparing two values:

LT - The first value is less than the second. GT - The first value is greater than the second. EQ - The first value is equal to the second.

Constructors

Instances

#Applicative

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

Instances

#Apply

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

Instances

#Bind

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

#BooleanAlgebra

class (HeytingAlgebra a) <= BooleanAlgebra a 

The BooleanAlgebra type class represents types that behave like boolean values.

Instances should satisfy the following laws in addition to the HeytingAlgebra law:

  • Excluded middle:
    • a || not a = tt

Instances

#Bounded

class (Ord a) <= Bounded a  where

The Bounded type class represents totally ordered types that have an upper and lower boundary.

Instances should satisfy the following law in addition to the Ord laws:

  • Bounded: bottom <= a <= top

Members

Instances

#Category

class Category :: forall k. (k -> k -> Type) -> Constraintclass (Semigroupoid a) <= Category a  where

Categorys consist of objects and composable morphisms between them, and as such are Semigroupoids, but unlike semigroupoids must have an identity element.

Instances must satisfy the following law in addition to the Semigroupoid law:

  • Identity: identity <<< p = p <<< identity = p

Members

Instances

#CommutativeRing

class (Ring a) <= CommutativeRing a 

The CommutativeRing class is for rings where multiplication is commutative.

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

  • Commutative multiplication: a * b = b * a

Instances

#Discard

class Discard a  where

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

Instances

#DivisionRing

class (Ring a) <= DivisionRing a  where

The DivisionRing class is for non-zero rings in which every non-zero element has a multiplicative inverse. Division rings are sometimes also called skew fields.

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

  • Non-zero ring: one /= zero
  • Non-zero multiplicative inverse: recip a * a = a * recip a = one for all non-zero a

The result of recip zero is left undefined; individual instances may choose how to handle this case.

If a type has both DivisionRing and CommutativeRing instances, then it is a field and should have a Field instance.

Members

Instances

#Eq

class Eq a  where

The Eq type class represents types which support decidable equality.

Eq instances should satisfy the following laws:

  • Reflexivity: x == x = true
  • Symmetry: x == y = y == x
  • Transitivity: if x == y and y == z then x == z

Note: The Number type is not an entirely law abiding member of this class due to the presence of NaN, since NaN /= NaN. Additionally, computing with Number can result in a loss of precision, so sometimes values that should be equivalent are not.

Members

Instances

#EuclideanRing

class (CommutativeRing a) <= EuclideanRing a  where

The EuclideanRing class is for commutative rings that support division. The mathematical structure this class is based on is sometimes also called a Euclidean domain.

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

  • Integral domain: one /= zero, and if a and b are both nonzero then so is their product a * b
  • Euclidean function degree:
    • Nonnegativity: For all nonzero a, degree a >= 0
    • Quotient/remainder: For all a and b, where b is nonzero, let q = a / b and r = a `mod` b; then a = q*b + r, and also either r = zero or degree r < degree b
  • Submultiplicative euclidean function:
    • For all nonzero a and b, degree a <= degree (a * b)

The behaviour of division by zero is unconstrained by these laws, meaning that individual instances are free to choose how to behave in this case. Similarly, there are no restrictions on what the result of degree zero is; it doesn't make sense to ask for degree zero in the same way that it doesn't make sense to divide by zero, so again, individual instances may choose how to handle this case.

For any EuclideanRing which is also a Field, one valid choice for degree is simply const 1. In fact, unless there's a specific reason not to, Field types should normally use this definition of degree.

The EuclideanRing Int instance is one of the most commonly used EuclideanRing instances and deserves a little more discussion. In particular, there are a few different sensible law-abiding implementations to choose from, with slightly different behaviour in the presence of negative dividends or divisors. The most common definitions are "truncating" division, where the result of a / b is rounded towards 0, and "Knuthian" or "flooring" division, where the result of a / b is rounded towards negative infinity. A slightly less common, but arguably more useful, option is "Euclidean" division, which is defined so as to ensure that a `mod` b is always nonnegative. With Euclidean division, a / b rounds towards negative infinity if the divisor is positive, and towards positive infinity if the divisor is negative. Note that all three definitions are identical if we restrict our attention to nonnegative dividends and divisors.

In versions 1.x, 2.x, and 3.x of the Prelude, the EuclideanRing Int instance used truncating division. As of 4.x, the EuclideanRing Int instance uses Euclidean division. Additional functions quot and rem are supplied if truncating division is desired.

Members

Instances

#Field

class (EuclideanRing a, DivisionRing a) <= Field a 

The Field class is for types that are (commutative) fields.

Mathematically, a field is a ring which is commutative and in which every nonzero element has a multiplicative inverse; these conditions correspond to the CommutativeRing and DivisionRing classes in PureScript respectively. However, the Field class has EuclideanRing and DivisionRing as superclasses, which seems like a stronger requirement (since CommutativeRing is a superclass of EuclideanRing). In fact, it is not stronger, since any type which has law-abiding CommutativeRing and DivisionRing instances permits exactly one law-abiding EuclideanRing instance. We use a EuclideanRing superclass here in order to ensure that a Field constraint on a function permits you to use div on that type, since div is a member of EuclideanRing.

This class has no laws or members of its own; it exists as a convenience, so a single constraint can be used when field-like behaviour is expected.

This module also defines a single Field instance for any type which has both EuclideanRing and DivisionRing instances. Any other instance would overlap with this instance, so no other Field instances should be defined in libraries. Instead, simply define EuclideanRing and DivisionRing instances, and this will permit your type to be used with a Field constraint.

Instances

#Functor

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

Instances

#HeytingAlgebra

class HeytingAlgebra a  where

The HeytingAlgebra type class represents types that are bounded lattices with an implication operator such that the following laws hold:

  • Associativity:
    • a || (b || c) = (a || b) || c
    • a && (b && c) = (a && b) && c
  • Commutativity:
    • a || b = b || a
    • a && b = b && a
  • Absorption:
    • a || (a && b) = a
    • a && (a || b) = a
  • Idempotent:
    • a || a = a
    • a && a = a
  • Identity:
    • a || ff = a
    • a && tt = a
  • Implication:
    • a `implies` a = tt
    • a && (a `implies` b) = a && b
    • b && (a `implies` b) = b
    • a `implies` (b && c) = (a `implies` b) && (a `implies` c)
  • Complemented:
    • not a = a `implies` ff

Members

Instances

#Monad

class Monad :: (Type -> Type) -> Constraintclass (Applicative m, Bind m) <= Monad m 

The Monad type class combines the operations of the Bind and Applicative type classes. Therefore, Monad instances represent type constructors which support sequential composition, and also lifting of functions of arbitrary arity.

Instances must satisfy the following laws in addition to the Applicative and Bind laws:

  • Left Identity: pure x >>= f = f x
  • Right Identity: x >>= pure = x

Instances

#Monoid

class (Semigroup m) <= Monoid m  where

A Monoid is a Semigroup with a value mempty, which is both a left and right unit for the associative operation <>:

  • Left unit: (mempty <> x) = x
  • Right unit: (x <> mempty) = x

Monoids are commonly used as the result of fold operations, where <> is used to combine individual results, and mempty gives the result of folding an empty collection of elements.

Newtypes for Monoid

Some types (e.g. Int, Boolean) can implement multiple law-abiding instances for Monoid. Let's use Int as an example

  1. <> could be + and mempty could be 0
  2. <> could be * and mempty could be 1.

To clarify these ambiguous situations, one should use the newtypes defined in Data.Monoid.<NewtypeName> modules.

In the above ambiguous situation, we could use Additive for the first situation or Multiplicative for the second one.

Members

Instances

#Ord

class (Eq a) <= Ord a  where

The Ord type class represents types which support comparisons with a total order.

Ord instances should satisfy the laws of total orderings:

  • Reflexivity: a <= a
  • Antisymmetry: if a <= b and b <= a then a == b
  • Transitivity: if a <= b and b <= c then a <= c

Note: The Number type is not an entirely law abiding member of this class due to the presence of NaN, since NaN <= NaN evaluates to false

Members

Instances

#Ring

class (Semiring a) <= Ring a  where

The Ring class is for types that support addition, multiplication, and subtraction operations.

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

  • Additive inverse: a - a = zero
  • Compatibility of sub and negate: a - b = a + (zero - b)

Members

  • sub :: a -> a -> a

Instances

#Semigroup

class Semigroup a  where

The Semigroup type class identifies an associative operation on a type.

Instances are required to satisfy the following law:

  • Associativity: (x <> y) <> z = x <> (y <> z)

One example of a Semigroup is String, with (<>) defined as string concatenation. Another example is List a, with (<>) defined as list concatenation.

Newtypes for Semigroup

There are two other ways to implement an instance for this type class regardless of which type is used. These instances can be used by wrapping the values in one of the two newtypes below:

  1. First - Use the first argument every time: append first _ = first.
  2. Last - Use the last argument every time: append _ last = last.

Members

Instances

#Semigroupoid

class Semigroupoid :: forall k. (k -> k -> Type) -> Constraintclass Semigroupoid a  where

A Semigroupoid is similar to a Category but does not require an identity element identity, just composable morphisms.

Semigroupoids must satisfy the following law:

  • Associativity: p <<< (q <<< r) = (p <<< q) <<< r

One example of a Semigroupoid is the function type constructor (->), with (<<<) defined as function composition.

Members

  • compose :: forall b c d. a c d -> a b c -> a b d

Instances

#Semiring

class Semiring a  where

The Semiring class is for types that support an addition and multiplication operation.

Instances must satisfy the following laws:

  • Commutative monoid under addition:
    • Associativity: (a + b) + c = a + (b + c)
    • Identity: zero + a = a + zero = a
    • Commutative: a + b = b + a
  • Monoid under multiplication:
    • Associativity: (a * b) * c = a * (b * c)
    • Identity: one * a = a * one = a
  • Multiplication distributes over addition:
    • Left distributivity: a * (b + c) = (a * b) + (a * c)
    • Right distributivity: (a + b) * c = (a * c) + (b * c)
  • Annihilation: zero * a = a * zero = zero

Note: The Number and Int types are not fully law abiding members of this class hierarchy due to the potential for arithmetic overflows, and in the case of Number, the presence of NaN and Infinity values. The behaviour is unspecified in these cases.

Members

Instances

#Show

class Show a  where

The Show type class represents those types which can be converted into a human-readable String representation.

While not required, it is recommended that for any expression x, the string show x be executable PureScript code which evaluates to the same value as the expression x.

Members

Instances

#whenM

whenM :: forall m. Monad m => m Boolean -> m Unit -> m Unit

Perform a monadic action when a condition is true, where the conditional value is also in a monadic context.

#when

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

Perform an applicative action when a condition is true.

#void

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)

#unlessM

unlessM :: forall m. Monad m => m Boolean -> m Unit -> m Unit

Perform a monadic action unless a condition is true, where the conditional value is also in a monadic context.

#unless

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

Perform an applicative action unless a condition is true.

#unit

unit :: Unit

unit is the sole inhabitant of the Unit type.

#otherwise

otherwise :: Boolean

An alias for true, which can be useful in guard clauses:

max x y | x >= y    = x
        | otherwise = y

#notEq

notEq :: forall a. Eq a => a -> a -> Boolean

notEq tests whether one value is not equal to another. Shorthand for not (eq x y).

#negate

negate :: forall a. Ring a => a -> a

negate x can be used as a shorthand for zero - x.

#min

min :: forall a. Ord a => a -> a -> a

Take the minimum of two values. If they are considered equal, the first argument is chosen.

#max

max :: forall a. Ord a => a -> a -> a

Take the maximum of two values. If they are considered equal, the first argument is chosen.

#liftM1

liftM1 :: forall m a b. Monad m => (a -> b) -> m a -> m b

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

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

instance functorF :: Functor F where
  map = liftM1

#liftA1

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

#lcm

lcm :: forall a. Eq a => EuclideanRing a => a -> a -> a

The least common multiple of two values.

#join

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

Collapse two applications of a monadic type constructor into one.

#ifM

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 "Heads")
         (trace "Tails")

#gcd

gcd :: forall a. Eq a => EuclideanRing a => a -> a -> a

The greatest common divisor of two values.

#flip

flip :: forall a b c. (a -> b -> c) -> b -> a -> c

Given a function that takes two arguments, applies the arguments to the function in a swapped order.

flip append "1" "2" == append "2" "1" == "21"

const 1 "two" == 1

flip const 1 "two" == const "two" 1 == "two"

#flap

flap :: forall f a b. Functor f => f (a -> b) -> a -> f b

Apply a value in a computational context to a value in no context.

Generalizes flip.

longEnough :: String -> Bool
hasSymbol :: String -> Bool
hasDigit :: String -> Bool
password :: String

validate :: String -> Array Bool
validate = flap [longEnough, hasSymbol, hasDigit]
flap (-) 3 4 == 1
threeve <$> Just 1 <@> 'a' <*> Just true == Just (threeve 1 'a' true)

#const

const :: forall a b. a -> b -> a

Returns its first argument and ignores its second.

const 1 "hello" = 1

It can also be thought of as creating a function that ignores its argument:

const 1 = \_ -> 1

#comparing

comparing :: forall a b. Ord b => (a -> b) -> (a -> a -> Ordering)

Compares two values by mapping them to a type with an Ord instance.

#clamp

clamp :: forall a. Ord a => a -> a -> a -> a

Clamp a value between a minimum and a maximum. For example:

let f = clamp 0 10
f (-5) == 0
f 5    == 5
f 15   == 10

#between

between :: forall a. Ord a => a -> a -> a -> Boolean

Test whether a value is between a minimum and a maximum (inclusive). For example:

let f = between 0 10
f 0    == true
f (-5) == false
f 5    == true
f 10   == true
f 15   == false

#ap

ap :: forall m a b. Monad m => m (a -> b) -> m a -> m b

ap provides a default implementation of (<*>) for any Monad, without using (<*>) as provided by the Apply-Monad superclass relationship.

ap can therefore be used to write Apply instances as follows:

instance applyF :: Apply F where
  apply = ap

#absurd

absurd :: forall a. Void -> a

Eliminator for the Void type. Useful for stating that some code branch is impossible because you've "acquired" a value of type Void (which you can't).

rightOnly :: forall t . Either Void t -> t
rightOnly (Left v) = absurd v
rightOnly (Right t) = t

#(||)

Operator alias for Data.HeytingAlgebra.disj (right-associative / precedence 2)

#(>>>)

Operator alias for Control.Semigroupoid.composeFlipped (right-associative / precedence 9)

#(>>=)

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

#(>=>)

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

#(>=)

Operator alias for Data.Ord.greaterThanOrEq (left-associative / precedence 4)

#(>)

Operator alias for Data.Ord.greaterThan (left-associative / precedence 4)

#(==)

Operator alias for Data.Eq.eq (non-associative / precedence 4)

#(=<<)

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

#(<@>)

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

#(<>)

Operator alias for Data.Semigroup.append (right-associative / precedence 5)

#(<=<)

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

#(<=)

Operator alias for Data.Ord.lessThanOrEq (left-associative / precedence 4)

#(<<<)

Operator alias for Control.Semigroupoid.compose (right-associative / precedence 9)

#(<*>)

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

#(<*)

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

#(<$>)

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

#(<$)

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

#(<#>)

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

#(<)

Operator alias for Data.Ord.lessThan (left-associative / precedence 4)

#(/=)

Operator alias for Data.Eq.notEq (non-associative / precedence 4)

#(/)

Operator alias for Data.EuclideanRing.div (left-associative / precedence 7)

#(-)

Operator alias for Data.Ring.sub (left-associative / precedence 6)

#(+)

Operator alias for Data.Semiring.add (left-associative / precedence 6)

#(*>)

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

#(*)

Operator alias for Data.Semiring.mul (left-associative / precedence 7)

#(&&)

Operator alias for Data.HeytingAlgebra.conj (right-associative / precedence 3)

#($>)

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

#($)

Operator alias for Data.Function.apply (right-associative / precedence 0)

Applies a function to an argument: the reverse of (#).

length $ groupBy productCategory $ filter isInStock $ products

is equivalent to:

length (groupBy productCategory (filter isInStock products))

Or another alternative equivalent, applying chain of composed functions to a value:

length <<< groupBy productCategory <<< filter isInStock $ products

#(#)

Operator alias for Data.Function.applyFlipped (left-associative / precedence 1)

Applies an argument to a function: the reverse of ($).

products # filter isInStock # groupBy productCategory # length

is equivalent to:

length (groupBy productCategory (filter isInStock products))

Or another alternative equivalent, applying a value to a chain of composed functions:

products # filter isInStock >>> groupBy productCategory >>> length

#type (~>)

Operator alias for Data.NaturalTransformation.NaturalTransformation (right-associative / precedence 4)

Modules