functor-combinators-0.1.1.1: Tools for functor combinator-based program design

Copyright(c) Justin Le 2019
LicenseBSD3
Maintainerjustin@jle.im
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.HFunctor.Interpret

Contents

Description

This module provides tools for working with unary functor combinators that represent interpretable schemas.

These are types t that take a functor f and return a new functor t f, enhancing f with new structure and abilities.

For these, we have:

inject :: f a -> t f a

which lets you "lift" an f a into its transformed version, and also:

interpret
    :: C t g
    => (forall x. f a -> g a)
    -> t f a
    -> g a

that lets you "interpret" a t f a into a context g a, essentially "running" the computaiton that it encodes. The context is required to have a typeclass constraints that reflects what is "required" to be able to run a functor combinator.

Every single instance provides different tools. Check out the instance list for a nice list of useful combinators, or also the README for a high-level rundown.

See Data.Functor.Tensor for binary functor combinators that mix together two or more different functors.

Synopsis

Documentation

class Inject t => Interpret t where Source #

An Interpret lets us move in and out of the "enhanced" Functor.

For example, Free f is f enhanced with monadic structure. We get:

inject    :: f a -> Free f a
interpret :: Monad m => (forall x. f x -> m x) -> Free f a -> m a

inject will let us use our f inside the enhanced Free f. interpret will let us "extract" the f from a Free f if we can give an interpreting function that interprets f into some target Monad.

The type family C tells us the typeclass constraint of the "target" functor. For Free, it is Monad, but for other Interpret instances, we might have other constraints.

We enforce that:

interpret id . inject == id
-- or
retract . inject == id

That is, if we lift a value into our structure, then immediately interpret it out as itself, it should lave the value unchanged.

Minimal complete definition

retract | interpret

Associated Types

type C t :: (Type -> Type) -> Constraint Source #

The constraint on the target context of interpret. It's basically the constraint that allows you to "exit" or "run" an Interpret.

Methods

retract :: C t f => t f ~> f Source #

Remove the f out of the enhanced t f structure, provided that f satisfies the necessary constraints. If it doesn't, it needs to be properly interpreted out.

interpret :: C t g => (f ~> g) -> t f ~> g Source #

Given an "interpeting function" from f to g, interpret the f out of the t f into a final context g.

Instances
Interpret Ap Source #

A free Applicative

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Ap :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Ap f => Ap f ~> f Source #

interpret :: C Ap g => (f ~> g) -> Ap f ~> g Source #

Interpret Ap Source #

A free Applicative

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Ap :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Ap f => Ap f ~> f Source #

interpret :: C Ap g => (f ~> g) -> Ap f ~> g Source #

Interpret Ap Source #

A free Applicative

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Ap :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Ap f => Ap f ~> f Source #

interpret :: C Ap g => (f ~> g) -> Ap f ~> g Source #

Interpret Alt Source #

A free Alternative

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Alt :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Alt f => Alt f ~> f Source #

interpret :: C Alt g => (f ~> g) -> Alt f ~> g Source #

Interpret Coyoneda Source #

A free Functor

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Coyoneda :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Coyoneda f => Coyoneda f ~> f Source #

interpret :: C Coyoneda g => (f ~> g) -> Coyoneda f ~> g Source #

Interpret WrappedApplicative Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C WrappedApplicative :: (Type -> Type) -> Constraint Source #

Interpret MaybeApply Source #

A free Pointed

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C MaybeApply :: (Type -> Type) -> Constraint Source #

Methods

retract :: C MaybeApply f => MaybeApply f ~> f Source #

interpret :: C MaybeApply g => (f ~> g) -> MaybeApply f ~> g Source #

Interpret Lift Source #

A free Pointed

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Lift :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Lift f => Lift f ~> f Source #

interpret :: C Lift g => (f ~> g) -> Lift f ~> g Source #

Interpret ListF Source #

A free Plus

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C ListF :: (Type -> Type) -> Constraint Source #

Methods

retract :: C ListF f => ListF f ~> f Source #

interpret :: C ListF g => (f ~> g) -> ListF f ~> g Source #

Interpret NonEmptyF Source #

A free Alt

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C NonEmptyF :: (Type -> Type) -> Constraint Source #

Methods

retract :: C NonEmptyF f => NonEmptyF f ~> f Source #

interpret :: C NonEmptyF g => (f ~> g) -> NonEmptyF f ~> g Source #

Interpret MaybeF Source #

Technically, C is over-constrained: we only need zero :: f a, but we don't really have that typeclass in any standard hierarchies. We use Plus here instead, but we never use <!>. This would only go wrong in situations where your type supports zero but not <!>, like instances of MonadFail without MonadPlus.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C MaybeF :: (Type -> Type) -> Constraint Source #

Methods

retract :: C MaybeF f => MaybeF f ~> f Source #

interpret :: C MaybeF g => (f ~> g) -> MaybeF f ~> g Source #

Interpret Free1 Source #

A free Bind

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Free1 :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Free1 f => Free1 f ~> f Source #

interpret :: C Free1 g => (f ~> g) -> Free1 f ~> g Source #

Interpret Free Source #

A free Monad

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Free :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Free f => Free f ~> f Source #

interpret :: C Free g => (f ~> g) -> Free f ~> g Source #

Interpret Ap1 Source # 
Instance details

Defined in Data.Functor.Apply.Free

Associated Types

type C Ap1 :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Ap1 f => Ap1 f ~> f Source #

interpret :: C Ap1 g => (f ~> g) -> Ap1 f ~> g Source #

Monoid e => Interpret (EnvT e) Source #

This ignores the environment, so interpret /= hbind

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (EnvT e) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (EnvT e) f => EnvT e f ~> f Source #

interpret :: C (EnvT e) g => (f ~> g) -> EnvT e f ~> g Source #

Interpret (IdentityT :: (Type -> Type) -> Type -> Type) Source #

A free Unconstrained

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C IdentityT :: (Type -> Type) -> Constraint Source #

Methods

retract :: C IdentityT f => IdentityT f ~> f Source #

interpret :: C IdentityT g => (f ~> g) -> IdentityT f ~> g Source #

Interpret (These1 f) Source #

Technically, C is over-constrained: we only need zero :: f a, but we don't really have that typeclass in any standard hierarchies. We use Plus here instead, but we never use <!>. This would only go wrong in situations where your type supports zero but not <!>, like instances of MonadFail without MonadPlus.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (These1 f) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (These1 f) f0 => These1 f f0 ~> f0 Source #

interpret :: C (These1 f) g => (f0 ~> g) -> These1 f f0 ~> g Source #

Interpret (Reverse :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Reverse :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Reverse f => Reverse f ~> f Source #

interpret :: C Reverse g => (f ~> g) -> Reverse f ~> g Source #

Interpret (Backwards :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Backwards :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Backwards f => Backwards f ~> f Source #

interpret :: C Backwards g => (f ~> g) -> Backwards f ~> g Source #

Monoid k => Interpret (MapF k) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (MapF k) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (MapF k) f => MapF k f ~> f Source #

interpret :: C (MapF k) g => (f ~> g) -> MapF k f ~> g Source #

Monoid k => Interpret (NEMapF k) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (NEMapF k) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (NEMapF k) f => NEMapF k f ~> f Source #

interpret :: C (NEMapF k) g => (f ~> g) -> NEMapF k f ~> g Source #

Interpret (Step :: (Type -> Type) -> Type -> Type) Source #

Equivalent to instance for EnvT (Sum Natural).

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Step :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Step f => Step f ~> f Source #

interpret :: C Step g => (f ~> g) -> Step f ~> g Source #

Interpret (Steps :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Steps :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Steps f => Steps f ~> f Source #

interpret :: C Steps g => (f ~> g) -> Steps f ~> g Source #

Interpret (Flagged :: (Type -> Type) -> Type -> Type) Source #

Equivalent to instance for EnvT Any and HLift IdentityT.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C Flagged :: (Type -> Type) -> Constraint Source #

Methods

retract :: C Flagged f => Flagged f ~> f Source #

interpret :: C Flagged g => (f ~> g) -> Flagged f ~> g Source #

Interpret (Final c) Source # 
Instance details

Defined in Data.HFunctor.Final

Associated Types

type C (Final c) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (Final c) f => Final c f ~> f Source #

interpret :: C (Final c) g => (f ~> g) -> Final c f ~> g Source #

Interpret ((:+:) f) Source #

Technically, C is over-constrained: we only need zero :: f a, but we don't really have that typeclass in any standard hierarchies. We use Plus here instead, but we never use <!>. This would only go wrong in situations where your type supports zero but not <!>, like instances of MonadFail without MonadPlus.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C ((:+:) f) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C ((:+:) f) f0 => (f :+: f0) ~> f0 Source #

interpret :: C ((:+:) f) g => (f0 ~> g) -> (f :+: f0) ~> g Source #

Plus f => Interpret ((:*:) f) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C ((:*:) f) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C ((:*:) f) f0 => (f :*: f0) ~> f0 Source #

interpret :: C ((:*:) f) g => (f0 ~> g) -> (f :*: f0) ~> g Source #

Plus f => Interpret (Product f) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (Product f) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (Product f) f0 => Product f f0 ~> f0 Source #

interpret :: C (Product f) g => (f0 ~> g) -> Product f f0 ~> g Source #

Interpret (Sum f) Source #

Technically, C is over-constrained: we only need zero :: f a, but we don't really have that typeclass in any standard hierarchies. We use Plus here instead, but we never use <!>. This would only go wrong in situations where your type supports zero but not <!>, like instances of MonadFail without MonadPlus.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (Sum f) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (Sum f) f0 => Sum f f0 ~> f0 Source #

interpret :: C (Sum f) g => (f0 ~> g) -> Sum f f0 ~> g Source #

(Interpret s, Interpret t) => Interpret (ComposeT s t) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (ComposeT s t) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (ComposeT s t) f => ComposeT s t f ~> f Source #

interpret :: C (ComposeT s t) g => (f ~> g) -> ComposeT s t f ~> g Source #

Interpret (ReaderT r :: (Type -> Type) -> Type -> Type) Source #

A free MonadReader, but only when applied to a Monad.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (ReaderT r) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (ReaderT r) f => ReaderT r f ~> f Source #

interpret :: C (ReaderT r) g => (f ~> g) -> ReaderT r f ~> g Source #

Interpret (ProxyF :: (Type -> Type) -> Type -> Type) Source #

The only way for this to obey retract . inject == id is to have it impossible to retract out of.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C ProxyF :: (Type -> Type) -> Constraint Source #

Methods

retract :: C ProxyF f => ProxyF f ~> f Source #

interpret :: C ProxyF g => (f ~> g) -> ProxyF f ~> g Source #

Interpret t => Interpret (HFree t) Source #

Never uses inject

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (HFree t) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (HFree t) f => HFree t f ~> f Source #

interpret :: C (HFree t) g => (f ~> g) -> HFree t f ~> g Source #

Interpret t => Interpret (HLift t) Source #

Never uses inject

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (HLift t) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (HLift t) f => HLift t f ~> f Source #

interpret :: C (HLift t) g => (f ~> g) -> HLift t f ~> g Source #

(HBifunctor t, Semigroupoidal t) => Interpret (Chain1 t) Source # 
Instance details

Defined in Data.HFunctor.Chain

Associated Types

type C (Chain1 t) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (Chain1 t) f => Chain1 t f ~> f Source #

interpret :: C (Chain1 t) g => (f ~> g) -> Chain1 t f ~> g Source #

Interpret (M1 i c :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (M1 i c) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (M1 i c) f => M1 i c f ~> f Source #

interpret :: C (M1 i c) g => (f ~> g) -> M1 i c f ~> g Source #

Monoid e => Interpret (ConstF e :: (Type -> Type) -> Type -> Type) Source #

The only way for this to obey retract . inject == id is to have it impossible to retract out of.

Instance details

Defined in Data.HFunctor.Interpret

Associated Types

type C (ConstF e) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (ConstF e) f => ConstF e f ~> f Source #

interpret :: C (ConstF e) g => (f ~> g) -> ConstF e f ~> g Source #

Interpret (RightF f :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HBifunctor

Associated Types

type C (RightF f) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (RightF f) f0 => RightF f f0 ~> f0 Source #

interpret :: C (RightF f) g => (f0 ~> g) -> RightF f f0 ~> g Source #

(Monoidal t, i ~ I t) => Interpret (Chain t i) Source #

We can collapse and interpret an Chain t i if we have Tensor t.

Instance details

Defined in Data.HFunctor.Chain

Associated Types

type C (Chain t i) :: (Type -> Type) -> Constraint Source #

Methods

retract :: C (Chain t i) f => Chain t i f ~> f Source #

interpret :: C (Chain t i) g => (f ~> g) -> Chain t i f ~> g Source #

forI :: (Interpret t, C t g) => t f a -> (f ~> g) -> g a Source #

A convenient flipped version of interpret.

Utilities

getI :: (Interpret t, C t (Const b)) => (forall x. f x -> b) -> t f a -> b Source #

Useful wrapper over interpret to allow you to directly extract a value b out of the t f a, if you can convert f x into b.

Note that depending on the constraints on the interpretation of t, you may have extra constraints on b.

For some constraints (like Monad), this will not be usable.

-- get the length of the Map String in the Step.
collectI length
     :: Step (Map String) Bool
     -> Int

collectI :: (Interpret t, C t (Const [b])) => (forall x. f x -> b) -> t f a -> [b] Source #

Useful wrapper over getI to allow you to collect a b from all instances of f inside a t f a.

This will work if C t is Unconstrained, Apply, or Applicative.

-- get the lengths of all Map Strings in the Ap.
collectI length
     :: Ap (Map String) Bool
     -> [Int]

class (c a, d a) => AndC c d a Source #

A constraint on a for both c a and d a. Requiring AndC Show Eq a is the same as requiring (Show a, Eq a).

Instances
(c a, d a) => AndC (c :: k -> Constraint) (d :: k -> Constraint) (a :: k) Source # 
Instance details

Defined in Data.HFunctor.Interpret