{-# OPTIONS_HADDOCK hide, not-home #-}

module Data.HFunctor.Chain.Internal (
    Chain1(..)
  , foldChain1, unfoldChain1
  , foldChain1A
  , toChain1, injectChain1
  , matchChain1
  , Chain(..)
  , foldChain, unfoldChain
  , foldChainA
  , splittingChain, unconsChain
  , DivAp1(..)
  , DivAp(..)
  , DecAlt(..)
  , DecAlt1(..)
  ) where

import           Control.Monad.Freer.Church
import           Control.Natural
import           Control.Natural.IsoF
import           Data.Functor.Apply
import           Data.Functor.Classes
import           Data.Functor.Contravariant
import           Data.Functor.Identity
import           Data.Functor.Invariant
import           Data.Functor.Invariant.Internative
import           Data.HBifunctor
import           Data.HFunctor
import           Data.HFunctor.Interpret
import           Data.HFunctor.HTraversable
import           Data.Kind
import           Data.Typeable
import           Data.Void
import           GHC.Generics
import qualified Data.Functor.Invariant.Day   as ID
import qualified Data.Functor.Invariant.Night as IN


-- | A useful construction that works like a "non-empty linked list" of @t
-- f@ applied to itself multiple times.  That is, it contains @t f f@, @t
-- f (t f f)@, @t f (t f (t f f))@, etc, with @f@ occuring /one or more/
-- times.  It is meant to be the same as @'NonEmptyBy' t@.
--
-- A @'Chain1' t f a@ is explicitly one of:
--
-- *  @f a@
-- *  @t f f a@
-- *  @t f (t f f) a@
-- *  @t f (t f (t f f)) a@
-- *  .. etc
--
-- Note that this is exactly the description of @'NonEmptyBy' t@.  And that's "the
-- point": for all instances of 'Associative', @'Chain1' t@ is
-- isomorphic to @'NonEmptyBy' t@ (witnessed by 'unrollingNE').  That's big picture
-- of 'NonEmptyBy': it's supposed to be a type that consists of all possible
-- self-applications of @f@ to @t@.
--
-- 'Chain1' gives you a way to work with all @'NonEmptyBy' t@ in a uniform way.
-- Unlike for @'NonEmptyBy' t f@ in general, you can always explicitly /pattern
-- match/ on a 'Chain1' (with its two constructors) and do what you please
-- with it.  You can also /construct/ 'Chain1' using normal constructors
-- and functions.
--
-- You can convert in between @'NonEmptyBy' t f@ and @'Chain1' t f@ with 'unrollNE'
-- and 'rerollNE'.  You can fully "collapse" a @'Chain1' t f@ into an @f@
-- with 'retract', if you have @'SemigroupIn' t f@; this could be considered
-- a fundamental property of semigroup-ness.
--
-- See 'Chain' for a version that has an "empty" value.
--
-- Another way of thinking of this is that @'Chain1' t@ is the "free
-- @'SemigroupIn' t@".  Given any functor @f@, @'Chain1' t f@ is
-- a semigroup in the semigroupoidal category of endofunctors enriched by
-- @t@.  So, @'Chain1' 'Control.Monad.Freer.Church.Comp'@ is the "free
-- 'Data.Functor.Bind.Bind'", @'Chain1' 'Day'@ is the "free
-- 'Data.Functor.Apply.Apply'", etc. You "lift" from @f a@ to @'Chain1'
-- t f a@ using 'inject'.
--
-- Note: this instance doesn't exist directly because of restrictions in
-- typeclasses, but is implemented as
--
-- @
-- 'Associative' t => 'SemigroupIn' ('WrapHBF' t) ('Chain1' t f)
-- @
--
-- where 'biretract' is 'appendChain1'.
--
-- You can fully "collapse" a @'Chain' t i f@ into an @f@ with
-- 'retract', if you have @'MonoidIn' t i f@; this could be considered
-- a fundamental property of monoid-ness.
--
--
-- This construction is inspired by iteratees and machines.
data Chain1 t f a = Done1 (f a)
                  | More1 (t f (Chain1 t f) a)
  deriving (Typeable, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k) x.
Rep (Chain1 t f a) x -> Chain1 t f a
forall k (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k) x.
Chain1 t f a -> Rep (Chain1 t f a) x
$cto :: forall k (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k) x.
Rep (Chain1 t f a) x -> Chain1 t f a
$cfrom :: forall k (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k) x.
Chain1 t f a -> Rep (Chain1 t f a) x
Generic)

deriving instance (Eq (f a), Eq (t f (Chain1 t f) a)) => Eq (Chain1 t f a)
deriving instance (Ord (f a), Ord (t f (Chain1 t f) a)) => Ord (Chain1 t f a)
deriving instance (Show (f a), Show (t f (Chain1 t f) a)) => Show (Chain1 t f a)
deriving instance (Read (f a), Read (t f (Chain1 t f) a)) => Read (Chain1 t f a)
deriving instance (Functor f, Functor (t f (Chain1 t f))) => Functor (Chain1 t f)
deriving instance (Foldable f, Foldable (t f (Chain1 t f))) => Foldable (Chain1 t f)
deriving instance (Traversable f, Traversable (t f (Chain1 t f))) => Traversable (Chain1 t f)

instance (Eq1 f, Eq1 (t f (Chain1 t f))) => Eq1 (Chain1 t f) where
    liftEq :: forall a b.
(a -> b -> Bool) -> Chain1 t f a -> Chain1 t f b -> Bool
liftEq a -> b -> Bool
eq = \case
      Done1 f a
x -> \case
        Done1 f b
y -> forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq f a
x f b
y
        More1 t f (Chain1 t f) b
_ -> Bool
False
      More1 t f (Chain1 t f) a
x -> \case
        Done1 f b
_ -> Bool
False
        More1 t f (Chain1 t f) b
y -> forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq t f (Chain1 t f) a
x t f (Chain1 t f) b
y

instance (Ord1 f, Ord1 (t f (Chain1 t f))) => Ord1 (Chain1 t f) where
    liftCompare :: forall a b.
(a -> b -> Ordering) -> Chain1 t f a -> Chain1 t f b -> Ordering
liftCompare a -> b -> Ordering
c = \case
      Done1 f a
x -> \case
        Done1 f b
y -> forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
c f a
x f b
y
        More1 t f (Chain1 t f) b
_ -> Ordering
LT
      More1 t f (Chain1 t f) a
x -> \case
        Done1 f b
_ -> Ordering
GT
        More1 t f (Chain1 t f) b
y -> forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
c t f (Chain1 t f) a
x t f (Chain1 t f) b
y

instance (Show1 (t f (Chain1 t f)), Show1 f) => Show1 (Chain1 t f) where
    liftShowsPrec :: forall a.
(Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Chain1 t f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d = \case
        Done1 f a
x  -> forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"Done1" Int
d f a
x
        More1 t f (Chain1 t f) a
xs -> forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"More1" Int
d t f (Chain1 t f) a
xs

instance (Functor f, Read1 (t f (Chain1 t f)), Read1 f) => Read1 (Chain1 t f) where
    liftReadsPrec :: forall a.
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Chain1 t f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
            forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"Done1" forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1
         forall a. Semigroup a => a -> a -> a
<> forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"More1" forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1

-- | @since 0.3.0.0
instance (Contravariant f, Contravariant (t f (Chain1 t f))) => Contravariant (Chain1 t f) where
    contramap :: forall a' a. (a' -> a) -> Chain1 t f a -> Chain1 t f a'
contramap a' -> a
f = \case
      Done1 f a
x  -> forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f f a
x )
      More1 t f (Chain1 t f) a
xs -> forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f t f (Chain1 t f) a
xs)

-- | @since 0.3.0.0
instance (Invariant f, Invariant (t f (Chain1 t f))) => Invariant (Chain1 t f) where
    invmap :: forall a b. (a -> b) -> (b -> a) -> Chain1 t f a -> Chain1 t f b
invmap a -> b
f b -> a
g = \case
      Done1 f a
x  -> forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g f a
x )
      More1 t f (Chain1 t f) a
xs -> forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g t f (Chain1 t f) a
xs)

instance HBifunctor t => HFunctor (Chain1 t) where
    hmap :: forall (f :: k1 -> *) (g :: k1 -> *).
(f ~> g) -> Chain1 t f ~> Chain1 t g
hmap f ~> g
f = forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (g :: k -> *).
HBifunctor t =>
(f ~> g) -> (t f g ~> g) -> Chain1 t f ~> g
foldChain1 (forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ~> g
f) (forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (j :: k -> *) (g :: k -> *).
HBifunctor t =>
(f ~> j) -> t f g ~> t j g
hleft f ~> g
f)

instance HBifunctor t => Inject (Chain1 t) where
    inject :: forall (f :: k -> *). f ~> Chain1 t f
inject  = forall {k} (f :: k -> *) (t :: (k -> *) -> (k -> *) -> k -> *).
f ~> Chain1 t f
injectChain1

-- | Recursively fold down a 'Chain1'.  Provide a function on how to handle
-- the "single @f@ case" ('inject'), and how to handle the "combined @t
-- f g@ case", and this will fold the entire @'Chain1' t f@ into a single
-- @g@.
--
-- This is a catamorphism.
foldChain1
    :: forall t f g. HBifunctor t
    => f ~> g                   -- ^ handle 'Done1'
    -> t f g ~> g               -- ^ handle 'More1'
    -> Chain1 t f ~> g
foldChain1 :: forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (g :: k -> *).
HBifunctor t =>
(f ~> g) -> (t f g ~> g) -> Chain1 t f ~> g
foldChain1 f ~> g
f t f g ~> g
g = Chain1 t f ~> g
go
  where
    go :: Chain1 t f ~> g
    go :: Chain1 t f ~> g
go = \case
      Done1 f x
x  -> f ~> g
f f x
x
      More1 t f (Chain1 t f) x
xs -> t f g ~> g
g (forall k (t :: (k -> *) -> (k -> *) -> k -> *) (g :: k -> *)
       (l :: k -> *) (f :: k -> *).
HBifunctor t =>
(g ~> l) -> t f g ~> t f l
hright Chain1 t f ~> g
go t f (Chain1 t f) x
xs)

-- | An "effectful" version of 'foldChain1', weaving Applicative effects.
--
-- @since 0.3.6.0
foldChain1A
    :: (HBifunctor t, Functor h)
    => (forall x. f x -> h (g x))                -- ^ handle 'Done1'
    -> (forall x. t f (Comp h g) x -> h (g x))   -- ^ handle 'More1'
    -> Chain1 t f a
    -> h (g a)
foldChain1A :: forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (f :: k -> *) (g :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). f x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain1 t f a
-> h (g a)
foldChain1A forall (x :: k). f x -> h (g x)
f forall (x :: k). t f (Comp h g) x -> h (g x)
g = forall {k} (f :: * -> *) (g :: k -> *) (a :: k).
Functor f =>
Comp f g a -> f (g a)
unComp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (g :: k -> *).
HBifunctor t =>
(f ~> g) -> (t f g ~> g) -> Chain1 t f ~> g
foldChain1 (forall {k} (f :: * -> *) (g :: k -> *) (a :: k).
Functor f =>
f (g a) -> Comp f g a
Comp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (x :: k). f x -> h (g x)
f) (forall {k} (f :: * -> *) (g :: k -> *) (a :: k).
Functor f =>
f (g a) -> Comp f g a
Comp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (x :: k). t f (Comp h g) x -> h (g x)
g)

-- | Recursively build up a 'Chain1'.  Provide a function that takes some
-- starting seed @g@ and returns either "done" (@f@) or "continue further"
-- (@t f g@), and it will create a @'Chain1' t f@ from a @g@.
--
-- This is an anamorphism.
unfoldChain1
    :: forall t f (g :: Type -> Type). HBifunctor t
    => (g ~> f :+: t f g)
    -> g ~> Chain1 t f
unfoldChain1 :: forall (t :: (* -> *) -> (* -> *) -> * -> *) (f :: * -> *)
       (g :: * -> *).
HBifunctor t =>
(g ~> (f :+: t f g)) -> g ~> Chain1 t f
unfoldChain1 g ~> (f :+: t f g)
f = g ~> Chain1 t f
go
  where
    go :: g ~> Chain1 t f
    go :: g ~> Chain1 t f
go = (\case L1 f x
x -> forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1 f x
x; R1 t f g x
y -> forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall k (t :: (k -> *) -> (k -> *) -> k -> *) (g :: k -> *)
       (l :: k -> *) (f :: k -> *).
HBifunctor t =>
(g ~> l) -> t f g ~> t f l
hright g ~> Chain1 t f
go t f g x
y)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. g ~> (f :+: t f g)
f

-- | Convert a tensor value pairing two @f@s into a two-item 'Chain1'.  An
-- analogue of 'toNonEmptyBy'.
--
-- @since 0.3.1.0
toChain1 :: HBifunctor t => t f f ~> Chain1 t f
toChain1 :: forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *).
HBifunctor t =>
t f f ~> Chain1 t f
toChain1 = forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (t :: (k -> *) -> (k -> *) -> k -> *) (g :: k -> *)
       (l :: k -> *) (f :: k -> *).
HBifunctor t =>
(g ~> l) -> t f g ~> t f l
hright forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1

-- | Create a singleton 'Chain1'.
--
-- @since 0.3.0.0
injectChain1 :: f ~> Chain1 t f
injectChain1 :: forall {k} (f :: k -> *) (t :: (k -> *) -> (k -> *) -> k -> *).
f ~> Chain1 t f
injectChain1 = forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1

-- | For completeness, an isomorphism between 'Chain1' and its two
-- constructors, to match 'matchNE'.
--
-- @since 0.3.0.0
matchChain1 :: Chain1 t f ~> (f :+: t f (Chain1 t f))
matchChain1 :: forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *).
Chain1 t f ~> (f :+: t f (Chain1 t f))
matchChain1 = \case
    Done1 f x
x  -> forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 f x
x
    More1 t f (Chain1 t f) x
xs -> forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 t f (Chain1 t f) x
xs

-- | A useful construction that works like a "linked list" of @t f@ applied
-- to itself multiple times.  That is, it contains @t f f@, @t f (t f f)@,
-- @t f (t f (t f f))@, etc, with @f@ occuring /zero or more/ times.  It is
-- meant to be the same as @'ListBy' t@.
--
-- If @t@ is 'Tensor', then it means we can "collapse" this linked list
-- into some final type @'ListBy' t@ ('reroll'), and also extract it back
-- into a linked list ('unroll').
--
-- So, a value of type @'Chain' t i f a@ is one of either:
--
-- *  @i a@
-- *  @f a@
-- *  @t f f a@
-- *  @t f (t f f) a@
-- *  @t f (t f (t f f)) a@
-- *  .. etc.
--
-- Note that this is /exactly/ what an @'ListBy' t@ is supposed to be.  Using
-- 'Chain' allows us to work with all @'ListBy' t@s in a uniform way, with
-- normal pattern matching and normal constructors.
--
-- You can fully "collapse" a @'Chain' t i f@ into an @f@ with
-- 'retract', if you have @'MonoidIn' t i f@; this could be considered
-- a fundamental property of monoid-ness.
--
-- Another way of thinking of this is that @'Chain' t i@ is the "free
-- @'MonoidIn' t i@".  Given any functor @f@, @'Chain' t i f@ is a monoid
-- in the monoidal category of endofunctors enriched by @t@.  So, @'Chain'
-- 'Control.Monad.Freer.Church.Comp' 'Data.Functor.Identity.Identity'@ is
-- the "free 'Monad'", @'Chain' 'Data.Functor.Day.Day'
-- 'Data.Functor.Identity.Identity'@ is the "free 'Applicative'", etc.  You
-- "lift" from @f a@ to @'Chain' t i f a@ using 'inject'.
--
-- Note: this instance doesn't exist directly because of restrictions in
-- typeclasses, but is implemented as
--
-- @
-- 'Tensor' t i => 'MonoidIn' ('WrapHBF' t) ('WrapF' i) ('Chain' t i f)
-- @
--
-- where 'pureT' is 'Done' and 'biretract' is 'appendChain'.
--
-- This construction is inspired by
-- <http://oleg.fi/gists/posts/2018-02-21-single-free.html>
data Chain t i f a = Done (i a)
                   | More (t f (Chain t i f) a)

deriving instance (Eq (i a), Eq (t f (Chain t i f) a)) => Eq (Chain t i f a)
deriving instance (Ord (i a), Ord (t f (Chain t i f) a)) => Ord (Chain t i f a)
deriving instance (Show (i a), Show (t f (Chain t i f) a)) => Show (Chain t i f a)
deriving instance (Read (i a), Read (t f (Chain t i f) a)) => Read (Chain t i f a)
deriving instance (Functor i, Functor (t f (Chain t i f))) => Functor (Chain t i f)
deriving instance (Foldable i, Foldable (t f (Chain t i f))) => Foldable (Chain t i f)
deriving instance (Traversable i, Traversable (t f (Chain t i f))) => Traversable (Chain t i f)

instance (Eq1 i, Eq1 (t f (Chain t i f))) => Eq1 (Chain t i f) where
    liftEq :: forall a b.
(a -> b -> Bool) -> Chain t i f a -> Chain t i f b -> Bool
liftEq a -> b -> Bool
eq = \case
      Done i a
x -> \case
        Done i b
y -> forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq i a
x i b
y
        More t f (Chain t i f) b
_ -> Bool
False
      More t f (Chain t i f) a
x -> \case
        Done i b
_ -> Bool
False
        More t f (Chain t i f) b
y -> forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq t f (Chain t i f) a
x t f (Chain t i f) b
y

instance (Ord1 i, Ord1 (t f (Chain t i f))) => Ord1 (Chain t i f) where
    liftCompare :: forall a b.
(a -> b -> Ordering) -> Chain t i f a -> Chain t i f b -> Ordering
liftCompare a -> b -> Ordering
c = \case
      Done i a
x -> \case
        Done i b
y -> forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
c i a
x i b
y
        More t f (Chain t i f) b
_ -> Ordering
LT
      More t f (Chain t i f) a
x -> \case
        Done i b
_ -> Ordering
GT
        More t f (Chain t i f) b
y -> forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
c t f (Chain t i f) a
x t f (Chain t i f) b
y

instance (Show1 (t f (Chain t i f)), Show1 i) => Show1 (Chain t i f) where
    liftShowsPrec :: forall a.
(Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Chain t i f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d = \case
        Done i a
x  -> forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"Done" Int
d i a
x
        More t f (Chain t i f) a
xs -> forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"More" Int
d t f (Chain t i f) a
xs

instance (Functor i, Read1 (t f (Chain t i f)), Read1 i) => Read1 (Chain t i f) where
    liftReadsPrec :: forall a.
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Chain t i f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
            forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"Done" forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done
         forall a. Semigroup a => a -> a -> a
<> forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"More" forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More

instance (Contravariant i, Contravariant (t f (Chain t i f))) => Contravariant (Chain t i f) where
    contramap :: forall a' a. (a' -> a) -> Chain t i f a -> Chain t i f a'
contramap a' -> a
f = \case
      Done i a
x  -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f i a
x )
      More t f (Chain t i f) a
xs -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f t f (Chain t i f) a
xs)

instance (Invariant i, Invariant (t f (Chain t i f))) => Invariant (Chain t i f) where
    invmap :: forall a b. (a -> b) -> (b -> a) -> Chain t i f a -> Chain t i f b
invmap a -> b
f b -> a
g = \case
      Done i a
x  -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g i a
x )
      More t f (Chain t i f) a
xs -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap a -> b
f b -> a
g t f (Chain t i f) a
xs)

instance HBifunctor t => HFunctor (Chain t i) where
    hmap :: forall (f :: k1 -> *) (g :: k1 -> *).
(f ~> g) -> Chain t i f ~> Chain t i g
hmap f ~> g
f = forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k -> *) (g :: k -> *).
HBifunctor t =>
(i ~> g) -> (t f g ~> g) -> Chain t i f ~> g
foldChain forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done (forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (j :: k -> *) (g :: k -> *).
HBifunctor t =>
(f ~> j) -> t f g ~> t j g
hleft f ~> g
f)

-- | Recursively fold down a 'Chain'.  Provide a function on how to handle
-- the "single @f@ case" ('nilLB'), and how to handle the "combined @t f g@
-- case", and this will fold the entire @'Chain' t i) f@ into a single @g@.
--
-- This is a catamorphism.
foldChain
    :: forall t i f g. HBifunctor t
    => (i ~> g)             -- ^ Handle 'Done'
    -> (t f g ~> g)         -- ^ Handle 'More'
    -> Chain t i f ~> g
foldChain :: forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k -> *) (g :: k -> *).
HBifunctor t =>
(i ~> g) -> (t f g ~> g) -> Chain t i f ~> g
foldChain i ~> g
f t f g ~> g
g = Chain t i f ~> g
go
  where
    go :: Chain t i f ~> g
    go :: Chain t i f ~> g
go = \case
      Done i x
x  -> i ~> g
f i x
x
      More t f (Chain t i f) x
xs -> t f g ~> g
g (forall k (t :: (k -> *) -> (k -> *) -> k -> *) (g :: k -> *)
       (l :: k -> *) (f :: k -> *).
HBifunctor t =>
(g ~> l) -> t f g ~> t f l
hright Chain t i f ~> g
go t f (Chain t i f) x
xs)

-- | An "effectful" version of 'foldChain', weaving Applicative effects.
--
-- @since 0.3.6.0
foldChainA
    :: (HBifunctor t, Functor h)
    => (forall x. i x -> h (g x))         -- ^ Handle 'Done'
    -> (forall x. t f (Comp h g) x -> h (g x))     -- ^ Handle 'More'
    -> Chain t i f a
    -> h (g a)
foldChainA :: forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (i :: k -> *) (g :: k -> *) (f :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). i x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain t i f a
-> h (g a)
foldChainA forall (x :: k). i x -> h (g x)
f forall (x :: k). t f (Comp h g) x -> h (g x)
g = forall {k} (f :: * -> *) (g :: k -> *) (a :: k).
Functor f =>
Comp f g a -> f (g a)
unComp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k -> *) (g :: k -> *).
HBifunctor t =>
(i ~> g) -> (t f g ~> g) -> Chain t i f ~> g
foldChain (forall {k} (f :: * -> *) (g :: k -> *) (a :: k).
Functor f =>
f (g a) -> Comp f g a
Comp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (x :: k). i x -> h (g x)
f) (forall {k} (f :: * -> *) (g :: k -> *) (a :: k).
Functor f =>
f (g a) -> Comp f g a
Comp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (x :: k). t f (Comp h g) x -> h (g x)
g)

-- | Recursively build up a 'Chain'.  Provide a function that takes some
-- starting seed @g@ and returns either "done" (@i@) or "continue further"
-- (@t f g@), and it will create a @'Chain' t i f@ from a @g@.
--
-- This is an anamorphism.
unfoldChain
    :: forall t f (g :: Type -> Type) i. HBifunctor t
    => (g ~> i :+: t f g)
    -> g ~> Chain t i f
unfoldChain :: forall (t :: (* -> *) -> (* -> *) -> * -> *) (f :: * -> *)
       (g :: * -> *) (i :: * -> *).
HBifunctor t =>
(g ~> (i :+: t f g)) -> g ~> Chain t i f
unfoldChain g ~> (i :+: t f g)
f = forall a. g a -> Chain t i f a
go
  where
    go :: g a -> Chain t i f a
    go :: forall a. g a -> Chain t i f a
go = (\case L1 i a
x -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done i a
x; R1 t f g a
y ->  forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall k (t :: (k -> *) -> (k -> *) -> k -> *) (g :: k -> *)
       (l :: k -> *) (f :: k -> *).
HBifunctor t =>
(g ~> l) -> t f g ~> t f l
hright forall a. g a -> Chain t i f a
go t f g a
y)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. g ~> (i :+: t f g)
f

-- | For completeness, an isomorphism between 'Chain' and its two
-- constructors, to match 'splittingLB'.
--
-- @since 0.3.0.0
splittingChain :: Chain t i f <~> (i :+: t f (Chain t i f))
splittingChain :: forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k).
Chain t i f <~> (i :+: t f (Chain t i f))
splittingChain = forall {k} (f :: k -> *) (g :: k -> *).
(f ~> g) -> (g ~> f) -> f <~> g
isoF forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k).
Chain t i f ~> (i :+: t f (Chain t i f))
unconsChain forall a b. (a -> b) -> a -> b
$ \case
      L1 i x
x  -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done i x
x
      R1 t f (Chain t i f) x
xs -> forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More t f (Chain t i f) x
xs

-- | An analogue of 'unconsLB': match one of the two constructors of
-- a 'Chain'.
--
-- @since 0.3.0.0
unconsChain :: Chain t i f ~> i :+: t f (Chain t i f)
unconsChain :: forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k).
Chain t i f ~> (i :+: t f (Chain t i f))
unconsChain = \case
    Done i x
x  -> forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 i x
x
    More t f (Chain t i f) x
xs -> forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 t f (Chain t i f) x
xs

-- | The invariant version of 'Ap1' and 'Div1': combines the capabilities
-- of both 'Ap1' and 'Div1' together.
--
-- Conceptually you can think of @'DivAp1' f a@ as a way of consuming and
-- producing @a@s that contains a (non-empty) collection of @f x@s of
-- different @x@s. When interpreting this, each @a@ is distributed across
-- all @f x@s to each interpret, and then re-combined again to produce the
-- resulting @a@.
--
-- To do this, the main tools to combine 'DivAp1's are its 'Inply'
-- instance, using 'gather' to combine two 'DivAp1's in
-- a parallel-fork-like manner (with the splitting and re-combining
-- function).
--
-- This does have an 'Interpret' function, but the target typeclass
-- ('Inply') doesn't have too many useful instances.  Instead, you are
-- probably going to run it into either 'Apply' instance (to "produce" an
-- @a@ from a @'DivAp1' f a@) with 'runCoDivAp1', or a 'Divise' instance
-- (to "consume" an @a@ from a @'DivAp1' f a@) with 'runContraDivAp1'.
--
-- If you think of this type as a combination of 'Ap1' and 'Div1', then
-- you can also extract the 'Ap1' part out using 'divApAp1', and
-- extract the 'Div1' part out using 'divApDiv1'.
--
-- Note that this type's utility is similar to that of @'PreT' 'Ap1'@,
-- except @'PreT' 'Ap1'@ lets you use 'Apply' typeclass methods to assemble
-- it.
--
-- @since 0.3.5.0
newtype DivAp1 f a = DivAp1_ { forall (f :: * -> *) a. DivAp1 f a -> Chain1 Day f a
unDivAp1 :: Chain1 ID.Day f a }
  deriving (forall a b. (a -> b) -> (b -> a) -> DivAp1 f a -> DivAp1 f b
forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> DivAp1 f a -> DivAp1 f b
forall (f :: * -> *).
(forall a b. (a -> b) -> (b -> a) -> f a -> f b) -> Invariant f
invmap :: forall a b. (a -> b) -> (b -> a) -> DivAp1 f a -> DivAp1 f b
$cinvmap :: forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> DivAp1 f a -> DivAp1 f b
Invariant, forall {k} {k1} (t :: (k -> *) -> k1 -> *).
(forall (f :: k -> *) (g :: k -> *). (f ~> g) -> t f ~> t g)
-> HFunctor t
forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DivAp1 f ~> DivAp1 g
hmap :: forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DivAp1 f ~> DivAp1 g
$chmap :: forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DivAp1 f ~> DivAp1 g
HFunctor, HFunctor DivAp1
forall {k} (t :: (k -> *) -> k -> *).
HFunctor t -> (forall (f :: k -> *). f ~> t f) -> Inject t
forall (f :: * -> *) x. f x -> DivAp1 f x
inject :: forall (f :: * -> *) x. f x -> DivAp1 f x
$cinject :: forall (f :: * -> *) x. f x -> DivAp1 f x
Inject)

instance HTraversable DivAp1 where
    htraverse :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *) a.
Applicative h =>
(forall x. f x -> h (g x)) -> DivAp1 f a -> h (DivAp1 g a)
htraverse forall x. f x -> h (g x)
f =
        forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (f :: k -> *) (g :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). f x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain1 t f a
-> h (g a)
foldChain1A
          (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a. Chain1 Day f a -> DivAp1 f a
DivAp1_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f x -> h (g x)
f)
          (\case ID.Day f b
x (Comp h (DivAp1 g c)
y) b -> c -> x
g x -> (b, c)
h ->
                     (\g b
x' Chain1 Day g c
y' -> forall (f :: * -> *) a. Chain1 Day f a -> DivAp1 f a
DivAp1_ (forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
ID.Day g b
x' Chain1 Day g c
y' b -> c -> x
g x -> (b, c)
h)))
                   forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> h (g x)
f f b
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a. DivAp1 f a -> Chain1 Day f a
unDivAp1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> h (DivAp1 g c)
y)
          )
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DivAp1 f a -> Chain1 Day f a
unDivAp1

instance HTraversable1 DivAp1 where
    htraverse1 :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *) a.
Apply h =>
(forall x. f x -> h (g x)) -> DivAp1 f a -> h (DivAp1 g a)
htraverse1 forall x. f x -> h (g x)
f =
        forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (f :: k -> *) (g :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). f x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain1 t f a
-> h (g a)
foldChain1A
          (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a. Chain1 Day f a -> DivAp1 f a
DivAp1_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f x -> h (g x)
f)
          (\case ID.Day f b
x (Comp h (DivAp1 g c)
y) b -> c -> x
g x -> (b, c)
h ->
                     (\g b
x' Chain1 Day g c
y' -> forall (f :: * -> *) a. Chain1 Day f a -> DivAp1 f a
DivAp1_ (forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
ID.Day g b
x' Chain1 Day g c
y' b -> c -> x
g x -> (b, c)
h)))
                   forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> h (g x)
f f b
x forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
<.> (forall (f :: * -> *) a. DivAp1 f a -> Chain1 Day f a
unDivAp1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> h (DivAp1 g c)
y)
          )
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DivAp1 f a -> Chain1 Day f a
unDivAp1

-- | The invariant version of 'Ap' and 'Div': combines the capabilities of
-- both 'Ap' and 'Div' together.
--
-- Conceptually you can think of @'DivAp' f a@ as a way of consuming and
-- producing @a@s that contains a collection of @f x@s of different @x@s.
-- When interpreting this, each @a@ is distributed across all @f x@s to
-- each interpret, and then re-combined again to produce the resulting @a@.
--
-- To do this, the main tools to combine 'DivAp's are its 'Inply'
-- instance, using 'gather' to combine two 'DivAp's in a choice-like
-- manner (with the splitting and re-combining function), and its
-- 'Inplicative' instance, using 'knot' to create an "empty" branch that
-- does not contribute to the structure.
--
-- This does have an 'Interpret' function, but the target typeclass
-- ('Inplicative') doesn't have too many useful instances.  Instead, you
-- are probably going to run it into either 'Applicative' instance (to
-- "produce" an @a@ from a @'DivAp' f a@) with 'runCoDivAp', or
-- a 'Divisible' instance (to "consume" an @a@ from a @'DivAp' f a@) with
-- 'runContraDivAp'.
--
-- If you think of this type as a combination of 'Ap' and 'Div', then
-- you can also extract the 'Ap' part out using 'divApAp', and
-- extract the 'Div' part out using 'divApDiv'.
--
-- Note that this type's utility is similar to that of @'PreT' 'Ap'@,
-- except @'PreT' 'Ap'@ lets you use 'Applicative' typeclass methods to
-- assemble it.
--
-- @since 0.3.5.0
newtype DivAp f a = DivAp { forall (f :: * -> *) a. DivAp f a -> Chain Day Identity f a
unDivAp :: Chain ID.Day Identity f a }
  deriving (forall a b. (a -> b) -> (b -> a) -> DivAp f a -> DivAp f b
forall (f :: * -> *).
(forall a b. (a -> b) -> (b -> a) -> f a -> f b) -> Invariant f
forall (f :: * -> *) a b.
(a -> b) -> (b -> a) -> DivAp f a -> DivAp f b
invmap :: forall a b. (a -> b) -> (b -> a) -> DivAp f a -> DivAp f b
$cinvmap :: forall (f :: * -> *) a b.
(a -> b) -> (b -> a) -> DivAp f a -> DivAp f b
Invariant, forall {k} {k1} (t :: (k -> *) -> k1 -> *).
(forall (f :: k -> *) (g :: k -> *). (f ~> g) -> t f ~> t g)
-> HFunctor t
forall (f :: * -> *) (g :: * -> *). (f ~> g) -> DivAp f ~> DivAp g
hmap :: forall (f :: * -> *) (g :: * -> *). (f ~> g) -> DivAp f ~> DivAp g
$chmap :: forall (f :: * -> *) (g :: * -> *). (f ~> g) -> DivAp f ~> DivAp g
HFunctor)

instance Inject DivAp where
    inject :: forall (f :: * -> *). f ~> DivAp f
inject f x
x = forall (f :: * -> *) a. Chain Day Identity f a -> DivAp f a
DivAp forall a b. (a -> b) -> a -> b
$ forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
ID.Day f x
x (forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done (forall a. a -> Identity a
Identity ())) forall a b. a -> b -> a
const (,()))

instance HTraversable DivAp where
    htraverse :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *) a.
Applicative h =>
(forall x. f x -> h (g x)) -> DivAp f a -> h (DivAp g a)
htraverse forall x. f x -> h (g x)
f =
        forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (i :: k -> *) (g :: k -> *) (f :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). i x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain t i f a
-> h (g a)
foldChainA
          (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Chain Day Identity f a -> DivAp f a
DivAp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done)
          (\case ID.Day f b
x (Comp h (DivAp g c)
y) b -> c -> x
g x -> (b, c)
h ->
                      (\g b
x' Chain Day Identity g c
y' -> forall (f :: * -> *) a. Chain Day Identity f a -> DivAp f a
DivAp (forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
ID.Day g b
x'  Chain Day Identity g c
y' b -> c -> x
g x -> (b, c)
h)))
                  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> h (g x)
f f b
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a. DivAp f a -> Chain Day Identity f a
unDivAp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> h (DivAp g c)
y)
          )
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DivAp f a -> Chain Day Identity f a
unDivAp


-- | The invariant version of 'NonEmptyF' and 'Dec1': combines the
-- capabilities of both 'NonEmptyF' and 'Dec1' together.
--
-- Conceptually you can think of @'DecAlt1' f a@ as a way of consuming and
-- producing @a@s that contains a (non-empty) collection of @f x@s of
-- different @x@s. When interpreting this, a /specific/ @f@ is chosen to
-- handle the interpreting; the @a@ is sent to that @f@, and the single
-- result is returned back out.
--
-- To do this, the main tools to combine 'DecAlt1's are its 'Inalt'
-- instance, using 'swerve' to combine two 'DecAlt1's in a choice-like
-- manner (with the choosing and re-injecting function).
--
-- This does have an 'Interpret' function, but the target typeclass
-- ('Inalt') doesn't have too many useful instances.  Instead, you are
-- probably going to run it into either an 'Alt' instance (to "produce" an
-- @a@ from a @'DecAlt1' f a@) with 'runCoDecAlt1', or a 'Decide' instance
-- (to "consume" an @a@ from a @'DecAlt1' f a@) with 'runContraDecAlt1'.
--
-- If you think of this type as a combination of 'NonEmptyF' and 'Dec1',
-- then you can also extract the 'NonEmptyF' part out using
-- 'decAltNonEmptyF', and extract the 'Dec1' part out using 'decAltDec1'.
--
-- Note that this type's utility is similar to that of @'PostT' 'Dec1'@,
-- except @'PostT' 'Dec1'@ lets you use 'Decide' typeclass methods to
-- assemble it.
--
-- @since 0.3.5.0
newtype DecAlt1 f a = DecAlt1_ { forall (f :: * -> *) a. DecAlt1 f a -> Chain1 Night f a
unDecAlt1 :: Chain1 IN.Night f a }
  deriving (forall a b. (a -> b) -> (b -> a) -> DecAlt1 f a -> DecAlt1 f b
forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> DecAlt1 f a -> DecAlt1 f b
forall (f :: * -> *).
(forall a b. (a -> b) -> (b -> a) -> f a -> f b) -> Invariant f
invmap :: forall a b. (a -> b) -> (b -> a) -> DecAlt1 f a -> DecAlt1 f b
$cinvmap :: forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> DecAlt1 f a -> DecAlt1 f b
Invariant, forall {k} {k1} (t :: (k -> *) -> k1 -> *).
(forall (f :: k -> *) (g :: k -> *). (f ~> g) -> t f ~> t g)
-> HFunctor t
forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DecAlt1 f ~> DecAlt1 g
hmap :: forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DecAlt1 f ~> DecAlt1 g
$chmap :: forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DecAlt1 f ~> DecAlt1 g
HFunctor, HFunctor DecAlt1
forall {k} (t :: (k -> *) -> k -> *).
HFunctor t -> (forall (f :: k -> *). f ~> t f) -> Inject t
forall (f :: * -> *) x. f x -> DecAlt1 f x
inject :: forall (f :: * -> *) x. f x -> DecAlt1 f x
$cinject :: forall (f :: * -> *) x. f x -> DecAlt1 f x
Inject)

instance HTraversable DecAlt1 where
    htraverse :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *) a.
Applicative h =>
(forall x. f x -> h (g x)) -> DecAlt1 f a -> h (DecAlt1 g a)
htraverse forall x. f x -> h (g x)
f =
        forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (f :: k -> *) (g :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). f x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain1 t f a
-> h (g a)
foldChain1A
          (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a. Chain1 Night f a -> DecAlt1 f a
DecAlt1_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f x -> h (g x)
f)
          (\case IN.Night f b1
x (Comp h (DecAlt1 g c1)
y) b1 -> x
g c1 -> x
h x -> Either b1 c1
k ->
                     (\g b1
x' Chain1 Night g c1
y' -> forall (f :: * -> *) a. Chain1 Night f a -> DecAlt1 f a
DecAlt1_ (forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall (a :: * -> *) b1 (b :: * -> *) c1 c.
a b1
-> b c1
-> (b1 -> c)
-> (c1 -> c)
-> (c -> Either b1 c1)
-> Night a b c
IN.Night g b1
x' Chain1 Night g c1
y' b1 -> x
g c1 -> x
h x -> Either b1 c1
k)))
                   forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> h (g x)
f f b1
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a. DecAlt1 f a -> Chain1 Night f a
unDecAlt1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> h (DecAlt1 g c1)
y)
          )
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DecAlt1 f a -> Chain1 Night f a
unDecAlt1

instance HTraversable1 DecAlt1 where
    htraverse1 :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *) a.
Apply h =>
(forall x. f x -> h (g x)) -> DecAlt1 f a -> h (DecAlt1 g a)
htraverse1 forall x. f x -> h (g x)
f =
        forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (f :: k -> *) (g :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). f x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain1 t f a
-> h (g a)
foldChain1A
          (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a. Chain1 Night f a -> DecAlt1 f a
DecAlt1_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
f a -> Chain1 t f a
Done1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f x -> h (g x)
f)
          (\case IN.Night f b1
x (Comp h (DecAlt1 g c1)
y) b1 -> x
g c1 -> x
h x -> Either b1 c1
k ->
                     (\g b1
x' Chain1 Night g c1
y' -> forall (f :: * -> *) a. Chain1 Night f a -> DecAlt1 f a
DecAlt1_ (forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (a :: k).
t f (Chain1 t f) a -> Chain1 t f a
More1 (forall (a :: * -> *) b1 (b :: * -> *) c1 c.
a b1
-> b c1
-> (b1 -> c)
-> (c1 -> c)
-> (c -> Either b1 c1)
-> Night a b c
IN.Night g b1
x' Chain1 Night g c1
y' b1 -> x
g c1 -> x
h x -> Either b1 c1
k)))
                   forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> h (g x)
f f b1
x forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
<.> (forall (f :: * -> *) a. DecAlt1 f a -> Chain1 Night f a
unDecAlt1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> h (DecAlt1 g c1)
y)
          )
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DecAlt1 f a -> Chain1 Night f a
unDecAlt1

-- | A free 'Inalt'
instance Inalt f => Interpret DecAlt1 f where
    interpret :: forall (g :: * -> *). (g ~> f) -> DecAlt1 g ~> f
interpret g ~> f
f (DecAlt1_ Chain1 Night g x
x) = forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (f :: k -> *)
       (g :: k -> *).
HBifunctor t =>
(f ~> g) -> (t f g ~> g) -> Chain1 t f ~> g
foldChain1 g ~> f
f (forall (h :: * -> *) (f :: * -> *) (g :: * -> *).
Inalt h =>
(f ~> h) -> (g ~> h) -> Night f g ~> h
IN.runNight g ~> f
f forall a. a -> a
id) Chain1 Night g x
x

-- | The invariant version of 'ListF' and 'Dec': combines the capabilities of
-- both 'ListF' and 'Dec' together.
--
-- Conceptually you can think of @'DecAlt' f a@ as a way of consuming and
-- producing @a@s that contains a collection of @f x@s of different @x@s.
-- When interpreting this, a /specific/ @f@ is chosen to handle the
-- interpreting; the @a@ is sent to that @f@, and the single result is
-- returned back out.
--
-- To do this, the main tools to combine 'DecAlt's are its 'Inalt'
-- instance, using 'swerve' to combine two 'DecAlt's in a choice-like
-- manner (with the choosing and re-injecting function), and its 'Inplus'
-- instance, using 'reject' to create an "empty" choice that is never
-- taken.
--
-- This does have an 'Interpret' function, but the target typeclass
-- ('Inplus') doesn't have too many useful instances.  Instead, you are
-- probably going to run it into either 'Plus' instance (to "produce" an
-- @a@ from a @'DecAlt' f a@) with 'runCoDecAlt', or a 'Choose' instance
-- (to "consume" an @a@ from a @'DecAlt' f a@) with 'runContraDecAlt'.
--
-- If you think of this type as a combination of 'ListF' and 'Dec', then
-- you can also extract the 'ListF' part out using 'decAltListF', and
-- extract the 'Dec' part out using 'decAltDec'.
--
-- Note that this type's utility is similar to that of @'PostT' 'Dec'@,
-- except @'PostT' 'Dec'@ lets you use 'Conclude' typeclass methods to
-- assemble it.
--
-- @since 0.3.5.0
newtype DecAlt f a = DecAlt { forall (f :: * -> *) a. DecAlt f a -> Chain Night Not f a
unDecAlt :: Chain IN.Night IN.Not f a }
  deriving (forall a b. (a -> b) -> (b -> a) -> DecAlt f a -> DecAlt f b
forall (f :: * -> *).
(forall a b. (a -> b) -> (b -> a) -> f a -> f b) -> Invariant f
forall (f :: * -> *) a b.
(a -> b) -> (b -> a) -> DecAlt f a -> DecAlt f b
invmap :: forall a b. (a -> b) -> (b -> a) -> DecAlt f a -> DecAlt f b
$cinvmap :: forall (f :: * -> *) a b.
(a -> b) -> (b -> a) -> DecAlt f a -> DecAlt f b
Invariant, forall {k} {k1} (t :: (k -> *) -> k1 -> *).
(forall (f :: k -> *) (g :: k -> *). (f ~> g) -> t f ~> t g)
-> HFunctor t
forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DecAlt f ~> DecAlt g
hmap :: forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DecAlt f ~> DecAlt g
$chmap :: forall (f :: * -> *) (g :: * -> *).
(f ~> g) -> DecAlt f ~> DecAlt g
HFunctor)

instance Inject DecAlt where
    inject :: forall (f :: * -> *). f ~> DecAlt f
inject f x
x = forall (f :: * -> *) a. Chain Night Not f a -> DecAlt f a
DecAlt forall a b. (a -> b) -> a -> b
$ forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall (a :: * -> *) b1 (b :: * -> *) c1 c.
a b1
-> b c1
-> (b1 -> c)
-> (c1 -> c)
-> (c -> Either b1 c1)
-> Night a b c
IN.Night f x
x (forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done Not Void
IN.refuted) forall a. a -> a
id forall a. Void -> a
absurd forall a b. a -> Either a b
Left)

instance HTraversable DecAlt where
    htraverse :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *) a.
Applicative h =>
(forall x. f x -> h (g x)) -> DecAlt f a -> h (DecAlt g a)
htraverse forall x. f x -> h (g x)
f =
        forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (h :: * -> *)
       (i :: k -> *) (g :: k -> *) (f :: k -> *) (a :: k).
(HBifunctor t, Functor h) =>
(forall (x :: k). i x -> h (g x))
-> (forall (x :: k). t f (Comp h g) x -> h (g x))
-> Chain t i f a
-> h (g a)
foldChainA (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Chain Night Not f a -> DecAlt f a
DecAlt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
i a -> Chain t i f a
Done)
          (\case IN.Night f b1
x (Comp h (DecAlt g c1)
y) b1 -> x
g c1 -> x
h x -> Either b1 c1
k ->
                     (\g b1
x' Chain Night Not g c1
y' -> forall (f :: * -> *) a. Chain Night Not f a -> DecAlt f a
DecAlt (forall {k} {k} (t :: k -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k) (a :: k).
t f (Chain t i f) a -> Chain t i f a
More (forall (a :: * -> *) b1 (b :: * -> *) c1 c.
a b1
-> b c1
-> (b1 -> c)
-> (c1 -> c)
-> (c -> Either b1 c1)
-> Night a b c
IN.Night g b1
x' Chain Night Not g c1
y' b1 -> x
g c1 -> x
h x -> Either b1 c1
k)))
                  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> h (g x)
f f b1
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a. DecAlt f a -> Chain Night Not f a
unDecAlt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> h (DecAlt g c1)
y)
          )
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DecAlt f a -> Chain Night Not f a
unDecAlt

-- | A free 'Inplus'
instance Inplus f => Interpret DecAlt f where
    interpret :: forall (g :: * -> *). (g ~> f) -> DecAlt g ~> f
interpret g ~> f
f (DecAlt Chain Night Not g x
x) = forall {k} (t :: (k -> *) -> (k -> *) -> k -> *) (i :: k -> *)
       (f :: k -> *) (g :: k -> *).
HBifunctor t =>
(i ~> g) -> (t f g ~> g) -> Chain t i f ~> g
foldChain (forall (f :: * -> *) a. Inplus f => (a -> Void) -> f a
reject forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Not a -> a -> Void
IN.refute) (forall (h :: * -> *) (f :: * -> *) (g :: * -> *).
Inalt h =>
(f ~> h) -> (g ~> h) -> Night f g ~> h
IN.runNight g ~> f
f forall a. a -> a
id) Chain Night Not g x
x