sv-0.1: Encode and decode separated values (CSV, PSV, ...)

Copyright(C) CSIRO 2017-2018
LicenseBSD3
MaintainerGeorge Wilson <george.wilson@data61.csiro.au>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Sv

Contents

Description

This module exports most of the other modules from the package. It is intended to be imported unqualified, along with some qualified imports for the Data.Sv.Decode and Data.Sv.Encode modules as needed.

import Data.Sv
import qualified Data.Sv.Decode as D
import qualified Data.Sv.Encode as E

Synopsis

Decoding

decode :: Decode' s a -> Sv s -> DecodeValidation s [a] Source #

Decodes a sv into a list of its values using the provided Decode

parseDecode :: Decode' ByteString a -> ParseOptions ByteString -> ByteString -> DecodeValidation ByteString [a] Source #

Parse a ByteString as an Sv, and then decode it with the given decoder.

This version uses Trifecta to parse the ByteString, which is assumed to be UTF-8 encoded. If you want a different library, use parseDecode'.

parseDecode' :: SvParser s -> Decode' s a -> ParseOptions s -> s -> DecodeValidation s [a] Source #

Parse text as an Sv, and then decode it with the given decoder.

This version lets you choose which parsing library to use by providing an SvParser. Common selections are trifecta and attoparsecByteString.

parseDecodeFromFile :: MonadIO m => Decode' ByteString a -> ParseOptions ByteString -> FilePath -> m (DecodeValidation ByteString [a]) Source #

Load a file, parse it, and decode it.

This version uses Trifecta to parse the file, which is assumed to be UTF-8 encoded.

parseDecodeFromFile' :: MonadIO m => SvParser s -> Decode' s a -> ParseOptions s -> FilePath -> m (DecodeValidation s [a]) Source #

Load a file, parse it, and decode it.

This version lets you choose which parsing library to use by providing an SvParser. Common selections are trifecta and attoparsecByteString.

decodeMay :: DecodeError e -> (s -> Maybe a) -> Decode e s a Source #

Build a Decode, given a function that returns Maybe.

Return the given error if the function returns Nothing.

decodeEither :: (s -> Either (DecodeError e) a) -> Decode e s a Source #

Build a Decode, given a function that returns Either.

decodeEither' :: (e -> DecodeError e') -> (s -> Either e a) -> Decode e' s a Source #

Build a Decode, given a function that returns Either, and a function to build the error.

(>>==) :: Decode e s a -> (a -> DecodeValidation e b) -> Decode e s b infixl 1 Source #

This can be used to build a Decode whose value depends on the result of another Decode. This is especially useful since Decode is not a Monad.

If you need something like this but with more power, look at bindDecode

(==<<) :: (a -> DecodeValidation e b) -> Decode e s a -> Decode e s b infixr 1 Source #

flipped >>==

Parsing

Printing

Encoding

encode :: Encode a -> EncodeOptions -> [a] -> ByteString Source #

Encode the given list with the given Encode, configured by the given EncodeOptions.

encodeToFile :: Encode a -> EncodeOptions -> [a] -> FilePath -> IO () Source #

Encode, writing to a file. This is way is more efficient than encoding to a ByteString and then writing to file.

encodeToHandle :: Encode a -> EncodeOptions -> [a] -> Handle -> IO () Source #

Encode, writing the output to a file handle.

encodeBuilder :: Encode a -> EncodeOptions -> [a] -> Builder Source #

Encode to a ByteString Builder, which is useful if you are going to combine the output with other ByteStrings.

encodeRow :: Encode a -> EncodeOptions -> a -> ByteString Source #

Encode one row only

encodeSv :: Encode a -> EncodeOptions -> Maybe (NonEmpty ByteString) -> [a] -> Sv ByteString Source #

Build an Sv rather than going straight to ByteString. This allows you to query the Sv or run sanity checks.

Core data types

Re-exports from contravariant and semigroupoids

class Functor f => Alt (f :: * -> *) where #

Laws:

<!> is associative:             (a <!> b) <!> c = a <!> (b <!> c)
<$> left-distributes over <!>:  f <$> (a <!> b) = (f <$> a) <!> (f <$> b)

If extended to an Alternative then <!> should equal <|>.

Ideally, an instance of Alt also satisfies the "left distributon" law of MonadPlus with respect to <.>:

<.> right-distributes over <!>: (a <!> b) <.> c = (a <.> c) <!> (b <.> c)

But Maybe, IO, Either a, ErrorT e m, and STM satisfy the alternative "left catch" law instead:

pure a <!> b = pure a

However, this variation cannot be stated purely in terms of the dependencies of Alt.

When and if MonadPlus is successfully refactored, this class should also be refactored to remove these instances.

The right distributive law should extend in the cases where the a Bind or Monad is provided to yield variations of the right distributive law:

(m <!> n) >>- f = (m >>- f) <!> (m >>- f)
(m <!> n) >>= f = (m >>= f) <!> (m >>= f)

Minimal complete definition

(<!>)

Methods

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

<|> without a required empty

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

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

Instances

Alt [] 

Methods

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

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

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

Alt Maybe 

Methods

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

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

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

Alt IO

This instance does not actually satisfy the (<.>) right distributive law It instead satisfies the Left-Catch law

Methods

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

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

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

Alt Option 

Methods

(<!>) :: Option a -> Option a -> Option a #

some :: Applicative Option => Option a -> Option [a] #

many :: Applicative Option => Option a -> Option [a] #

Alt NonEmpty 
Alt IntMap 

Methods

(<!>) :: IntMap a -> IntMap a -> IntMap a #

some :: Applicative IntMap => IntMap a -> IntMap [a] #

many :: Applicative IntMap => IntMap a -> IntMap [a] #

Alt Seq 

Methods

(<!>) :: Seq a -> Seq a -> Seq a #

some :: Applicative Seq => Seq a -> Seq [a] #

many :: Applicative Seq => Seq a -> Seq [a] #

Alt (Either a) 

Methods

(<!>) :: Either a a -> Either a a -> Either a a #

some :: Applicative (Either a) => Either a a -> Either a [a] #

many :: Applicative (Either a) => Either a a -> Either a [a] #

Alt (V1 *) 

Methods

(<!>) :: V1 * a -> V1 * a -> V1 * a #

some :: Applicative (V1 *) => V1 * a -> V1 * [a] #

many :: Applicative (V1 *) => V1 * a -> V1 * [a] #

Alt (U1 *) 

Methods

(<!>) :: U1 * a -> U1 * a -> U1 * a #

some :: Applicative (U1 *) => U1 * a -> U1 * [a] #

many :: Applicative (U1 *) => U1 * a -> U1 * [a] #

MonadPlus m => Alt (WrappedMonad m) 
Alt (Proxy *) 

Methods

(<!>) :: Proxy * a -> Proxy * a -> Proxy * a #

some :: Applicative (Proxy *) => Proxy * a -> Proxy * [a] #

many :: Applicative (Proxy *) => Proxy * a -> Proxy * [a] #

Ord k => Alt (Map k) 

Methods

(<!>) :: Map k a -> Map k a -> Map k a #

some :: Applicative (Map k) => Map k a -> Map k [a] #

many :: Applicative (Map k) => Map k a -> Map k [a] #

Alt (Alt f) 

Methods

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

some :: Applicative (Alt f) => Alt f a -> Alt f [a] #

many :: Applicative (Alt f) => Alt f a -> Alt f [a] #

Alt f => Alt (Yoneda f) 

Methods

(<!>) :: Yoneda f a -> Yoneda f a -> Yoneda f a #

some :: Applicative (Yoneda f) => Yoneda f a -> Yoneda f [a] #

many :: Applicative (Yoneda f) => Yoneda f a -> Yoneda f [a] #

Alt (ReifiedFold s) 
Apply f => Alt (ListT f) 

Methods

(<!>) :: ListT f a -> ListT f a -> ListT f a #

some :: Applicative (ListT f) => ListT f a -> ListT f [a] #

many :: Applicative (ListT f) => ListT f a -> ListT f [a] #

Alternative f => Alt (WrappedApplicative f) 
Alt f => Alt (Lift f) 

Methods

(<!>) :: Lift f a -> Lift f a -> Lift f a #

some :: Applicative (Lift f) => Lift f a -> Lift f [a] #

many :: Applicative (Lift f) => Lift f a -> Lift f [a] #

(Bind f, Monad f) => Alt (MaybeT f) 

Methods

(<!>) :: MaybeT f a -> MaybeT f a -> MaybeT f a #

some :: Applicative (MaybeT f) => MaybeT f a -> MaybeT f [a] #

many :: Applicative (MaybeT f) => MaybeT f a -> MaybeT f [a] #

Alt (Validation err) 

Methods

(<!>) :: Validation err a -> Validation err a -> Validation err a #

some :: Applicative (Validation err) => Validation err a -> Validation err [a] #

many :: Applicative (Validation err) => Validation err a -> Validation err [a] #

Alt f => Alt (Rec1 * f) 

Methods

(<!>) :: Rec1 * f a -> Rec1 * f a -> Rec1 * f a #

some :: Applicative (Rec1 * f) => Rec1 * f a -> Rec1 * f [a] #

many :: Applicative (Rec1 * f) => Rec1 * f a -> Rec1 * f [a] #

ArrowPlus a => Alt (WrappedArrow a b) 

Methods

(<!>) :: WrappedArrow a b a -> WrappedArrow a b a -> WrappedArrow a b a #

some :: Applicative (WrappedArrow a b) => WrappedArrow a b a -> WrappedArrow a b [a] #

many :: Applicative (WrappedArrow a b) => WrappedArrow a b a -> WrappedArrow a b [a] #

Alt f => Alt (IdentityT * f) 

Methods

(<!>) :: IdentityT * f a -> IdentityT * f a -> IdentityT * f a #

some :: Applicative (IdentityT * f) => IdentityT * f a -> IdentityT * f [a] #

many :: Applicative (IdentityT * f) => IdentityT * f a -> IdentityT * f [a] #

(Bind f, Monad f, Semigroup e) => Alt (ExceptT e f) 

Methods

(<!>) :: ExceptT e f a -> ExceptT e f a -> ExceptT e f a #

some :: Applicative (ExceptT e f) => ExceptT e f a -> ExceptT e f [a] #

many :: Applicative (ExceptT e f) => ExceptT e f a -> ExceptT e f [a] #

(Bind f, Monad f) => Alt (ErrorT e f) 

Methods

(<!>) :: ErrorT e f a -> ErrorT e f a -> ErrorT e f a #

some :: Applicative (ErrorT e f) => ErrorT e f a -> ErrorT e f [a] #

many :: Applicative (ErrorT e f) => ErrorT e f a -> ErrorT e f [a] #

Alt f => Alt (Backwards * f) 

Methods

(<!>) :: Backwards * f a -> Backwards * f a -> Backwards * f a #

some :: Applicative (Backwards * f) => Backwards * f a -> Backwards * f [a] #

many :: Applicative (Backwards * f) => Backwards * f a -> Backwards * f [a] #

Alt (ReifiedIndexedFold i s) 
Alt f => Alt (StateT e f) 

Methods

(<!>) :: StateT e f a -> StateT e f a -> StateT e f a #

some :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] #

many :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] #

Alt f => Alt (StateT e f) 

Methods

(<!>) :: StateT e f a -> StateT e f a -> StateT e f a #

some :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] #

many :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] #

Alt f => Alt (WriterT w f) 

Methods

(<!>) :: WriterT w f a -> WriterT w f a -> WriterT w f a #

some :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #

many :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #

Alt f => Alt (WriterT w f) 

Methods

(<!>) :: WriterT w f a -> WriterT w f a -> WriterT w f a #

some :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #

many :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #

Alt f => Alt (Static f a) 

Methods

(<!>) :: Static f a a -> Static f a a -> Static f a a #

some :: Applicative (Static f a) => Static f a a -> Static f a [a] #

many :: Applicative (Static f a) => Static f a a -> Static f a [a] #

Alt f => Alt (Reverse * f) 

Methods

(<!>) :: Reverse * f a -> Reverse * f a -> Reverse * f a #

some :: Applicative (Reverse * f) => Reverse * f a -> Reverse * f [a] #

many :: Applicative (Reverse * f) => Reverse * f a -> Reverse * f [a] #

Alt (Decode e s) # 

Methods

(<!>) :: Decode e s a -> Decode e s a -> Decode e s a #

some :: Applicative (Decode e s) => Decode e s a -> Decode e s [a] #

many :: Applicative (Decode e s) => Decode e s a -> Decode e s [a] #

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

Methods

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

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

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

(Alt f, Alt g) => Alt (Product * f g) 

Methods

(<!>) :: Product * f g a -> Product * f g a -> Product * f g a #

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

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

Alt f => Alt (ReaderT * e f) 

Methods

(<!>) :: ReaderT * e f a -> ReaderT * e f a -> ReaderT * e f a #

some :: Applicative (ReaderT * e f) => ReaderT * e f a -> ReaderT * e f [a] #

many :: Applicative (ReaderT * e f) => ReaderT * e f a -> ReaderT * e f [a] #

Alt f => Alt (M1 * i c f) 

Methods

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

some :: Applicative (M1 * i c f) => M1 * i c f a -> M1 * i c f [a] #

many :: Applicative (M1 * i c f) => M1 * i c f a -> M1 * i c f [a] #

(Alt f, Functor g) => Alt (Compose * * f g) 

Methods

(<!>) :: Compose * * f g a -> Compose * * f g a -> Compose * * f g a #

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

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

Alt f => Alt (RWST r w s f) 

Methods

(<!>) :: RWST r w s f a -> RWST r w s f a -> RWST r w s f a #

some :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #

many :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #

Alt f => Alt (RWST r w s f) 

Methods

(<!>) :: RWST r w s f a -> RWST r w s f a -> RWST r w s f a #

some :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #

many :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #

class Contravariant (f :: * -> *) where #

The class of contravariant functors.

Whereas in Haskell, one can think of a Functor as containing or producing values, a contravariant functor is a functor that can be thought of as consuming values.

As an example, consider the type of predicate functions a -> Bool. One such predicate might be negative x = x < 0, which classifies integers as to whether they are negative. However, given this predicate, we can re-use it in other situations, providing we have a way to map values to integers. For instance, we can use the negative predicate on a person's bank balance to work out if they are currently overdrawn:

newtype Predicate a = Predicate { getPredicate :: a -> Bool }

instance Contravariant Predicate where
  contramap f (Predicate p) = Predicate (p . f)
                                         |   `- First, map the input...
                                         `----- then apply the predicate.

overdrawn :: Predicate Person
overdrawn = contramap personBankBalance negative

Any instance should be subject to the following laws:

contramap id = id
contramap f . contramap g = contramap (g . f)

Note, that the second law follows from the free theorem of the type of contramap and the first law, so you need only check that the former condition holds.

Minimal complete definition

contramap

Methods

contramap :: (a -> b) -> f b -> f a #

(>$) :: b -> f b -> f a infixl 4 #

Replace all locations in the output with the same value. The default definition is contramap . const, but this may be overridden with a more efficient version.

Instances

Contravariant SettableStateVar 
Contravariant Predicate

A Predicate is a Contravariant Functor, because contramap can apply its function argument to the input of the predicate.

Methods

contramap :: (a -> b) -> Predicate b -> Predicate a #

(>$) :: b -> Predicate b -> Predicate a #

Contravariant Comparison

A Comparison is a Contravariant Functor, because contramap can apply its function argument to each input of the comparison function.

Methods

contramap :: (a -> b) -> Comparison b -> Comparison a #

(>$) :: b -> Comparison b -> Comparison a #

Contravariant Equivalence

Equivalence relations are Contravariant, because you can apply the contramapped function to each input to the equivalence relation.

Methods

contramap :: (a -> b) -> Equivalence b -> Equivalence a #

(>$) :: b -> Equivalence b -> Equivalence a #

Contravariant Encode # 

Methods

contramap :: (a -> b) -> Encode b -> Encode a #

(>$) :: b -> Encode b -> Encode a #

Contravariant (V1 *) 

Methods

contramap :: (a -> b) -> V1 * b -> V1 * a #

(>$) :: b -> V1 * b -> V1 * a #

Contravariant (U1 *) 

Methods

contramap :: (a -> b) -> U1 * b -> U1 * a #

(>$) :: b -> U1 * b -> U1 * a #

Contravariant (Op a) 

Methods

contramap :: (a -> b) -> Op a b -> Op a a #

(>$) :: b -> Op a b -> Op a a #

Contravariant (Proxy *) 

Methods

contramap :: (a -> b) -> Proxy * b -> Proxy * a #

(>$) :: b -> Proxy * b -> Proxy * a #

Contravariant f => Contravariant (Indexing f) 

Methods

contramap :: (a -> b) -> Indexing f b -> Indexing f a #

(>$) :: b -> Indexing f b -> Indexing f a #

Contravariant f => Contravariant (Indexing64 f) 

Methods

contramap :: (a -> b) -> Indexing64 f b -> Indexing64 f a #

(>$) :: b -> Indexing64 f b -> Indexing64 f a #

Contravariant m => Contravariant (ListT m) 

Methods

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

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

Contravariant m => Contravariant (MaybeT m) 

Methods

contramap :: (a -> b) -> MaybeT m b -> MaybeT m a #

(>$) :: b -> MaybeT m b -> MaybeT m a #

Contravariant f => Contravariant (Rec1 * f) 

Methods

contramap :: (a -> b) -> Rec1 * f b -> Rec1 * f a #

(>$) :: b -> Rec1 * f b -> Rec1 * f a #

Contravariant (Const * a) 

Methods

contramap :: (a -> b) -> Const * a b -> Const * a a #

(>$) :: b -> Const * a b -> Const * a a #

Contravariant f => Contravariant (Alt * f) 

Methods

contramap :: (a -> b) -> Alt * f b -> Alt * f a #

(>$) :: b -> Alt * f b -> Alt * f a #

Contravariant f => Contravariant (IdentityT * f) 

Methods

contramap :: (a -> b) -> IdentityT * f b -> IdentityT * f a #

(>$) :: b -> IdentityT * f b -> IdentityT * f a #

(Functor f, Contravariant g) => Contravariant (ComposeFC f g) 

Methods

contramap :: (a -> b) -> ComposeFC f g b -> ComposeFC f g a #

(>$) :: b -> ComposeFC f g b -> ComposeFC f g a #

(Contravariant f, Functor g) => Contravariant (ComposeCF f g) 

Methods

contramap :: (a -> b) -> ComposeCF f g b -> ComposeCF f g a #

(>$) :: b -> ComposeCF f g b -> ComposeCF f g a #

Contravariant m => Contravariant (ExceptT e m) 

Methods

contramap :: (a -> b) -> ExceptT e m b -> ExceptT e m a #

(>$) :: b -> ExceptT e m b -> ExceptT e m a #

Contravariant m => Contravariant (ErrorT e m) 

Methods

contramap :: (a -> b) -> ErrorT e m b -> ErrorT e m a #

(>$) :: b -> ErrorT e m b -> ErrorT e m a #

Contravariant f => Contravariant (Backwards * f) 

Methods

contramap :: (a -> b) -> Backwards * f b -> Backwards * f a #

(>$) :: b -> Backwards * f b -> Backwards * f a #

Contravariant m => Contravariant (StateT s m) 

Methods

contramap :: (a -> b) -> StateT s m b -> StateT s m a #

(>$) :: b -> StateT s m b -> StateT s m a #

Contravariant m => Contravariant (StateT s m) 

Methods

contramap :: (a -> b) -> StateT s m b -> StateT s m a #

(>$) :: b -> StateT s m b -> StateT s m a #

Contravariant m => Contravariant (WriterT w m) 

Methods

contramap :: (a -> b) -> WriterT w m b -> WriterT w m a #

(>$) :: b -> WriterT w m b -> WriterT w m a #

Contravariant m => Contravariant (WriterT w m) 

Methods

contramap :: (a -> b) -> WriterT w m b -> WriterT w m a #

(>$) :: b -> WriterT w m b -> WriterT w m a #

Contravariant f => Contravariant (Reverse * f) 

Methods

contramap :: (a -> b) -> Reverse * f b -> Reverse * f a #

(>$) :: b -> Reverse * f b -> Reverse * f a #

Contravariant (Constant * a) 

Methods

contramap :: (a -> b) -> Constant * a b -> Constant * a a #

(>$) :: b -> Constant * a b -> Constant * a a #

Contravariant (K1 * i c) 

Methods

contramap :: (a -> b) -> K1 * i c b -> K1 * i c a #

(>$) :: b -> K1 * i c b -> K1 * i c a #

(Contravariant f, Contravariant g) => Contravariant ((:+:) * f g) 

Methods

contramap :: (a -> b) -> (* :+: f) g b -> (* :+: f) g a #

(>$) :: b -> (* :+: f) g b -> (* :+: f) g a #

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

Methods

contramap :: (a -> b) -> (* :*: f) g b -> (* :*: f) g a #

(>$) :: b -> (* :*: f) g b -> (* :*: f) g a #

(Contravariant f, Contravariant g) => Contravariant (Product * f g) 

Methods

contramap :: (a -> b) -> Product * f g b -> Product * f g a #

(>$) :: b -> Product * f g b -> Product * f g a #

(Contravariant f, Contravariant g) => Contravariant (Sum * f g) 

Methods

contramap :: (a -> b) -> Sum * f g b -> Sum * f g a #

(>$) :: b -> Sum * f g b -> Sum * f g a #

Contravariant m => Contravariant (ReaderT * r m) 

Methods

contramap :: (a -> b) -> ReaderT * r m b -> ReaderT * r m a #

(>$) :: b -> ReaderT * r m b -> ReaderT * r m a #

Contravariant f => Contravariant (M1 * i c f) 

Methods

contramap :: (a -> b) -> M1 * i c f b -> M1 * i c f a #

(>$) :: b -> M1 * i c f b -> M1 * i c f a #

(Functor f, Contravariant g) => Contravariant ((:.:) * * f g) 

Methods

contramap :: (a -> b) -> (* :.: *) f g b -> (* :.: *) f g a #

(>$) :: b -> (* :.: *) f g b -> (* :.: *) f g a #

(Functor f, Contravariant g) => Contravariant (Compose * * f g) 

Methods

contramap :: (a -> b) -> Compose * * f g b -> Compose * * f g a #

(>$) :: b -> Compose * * f g b -> Compose * * f g a #

Contravariant m => Contravariant (RWST r w s m) 

Methods

contramap :: (a -> b) -> RWST r w s m b -> RWST r w s m a #

(>$) :: b -> RWST r w s m b -> RWST r w s m a #

Contravariant m => Contravariant (RWST r w s m) 

Methods

contramap :: (a -> b) -> RWST r w s m b -> RWST r w s m a #

(>$) :: b -> RWST r w s m b -> RWST r w s m a #

class Contravariant f => Divisible (f :: * -> *) where #

A Divisible contravariant functor is the contravariant analogue of Applicative.

Continuing the intuition that Contravariant functors consume input, a Divisible contravariant functor also has the ability to be composed "beside" another contravariant functor.

Serializers provide a good example of Divisible contravariant functors. To begin let's start with the type of serializers for specific types:

newtype Serializer a = Serializer { runSerializer :: a -> ByteString }

This is a contravariant functor:

instance Contravariant Serializer where
  contramap f s = Serializer (runSerializer s . f)

That is, given a serializer for a (s :: Serializer a), and a way to turn bs into as (a mapping f :: b -> a), we have a serializer for b: contramap f s :: Serializer b.

Divisible gives us a way to combine two serializers that focus on different parts of a structure. If we postulate the existance of two primitive serializers - string :: Serializer String and int :: Serializer Int, we would like to be able to combine these into a serializer for pairs of Strings and Ints. How can we do this? Simply run both serializer and combine their output!

data StringAndInt = StringAndInt String Int

stringAndInt :: Serializer StringAndInt
stringAndInt = Serializer $ (StringAndInt s i) ->
  let sBytes = runSerializer string s
      iBytes = runSerializer int i
  in sBytes <> iBytes

divide is a generalization by also taking a contramap like function to split any a into a pair. This conveniently allows you to target fields of a record, for instance, by extracting the values under two fields and combining them into a tuple.

To complete the example, here is how to write stringAndInt using a Divisible instance:

instance Divisible Serializer where
  conquer = Serializer (const mempty)

  divide toBC bSerializer cSerializer = Serializer $ a ->
    case toBC a of
      (b, c) ->
        let bBytes = runSerializer bSerializer b
            cBytes = runSerializer cSerializer c
        in bBytes <> cBytes

stringAndInt :: Serializer StringAndInt
stringAndInt =
  divide ((StringAndInt s i) -> (s, i)) string int

Minimal complete definition

divide, conquer

Methods

divide :: (a -> (b, c)) -> f b -> f c -> f a #

conquer :: f a #

Conquer acts as an identity for combining Divisible functors.

Instances

Divisible SettableStateVar 
Divisible Predicate 

Methods

divide :: (a -> (b, c)) -> Predicate b -> Predicate c -> Predicate a #

conquer :: Predicate a #

Divisible Comparison 

Methods

divide :: (a -> (b, c)) -> Comparison b -> Comparison c -> Comparison a #

conquer :: Comparison a #

Divisible Equivalence 

Methods

divide :: (a -> (b, c)) -> Equivalence b -> Equivalence c -> Equivalence a #

conquer :: Equivalence a #

Divisible Encode # 

Methods

divide :: (a -> (b, c)) -> Encode b -> Encode c -> Encode a #

conquer :: Encode a #

Divisible (U1 *) 

Methods

divide :: (a -> (b, c)) -> U1 * b -> U1 * c -> U1 * a #

conquer :: U1 * a #

Monoid r => Divisible (Op r) 

Methods

divide :: (a -> (b, c)) -> Op r b -> Op r c -> Op r a #

conquer :: Op r a #

Divisible (Proxy *) 

Methods

divide :: (a -> (b, c)) -> Proxy * b -> Proxy * c -> Proxy * a #

conquer :: Proxy * a #

Divisible m => Divisible (ListT m) 

Methods

divide :: (a -> (b, c)) -> ListT m b -> ListT m c -> ListT m a #

conquer :: ListT m a #

Divisible m => Divisible (MaybeT m) 

Methods

divide :: (a -> (b, c)) -> MaybeT m b -> MaybeT m c -> MaybeT m a #

conquer :: MaybeT m a #

Divisible f => Divisible (Rec1 * f) 

Methods

divide :: (a -> (b, c)) -> Rec1 * f b -> Rec1 * f c -> Rec1 * f a #

conquer :: Rec1 * f a #

Monoid m => Divisible (Const * m) 

Methods

divide :: (a -> (b, c)) -> Const * m b -> Const * m c -> Const * m a #

conquer :: Const * m a #

Divisible f => Divisible (Alt * f) 

Methods

divide :: (a -> (b, c)) -> Alt * f b -> Alt * f c -> Alt * f a #

conquer :: Alt * f a #

Divisible f => Divisible (IdentityT * f) 

Methods

divide :: (a -> (b, c)) -> IdentityT * f b -> IdentityT * f c -> IdentityT * f a #

conquer :: IdentityT * f a #

(Applicative f, Divisible g) => Divisible (ComposeFC f g) 

Methods

divide :: (a -> (b, c)) -> ComposeFC f g b -> ComposeFC f g c -> ComposeFC f g a #

conquer :: ComposeFC f g a #

(Divisible f, Applicative g) => Divisible (ComposeCF f g) 

Methods

divide :: (a -> (b, c)) -> ComposeCF f g b -> ComposeCF f g c -> ComposeCF f g a #

conquer :: ComposeCF f g a #

Divisible m => Divisible (ExceptT e m) 

Methods

divide :: (a -> (b, c)) -> ExceptT e m b -> ExceptT e m c -> ExceptT e m a #

conquer :: ExceptT e m a #

Divisible m => Divisible (ErrorT e m) 

Methods

divide :: (a -> (b, c)) -> ErrorT e m b -> ErrorT e m c -> ErrorT e m a #

conquer :: ErrorT e m a #

Divisible f => Divisible (Backwards * f) 

Methods

divide :: (a -> (b, c)) -> Backwards * f b -> Backwards * f c -> Backwards * f a #

conquer :: Backwards * f a #

Divisible m => Divisible (StateT s m) 

Methods

divide :: (a -> (b, c)) -> StateT s m b -> StateT s m c -> StateT s m a #

conquer :: StateT s m a #

Divisible m => Divisible (StateT s m) 

Methods

divide :: (a -> (b, c)) -> StateT s m b -> StateT s m c -> StateT s m a #

conquer :: StateT s m a #

Divisible m => Divisible (WriterT w m) 

Methods

divide :: (a -> (b, c)) -> WriterT w m b -> WriterT w m c -> WriterT w m a #

conquer :: WriterT w m a #

Divisible m => Divisible (WriterT w m) 

Methods

divide :: (a -> (b, c)) -> WriterT w m b -> WriterT w m c -> WriterT w m a #

conquer :: WriterT w m a #

Divisible f => Divisible (Reverse * f) 

Methods

divide :: (a -> (b, c)) -> Reverse * f b -> Reverse * f c -> Reverse * f a #

conquer :: Reverse * f a #

Monoid m => Divisible (Constant * m) 

Methods

divide :: (a -> (b, c)) -> Constant * m b -> Constant * m c -> Constant * m a #

conquer :: Constant * m a #

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

Methods

divide :: (a -> (b, c)) -> (* :*: f) g b -> (* :*: f) g c -> (* :*: f) g a #

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

(Divisible f, Divisible g) => Divisible (Product * f g) 

Methods

divide :: (a -> (b, c)) -> Product * f g b -> Product * f g c -> Product * f g a #

conquer :: Product * f g a #

Divisible m => Divisible (ReaderT * r m) 

Methods

divide :: (a -> (b, c)) -> ReaderT * r m b -> ReaderT * r m c -> ReaderT * r m a #

conquer :: ReaderT * r m a #

Divisible f => Divisible (M1 * i c f) 

Methods

divide :: (a -> (b, c)) -> M1 * i c f b -> M1 * i c f c -> M1 * i c f a #

conquer :: M1 * i c f a #

(Applicative f, Divisible g) => Divisible ((:.:) * * f g) 

Methods

divide :: (a -> (b, c)) -> (* :.: *) f g b -> (* :.: *) f g c -> (* :.: *) f g a #

conquer :: (* :.: *) f g a #

(Applicative f, Divisible g) => Divisible (Compose * * f g) 

Methods

divide :: (a -> (b, c)) -> Compose * * f g b -> Compose * * f g c -> Compose * * f g a #

conquer :: Compose * * f g a #

Divisible m => Divisible (RWST r w s m) 

Methods

divide :: (a -> (b, c)) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a #

conquer :: RWST r w s m a #

Divisible m => Divisible (RWST r w s m) 

Methods

divide :: (a -> (b, c)) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a #

conquer :: RWST r w s m a #

divided :: Divisible f => f a -> f b -> f (a, b) #

class Divisible f => Decidable (f :: * -> *) where #

A Decidable contravariant functor is the contravariant analogue of Alternative.

Noting the superclass constraint that f must also be Divisible, a Decidable functor has the ability to "fan out" input, under the intuition that contravariant functors consume input.

In the dicussion for Divisible, an example was demonstrated with Serializers, that turn as into ByteStrings. Divisible allowed us to serialize the product of multiple values by concatenation. By making our Serializer also Decidable- we now have the ability to serialize the sum of multiple values - for example different constructors in an ADT.

Consider serializing arbitrary identifiers that can be either Strings or Ints:

data Identifier = StringId String | IntId Int

We know we have serializers for Strings and Ints, but how do we combine them into a Serializer for Identifier? Essentially, our Serializer needs to scrutinise the incoming value and choose how to serialize it:

identifier :: Serializer Identifier
identifier = Serializer $ identifier ->
  case identifier of
    StringId s -> runSerializer string s
    IntId i -> runSerializer int i

It is exactly this notion of choice that Decidable encodes. Hence if we add an instance of Decidable for Serializer...

instance Decidable Serializer where
  lose f = Serializer $ a -> absurd (f a)
  choose split l r = Serializer $ a ->
    either (runSerializer l) (runSerializer r) (split a)

Then our identifier Serializer is

identifier :: Serializer Identifier
identifier = choose toEither string int where
  toEither (StringId s) = Left s
  toEither (IntId i) = Right i

Minimal complete definition

lose, choose

Methods

lose :: (a -> Void) -> f a #

Acts as identity to choose.

choose :: (a -> Either b c) -> f b -> f c -> f a #

Instances

Decidable SettableStateVar 

Methods

lose :: (a -> Void) -> SettableStateVar a #

choose :: (a -> Either b c) -> SettableStateVar b -> SettableStateVar c -> SettableStateVar a #

Decidable Predicate 

Methods

lose :: (a -> Void) -> Predicate a #

choose :: (a -> Either b c) -> Predicate b -> Predicate c -> Predicate a #

Decidable Comparison 

Methods

lose :: (a -> Void) -> Comparison a #

choose :: (a -> Either b c) -> Comparison b -> Comparison c -> Comparison a #

Decidable Equivalence 

Methods

lose :: (a -> Void) -> Equivalence a #

choose :: (a -> Either b c) -> Equivalence b -> Equivalence c -> Equivalence a #

Decidable Encode # 

Methods

lose :: (a -> Void) -> Encode a #

choose :: (a -> Either b c) -> Encode b -> Encode c -> Encode a #

Decidable (U1 *) 

Methods

lose :: (a -> Void) -> U1 * a #

choose :: (a -> Either b c) -> U1 * b -> U1 * c -> U1 * a #

Monoid r => Decidable (Op r) 

Methods

lose :: (a -> Void) -> Op r a #

choose :: (a -> Either b c) -> Op r b -> Op r c -> Op r a #

Decidable (Proxy *) 

Methods

lose :: (a -> Void) -> Proxy * a #

choose :: (a -> Either b c) -> Proxy * b -> Proxy * c -> Proxy * a #

Divisible m => Decidable (ListT m) 

Methods

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

choose :: (a -> Either b c) -> ListT m b -> ListT m c -> ListT m a #

Divisible m => Decidable (MaybeT m) 

Methods

lose :: (a -> Void) -> MaybeT m a #

choose :: (a -> Either b c) -> MaybeT m b -> MaybeT m c -> MaybeT m a #

Decidable f => Decidable (Rec1 * f) 

Methods

lose :: (a -> Void) -> Rec1 * f a #

choose :: (a -> Either b c) -> Rec1 * f b -> Rec1 * f c -> Rec1 * f a #

Decidable f => Decidable (Alt * f) 

Methods

lose :: (a -> Void) -> Alt * f a #

choose :: (a -> Either b c) -> Alt * f b -> Alt * f c -> Alt * f a #

Decidable f => Decidable (IdentityT * f) 

Methods

lose :: (a -> Void) -> IdentityT * f a #

choose :: (a -> Either b c) -> IdentityT * f b -> IdentityT * f c -> IdentityT * f a #

(Applicative f, Decidable g) => Decidable (ComposeFC f g) 

Methods

lose :: (a -> Void) -> ComposeFC f g a #

choose :: (a -> Either b c) -> ComposeFC f g b -> ComposeFC f g c -> ComposeFC f g a #

Decidable f => Decidable (Backwards * f) 

Methods

lose :: (a -> Void) -> Backwards * f a #

choose :: (a -> Either b c) -> Backwards * f b -> Backwards * f c -> Backwards * f a #

Decidable m => Decidable (StateT s m) 

Methods

lose :: (a -> Void) -> StateT s m a #

choose :: (a -> Either b c) -> StateT s m b -> StateT s m c -> StateT s m a #

Decidable m => Decidable (StateT s m) 

Methods

lose :: (a -> Void) -> StateT s m a #

choose :: (a -> Either b c) -> StateT s m b -> StateT s m c -> StateT s m a #

Decidable m => Decidable (WriterT w m) 

Methods

lose :: (a -> Void) -> WriterT w m a #

choose :: (a -> Either b c) -> WriterT w m b -> WriterT w m c -> WriterT w m a #

Decidable m => Decidable (WriterT w m) 

Methods

lose :: (a -> Void) -> WriterT w m a #

choose :: (a -> Either b c) -> WriterT w m b -> WriterT w m c -> WriterT w m a #

Decidable f => Decidable (Reverse * f) 

Methods

lose :: (a -> Void) -> Reverse * f a #

choose :: (a -> Either b c) -> Reverse * f b -> Reverse * f c -> Reverse * f a #

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

Methods

lose :: (a -> Void) -> (* :*: f) g a #

choose :: (a -> Either b c) -> (* :*: f) g b -> (* :*: f) g c -> (* :*: f) g a #

(Decidable f, Decidable g) => Decidable (Product * f g) 

Methods

lose :: (a -> Void) -> Product * f g a #

choose :: (a -> Either b c) -> Product * f g b -> Product * f g c -> Product * f g a #

Decidable m => Decidable (ReaderT * r m) 

Methods

lose :: (a -> Void) -> ReaderT * r m a #

choose :: (a -> Either b c) -> ReaderT * r m b -> ReaderT * r m c -> ReaderT * r m a #

Decidable f => Decidable (M1 * i c f) 

Methods

lose :: (a -> Void) -> M1 * i c f a #

choose :: (a -> Either b c) -> M1 * i c f b -> M1 * i c f c -> M1 * i c f a #

(Applicative f, Decidable g) => Decidable ((:.:) * * f g) 

Methods

lose :: (a -> Void) -> (* :.: *) f g a #

choose :: (a -> Either b c) -> (* :.: *) f g b -> (* :.: *) f g c -> (* :.: *) f g a #

(Applicative f, Decidable g) => Decidable (Compose * * f g) 

Methods

lose :: (a -> Void) -> Compose * * f g a #

choose :: (a -> Either b c) -> Compose * * f g b -> Compose * * f g c -> Compose * * f g a #

Decidable m => Decidable (RWST r w s m) 

Methods

lose :: (a -> Void) -> RWST r w s m a #

choose :: (a -> Either b c) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a #

Decidable m => Decidable (RWST r w s m) 

Methods

lose :: (a -> Void) -> RWST r w s m a #

choose :: (a -> Either b c) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a #

chosen :: Decidable f => f b -> f c -> f (Either b c) #