| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
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 ,
where Automaton m a (Maybe b)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
- newtype FilterAutomaton (m :: Type -> Type) (f :: Type -> Type) a b = FilterAutomaton {
- getFilterAutomaton :: Automaton m a (f b)
- fromFilterStream :: forall (m :: Type -> Type) (f :: Type -> Type) a any. Monad m => FilterStream m f a -> FilterAutomaton m f any a
- toFilterStream :: forall (m :: Type -> Type) (f :: Type -> Type) a b. Functor m => FilterAutomaton m f a b -> FilterStream (ReaderT a m) f b
- arrFilter :: forall (m :: Type -> Type) a f b. Monad m => (a -> f b) -> FilterAutomaton m f a b
- filterS :: forall (m :: Type -> Type) (f :: Type -> Type) a. (Monad m, Filterable f, Applicative f) => (a -> Bool) -> FilterAutomaton m f a a
- 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
- id' :: forall (m :: Type -> Type) (f :: Type -> Type) a. (Monad m, Applicative f) => FilterAutomaton m f a a
- liftFilter :: forall (m :: Type -> Type) (f :: Type -> Type) a b. (Monad m, Applicative f) => Automaton m a b -> FilterAutomaton m f a b
- 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
- 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
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 can filter in the sense that not every input necessarily leads to an output.FilterAutomaton Maybe a b
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
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.