-- |
-- Module      : Data.HFunctor.Final
-- Copyright   : (c) Justin Le 2019
-- License     : BSD3
--
-- Maintainer  : justin@jle.im
-- Stability   : experimental
-- Portability : non-portable
--
-- Provides 'Final', which can be considered the "free 'Interpret' over
-- a constraint": generate a handy 'Interpret' instance for any constraint
-- @c@.
module Data.HFunctor.Final (
    Final(..)
  , fromFinal, toFinal
  , FreeOf(..), finalizing
  , hoistFinalC
  , liftFinal0
  , liftFinal1
  , liftFinal2
  ) where

import           Control.Applicative
import           Control.Applicative.Free
import           Control.Applicative.Lift
import           Control.Applicative.ListF
import           Control.Monad
import           Control.Monad.Freer.Church hiding         (toFree)
import           Control.Monad.Reader
import           Control.Monad.Trans.Identity
import           Control.Natural
import           Control.Natural.IsoF
import           Data.Constraint.Trivial
import           Data.Functor.Apply.Free
import           Data.Functor.Bind
import           Data.Functor.Contravariant
import           Data.Functor.Contravariant.Conclude
import           Data.Functor.Contravariant.Decide
import           Data.Functor.Contravariant.Divise
import           Data.Functor.Contravariant.Divisible
import           Data.Functor.Contravariant.Divisible.Free
import           Data.Functor.Coyoneda
import           Data.Functor.Invariant
import           Data.Functor.Invariant.Inplicative
import           Data.Functor.Invariant.Inplicative.Free
import           Data.Functor.Invariant.Internative
import           Data.Functor.Invariant.Internative.Free
import           Data.Functor.Plus
import           Data.HFunctor
import           Data.HFunctor.Interpret
import           Data.Kind
import           Data.Pointed
import qualified Control.Alternative.Free                  as Alt
import qualified Control.Applicative.Free.Fast             as FAF
import qualified Data.Functor.Contravariant.Coyoneda       as CCY

-- | 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', 'Data.Functor.Identity.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 'Control.Applicative.Step.Step',
-- 'Control.Applicative.Step.Steps',
-- 'Control.Applicative.Backwards.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).
newtype Final c f a = Final
    { forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal :: forall g. c g => (forall x. f x -> g x) -> g a }

-- | Lift an action into a 'Final'.
liftFinal0
    :: (forall g. c g => g a)
    -> Final c f a
liftFinal0 :: forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (g :: k -> *). c g => g a
x = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall (x :: k). f x -> g x
_ -> forall (g :: k -> *). c g => g a
x

-- | Map the action in a 'Final'.
liftFinal1
    :: (forall g. c g => g a -> g b)
    -> Final c f a
    -> Final c f b
liftFinal1 :: forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 forall (g :: k -> *). c g => g a -> g b
f Final c f a
x = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall (x :: k). f x -> g x
r -> forall (g :: k -> *). c g => g a -> g b
f (forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final c f a
x forall (x :: k). f x -> g x
r)

-- | Merge two 'Final' actions.
liftFinal2
    :: (forall g. c g => g a -> g b -> g d)
    -> Final c f a
    -> Final c f b
    -> Final c f d
liftFinal2 :: forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (g :: k -> *). c g => g a -> g b -> g d
f Final c f a
x Final c f b
y = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall (x :: k). f x -> g x
r -> forall (g :: k -> *). c g => g a -> g b -> g d
f (forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final c f a
x forall (x :: k). f x -> g x
r) (forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final c f b
y forall (x :: k). f x -> g x
r)

instance Functor (Final Functor f) where
    fmap :: forall a b. (a -> b) -> Final Functor f a -> Final Functor f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)

instance Functor (Final Apply f) where
    fmap :: forall a b. (a -> b) -> Final Apply f a -> Final Apply f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Apply (Final Apply f) where
    <.> :: forall a b.
Final Apply f (a -> b) -> Final Apply f a -> Final Apply f b
(<.>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
(<.>)
    liftF2 :: forall a b c.
(a -> b -> c)
-> Final Apply f a -> Final Apply f b -> Final Apply f c
liftF2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 a -> b -> c
f)

instance Functor (Final Bind f) where
    fmap :: forall a b. (a -> b) -> Final Bind f a -> Final Bind f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Apply (Final Bind f) where
    <.> :: forall a b.
Final Bind f (a -> b) -> Final Bind f a -> Final Bind f b
(<.>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
(<.>)
    liftF2 :: forall a b c.
(a -> b -> c) -> Final Bind f a -> Final Bind f b -> Final Bind f c
liftF2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 a -> b -> c
f)
instance Bind (Final Bind f) where
    Final Bind f a
x >>- :: forall a b.
Final Bind f a -> (a -> Final Bind f b) -> Final Bind f b
>>- a -> Final Bind f b
f = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
r -> forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final Bind f a
x forall x. f x -> g x
r forall (m :: * -> *) a b. Bind m => m a -> (a -> m b) -> m b
>>- \a
y -> forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal (a -> Final Bind f b
f a
y) forall x. f x -> g x
r

instance Functor (Final Applicative f) where
    fmap :: forall a b.
(a -> b) -> Final Applicative f a -> Final Applicative f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Apply (Final Applicative f) where
    <.> :: forall a b.
Final Applicative f (a -> b)
-> Final Applicative f a -> Final Applicative f b
(<.>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftF2 :: forall a b c.
(a -> b -> c)
-> Final Applicative f a
-> Final Applicative f b
-> Final Applicative f c
liftF2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Applicative (Final Applicative f) where
    pure :: forall a. a -> Final Applicative f a
pure a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    <*> :: forall a b.
Final Applicative f (a -> b)
-> Final Applicative f a -> Final Applicative f b
(<*>)  = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftA2 :: forall a b c.
(a -> b -> c)
-> Final Applicative f a
-> Final Applicative f b
-> Final Applicative f c
liftA2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)

instance Functor (Final Alternative f) where
    fmap :: forall a b.
(a -> b) -> Final Alternative f a -> Final Alternative f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Apply (Final Alternative f) where
    <.> :: forall a b.
Final Alternative f (a -> b)
-> Final Alternative f a -> Final Alternative f b
(<.>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftF2 :: forall a b c.
(a -> b -> c)
-> Final Alternative f a
-> Final Alternative f b
-> Final Alternative f c
liftF2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Applicative (Final Alternative f) where
    pure :: forall a. a -> Final Alternative f a
pure a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    <*> :: forall a b.
Final Alternative f (a -> b)
-> Final Alternative f a -> Final Alternative f b
(<*>)  = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftA2 :: forall a b c.
(a -> b -> c)
-> Final Alternative f a
-> Final Alternative f b
-> Final Alternative f c
liftA2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
-- | @since 0.3.0.0
instance Alt (Final Alternative f) where
    <!> :: forall a.
Final Alternative f a
-> Final Alternative f a -> Final Alternative f a
(<!>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
-- | @since 0.3.0.0
instance Plus (Final Alternative f) where
    zero :: forall a. Final Alternative f a
zero = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Alternative f => f a
empty
instance Alternative (Final Alternative f) where
    empty :: forall a. Final Alternative f a
empty = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Alternative f => f a
empty
    <|> :: forall a.
Final Alternative f a
-> Final Alternative f a -> Final Alternative f a
(<|>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)

instance Functor (Final Monad f) where
    fmap :: forall a b. (a -> b) -> Final Monad f a -> Final Monad f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Apply (Final Monad f) where
    <.> :: forall a b.
Final Monad f (a -> b) -> Final Monad f a -> Final Monad f b
(<.>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftF2 :: forall a b c.
(a -> b -> c)
-> Final Monad f a -> Final Monad f b -> Final Monad f c
liftF2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Applicative (Final Monad f) where
    pure :: forall a. a -> Final Monad f a
pure a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    <*> :: forall a b.
Final Monad f (a -> b) -> Final Monad f a -> Final Monad f b
(<*>)  = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftA2 :: forall a b c.
(a -> b -> c)
-> Final Monad f a -> Final Monad f b -> Final Monad f c
liftA2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Monad (Final Monad f) where
    Final Monad f a
x >>= :: forall a b.
Final Monad f a -> (a -> Final Monad f b) -> Final Monad f b
>>= a -> Final Monad f b
f  = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
r -> do
      a
y <- forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final Monad f a
x forall x. f x -> g x
r
      forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal (a -> Final Monad f b
f a
y) forall x. f x -> g x
r

instance Functor (Final MonadPlus f) where
    fmap :: forall a b. (a -> b) -> Final MonadPlus f a -> Final MonadPlus f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Applicative (Final MonadPlus f) where
    pure :: forall a. a -> Final MonadPlus f a
pure a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    <*> :: forall a b.
Final MonadPlus f (a -> b)
-> Final MonadPlus f a -> Final MonadPlus f b
(<*>)  = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftA2 :: forall a b c.
(a -> b -> c)
-> Final MonadPlus f a
-> Final MonadPlus f b
-> Final MonadPlus f c
liftA2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Monad (Final MonadPlus f) where
    Final MonadPlus f a
x >>= :: forall a b.
Final MonadPlus f a
-> (a -> Final MonadPlus f b) -> Final MonadPlus f b
>>= a -> Final MonadPlus f b
f  = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
r -> do
      a
y <- forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final MonadPlus f a
x forall x. f x -> g x
r
      forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal (a -> Final MonadPlus f b
f a
y) forall x. f x -> g x
r
-- | @since 0.3.0.0
instance Alt (Final MonadPlus f) where
    <!> :: forall a.
Final MonadPlus f a -> Final MonadPlus f a -> Final MonadPlus f a
(<!>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
-- | @since 0.3.0.0
instance Plus (Final MonadPlus f) where
    zero :: forall a. Final MonadPlus f a
zero = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Alternative f => f a
empty
instance Alternative (Final MonadPlus f) where
    empty :: forall a. Final MonadPlus f a
empty = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Alternative f => f a
empty
    <|> :: forall a.
Final MonadPlus f a -> Final MonadPlus f a -> Final MonadPlus f a
(<|>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
instance MonadPlus (Final MonadPlus f) where
    mzero :: forall a. Final MonadPlus f a
mzero = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (m :: * -> *) a. MonadPlus m => m a
mzero
    mplus :: forall a.
Final MonadPlus f a -> Final MonadPlus f a -> Final MonadPlus f a
mplus = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus

instance Pointed (Final Pointed f) where
    point :: forall a. a -> Final Pointed f a
point a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (p :: * -> *) a. Pointed p => a -> p a
point a
x)

instance Functor (Final (MonadReader r) f) where
    fmap :: forall a b.
(a -> b) -> Final (MonadReader r) f a -> Final (MonadReader r) f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Applicative (Final (MonadReader r) f) where
    pure :: forall a. a -> Final (MonadReader r) f a
pure a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    <*> :: forall a b.
Final (MonadReader r) f (a -> b)
-> Final (MonadReader r) f a -> Final (MonadReader r) f b
(<*>)  = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftA2 :: forall a b c.
(a -> b -> c)
-> Final (MonadReader r) f a
-> Final (MonadReader r) f b
-> Final (MonadReader r) f c
liftA2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Apply (Final (MonadReader r) f) where
    <.> :: forall a b.
Final (MonadReader r) f (a -> b)
-> Final (MonadReader r) f a -> Final (MonadReader r) f b
(<.>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    liftF2 :: forall a b c.
(a -> b -> c)
-> Final (MonadReader r) f a
-> Final (MonadReader r) f b
-> Final (MonadReader r) f c
liftF2 a -> b -> c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f)
instance Monad (Final (MonadReader r) f) where
    Final (MonadReader r) f a
x >>= :: forall a b.
Final (MonadReader r) f a
-> (a -> Final (MonadReader r) f b) -> Final (MonadReader r) f b
>>= a -> Final (MonadReader r) f b
f  = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
r -> do
      a
y <- forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final (MonadReader r) f a
x forall x. f x -> g x
r
      forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal (a -> Final (MonadReader r) f b
f a
y) forall x. f x -> g x
r
instance MonadReader r (Final (MonadReader r) f) where
    ask :: Final (MonadReader r) f r
ask     = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall r (m :: * -> *). MonadReader r m => m r
ask
    local :: forall a.
(r -> r) -> Final (MonadReader r) f a -> Final (MonadReader r) f a
local r -> r
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f)

instance Functor (Final Alt f) where
    fmap :: forall a b. (a -> b) -> Final Alt f a -> Final Alt f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Alt (Final Alt f) where
    <!> :: forall a. Final Alt f a -> Final Alt f a -> Final Alt f a
(<!>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a. Alt f => f a -> f a -> f a
(<!>)

instance Functor (Final Plus f) where
    fmap :: forall a b. (a -> b) -> Final Plus f a -> Final Plus f b
fmap a -> b
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Alt (Final Plus f) where
    <!> :: forall a. Final Plus f a -> Final Plus f a -> Final Plus f a
(<!>) = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a. Alt f => f a -> f a -> f a
(<!>)
instance Plus (Final Plus f) where
    zero :: forall a. Final Plus f a
zero = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Plus f => f a
zero

-- | @since 0.3.0.0
instance Contravariant (Final Contravariant f) where
    contramap :: forall a' a.
(a' -> a) -> Final Contravariant f a -> Final Contravariant f a'
contramap a' -> a
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f)

-- | @since 0.3.0.0
instance Contravariant (Final Divise f) where
    contramap :: forall a' a. (a' -> a) -> Final Divise f a -> Final Divise f a'
contramap a' -> a
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f)
-- | @since 0.3.0.0
instance Divise (Final Divise f) where
    divise :: forall a b c.
(a -> (b, c))
-> Final Divise f b -> Final Divise f c -> Final Divise f a
divise a -> (b, c)
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Divise f =>
(a -> (b, c)) -> f b -> f c -> f a
divise a -> (b, c)
f)

-- | @since 0.3.0.0
instance Contravariant (Final Divisible f) where
    contramap :: forall a' a.
(a' -> a) -> Final Divisible f a -> Final Divisible f a'
contramap a' -> a
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f)
-- | @since 0.3.0.0
instance Divise (Final Divisible f) where
    divise :: forall a b c.
(a -> (b, c))
-> Final Divisible f b
-> Final Divisible f c
-> Final Divisible f a
divise a -> (b, c)
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Divisible f =>
(a -> (b, c)) -> f b -> f c -> f a
divide a -> (b, c)
f)
-- | @since 0.3.0.0
instance Divisible (Final Divisible f) where
    divide :: forall a b c.
(a -> (b, c))
-> Final Divisible f b
-> Final Divisible f c
-> Final Divisible f a
divide a -> (b, c)
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Divisible f =>
(a -> (b, c)) -> f b -> f c -> f a
divide a -> (b, c)
f)
    conquer :: forall a. Final Divisible f a
conquer = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Divisible f => f a
conquer

-- | @since 0.3.0.0
instance Contravariant (Final Decide f) where
    contramap :: forall a' a. (a' -> a) -> Final Decide f a -> Final Decide f a'
contramap a' -> a
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f)
-- | @since 0.3.0.0
instance Decide (Final Decide f) where
    decide :: forall a b c.
(a -> Either b c)
-> Final Decide f b -> Final Decide f c -> Final Decide f a
decide a -> Either b c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Decide f =>
(a -> Either b c) -> f b -> f c -> f a
decide a -> Either b c
f)

-- | @since 0.3.0.0
instance Contravariant (Final Conclude f) where
    contramap :: forall a' a. (a' -> a) -> Final Conclude f a -> Final Conclude f a'
contramap a' -> a
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f)
-- | @since 0.3.0.0
instance Decide (Final Conclude f) where
    decide :: forall a b c.
(a -> Either b c)
-> Final Conclude f b -> Final Conclude f c -> Final Conclude f a
decide a -> Either b c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Decide f =>
(a -> Either b c) -> f b -> f c -> f a
decide a -> Either b c
f)
-- | @since 0.3.0.0
instance Conclude (Final Conclude f) where
    conclude :: forall a. (a -> Void) -> Final Conclude f a
conclude a -> Void
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Conclude f => (a -> Void) -> f a
conclude a -> Void
f)

-- | @since 0.3.0.0
instance Contravariant (Final Decidable f) where
    contramap :: forall a' a.
(a' -> a) -> Final Decidable f a -> Final Decidable f a'
contramap a' -> a
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f)
-- | @since 0.3.0.0
instance Divisible (Final Decidable f) where
    divide :: forall a b c.
(a -> (b, c))
-> Final Decidable f b
-> Final Decidable f c
-> Final Decidable f a
divide a -> (b, c)
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Divisible f =>
(a -> (b, c)) -> f b -> f c -> f a
divide a -> (b, c)
f)
    conquer :: forall a. Final Decidable f a
conquer = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 forall (f :: * -> *) a. Divisible f => f a
conquer
-- | @since 0.3.0.0
instance Decide (Final Decidable f) where
    decide :: forall a b c.
(a -> Either b c)
-> Final Decidable f b
-> Final Decidable f c
-> Final Decidable f a
decide a -> Either b c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Decidable f =>
(a -> Either b c) -> f b -> f c -> f a
choose a -> Either b c
f)
-- | @since 0.3.0.0
instance Conclude (Final Decidable f) where
    conclude :: forall a. (a -> Void) -> Final Decidable f a
conclude a -> Void
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Decidable f => (a -> Void) -> f a
lose a -> Void
f)
-- | @since 0.3.0.0
instance Decidable (Final Decidable f) where
    choose :: forall a b c.
(a -> Either b c)
-> Final Decidable f b
-> Final Decidable f c
-> Final Decidable f a
choose a -> Either b c
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) a b c.
Decidable f =>
(a -> Either b c) -> f b -> f c -> f a
choose a -> Either b c
f)
    lose :: forall a. (a -> Void) -> Final Decidable f a
lose a -> Void
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Decidable f => (a -> Void) -> f a
lose a -> Void
f)

-- | @since 0.3.0.0
instance Invariant (Final Invariant f) where
    invmap :: forall a b.
(a -> b) -> (b -> a) -> Final Invariant f a -> Final Invariant f b
invmap a -> b
f b -> a
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g)

-- | @since 0.4.0.0
instance Invariant (Final Inply f) where
    invmap :: forall a b.
(a -> b) -> (b -> a) -> Final Inply f a -> Final Inply f b
invmap a -> b
f b -> a
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g)
-- | @since 0.4.0.0
instance Inply (Final Inply f) where
    gather :: forall b c a.
(b -> c -> a)
-> (a -> (b, c))
-> Final Inply f b
-> Final Inply f c
-> Final Inply f a
gather b -> c -> a
f a -> (b, c)
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) b c a.
Inply f =>
(b -> c -> a) -> (a -> (b, c)) -> f b -> f c -> f a
gather b -> c -> a
f a -> (b, c)
g)
    gathered :: forall a b.
Final Inply f a -> Final Inply f b -> Final Inply f (a, b)
gathered = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Inply f => f a -> f b -> f (a, b)
gathered

-- | @since 0.4.0.0
instance Invariant (Final Inplicative f) where
    invmap :: forall a b.
(a -> b)
-> (b -> a) -> Final Inplicative f a -> Final Inplicative f b
invmap a -> b
f b -> a
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g)
-- | @since 0.4.0.0
instance Inply (Final Inplicative f) where
    gather :: forall b c a.
(b -> c -> a)
-> (a -> (b, c))
-> Final Inplicative f b
-> Final Inplicative f c
-> Final Inplicative f a
gather b -> c -> a
f a -> (b, c)
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) b c a.
Inply f =>
(b -> c -> a) -> (a -> (b, c)) -> f b -> f c -> f a
gather b -> c -> a
f a -> (b, c)
g)
    gathered :: forall a b.
Final Inplicative f a
-> Final Inplicative f b -> Final Inplicative f (a, b)
gathered = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Inply f => f a -> f b -> f (a, b)
gathered
-- | @since 0.4.0.0
instance Inplicative (Final Inplicative f) where
    knot :: forall a. a -> Final Inplicative f a
knot a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Inplicative f => a -> f a
knot a
x)

-- | @since 0.4.0.0
instance Invariant (Final Inalt f) where
    invmap :: forall a b.
(a -> b) -> (b -> a) -> Final Inalt f a -> Final Inalt f b
invmap a -> b
f b -> a
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g)
-- | @since 0.4.0.0
instance Inalt (Final Inalt f) where
    swerve :: forall b a c.
(b -> a)
-> (c -> a)
-> (a -> Either b c)
-> Final Inalt f b
-> Final Inalt f c
-> Final Inalt f a
swerve b -> a
f c -> a
g a -> Either b c
h = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) b a c.
Inalt f =>
(b -> a) -> (c -> a) -> (a -> Either b c) -> f b -> f c -> f a
swerve b -> a
f c -> a
g a -> Either b c
h)
    swerved :: forall a b.
Final Inalt f a -> Final Inalt f b -> Final Inalt f (Either a b)
swerved = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Inalt f => f a -> f b -> f (Either a b)
swerved

-- | @since 0.4.0.0
instance Invariant (Final Inplus f) where
    invmap :: forall a b.
(a -> b) -> (b -> a) -> Final Inplus f a -> Final Inplus f b
invmap a -> b
f b -> a
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g)
-- | @since 0.4.0.0
instance Inalt (Final Inplus f) where
    swerve :: forall b a c.
(b -> a)
-> (c -> a)
-> (a -> Either b c)
-> Final Inplus f b
-> Final Inplus f c
-> Final Inplus f a
swerve b -> a
f c -> a
g a -> Either b c
h = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) b a c.
Inalt f =>
(b -> a) -> (c -> a) -> (a -> Either b c) -> f b -> f c -> f a
swerve b -> a
f c -> a
g a -> Either b c
h)
    swerved :: forall a b.
Final Inplus f a -> Final Inplus f b -> Final Inplus f (Either a b)
swerved = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Inalt f => f a -> f b -> f (Either a b)
swerved
-- | @since 0.4.0.0
instance Inplus (Final Inplus f) where
    reject :: forall a. (a -> Void) -> Final Inplus f a
reject a -> Void
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Inplus f => (a -> Void) -> f a
reject a -> Void
f)

-- | @since 0.4.0.0
instance Invariant (Final Internative f) where
    invmap :: forall a b.
(a -> b)
-> (b -> a) -> Final Internative f a -> Final Internative f b
invmap a -> b
f b -> a
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b)
-> Final c f a -> Final c f b
liftFinal1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g)
-- | @since 0.4.0.0
instance Inply (Final Internative f) where
    gather :: forall b c a.
(b -> c -> a)
-> (a -> (b, c))
-> Final Internative f b
-> Final Internative f c
-> Final Internative f a
gather b -> c -> a
f a -> (b, c)
g = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) b c a.
Inply f =>
(b -> c -> a) -> (a -> (b, c)) -> f b -> f c -> f a
gather b -> c -> a
f a -> (b, c)
g)
    gathered :: forall a b.
Final Internative f a
-> Final Internative f b -> Final Internative f (a, b)
gathered = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Inply f => f a -> f b -> f (a, b)
gathered
-- | @since 0.4.0.0
instance Inplicative (Final Internative f) where
    knot :: forall a. a -> Final Internative f a
knot a
x = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Inplicative f => a -> f a
knot a
x)
-- | @since 0.4.0.0
instance Inalt (Final Internative f) where
    swerve :: forall b a c.
(b -> a)
-> (c -> a)
-> (a -> Either b c)
-> Final Internative f b
-> Final Internative f c
-> Final Internative f a
swerve b -> a
f c -> a
g a -> Either b c
h = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 (forall (f :: * -> *) b a c.
Inalt f =>
(b -> a) -> (c -> a) -> (a -> Either b c) -> f b -> f c -> f a
swerve b -> a
f c -> a
g a -> Either b c
h)
    swerved :: forall a b.
Final Internative f a
-> Final Internative f b -> Final Internative f (Either a b)
swerved = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (b :: k) (d :: k)
       (f :: k -> *).
(forall (g :: k -> *). c g => g a -> g b -> g d)
-> Final c f a -> Final c f b -> Final c f d
liftFinal2 forall (f :: * -> *) a b. Inalt f => f a -> f b -> f (Either a b)
swerved
-- | @since 0.4.0.0
instance Inplus (Final Internative f) where
    reject :: forall a. (a -> Void) -> Final Internative f a
reject a -> Void
f = forall {k} (c :: (k -> *) -> Constraint) (a :: k) (f :: k -> *).
(forall (g :: k -> *). c g => g a) -> Final c f a
liftFinal0 (forall (f :: * -> *) a. Inplus f => (a -> Void) -> f a
reject a -> Void
f)

-- | Re-interpret the context under a 'Final'.
hoistFinalC
    :: (forall g x. (c g => g x) -> (d g => g x))
    -> Final c f a
    -> Final d f a
hoistFinalC :: forall {k} (c :: (k -> *) -> Constraint)
       (d :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *) (x :: k). (c g => g x) -> d g => g x)
-> Final c f a -> Final d f a
hoistFinalC forall (g :: k -> *) (x :: k). (c g => g x) -> d g => g x
f (Final forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a
x) = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall (x :: k). f x -> g x
r -> forall (g :: k -> *) (x :: k). (c g => g x) -> d g => g x
f (forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a
x (\f x
y -> forall (g :: k -> *) (x :: k). (c g => g x) -> d g => g x
f (forall (x :: k). f x -> g x
r f x
y)))

instance HFunctor (Final c) where
    hmap :: forall (f :: k1 -> *) (g :: k1 -> *).
(f ~> g) -> Final c f ~> Final c g
hmap f ~> g
f Final c f x
x = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall (x :: k1). g x -> g x
r -> forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final c f x
x (forall (x :: k1). g x -> g x
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ~> g
f)

instance Inject (Final c) where
    inject :: forall (f :: k -> *). f ~> Final c f
inject f x
x = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
(forall (g :: k -> *). c g => (forall (x :: k). f x -> g x) -> g a)
-> Final c f a
Final forall a b. (a -> b) -> a -> b
$ \forall (x :: k). f x -> g x
f -> forall (x :: k). f x -> g x
f f x
x

instance c f => Interpret (Final c) f where
    retract :: Final c f ~> f
retract Final c f x
x = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final c f x
x forall a. a -> a
id
    interpret :: forall (g :: k -> *). (g ~> f) -> Final c g ~> f
interpret g ~> f
f Final c g x
x = forall {k} (c :: (k -> *) -> Constraint) (f :: k -> *) (a :: k).
Final c f a
-> forall (g :: k -> *).
   c g =>
   (forall (x :: k). f x -> g x) -> g a
runFinal Final c g x
x g ~> f
f

-- | "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' :: 'Control.Applicative.Step.Steps' f ~> 'Final' 'Alt' f
-- @
--
-- In this process, we lose the "positional" structure of
-- 'Control.Applicative.Step.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.
toFinal :: Interpret t (Final c f) => t f ~> Final c f
toFinal :: forall {k} (t :: (k -> *) -> k -> *) (c :: (k -> *) -> Constraint)
       (f :: k -> *).
Interpret t (Final c f) =>
t f ~> Final c f
toFinal = forall {k} (t :: (k -> *) -> k -> *) (f :: k -> *) (g :: k -> *).
Interpret t f =>
(g ~> f) -> t g ~> f
interpret forall {k} (t :: (k -> *) -> k -> *) (f :: k -> *).
Inject t =>
f ~> t f
inject

-- | "Concretize" a 'Final'.
--
-- @
-- fromFinal :: 'Final' 'Functor' f '~>' 'Coyoneda' f
-- fromFinal :: 'Final' 'Applicative' f '~>' 'Ap' f
-- fromFinal :: 'Final' 'Alternative' f '~>' 'Alt' f
-- fromFinal :: 'Final' 'Monad' f '~>' 'Free' f
-- fromFinal :: 'Final' 'Pointed' f '~>' 'Lift' f
-- fromFinal :: 'Final' 'Plus' f '~>' 'ListF' f
-- @
--
-- This can be useful because 'Final' doesn't have a concrete structure
-- that you can pattern match on and inspect, but @t@ might.
--
-- In the case that this forms an isomorphism with 'toFinal', the @t@ will
-- have an instance of 'FreeOf'.
fromFinal :: (Inject t, c (t f)) => Final c f ~> t f
fromFinal :: forall {k} (t :: (k -> *) -> k -> *) (c :: (k -> *) -> Constraint)
       (f :: k -> *).
(Inject t, c (t f)) =>
Final c f ~> t f
fromFinal = forall {k} (t :: (k -> *) -> k -> *) (f :: k -> *) (g :: k -> *).
Interpret t f =>
(g ~> f) -> t g ~> f
interpret forall {k} (t :: (k -> *) -> k -> *) (f :: k -> *).
Inject t =>
f ~> t f
inject

-- | 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.
class FreeOf c t | t -> c where
    -- | What "type" of functor is expected: should be either
    -- 'Unconstrained', 'Functor', 'Contravariant', or 'Invariant'.
    --
    -- @since 0.3.0.0
    type FreeFunctorBy t :: (Type -> Type) -> Constraint
    type FreeFunctorBy t = Unconstrained

    fromFree :: t f ~> Final c f
    toFree   :: FreeFunctorBy t f => Final c f ~> t f

    default fromFree :: Interpret t (Final c f) => t f ~> Final c f
    fromFree = forall {k} (t :: (k -> *) -> k -> *) (c :: (k -> *) -> Constraint)
       (f :: k -> *).
Interpret t (Final c f) =>
t f ~> Final c f
toFinal
    default toFree :: (Inject t, c (t f)) => Final c f ~> t f
    toFree = forall {k} (t :: (k -> *) -> k -> *) (c :: (k -> *) -> Constraint)
       (f :: k -> *).
(Inject t, c (t f)) =>
Final c f ~> t f
fromFinal

-- | The isomorphism between a free structure and its encoding as 'Final'.
finalizing :: (FreeOf c t, FreeFunctorBy t f) => t f <~> Final c f
finalizing :: forall (c :: (* -> *) -> Constraint) (t :: (* -> *) -> * -> *)
       (f :: * -> *).
(FreeOf c t, FreeFunctorBy t f) =>
t f <~> Final c f
finalizing = forall {k} (f :: k -> *) (g :: k -> *).
(f ~> g) -> (g ~> f) -> f <~> g
isoF forall (c :: (* -> *) -> Constraint) (t :: (* -> *) -> * -> *)
       (f :: * -> *).
FreeOf c t =>
t f ~> Final c f
fromFree forall (c :: (* -> *) -> Constraint) (t :: (* -> *) -> * -> *)
       (f :: * -> *).
(FreeOf c t, FreeFunctorBy t f) =>
Final c f ~> t f
toFree

instance FreeOf Functor       Coyoneda
-- | @since 0.3.0.0
instance FreeOf Contravariant CCY.Coyoneda
instance FreeOf Applicative   Ap
instance FreeOf Apply         Ap1
instance FreeOf Applicative   FAF.Ap
instance FreeOf Alternative   Alt.Alt
instance FreeOf Monad         Free
instance FreeOf Bind          Free1
instance FreeOf Pointed       Lift
instance FreeOf Pointed       MaybeApply
-- | This could also be @'FreeOf' 'Divise'@ if @'FreeFunctorBy' 'NonEmptyF'
-- ~ 'Contravariant'@.  However, there isn't really a way to express this
-- at the moment.
instance FreeOf Alt           NonEmptyF  where type FreeFunctorBy NonEmptyF = Functor
-- | This could also be @'FreeOf' 'Divisible'@ if @'FreeFunctorBy' 'ListF'
-- ~ 'Contravariant'@.  However, there isn't really a way to express this
-- at the moment.
instance FreeOf Plus          ListF      where type FreeFunctorBy ListF = Functor
-- | @since 0.3.0.0
instance FreeOf Divise        Div1
-- | @since 0.3.0.0
instance FreeOf Divisible     Div
-- | @since 0.3.0.0
instance FreeOf Decide        Dec1
-- | @since 0.3.0.0
instance FreeOf Conclude      Dec
-- | @since 0.4.0.0
instance FreeOf Inply         DivAp1    where type FreeFunctorBy DivAp1 = Invariant
-- | @since 0.4.0.0
instance FreeOf Inplicative   DivAp
-- | @since 0.4.0.0
instance FreeOf Inalt         DecAlt1   where type FreeFunctorBy DecAlt1 = Invariant
-- | @since 0.4.0.0
instance FreeOf Inplus        DecAlt
instance FreeOf Unconstrained IdentityT