automaton-1.6: Effectful streams and automata in coalgebraic encoding
Safe HaskellNone
LanguageHaskell2010

Data.Automaton.Filter

Description

Often, we want to work with automata that don't produce exactly one output per input. In these situations, FilterAutomaton is a good abstraction.

For example, we might want to filter out certain values at some point, and only work with the remaining ones from there on. So for each input, the automaton will either output a value, or it will not output a value. A simple ad hoc solution is to create an automaton of type Automaton m a (Maybe b), where Nothing encodes the absence of a value. When composing a longer chain of such filtering automata, it often gets tedious to join all of the Maybe layers. FilterAutomaton abstracts this.

Instead of locking into the concrete Maybe type, a more general API based on well-established type classes such as Witherable was chosen. As a result, it can be used with a custom type encoding optionality, and even generalised to situations where you might have several outputs per input, using e.g. the list monad.

Synopsis

Documentation

newtype FilterAutomaton (m :: Type -> Type) (f :: Type -> Type) a b Source #

An automaton that filters or traverses its output using a type operator f.

When f is Maybe, then FilterAutomaton Maybe a b can filter in the sense that not every input necessarily leads to an output.

f can also be a type that allows multiple positions, such as lists, or NonEmpty. In this case, FilterAutomaton branches out and can explore multiple outputs at the same time. (It keeps a single state, though.)

Constructors

FilterAutomaton 

Fields

Instances

Instances details
(Monad m, Traversable f, Monad f) => Category (FilterAutomaton m f :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

id :: FilterAutomaton m f a a #

(.) :: FilterAutomaton m f b c -> FilterAutomaton m f a b -> FilterAutomaton m f a c #

(Monad m, Traversable f, Monad f) => Arrow (FilterAutomaton m f) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

arr :: (b -> c) -> FilterAutomaton m f b c #

first :: FilterAutomaton m f b c -> FilterAutomaton m f (b, d) (c, d) #

second :: FilterAutomaton m f b c -> FilterAutomaton m f (d, b) (d, c) #

(***) :: FilterAutomaton m f b c -> FilterAutomaton m f b' c' -> FilterAutomaton m f (b, b') (c, c') #

(&&&) :: FilterAutomaton m f b c -> FilterAutomaton m f b c' -> FilterAutomaton m f b (c, c') #

(Traversable f, Monad m, Monad f) => ArrowChoice (FilterAutomaton m f) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

left :: FilterAutomaton m f b c -> FilterAutomaton m f (Either b d) (Either c d) #

right :: FilterAutomaton m f b c -> FilterAutomaton m f (Either d b) (Either d c) #

(+++) :: FilterAutomaton m f b c -> FilterAutomaton m f b' c' -> FilterAutomaton m f (Either b b') (Either c c') #

(|||) :: FilterAutomaton m f b d -> FilterAutomaton m f c d -> FilterAutomaton m f (Either b c) d #

(Monad m, Monad f, Traversable f) => Choice (FilterAutomaton m f) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

left' :: FilterAutomaton m f a b -> FilterAutomaton m f (Either a c) (Either b c) #

right' :: FilterAutomaton m f a b -> FilterAutomaton m f (Either c a) (Either c b) #

(Monad m, Traversable f) => Cochoice (FilterAutomaton m f) Source #

When looping, this will break when any of the positions in f breaks.

Instance details

Defined in Data.Automaton.Filter

Methods

unleft :: FilterAutomaton m f (Either a d) (Either b d) -> FilterAutomaton m f a b #

unright :: FilterAutomaton m f (Either d a) (Either d b) -> FilterAutomaton m f a b #

(Monad m, Monad f, Traversable f) => Strong (FilterAutomaton m f) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

first' :: FilterAutomaton m f a b -> FilterAutomaton m f (a, c) (b, c) #

second' :: FilterAutomaton m f a b -> FilterAutomaton m f (c, a) (c, b) #

(Functor m, Functor f) => Profunctor (FilterAutomaton m f) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

dimap :: (a -> b) -> (c -> d) -> FilterAutomaton m f b c -> FilterAutomaton m f a d #

lmap :: (a -> b) -> FilterAutomaton m f b c -> FilterAutomaton m f a c #

rmap :: (b -> c) -> FilterAutomaton m f a b -> FilterAutomaton m f a c #

(#.) :: forall a b c q. Coercible c b => q b c -> FilterAutomaton m f a b -> FilterAutomaton m f a c #

(.#) :: forall a b c q. Coercible b a => FilterAutomaton m f b c -> q a b -> FilterAutomaton m f a c #

(Alternative m, Applicative f) => Alternative (FilterAutomaton m f a) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

empty :: FilterAutomaton m f a a0 #

(<|>) :: FilterAutomaton m f a a0 -> FilterAutomaton m f a a0 -> FilterAutomaton m f a a0 #

some :: FilterAutomaton m f a a0 -> FilterAutomaton m f a [a0] #

many :: FilterAutomaton m f a a0 -> FilterAutomaton m f a [a0] #

(Applicative m, Applicative f) => Applicative (FilterAutomaton m f a) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

pure :: a0 -> FilterAutomaton m f a a0 #

(<*>) :: FilterAutomaton m f a (a0 -> b) -> FilterAutomaton m f a a0 -> FilterAutomaton m f a b #

liftA2 :: (a0 -> b -> c) -> FilterAutomaton m f a a0 -> FilterAutomaton m f a b -> FilterAutomaton m f a c #

(*>) :: FilterAutomaton m f a a0 -> FilterAutomaton m f a b -> FilterAutomaton m f a b #

(<*) :: FilterAutomaton m f a a0 -> FilterAutomaton m f a b -> FilterAutomaton m f a a0 #

(Functor m, Functor f) => Functor (FilterAutomaton m f a) Source # 
Instance details

Defined in Data.Automaton.Filter

Methods

fmap :: (a0 -> b) -> FilterAutomaton m f a a0 -> FilterAutomaton m f a b #

(<$) :: a0 -> FilterAutomaton m f a b -> FilterAutomaton m f a a0 #

(Applicative m, Align f) => Align (FilterAutomaton m f a) Source #

Constantly output the empty shape.

Instance details

Defined in Data.Automaton.Filter

Methods

nil :: FilterAutomaton m f a a0 #

(Applicative m, Semialign f) => Semialign (FilterAutomaton m f a) Source #

Run two automata in parallel and align their outputs.

Instance details

Defined in Data.Automaton.Filter

Methods

align :: FilterAutomaton m f a a0 -> FilterAutomaton m f a b -> FilterAutomaton m f a (These a0 b) #

alignWith :: (These a0 b -> c) -> FilterAutomaton m f a a0 -> FilterAutomaton m f a b -> FilterAutomaton m f a c #

(Functor m, Filterable f) => Filterable (FilterAutomaton m f a) Source #

There is no Witherable instance though since it isn't Traversable.

Instance details

Defined in Data.Automaton.Filter

Methods

mapMaybe :: (a0 -> Maybe b) -> FilterAutomaton m f a a0 -> FilterAutomaton m f a b #

catMaybes :: FilterAutomaton m f a (Maybe a0) -> FilterAutomaton m f a a0 #

filter :: (a0 -> Bool) -> FilterAutomaton m f a a0 -> FilterAutomaton m f a a0 #

drain :: FilterAutomaton m f a a0 -> FilterAutomaton m f a b #

fromFilterStream :: forall (m :: Type -> Type) (f :: Type -> Type) a any. Monad m => FilterStream m f a -> FilterAutomaton m f any a Source #

Create a FilterAutomaton that ignores its input from a FilterStream.

toFilterStream :: forall (m :: Type -> Type) (f :: Type -> Type) a b. Functor m => FilterAutomaton m f a b -> FilterStream (ReaderT a m) f b Source #

Create a FilterStream from a FilterAutomaton.

The current input can be read as an effect in ReaderT.

arrFilter :: forall (m :: Type -> Type) a f b. Monad m => (a -> f b) -> FilterAutomaton m f a b Source #

Use a filtering function to create a FilterAutomaton.

filterS :: forall (m :: Type -> Type) (f :: Type -> Type) a. (Monad m, Filterable f, Applicative f) => (a -> Bool) -> FilterAutomaton m f a a Source #

Filter the input according to a predicate.

automatonFilter :: forall (f :: Type -> Type) (m :: Type -> Type) a b. (Monad f, Traversable f, Monad m) => Automaton (Compose m f) a b -> FilterAutomaton m f a b Source #

Given an f-effect in the step, push it into the output type.

This works by internally tracking the f effects in the state, and at the same time joining them in the output.

For example, if f is lists, and automaton :: Automaton (Compose m []) a b creates a 2-element list at some point, the internal state of automatonFilter automaton will split into two, and there are two outputs.

Likewise, if f is Maybe, and a Nothing occurs at some point, then this automaton is deactivated forever.

id' :: forall (m :: Type -> Type) (f :: Type -> Type) a. (Monad m, Applicative f) => FilterAutomaton m f a a Source #

Like id, but only requiring Applicative f.

liftFilter :: forall (m :: Type -> Type) (f :: Type -> Type) a b. (Monad m, Applicative f) => Automaton m a b -> FilterAutomaton m f a b Source #

Lift a regular Automaton (which doesn't filter) to a FilterAutomaton.

rmapS :: forall (f :: Type -> Type) (m :: Type -> Type) a b c. (Traversable f, Monad m) => FilterAutomaton m f a b -> Automaton m b c -> FilterAutomaton m f a c Source #

Postcompose with an Automaton.

The postcomposed automaton will be stepped for every output of the filter automaton.

lmapS :: forall (m :: Type -> Type) a b (f :: Type -> Type) c. Monad m => Automaton m a b -> FilterAutomaton m f b c -> FilterAutomaton m f a c Source #

Precompose with an Automaton.