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.Final

Description

Provides Final, which can be considered the "free Interpret over a constraint": generate a handy Interpret instance for any constraint c.

Synopsis

Documentation

newtype Final c f a Source #

A simple way to inject/reject into any eventual typeclass.

In a way, this is the "ultimate" multi-purpose Interpret instance. You can use this to inject an f into a free structure of any typeclass. If you want f to have a Monad instance, for example, just use

inject :: f a -> Final Monad f a

When you want to eventually interpret out the data, use:

interpret :: (f ~> g) -> Final c f a -> g a

Essentially, Final c is the "free c". Final Monad is the free Monad, etc.

Final can theoretically replace Ap, Ap1, ListF, NonEmptyF, MaybeF, Free, Identity, Coyoneda, and other instances of FreeOf, if you don't care about being able to pattern match on explicit structure.

However, it cannot replace Interpret instances that are not free structures, like Step, Steps, Backwards, etc.

Note that this doesn't have instances for all the typeclasses you could lift things into; you probably have to define your own if you want to use Final c as an instance of c (using liftFinal0, liftFinal1, liftFinal2 for help).

Constructors

Final 

Fields

  • runFinal :: forall g. c g => (forall x. f x -> g x) -> g a
     
Instances
MonadReader r (Final (MonadReader r) f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

ask :: Final (MonadReader r) f r #

local :: (r -> r) -> Final (MonadReader r) f a -> Final (MonadReader r) f a #

reader :: (r -> a) -> Final (MonadReader r) f a #

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 #

Inject (Final c :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

inject :: f ~> Final c f Source #

HFunctor (Final c :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

hmap :: (f ~> g) -> Final c f ~> Final c g Source #

Monad (Final Monad f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(>>=) :: Final Monad f a -> (a -> Final Monad f b) -> Final Monad f b #

(>>) :: Final Monad f a -> Final Monad f b -> Final Monad f b #

return :: a -> Final Monad f a #

fail :: String -> Final Monad f a #

Monad (Final (MonadReader r) f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(>>=) :: Final (MonadReader r) f a -> (a -> Final (MonadReader r) f b) -> Final (MonadReader r) f b #

(>>) :: Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f b #

return :: a -> Final (MonadReader r) f a #

fail :: String -> Final (MonadReader r) f a #

Monad (Final MonadPlus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(>>=) :: Final MonadPlus f a -> (a -> Final MonadPlus f b) -> Final MonadPlus f b #

(>>) :: Final MonadPlus f a -> Final MonadPlus f b -> Final MonadPlus f b #

return :: a -> Final MonadPlus f a #

fail :: String -> Final MonadPlus f a #

Functor (Final Monad f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Monad f a -> Final Monad f b #

(<$) :: a -> Final Monad f b -> Final Monad f a #

Functor (Final Functor f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Functor f a -> Final Functor f b #

(<$) :: a -> Final Functor f b -> Final Functor f a #

Functor (Final Applicative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Applicative f a -> Final Applicative f b #

(<$) :: a -> Final Applicative f b -> Final Applicative f a #

Functor (Final (MonadReader r) f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final (MonadReader r) f a -> Final (MonadReader r) f b #

(<$) :: a -> Final (MonadReader r) f b -> Final (MonadReader r) f a #

Functor (Final Alternative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Alternative f a -> Final Alternative f b #

(<$) :: a -> Final Alternative f b -> Final Alternative f a #

Functor (Final MonadPlus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final MonadPlus f a -> Final MonadPlus f b #

(<$) :: a -> Final MonadPlus f b -> Final MonadPlus f a #

Functor (Final Apply f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Apply f a -> Final Apply f b #

(<$) :: a -> Final Apply f b -> Final Apply f a #

Functor (Final Plus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Plus f a -> Final Plus f b #

(<$) :: a -> Final Plus f b -> Final Plus f a #

Functor (Final Alt f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Alt f a -> Final Alt f b #

(<$) :: a -> Final Alt f b -> Final Alt f a #

Functor (Final Bind f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

fmap :: (a -> b) -> Final Bind f a -> Final Bind f b #

(<$) :: a -> Final Bind f b -> Final Bind f a #

Applicative (Final Monad f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

pure :: a -> Final Monad f a #

(<*>) :: Final Monad f (a -> b) -> Final Monad f a -> Final Monad f b #

liftA2 :: (a -> b -> c) -> Final Monad f a -> Final Monad f b -> Final Monad f c #

(*>) :: Final Monad f a -> Final Monad f b -> Final Monad f b #

(<*) :: Final Monad f a -> Final Monad f b -> Final Monad f a #

Applicative (Final Applicative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

pure :: a -> Final Applicative f a #

(<*>) :: Final Applicative f (a -> b) -> Final Applicative f a -> Final Applicative f b #

liftA2 :: (a -> b -> c) -> Final Applicative f a -> Final Applicative f b -> Final Applicative f c #

(*>) :: Final Applicative f a -> Final Applicative f b -> Final Applicative f b #

(<*) :: Final Applicative f a -> Final Applicative f b -> Final Applicative f a #

Applicative (Final (MonadReader r) f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

pure :: a -> Final (MonadReader r) f a #

(<*>) :: Final (MonadReader r) f (a -> b) -> Final (MonadReader r) f a -> Final (MonadReader r) f b #

liftA2 :: (a -> b -> c) -> Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f c #

(*>) :: Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f b #

(<*) :: Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f a #

Applicative (Final Alternative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

pure :: a -> Final Alternative f a #

(<*>) :: Final Alternative f (a -> b) -> Final Alternative f a -> Final Alternative f b #

liftA2 :: (a -> b -> c) -> Final Alternative f a -> Final Alternative f b -> Final Alternative f c #

(*>) :: Final Alternative f a -> Final Alternative f b -> Final Alternative f b #

(<*) :: Final Alternative f a -> Final Alternative f b -> Final Alternative f a #

Applicative (Final MonadPlus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

pure :: a -> Final MonadPlus f a #

(<*>) :: Final MonadPlus f (a -> b) -> Final MonadPlus f a -> Final MonadPlus f b #

liftA2 :: (a -> b -> c) -> Final MonadPlus f a -> Final MonadPlus f b -> Final MonadPlus f c #

(*>) :: Final MonadPlus f a -> Final MonadPlus f b -> Final MonadPlus f b #

(<*) :: Final MonadPlus f a -> Final MonadPlus f b -> Final MonadPlus f a #

Alternative (Final Alternative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Alternative (Final MonadPlus f) Source # 
Instance details

Defined in Data.HFunctor.Final

MonadPlus (Final MonadPlus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Apply (Final Monad f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(<.>) :: Final Monad f (a -> b) -> Final Monad f a -> Final Monad f b #

(.>) :: Final Monad f a -> Final Monad f b -> Final Monad f b #

(<.) :: Final Monad f a -> Final Monad f b -> Final Monad f a #

liftF2 :: (a -> b -> c) -> Final Monad f a -> Final Monad f b -> Final Monad f c #

Apply (Final Applicative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Apply (Final (MonadReader r) f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(<.>) :: Final (MonadReader r) f (a -> b) -> Final (MonadReader r) f a -> Final (MonadReader r) f b #

(.>) :: Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f b #

(<.) :: Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f a #

liftF2 :: (a -> b -> c) -> Final (MonadReader r) f a -> Final (MonadReader r) f b -> Final (MonadReader r) f c #

Apply (Final Alternative f) Source # 
Instance details

Defined in Data.HFunctor.Final

Apply (Final Apply f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(<.>) :: Final Apply f (a -> b) -> Final Apply f a -> Final Apply f b #

(.>) :: Final Apply f a -> Final Apply f b -> Final Apply f b #

(<.) :: Final Apply f a -> Final Apply f b -> Final Apply f a #

liftF2 :: (a -> b -> c) -> Final Apply f a -> Final Apply f b -> Final Apply f c #

Apply (Final Bind f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(<.>) :: Final Bind f (a -> b) -> Final Bind f a -> Final Bind f b #

(.>) :: Final Bind f a -> Final Bind f b -> Final Bind f b #

(<.) :: Final Bind f a -> Final Bind f b -> Final Bind f a #

liftF2 :: (a -> b -> c) -> Final Bind f a -> Final Bind f b -> Final Bind f c #

Pointed (Final Pointed f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

point :: a -> Final Pointed f a #

Plus (Final Plus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

zero :: Final Plus f a #

Alt (Final Plus f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(<!>) :: Final Plus f a -> Final Plus f a -> Final Plus f a #

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

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

Alt (Final Alt f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

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

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

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

Bind (Final Bind f) Source # 
Instance details

Defined in Data.HFunctor.Final

Methods

(>>-) :: Final Bind f a -> (a -> Final Bind f b) -> Final Bind f b #

join :: Final Bind f (Final Bind f a) -> Final Bind f a #

type C (Final c) Source # 
Instance details

Defined in Data.HFunctor.Final

type C (Final c) = c

fromFinal :: (Interpret t, c (t f)) => Final c f ~> t f Source #

toFinal :: (Interpret t, C t (Final c f)) => t f ~> Final c f Source #

Finalize an Interpret instance.

toFinal :: Coyoneda f ~> Final Functor f
toFinal :: Ap f ~> Final Applicative f
toFinal :: Alt f ~> Final Alternative f
toFinal :: Free f ~> Final Monad f
toFinal :: Lift f ~> Final Pointed f
toFinal :: ListF f ~> Final Plus f

Note that the instance of c for Final c must be defined.

This operation can potentially forget structure in t. For example, we have:

toFinal :: Steps f ~> Final Alt f

In this process, we lose the "positional" structure of Steps.

In the case where toFinal doesn't lose any information, this will form an isomorphism with fromFinal, and t is known as the "Free c". For such a situation, t will have a FreeOf instance.

class Interpret t => FreeOf c t | t -> c where Source #

A typeclass associating a free structure with the typeclass it is free on.

This essentially lists instances of Interpret where a "trip" through Final will leave it unchanged.

fromFree . toFree == id
toFree . fromFree == id

This can be useful because Final doesn't have a concrete structure that you can pattern match on and inspect, but t might. This lets you work on a concrete structure if you desire.

Minimal complete definition

Nothing

Methods

fromFree :: t f ~> Final c f Source #

toFree :: Functor f => Final c f ~> t f Source #

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

toFree :: c (t f) => Final c f ~> t f Source #

Instances
FreeOf Monad Free Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Functor Coyoneda Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Applicative Ap Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Applicative Ap Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Alternative Alt Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Apply Ap1 Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Pointed MaybeApply Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Pointed Lift Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Plus ListF Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Alt NonEmptyF Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf Bind Free1 Source # 
Instance details

Defined in Data.HFunctor.Final

FreeOf (Unconstrained :: (Type -> Type) -> Constraint) (IdentityT :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Data.HFunctor.Final

finalizing :: (FreeOf c t, Functor f) => t f <~> Final c f Source #

The isomorphism between a free structure and its encoding as Final.

hoistFinalC :: (forall g x. (c g => g x) -> d g => g x) -> Final c f a -> Final d f a Source #

Re-interpret the context under a Final.

liftFinal0 :: (forall g. c g => g a) -> Final c f a Source #

Lift an action into a Final.

liftFinal1 :: (forall g. c g => g a -> g b) -> Final c f a -> Final c f b Source #

Map the action in a Final.

liftFinal2 :: (forall g. c g => g a -> g b -> g d) -> Final c f a -> Final c f b -> Final c f d Source #

Merge two Final actions.