Safe Haskell | None |
---|---|

Language | Haskell2010 |

Invariant monoidal functors.

This roughly corresponds to Control.Applicative, but exposes a non-overlapping API so can be imported unqualified. It does, however, use operators similar to those provided by contravariant.

## Synopsis

- data Bijection (a :: * -> * -> *) b c = (:<->:) {}
- biCase :: QuasiQuoter
- (>$<) :: Functor f => (a <-> b) -> f a -> f b
- (>$) :: Functor f => a -> f a -> f ()
- ($<) :: Functor f => f a -> a -> f ()
- class Functor f => Monoidal f where
- unitDefault :: Applicative f => f ()
- pairADefault :: Applicative f => f a -> f b -> f (a, b)
- (>*) :: Monoidal f => f a -> f () -> f a
- (*<) :: Monoidal f => f () -> f a -> f a
- liftI2 :: Monoidal f => ((a, b) <-> c) -> f a -> f b -> f c
- liftI3 :: Monoidal f => ((a, b, c) <-> d) -> f a -> f b -> f c -> f d
- liftI4 :: Monoidal f => ((a, b, c, d) <-> e) -> f a -> f b -> f c -> f d -> f e
- liftI5 :: Monoidal f => ((a, b, c, d, e) <-> g) -> f a -> f b -> f c -> f d -> f e -> f g
- (>*<<) :: Monoidal f => f a -> f (b, c) -> f (a, b, c)
- (>*<<<) :: Monoidal f => f a -> f (b, c, d) -> f (a, b, c, d)
- (>*<<<<) :: Monoidal f => f a -> f (b, c, d, e) -> f (a, b, c, d, e)
- (>>*<) :: Monoidal f => f (a, b) -> f c -> f (a, b, c)
- (>>>*<) :: Monoidal f => f (a, b, c) -> f d -> f (a, b, c, d)
- (>>>>*<) :: Monoidal f => f (a, b, c, d) -> f e -> f (a, b, c, d, e)
- (>>*<<) :: Monoidal f => f (a, b) -> f (c, d) -> f (a, b, c, d)
- pureI :: Monoidal f => a -> f a
- constI :: Monoidal f => a -> f a -> f ()
- sequenceI_ :: (Foldable t, Monoidal f) => t (f ()) -> f ()
- mapI_ :: (Foldable t, Monoidal f) => (a -> f ()) -> t a -> f ()
- forI_ :: (Foldable t, Monoidal f) => t a -> (a -> f ()) -> f ()
- sequenceMaybesI :: Monoidal f => [f (Maybe a)] -> f [a]
- mapMaybeI :: Monoidal f => (a -> f (Maybe b)) -> [a] -> f [b]
- class Monoidal f => MonoidalAlt f where
- eitherADefault :: Alternative f => f a -> f b -> f (Either a b)
- (>|) :: MonoidalAlt f => f a -> f a -> f a
- (|<) :: MonoidalAlt f => f a -> f a -> f a
- optionalI :: MonoidalAlt f => f a -> f (Maybe a)
- defaulting :: (MonoidalAlt f, Eq a) => a -> f a -> f a
- manyI :: MonoidalAlt f => f a -> f [a]
- msumIndex :: MonoidalAlt f => [f ()] -> f Int
- msumFirst :: (MonoidalAlt f, Traversable t) => t (f a) -> f a
- msumLast :: (MonoidalAlt f, Traversable t) => t (f a) -> f a
- oneOfI :: (MonoidalAlt f, Eq a) => (a -> f ()) -> [a] -> f a

# Documentation

data Bijection (a :: * -> * -> *) b c Source #

A representation of a bidirectional arrow (embedding-projection pair of arrows transformer): an arrow and its inverse.
Most uses will prefer the specialized `<->`

type for function arrows.

To constitute a valid bijection, `biTo`

and `biFrom`

should be inverses:

biTo . biFrom = id

biFrom . biTo = id

It may be argued that the arguments should be in the opposite order due to the arrow syntax, but it makes more sense to me to have the forward function come first.

## Instances

biCase :: QuasiQuoter Source #

Construct an expression representing a function bijection based on a set of newline- or semicolon-separated cases.
Each case should be two pattern-expressions separated by `-`

.
Each pattern-expression is a haskell pattern that can also be interpreted as an expression.
You can think of these as symmetric or bidirectional case expressions.
The result will be a bijection that is the combination of two lambdas, one with the cases intepreted forward, and one reverse.
For example:

newtype T a = C a biC :: T a <-> a biC = [biCase| C a <-> a |]

isJust :: Maybe () <-> Bool isJust = [biCase| Just () <-> True Nothing <-> False |]

# Functor

(>$<) :: Functor f => (a <-> b) -> f a -> f b infixl 4 Source #

Another synonym for `fmap`

to match other operators in this module.

# Monoidal

class Functor f => Monoidal f where Source #

Invariant monoidal functor.
This roughly corresponds to `Applicative`

, which, for covariant functors, is equivalent to a monoidal functor.
Invariant functors, however, may admit a monoidal instance but not applicative.

Lift a unit value, analogous to

(but also like `pure`

()`const ()`

).

(>*<) :: f a -> f b -> f (a, b) infixl 4 Source #

Merge two functors into a tuple, analogous to

. (Sometimes known as `liftA2`

(,)`**`

.)

unitDefault :: Applicative f => f () Source #

Default `unit`

implementation for non-invertible `Applicative`

s.

pairADefault :: Applicative f => f a -> f b -> f (a, b) Source #

Default '>*< implementation for non-invertible `Applicative`

s.

(>*) :: Monoidal f => f a -> f () -> f a infixl 4 Source #

Sequence actions, discarding/inhabiting the unit value of the second argument.

(*<) :: Monoidal f => f () -> f a -> f a infixl 4 Source #

Sequence actions, discarding/inhabiting the unit value of the first argument.

## Tuple combinators

liftI2 :: Monoidal f => ((a, b) <-> c) -> f a -> f b -> f c Source #

Lift an (uncurried) bijection into a monoidal functor.

pureI :: Monoidal f => a -> f a Source #

A constant monoidal (like `pure`

), which always produces the same value and ignores everything.

constI :: Monoidal f => a -> f a -> f () Source #

Supply a constant value to a monoidal and ignore whatever is produced.

sequenceI_ :: (Foldable t, Monoidal f) => t (f ()) -> f () Source #

Sequence (like `sequenceA_`

) a list of monoidals, ignoring (

) all the results.`const`

()

mapI_ :: (Foldable t, Monoidal f) => (a -> f ()) -> t a -> f () Source #

Map each element to a monoidal and `sequenceI_`

the results.

sequenceMaybesI :: Monoidal f => [f (Maybe a)] -> f [a] Source #

# MonoidalAlt

class Monoidal f => MonoidalAlt f where Source #

Monoidal functors that allow choice.

An always-failing (and thus impossible) value.

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

Associative binary choice.

## Instances

Monoidal m => MonoidalAlt (MaybeT m) Source # | |

MonoidalAlt (Free f) Source # | |

eitherADefault :: Alternative f => f a -> f b -> f (Either a b) Source #

Default `>|<`

implementation for non-invertible `Alternative`

s.

(>|) :: MonoidalAlt f => f a -> f a -> f a infixl 3 Source #

Assymetric (and therefore probably not bijective) version of `>|<`

that returns whichever action succeeds but always uses the left one on inputs.

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

Assymetric (and therefore probably not bijective) version of `>|<`

that returns whichever action succeeds but always uses the right one on inputs.

defaulting :: (MonoidalAlt f, Eq a) => a -> f a -> f a Source #

Return a default value if a monoidal functor fails, and only apply it to non-default values.

manyI :: MonoidalAlt f => f a -> f [a] Source #

Repeatedly apply a monoidal functor until it fails. Analogous to `many`

.

msumIndex :: MonoidalAlt f => [f ()] -> f Int Source #

Try a list of monoidal actions in sequence, producing the index of the first successful action, and evaluating the action with the given index.

msumFirst :: (MonoidalAlt f, Traversable t) => t (f a) -> f a Source #

msumLast :: (MonoidalAlt f, Traversable t) => t (f a) -> f a Source #

oneOfI :: (MonoidalAlt f, Eq a) => (a -> f ()) -> [a] -> f a Source #

Take a list of items and apply them to the action in sequence until one succeeds and return the cooresponding item; match the input with the list and apply the corresponding action (or produce an error if the input is not an element of the list).