{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module      : Cek.Core.Dissect
-- Description : McBride Clowns-and-Jokers polynomial functor dissection for CEK frames.
--
-- Automatically derives small-step continuation frames for any syntax functor
-- with a 'Generic1' instance, separating evaluated values (Clowns, left of the hole)
-- from unevaluated terms (Jokers, right of the hole).
--
-- === Theoretical Foundation: Functor Dissection
--
-- Conor McBride's classic functional pearl /"Clowns to the left of me, Jokers to the right"/
-- (POPL 2008) showed that the continuation frames of small-step abstract machines (such as
-- the CEK, SECD, and Krivine machines) are precisely the /dissections/ of syntax functors.
--
-- For any polynomial functor \(F(X)\), its dissection is a bifunctor \(F'(C, J)\) where:
--
--   * \(C\) represents /Clowns/: values in subterms to the left of the active evaluation hole
--     that have already been evaluated to weak-head or full normal forms.
--   * \(J\) represents /Jokers/: terms in subterms to the right of the active evaluation hole
--     that remain unevaluated in their initial syntactic state.
--
-- By instantiating dissection generically over GHC's 'Generic1' representation, any AST
-- functor equipped with 'DeriveGeneric1' automatically inherits correct, type-safe
-- small-step evaluation context frames without manual AST-traversal boilerplate.
--
-- === Machine Strategy Taxonomy
--
-- To support diverse evaluation semantics across the language spectrum (e.g. Scheme's
-- strict Call-by-Value, Dhall's Strong Call-by-Need, and Mercury's backtracking search),
-- the abstract machine is parameterized along three orthogonal axes:
--
--   1. 'BindPolicy': How variable bindings are captured and evaluated.
--   2. 'NormDepth': How deeply subterms are evaluated (WHNF vs full normal form).
--   3. 'ControlMode': How continuations and branching are managed.
module Cek.Core.Dissect
  ( -- * Dissection Type Classes
    Dissectable(..)
  , GDissect(..)
  , GenericDissect(..)

    -- * Dissection Representation Bifunctors
  , Void2
  , Unit2(..)
  , Sum2(..)
  , Prod2(..)
  , ListDissect(..)

    -- * Abstract Machine Classification Axes
  , BindPolicy(..)
  , NormDepth(..)
  , ControlMode(..)
  , MachineProfile(..)
  ) where

import Data.Bifunctor (Bifunctor(..))
import Data.Kind (Type)
import GHC.Generics
  ( Generic1(..)
  , K1(..)
  , M1(..)
  , Par1(..)
  , Rec1(..)
  , U1(..)
  , (:*:)(..)
  , (:+:)(..)
  )

-- | Axis 1: Variable binding evaluation strategy.
--
-- Determines when and how sub-expressions assigned to variables are evaluated:
data BindPolicy
  = BindCBV
    -- ^ Strict Call-by-Value: arguments are evaluated to values before function application
    -- (e.g. standard Scheme, ML, OCaml, and Strict Haskell).
  | BindCBN
    -- ^ Pure Call-by-Name: arguments are passed unevaluated as closures and re-evaluated
    -- on every variable reference (e.g. Algol 60, pure lambda-calculus).
  | BindNeed
    -- ^ Strong Call-by-Need / Lazy: arguments are passed as shared thunks and evaluated
    -- at most once on first reference, updating the cell in-place (e.g. Haskell, Dhall).
  deriving (BindPolicy -> BindPolicy -> Bool
(BindPolicy -> BindPolicy -> Bool)
-> (BindPolicy -> BindPolicy -> Bool) -> Eq BindPolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BindPolicy -> BindPolicy -> Bool
== :: BindPolicy -> BindPolicy -> Bool
$c/= :: BindPolicy -> BindPolicy -> Bool
/= :: BindPolicy -> BindPolicy -> Bool
Eq, Eq BindPolicy
Eq BindPolicy =>
(BindPolicy -> BindPolicy -> Ordering)
-> (BindPolicy -> BindPolicy -> Bool)
-> (BindPolicy -> BindPolicy -> Bool)
-> (BindPolicy -> BindPolicy -> Bool)
-> (BindPolicy -> BindPolicy -> Bool)
-> (BindPolicy -> BindPolicy -> BindPolicy)
-> (BindPolicy -> BindPolicy -> BindPolicy)
-> Ord BindPolicy
BindPolicy -> BindPolicy -> Bool
BindPolicy -> BindPolicy -> Ordering
BindPolicy -> BindPolicy -> BindPolicy
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: BindPolicy -> BindPolicy -> Ordering
compare :: BindPolicy -> BindPolicy -> Ordering
$c< :: BindPolicy -> BindPolicy -> Bool
< :: BindPolicy -> BindPolicy -> Bool
$c<= :: BindPolicy -> BindPolicy -> Bool
<= :: BindPolicy -> BindPolicy -> Bool
$c> :: BindPolicy -> BindPolicy -> Bool
> :: BindPolicy -> BindPolicy -> Bool
$c>= :: BindPolicy -> BindPolicy -> Bool
>= :: BindPolicy -> BindPolicy -> Bool
$cmax :: BindPolicy -> BindPolicy -> BindPolicy
max :: BindPolicy -> BindPolicy -> BindPolicy
$cmin :: BindPolicy -> BindPolicy -> BindPolicy
min :: BindPolicy -> BindPolicy -> BindPolicy
Ord, Int -> BindPolicy -> ShowS
[BindPolicy] -> ShowS
BindPolicy -> String
(Int -> BindPolicy -> ShowS)
-> (BindPolicy -> String)
-> ([BindPolicy] -> ShowS)
-> Show BindPolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BindPolicy -> ShowS
showsPrec :: Int -> BindPolicy -> ShowS
$cshow :: BindPolicy -> String
show :: BindPolicy -> String
$cshowList :: [BindPolicy] -> ShowS
showList :: [BindPolicy] -> ShowS
Show)

-- | Axis 2: Normalization depth.
--
-- Determines whether reduction stops at top-level constructors or proceeds recursively:
data NormDepth
  = DepthWHNF
    -- ^ Weak-Head Normal Form: evaluation stops at the outermost constructor, lambda,
    -- or primitive literal. Subterms under lambdas or constructor arguments are not forced.
  | DepthNF
    -- ^ Full Normal Form: evaluation reduces terms completely under binders and constructors.
    -- Essential for type-level languages, proof assistants, and Dhall's semantic hashing.
  deriving (NormDepth -> NormDepth -> Bool
(NormDepth -> NormDepth -> Bool)
-> (NormDepth -> NormDepth -> Bool) -> Eq NormDepth
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NormDepth -> NormDepth -> Bool
== :: NormDepth -> NormDepth -> Bool
$c/= :: NormDepth -> NormDepth -> Bool
/= :: NormDepth -> NormDepth -> Bool
Eq, Eq NormDepth
Eq NormDepth =>
(NormDepth -> NormDepth -> Ordering)
-> (NormDepth -> NormDepth -> Bool)
-> (NormDepth -> NormDepth -> Bool)
-> (NormDepth -> NormDepth -> Bool)
-> (NormDepth -> NormDepth -> Bool)
-> (NormDepth -> NormDepth -> NormDepth)
-> (NormDepth -> NormDepth -> NormDepth)
-> Ord NormDepth
NormDepth -> NormDepth -> Bool
NormDepth -> NormDepth -> Ordering
NormDepth -> NormDepth -> NormDepth
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: NormDepth -> NormDepth -> Ordering
compare :: NormDepth -> NormDepth -> Ordering
$c< :: NormDepth -> NormDepth -> Bool
< :: NormDepth -> NormDepth -> Bool
$c<= :: NormDepth -> NormDepth -> Bool
<= :: NormDepth -> NormDepth -> Bool
$c> :: NormDepth -> NormDepth -> Bool
> :: NormDepth -> NormDepth -> Bool
$c>= :: NormDepth -> NormDepth -> Bool
>= :: NormDepth -> NormDepth -> Bool
$cmax :: NormDepth -> NormDepth -> NormDepth
max :: NormDepth -> NormDepth -> NormDepth
$cmin :: NormDepth -> NormDepth -> NormDepth
min :: NormDepth -> NormDepth -> NormDepth
Ord, Int -> NormDepth -> ShowS
[NormDepth] -> ShowS
NormDepth -> String
(Int -> NormDepth -> ShowS)
-> (NormDepth -> String)
-> ([NormDepth] -> ShowS)
-> Show NormDepth
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NormDepth -> ShowS
showsPrec :: Int -> NormDepth -> ShowS
$cshow :: NormDepth -> String
show :: NormDepth -> String
$cshowList :: [NormDepth] -> ShowS
showList :: [NormDepth] -> ShowS
Show)

-- | Axis 3: Continuation and state-space branching discipline.
--
-- Governs the execution topology and multi-shot continuation capabilities:
data ControlMode
  = DetOneShot
    -- ^ Deterministic single-shot execution: the continuation stack is strictly linear
    -- and discarded upon return or unhandled exception.
  | MultiShotCtl
    -- ^ First-class multi-shot continuations: allows capturing continuations as values
    -- (e.g. Scheme's @call/cc@, delimited control operators @shift@/@reset@).
  | NondetAAM
    -- ^ Abstract Accounting Machine / Relational Search: continuation frames branch
    -- across choice-points, supporting backtracking (e.g. Prolog, Mercury, Kanren).
  deriving (ControlMode -> ControlMode -> Bool
(ControlMode -> ControlMode -> Bool)
-> (ControlMode -> ControlMode -> Bool) -> Eq ControlMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ControlMode -> ControlMode -> Bool
== :: ControlMode -> ControlMode -> Bool
$c/= :: ControlMode -> ControlMode -> Bool
/= :: ControlMode -> ControlMode -> Bool
Eq, Eq ControlMode
Eq ControlMode =>
(ControlMode -> ControlMode -> Ordering)
-> (ControlMode -> ControlMode -> Bool)
-> (ControlMode -> ControlMode -> Bool)
-> (ControlMode -> ControlMode -> Bool)
-> (ControlMode -> ControlMode -> Bool)
-> (ControlMode -> ControlMode -> ControlMode)
-> (ControlMode -> ControlMode -> ControlMode)
-> Ord ControlMode
ControlMode -> ControlMode -> Bool
ControlMode -> ControlMode -> Ordering
ControlMode -> ControlMode -> ControlMode
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ControlMode -> ControlMode -> Ordering
compare :: ControlMode -> ControlMode -> Ordering
$c< :: ControlMode -> ControlMode -> Bool
< :: ControlMode -> ControlMode -> Bool
$c<= :: ControlMode -> ControlMode -> Bool
<= :: ControlMode -> ControlMode -> Bool
$c> :: ControlMode -> ControlMode -> Bool
> :: ControlMode -> ControlMode -> Bool
$c>= :: ControlMode -> ControlMode -> Bool
>= :: ControlMode -> ControlMode -> Bool
$cmax :: ControlMode -> ControlMode -> ControlMode
max :: ControlMode -> ControlMode -> ControlMode
$cmin :: ControlMode -> ControlMode -> ControlMode
min :: ControlMode -> ControlMode -> ControlMode
Ord, Int -> ControlMode -> ShowS
[ControlMode] -> ShowS
ControlMode -> String
(Int -> ControlMode -> ShowS)
-> (ControlMode -> String)
-> ([ControlMode] -> ShowS)
-> Show ControlMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ControlMode -> ShowS
showsPrec :: Int -> ControlMode -> ShowS
$cshow :: ControlMode -> String
show :: ControlMode -> String
$cshowList :: [ControlMode] -> ShowS
showList :: [ControlMode] -> ShowS
Show)

-- | Compile-time profile specializing the CEK/CESK abstract machine.
--
-- Encapsulates the complete behavioral profile of an interpreter engine.
data MachineProfile = Profile
  { MachineProfile -> BindPolicy
profBind    :: !BindPolicy
    -- ^ The binding evaluation strategy for variables and arguments.
  , MachineProfile -> NormDepth
profDepth   :: !NormDepth
    -- ^ The target normalization depth for expressions.
  , MachineProfile -> ControlMode
profControl :: !ControlMode
    -- ^ The control-flow and continuation capture capability.
  }
  deriving (MachineProfile -> MachineProfile -> Bool
(MachineProfile -> MachineProfile -> Bool)
-> (MachineProfile -> MachineProfile -> Bool) -> Eq MachineProfile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MachineProfile -> MachineProfile -> Bool
== :: MachineProfile -> MachineProfile -> Bool
$c/= :: MachineProfile -> MachineProfile -> Bool
/= :: MachineProfile -> MachineProfile -> Bool
Eq, Eq MachineProfile
Eq MachineProfile =>
(MachineProfile -> MachineProfile -> Ordering)
-> (MachineProfile -> MachineProfile -> Bool)
-> (MachineProfile -> MachineProfile -> Bool)
-> (MachineProfile -> MachineProfile -> Bool)
-> (MachineProfile -> MachineProfile -> Bool)
-> (MachineProfile -> MachineProfile -> MachineProfile)
-> (MachineProfile -> MachineProfile -> MachineProfile)
-> Ord MachineProfile
MachineProfile -> MachineProfile -> Bool
MachineProfile -> MachineProfile -> Ordering
MachineProfile -> MachineProfile -> MachineProfile
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MachineProfile -> MachineProfile -> Ordering
compare :: MachineProfile -> MachineProfile -> Ordering
$c< :: MachineProfile -> MachineProfile -> Bool
< :: MachineProfile -> MachineProfile -> Bool
$c<= :: MachineProfile -> MachineProfile -> Bool
<= :: MachineProfile -> MachineProfile -> Bool
$c> :: MachineProfile -> MachineProfile -> Bool
> :: MachineProfile -> MachineProfile -> Bool
$c>= :: MachineProfile -> MachineProfile -> Bool
>= :: MachineProfile -> MachineProfile -> Bool
$cmax :: MachineProfile -> MachineProfile -> MachineProfile
max :: MachineProfile -> MachineProfile -> MachineProfile
$cmin :: MachineProfile -> MachineProfile -> MachineProfile
min :: MachineProfile -> MachineProfile -> MachineProfile
Ord, Int -> MachineProfile -> ShowS
[MachineProfile] -> ShowS
MachineProfile -> String
(Int -> MachineProfile -> ShowS)
-> (MachineProfile -> String)
-> ([MachineProfile] -> ShowS)
-> Show MachineProfile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MachineProfile -> ShowS
showsPrec :: Int -> MachineProfile -> ShowS
$cshow :: MachineProfile -> String
show :: MachineProfile -> String
$cshowList :: [MachineProfile] -> ShowS
showList :: [MachineProfile] -> ShowS
Show)

-- | Class of syntax functors admitting small-step left-to-right dissection.
--
-- Any syntax functor @f@ that can be dissected yields an associated bifunctor
-- @Dissect f c j@ representing the one-hole evaluation context where:
--
--   * @c@ is the type of evaluated Clowns (values to the left of the hole).
--   * @j@ is the type of unevaluated Jokers (terms to the right of the hole).
class (Functor f, Bifunctor (Dissect f)) => Dissectable f where
  -- | Associated dissection bifunctor representing continuation frames.
  type Dissect f :: Type -> Type -> Type
  type Dissect f = GenericDissect (Rep1 f)

  -- | Enter the leftmost child hole of a syntax node.
  --
  -- Returns 'Left' with the node intact if it contains no child holes (i.e. is a leaf value),
  -- or 'Right' with the first child term @j@ paired with the continuation frame.
  stepIn :: f j -> Either (f c) (j, Dissect f c j)
  default stepIn
    :: (Generic1 f, GDissect (Rep1 f), Dissect f ~ GenericDissect (Rep1 f))
    => f j -> Either (f c) (j, Dissect f c j)
  stepIn f j
fj = case Rep1 f j -> Either (Rep1 f c) (j, GDissectRep (Rep1 f) c j)
forall j c.
Rep1 f j -> Either (Rep1 f c) (j, GDissectRep (Rep1 f) c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn (f j -> Rep1 f j
forall a. f a -> Rep1 f a
forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f j
fj) of
    Left Rep1 f c
fc        -> f c -> Either (f c) (j, GenericDissect (Rep1 f) c j)
forall a b. a -> Either a b
Left (Rep1 f c -> f c
forall a. Rep1 f a -> f a
forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 Rep1 f c
fc)
    Right (j
j, GDissectRep (Rep1 f) c j
dis) -> (j, GenericDissect (Rep1 f) c j)
-> Either (f c) (j, GenericDissect (Rep1 f) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep (Rep1 f) c j -> GenericDissect (Rep1 f) c j
forall (rep :: * -> *) c j.
GDissectRep rep c j -> GenericDissect rep c j
GenericDissect GDissectRep (Rep1 f) c j
dis)

  -- | Plug an evaluated value 'c' into the active hole and advance to the next hole.
  --
  -- If all child holes have been filled with evaluated values, returns 'Left' with the
  -- completed node. Otherwise, returns 'Right' with the next unevaluated child term @j@
  -- and the updated continuation frame.
  stepNext :: c -> Dissect f c j -> Either (f c) (j, Dissect f c j)
  default stepNext
    :: (Generic1 f, GDissect (Rep1 f), Dissect f ~ GenericDissect (Rep1 f))
    => c -> Dissect f c j -> Either (f c) (j, Dissect f c j)
  stepNext c
c (GenericDissect GDissectRep (Rep1 f) c j
dis) = case c
-> GDissectRep (Rep1 f) c j
-> Either (Rep1 f c) (j, GDissectRep (Rep1 f) c j)
forall c j.
c
-> GDissectRep (Rep1 f) c j
-> Either (Rep1 f c) (j, GDissectRep (Rep1 f) c j)
forall (rep :: * -> *) c j.
GDissect rep =>
c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
gStepNext c
c GDissectRep (Rep1 f) c j
dis of
    Left Rep1 f c
fc         -> f c -> Either (f c) (j, GenericDissect (Rep1 f) c j)
forall a b. a -> Either a b
Left (Rep1 f c -> f c
forall a. Rep1 f a -> f a
forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 Rep1 f c
fc)
    Right (j
j, GDissectRep (Rep1 f) c j
dis') -> (j, GenericDissect (Rep1 f) c j)
-> Either (f c) (j, GenericDissect (Rep1 f) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep (Rep1 f) c j -> GenericDissect (Rep1 f) c j
forall (rep :: * -> *) c j.
GDissectRep rep c j -> GenericDissect rep c j
GenericDissect GDissectRep (Rep1 f) c j
dis')

-- | Wrapper providing a 'Bifunctor' instance for 'Generic1'-derived dissections.
newtype GenericDissect (rep :: Type -> Type) c j =
  GenericDissect (GDissectRep rep c j)

instance Bifunctor (GDissectRep rep) => Functor (GenericDissect rep c) where
  fmap :: forall a b.
(a -> b) -> GenericDissect rep c a -> GenericDissect rep c b
fmap a -> b
g (GenericDissect GDissectRep rep c a
d) = GDissectRep rep c b -> GenericDissect rep c b
forall (rep :: * -> *) c j.
GDissectRep rep c j -> GenericDissect rep c j
GenericDissect ((a -> b) -> GDissectRep rep c a -> GDissectRep rep c b
forall b c a.
(b -> c) -> GDissectRep rep a b -> GDissectRep rep a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second a -> b
g GDissectRep rep c a
d)

instance Bifunctor (GDissectRep rep) => Bifunctor (GenericDissect rep) where
  bimap :: forall a b c d.
(a -> b)
-> (c -> d) -> GenericDissect rep a c -> GenericDissect rep b d
bimap a -> b
f c -> d
g (GenericDissect GDissectRep rep a c
d) = GDissectRep rep b d -> GenericDissect rep b d
forall (rep :: * -> *) c j.
GDissectRep rep c j -> GenericDissect rep c j
GenericDissect ((a -> b) -> (c -> d) -> GDissectRep rep a c -> GDissectRep rep b d
forall a b c d.
(a -> b) -> (c -> d) -> GDissectRep rep a c -> GDissectRep rep b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g GDissectRep rep a c
d)

-- | Generic dissection algebra over GHC's 'Rep1' representation types.
--
-- Defines inductive rules for computing the derivative of sums, products, and constants.
class Bifunctor (GDissectRep rep) => GDissect (rep :: Type -> Type) where
  -- | Representation of the one-hole context for this structural representation.
  type GDissectRep rep :: Type -> Type -> Type

  -- | Step into the leftmost subterm of this representation.
  gStepIn   :: rep j -> Either (rep c) (j, GDissectRep rep c j)

  -- | Supply a value to the current hole and step to the next subterm.
  gStepNext :: c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)

-- | Empty dissection bifunctor for nullary constructors and constant fields.
data Void2 c j deriving ((forall a b. (a -> b) -> Void2 c a -> Void2 c b)
-> (forall a b. a -> Void2 c b -> Void2 c a) -> Functor (Void2 c)
forall a b. a -> Void2 c b -> Void2 c a
forall a b. (a -> b) -> Void2 c a -> Void2 c b
forall c a b. a -> Void2 c b -> Void2 c a
forall c a b. (a -> b) -> Void2 c a -> Void2 c b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall c a b. (a -> b) -> Void2 c a -> Void2 c b
fmap :: forall a b. (a -> b) -> Void2 c a -> Void2 c b
$c<$ :: forall c a b. a -> Void2 c b -> Void2 c a
<$ :: forall a b. a -> Void2 c b -> Void2 c a
Functor)

instance Bifunctor Void2 where
  bimap :: forall a b c d. (a -> b) -> (c -> d) -> Void2 a c -> Void2 b d
bimap a -> b
_ c -> d
_ Void2 a c
v = case Void2 a c
v of {}

-- | Dissection of 'U1' (empty constructor without fields).
--
-- Since there are no fields, stepping in immediately returns 'Left U1'.
instance GDissect U1 where
  type GDissectRep U1 = Void2
  gStepIn :: forall j c. U1 j -> Either (U1 c) (j, GDissectRep U1 c j)
gStepIn U1 j
U1    = U1 c -> Either (U1 c) (j, Void2 c j)
forall a b. a -> Either a b
Left U1 c
forall k (p :: k). U1 p
U1
  gStepNext :: forall c j.
c -> GDissectRep U1 c j -> Either (U1 c) (j, GDissectRep U1 c j)
gStepNext c
_ GDissectRep U1 c j
v = case GDissectRep U1 c j
v of {}

-- | Dissection of 'K1' (constant data field not parameterized by the recursive term).
--
-- Constant fields (such as variable names, primitive integers, strings) cannot be stepped into.
instance GDissect (K1 i a) where
  type GDissectRep (K1 i a) = Void2
  gStepIn :: forall j c.
K1 i a j -> Either (K1 i a c) (j, GDissectRep (K1 i a) c j)
gStepIn (K1 a
a) = K1 i a c -> Either (K1 i a c) (j, Void2 c j)
forall a b. a -> Either a b
Left (a -> K1 i a c
forall k i c (p :: k). c -> K1 i c p
K1 a
a)
  gStepNext :: forall c j.
c
-> GDissectRep (K1 i a) c j
-> Either (K1 i a c) (j, GDissectRep (K1 i a) c j)
gStepNext c
_ GDissectRep (K1 i a) c j
v  = case GDissectRep (K1 i a) c j
v of {}

-- | Singleton dissection frame for a single recursive child term.
data Unit2 c j = Unit2 deriving (Unit2 c j -> Unit2 c j -> Bool
(Unit2 c j -> Unit2 c j -> Bool)
-> (Unit2 c j -> Unit2 c j -> Bool) -> Eq (Unit2 c j)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall c j. Unit2 c j -> Unit2 c j -> Bool
$c== :: forall c j. Unit2 c j -> Unit2 c j -> Bool
== :: Unit2 c j -> Unit2 c j -> Bool
$c/= :: forall c j. Unit2 c j -> Unit2 c j -> Bool
/= :: Unit2 c j -> Unit2 c j -> Bool
Eq, Int -> Unit2 c j -> ShowS
[Unit2 c j] -> ShowS
Unit2 c j -> String
(Int -> Unit2 c j -> ShowS)
-> (Unit2 c j -> String)
-> ([Unit2 c j] -> ShowS)
-> Show (Unit2 c j)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall c j. Int -> Unit2 c j -> ShowS
forall c j. [Unit2 c j] -> ShowS
forall c j. Unit2 c j -> String
$cshowsPrec :: forall c j. Int -> Unit2 c j -> ShowS
showsPrec :: Int -> Unit2 c j -> ShowS
$cshow :: forall c j. Unit2 c j -> String
show :: Unit2 c j -> String
$cshowList :: forall c j. [Unit2 c j] -> ShowS
showList :: [Unit2 c j] -> ShowS
Show, (forall a b. (a -> b) -> Unit2 c a -> Unit2 c b)
-> (forall a b. a -> Unit2 c b -> Unit2 c a) -> Functor (Unit2 c)
forall a b. a -> Unit2 c b -> Unit2 c a
forall a b. (a -> b) -> Unit2 c a -> Unit2 c b
forall c a b. a -> Unit2 c b -> Unit2 c a
forall c a b. (a -> b) -> Unit2 c a -> Unit2 c b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall c a b. (a -> b) -> Unit2 c a -> Unit2 c b
fmap :: forall a b. (a -> b) -> Unit2 c a -> Unit2 c b
$c<$ :: forall c a b. a -> Unit2 c b -> Unit2 c a
<$ :: forall a b. a -> Unit2 c b -> Unit2 c a
Functor)

instance Bifunctor Unit2 where
  bimap :: forall a b c d. (a -> b) -> (c -> d) -> Unit2 a c -> Unit2 b d
bimap a -> b
_ c -> d
_ Unit2 a c
Unit2 = Unit2 b d
forall c j. Unit2 c j
Unit2

-- | Dissection of 'Par1' (the recursive parameter hole).
--
-- Stepping in immediately finds the single hole, yielding 'Unit2'.
instance GDissect Par1 where
  type GDissectRep Par1 = Unit2
  gStepIn :: forall j c. Par1 j -> Either (Par1 c) (j, GDissectRep Par1 c j)
gStepIn (Par1 j
j)  = (j, Unit2 c j) -> Either (Par1 c) (j, Unit2 c j)
forall a b. b -> Either a b
Right (j
j, Unit2 c j
forall c j. Unit2 c j
Unit2)
  gStepNext :: forall c j.
c
-> GDissectRep Par1 c j
-> Either (Par1 c) (j, GDissectRep Par1 c j)
gStepNext c
c GDissectRep Par1 c j
Unit2 c j
Unit2 = Par1 c -> Either (Par1 c) (j, Unit2 c j)
forall a b. a -> Either a b
Left (c -> Par1 c
forall p. p -> Par1 p
Par1 c
c)

-- | Dissection through container functor 'Rec1 f'.
--
-- Delegates dissection directly to the inner 'Dissectable' container functor @f@.
instance Dissectable f => GDissect (Rec1 f) where
  type GDissectRep (Rec1 f) = Dissect f
  gStepIn :: forall j c.
Rec1 f j -> Either (Rec1 f c) (j, GDissectRep (Rec1 f) c j)
gStepIn (Rec1 f j
fj) = case f j -> Either (f c) (j, Dissect f c j)
forall j c. f j -> Either (f c) (j, Dissect f c j)
forall (f :: * -> *) j c.
Dissectable f =>
f j -> Either (f c) (j, Dissect f c j)
stepIn f j
fj of
    Left f c
fc       -> Rec1 f c -> Either (Rec1 f c) (j, Dissect f c j)
forall a b. a -> Either a b
Left (f c -> Rec1 f c
forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 f c
fc)
    Right (j
j, Dissect f c j
df) -> (j, Dissect f c j) -> Either (Rec1 f c) (j, Dissect f c j)
forall a b. b -> Either a b
Right (j
j, Dissect f c j
df)
  gStepNext :: forall c j.
c
-> GDissectRep (Rec1 f) c j
-> Either (Rec1 f c) (j, GDissectRep (Rec1 f) c j)
gStepNext c
c GDissectRep (Rec1 f) c j
df = case c -> Dissect f c j -> Either (f c) (j, Dissect f c j)
forall c j. c -> Dissect f c j -> Either (f c) (j, Dissect f c j)
forall (f :: * -> *) c j.
Dissectable f =>
c -> Dissect f c j -> Either (f c) (j, Dissect f c j)
stepNext c
c Dissect f c j
GDissectRep (Rec1 f) c j
df of
    Left f c
fc        -> Rec1 f c -> Either (Rec1 f c) (j, Dissect f c j)
forall a b. a -> Either a b
Left (f c -> Rec1 f c
forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 f c
fc)
    Right (j
j, Dissect f c j
df') -> (j, Dissect f c j) -> Either (Rec1 f c) (j, Dissect f c j)
forall a b. b -> Either a b
Right (j
j, Dissect f c j
df')

-- | Dissection through metadata wrappers 'M1'.
--
-- Passes through directly to the underlying representation.
instance GDissect f => GDissect (M1 i meta f) where
  type GDissectRep (M1 i meta f) = GDissectRep f
  gStepIn :: forall j c.
M1 i meta f j
-> Either (M1 i meta f c) (j, GDissectRep (M1 i meta f) c j)
gStepIn (M1 f j
fj) = case f j -> Either (f c) (j, GDissectRep f c j)
forall j c. f j -> Either (f c) (j, GDissectRep f c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn f j
fj of
    Left f c
fc       -> M1 i meta f c -> Either (M1 i meta f c) (j, GDissectRep f c j)
forall a b. a -> Either a b
Left (f c -> M1 i meta f c
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 f c
fc)
    Right (j
j, GDissectRep f c j
df) -> (j, GDissectRep f c j)
-> Either (M1 i meta f c) (j, GDissectRep f c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep f c j
df)
  gStepNext :: forall c j.
c
-> GDissectRep (M1 i meta f) c j
-> Either (M1 i meta f c) (j, GDissectRep (M1 i meta f) c j)
gStepNext c
c GDissectRep (M1 i meta f) c j
df = case c -> GDissectRep f c j -> Either (f c) (j, GDissectRep f c j)
forall c j.
c -> GDissectRep f c j -> Either (f c) (j, GDissectRep f c j)
forall (rep :: * -> *) c j.
GDissect rep =>
c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
gStepNext c
c GDissectRep f c j
GDissectRep (M1 i meta f) c j
df of
    Left f c
fc        -> M1 i meta f c -> Either (M1 i meta f c) (j, GDissectRep f c j)
forall a b. a -> Either a b
Left (f c -> M1 i meta f c
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 f c
fc)
    Right (j
j, GDissectRep f c j
df') -> (j, GDissectRep f c j)
-> Either (M1 i meta f c) (j, GDissectRep f c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep f c j
df')

-- | Sum dissection: disjoint union of two alternative continuation frames.
data Sum2 d1 d2 c j = InL2 !(d1 c j) | InR2 !(d2 c j)
  deriving (Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool
(Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool)
-> (Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool)
-> Eq (Sum2 d1 d2 c j)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Eq (d1 c j), Eq (d2 c j)) =>
Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool
$c== :: forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Eq (d1 c j), Eq (d2 c j)) =>
Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool
== :: Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool
$c/= :: forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Eq (d1 c j), Eq (d2 c j)) =>
Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool
/= :: Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool
Eq, Int -> Sum2 d1 d2 c j -> ShowS
[Sum2 d1 d2 c j] -> ShowS
Sum2 d1 d2 c j -> String
(Int -> Sum2 d1 d2 c j -> ShowS)
-> (Sum2 d1 d2 c j -> String)
-> ([Sum2 d1 d2 c j] -> ShowS)
-> Show (Sum2 d1 d2 c j)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Show (d1 c j), Show (d2 c j)) =>
Int -> Sum2 d1 d2 c j -> ShowS
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Show (d1 c j), Show (d2 c j)) =>
[Sum2 d1 d2 c j] -> ShowS
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Show (d1 c j), Show (d2 c j)) =>
Sum2 d1 d2 c j -> String
$cshowsPrec :: forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Show (d1 c j), Show (d2 c j)) =>
Int -> Sum2 d1 d2 c j -> ShowS
showsPrec :: Int -> Sum2 d1 d2 c j -> ShowS
$cshow :: forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Show (d1 c j), Show (d2 c j)) =>
Sum2 d1 d2 c j -> String
show :: Sum2 d1 d2 c j -> String
$cshowList :: forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
(Show (d1 c j), Show (d2 c j)) =>
[Sum2 d1 d2 c j] -> ShowS
showList :: [Sum2 d1 d2 c j] -> ShowS
Show)

instance (Bifunctor d1, Bifunctor d2) => Functor (Sum2 d1 d2 c) where
  fmap :: forall a b. (a -> b) -> Sum2 d1 d2 c a -> Sum2 d1 d2 c b
fmap a -> b
g (InL2 d1 c a
x) = d1 c b -> Sum2 d1 d2 c b
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d1 c j -> Sum2 d1 d2 c j
InL2 ((a -> b) -> d1 c a -> d1 c b
forall b c a. (b -> c) -> d1 a b -> d1 a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second a -> b
g d1 c a
x)
  fmap a -> b
g (InR2 d2 c a
y) = d2 c b -> Sum2 d1 d2 c b
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d2 c j -> Sum2 d1 d2 c j
InR2 ((a -> b) -> d2 c a -> d2 c b
forall b c a. (b -> c) -> d2 a b -> d2 a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second a -> b
g d2 c a
y)

instance (Bifunctor d1, Bifunctor d2) => Bifunctor (Sum2 d1 d2) where
  bimap :: forall a b c d.
(a -> b) -> (c -> d) -> Sum2 d1 d2 a c -> Sum2 d1 d2 b d
bimap a -> b
f c -> d
g (InL2 d1 a c
x) = d1 b d -> Sum2 d1 d2 b d
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d1 c j -> Sum2 d1 d2 c j
InL2 ((a -> b) -> (c -> d) -> d1 a c -> d1 b d
forall a b c d. (a -> b) -> (c -> d) -> d1 a c -> d1 b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g d1 a c
x)
  bimap a -> b
f c -> d
g (InR2 d2 a c
y) = d2 b d -> Sum2 d1 d2 b d
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d2 c j -> Sum2 d1 d2 c j
InR2 ((a -> b) -> (c -> d) -> d2 a c -> d2 b d
forall a b c d. (a -> b) -> (c -> d) -> d2 a c -> d2 b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g d2 a c
y)

-- | Dissection of constructor sum 'f :+: g'.
--
-- Distributes over sum alternatives: evaluates within the chosen constructor branch.
instance (GDissect f, GDissect g) => GDissect (f :+: g) where
  type GDissectRep (f :+: g) = Sum2 (GDissectRep f) (GDissectRep g)
  gStepIn :: forall j c.
(:+:) f g j -> Either ((:+:) f g c) (j, GDissectRep (f :+: g) c j)
gStepIn (L1 f j
fj) = case f j -> Either (f c) (j, GDissectRep f c j)
forall j c. f j -> Either (f c) (j, GDissectRep f c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn f j
fj of
    Left f c
fc       -> (:+:) f g c
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (f c -> (:+:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 f c
fc)
    Right (j
j, GDissectRep f c j
df) -> (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep f c j -> Sum2 (GDissectRep f) (GDissectRep g) c j
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d1 c j -> Sum2 d1 d2 c j
InL2 GDissectRep f c j
df)
  gStepIn (R1 g j
gj) = case g j -> Either (g c) (j, GDissectRep g c j)
forall j c. g j -> Either (g c) (j, GDissectRep g c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn g j
gj of
    Left g c
gc       -> (:+:) f g c
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (g c -> (:+:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 g c
gc)
    Right (j
j, GDissectRep g c j
dg) -> (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep g c j -> Sum2 (GDissectRep f) (GDissectRep g) c j
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d2 c j -> Sum2 d1 d2 c j
InR2 GDissectRep g c j
dg)
  gStepNext :: forall c j.
c
-> GDissectRep (f :+: g) c j
-> Either ((:+:) f g c) (j, GDissectRep (f :+: g) c j)
gStepNext c
c (InL2 GDissectRep f c j
df) = case c -> GDissectRep f c j -> Either (f c) (j, GDissectRep f c j)
forall c j.
c -> GDissectRep f c j -> Either (f c) (j, GDissectRep f c j)
forall (rep :: * -> *) c j.
GDissect rep =>
c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
gStepNext c
c GDissectRep f c j
df of
    Left f c
fc        -> (:+:) f g c
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (f c -> (:+:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 f c
fc)
    Right (j
j, GDissectRep f c j
df') -> (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep f c j -> Sum2 (GDissectRep f) (GDissectRep g) c j
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d1 c j -> Sum2 d1 d2 c j
InL2 GDissectRep f c j
df')
  gStepNext c
c (InR2 GDissectRep g c j
dg) = case c -> GDissectRep g c j -> Either (g c) (j, GDissectRep g c j)
forall c j.
c -> GDissectRep g c j -> Either (g c) (j, GDissectRep g c j)
forall (rep :: * -> *) c j.
GDissect rep =>
c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
gStepNext c
c GDissectRep g c j
dg of
    Left g c
gc        -> (:+:) f g c
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (g c -> (:+:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 g c
gc)
    Right (j
j, GDissectRep g c j
dg') -> (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:+:) f g c) (j, Sum2 (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep g c j -> Sum2 (GDissectRep f) (GDissectRep g) c j
forall (d1 :: * -> * -> *) (d2 :: * -> * -> *) c j.
d2 c j -> Sum2 d1 d2 c j
InR2 GDissectRep g c j
dg')

-- | Leibniz product dissection:
--
-- By the product rule \(\partial(F \times G) = (\partial F \times G) + (F \times \partial G)\),
-- evaluation proceeds in two phases:
--
--   1. 'StepL': stepping inside the left factor @df@ while the right factor @g j@ waits as a Joker.
--   2. 'StepR': the left factor @f c@ is fully evaluated to Clowns, and stepping proceeds in @dg@.
data Prod2 f g df dg c j
  = StepL !(df c j) !(g j)
    -- ^ The left factor is currently being stepped into; right factor is unevaluated.
  | StepR !(f c)    !(dg c j)
    -- ^ The left factor is complete (all Clowns); the right factor is being stepped into.

instance (Functor g, Bifunctor df, Bifunctor dg)
      => Functor (Prod2 f g df dg c) where
  fmap :: forall a b. (a -> b) -> Prod2 f g df dg c a -> Prod2 f g df dg c b
fmap a -> b
g (StepL df c a
df g a
gj) = df c b -> g b -> Prod2 f g df dg c b
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
df c j -> g j -> Prod2 f g df dg c j
StepL ((a -> b) -> df c a -> df c b
forall b c a. (b -> c) -> df a b -> df a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second a -> b
g df c a
df) ((a -> b) -> g a -> g b
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g g a
gj)
  fmap a -> b
g (StepR f c
fc dg c a
dg) = f c -> dg c b -> Prod2 f g df dg c b
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
f c -> dg c j -> Prod2 f g df dg c j
StepR f c
fc            ((a -> b) -> dg c a -> dg c b
forall b c a. (b -> c) -> dg a b -> dg a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second a -> b
g dg c a
dg)

instance (Functor f, Functor g, Bifunctor df, Bifunctor dg)
      => Bifunctor (Prod2 f g df dg) where
  bimap :: forall a b c d.
(a -> b) -> (c -> d) -> Prod2 f g df dg a c -> Prod2 f g df dg b d
bimap a -> b
f c -> d
g (StepL df a c
df g c
gj) = df b d -> g d -> Prod2 f g df dg b d
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
df c j -> g j -> Prod2 f g df dg c j
StepL ((a -> b) -> (c -> d) -> df a c -> df b d
forall a b c d. (a -> b) -> (c -> d) -> df a c -> df b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g df a c
df) ((c -> d) -> g c -> g d
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> d
g g c
gj)
  bimap a -> b
f c -> d
g (StepR f a
fc dg a c
dg) = f b -> dg b d -> Prod2 f g df dg b d
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
f c -> dg c j -> Prod2 f g df dg c j
StepR ((a -> b) -> f a -> f b
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f f a
fc)    ((a -> b) -> (c -> d) -> dg a c -> dg b d
forall a b c d. (a -> b) -> (c -> d) -> dg a c -> dg b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g dg a c
dg)

-- | Dissection of pair product 'f :*: g'.
--
-- Implements standard left-to-right applicative order evaluation across pairs of fields.
instance (Functor f, Functor g, GDissect f, GDissect g) => GDissect (f :*: g) where
  type GDissectRep (f :*: g) = Prod2 f g (GDissectRep f) (GDissectRep g)
  gStepIn :: forall j c.
(:*:) f g j -> Either ((:*:) f g c) (j, GDissectRep (f :*: g) c j)
gStepIn (f j
fj :*: g j
gj) = case f j -> Either (f c) (j, GDissectRep f c j)
forall j c. f j -> Either (f c) (j, GDissectRep f c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn f j
fj of
    Right (j
j, GDissectRep f c j
df) -> (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep f c j
-> g j -> Prod2 f g (GDissectRep f) (GDissectRep g) c j
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
df c j -> g j -> Prod2 f g df dg c j
StepL GDissectRep f c j
df g j
gj)
    Left f c
fc       -> case g j -> Either (g c) (j, GDissectRep g c j)
forall j c. g j -> Either (g c) (j, GDissectRep g c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn g j
gj of
      Right (j
j, GDissectRep g c j
dg) -> (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, f c
-> GDissectRep g c j
-> Prod2 f g (GDissectRep f) (GDissectRep g) c j
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
f c -> dg c j -> Prod2 f g df dg c j
StepR f c
fc GDissectRep g c j
dg)
      Left g c
gc       -> (:*:) f g c
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (f c
fc f c -> g c -> (:*:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g c
gc)
  gStepNext :: forall c j.
c
-> GDissectRep (f :*: g) c j
-> Either ((:*:) f g c) (j, GDissectRep (f :*: g) c j)
gStepNext c
c (StepL GDissectRep f c j
df g j
gj) = case c -> GDissectRep f c j -> Either (f c) (j, GDissectRep f c j)
forall c j.
c -> GDissectRep f c j -> Either (f c) (j, GDissectRep f c j)
forall (rep :: * -> *) c j.
GDissect rep =>
c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
gStepNext c
c GDissectRep f c j
df of
    Right (j
j, GDissectRep f c j
df') -> (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, GDissectRep f c j
-> g j -> Prod2 f g (GDissectRep f) (GDissectRep g) c j
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
df c j -> g j -> Prod2 f g df dg c j
StepL GDissectRep f c j
df' g j
gj)
    Left f c
fc        -> case g j -> Either (g c) (j, GDissectRep g c j)
forall j c. g j -> Either (g c) (j, GDissectRep g c j)
forall (rep :: * -> *) j c.
GDissect rep =>
rep j -> Either (rep c) (j, GDissectRep rep c j)
gStepIn g j
gj of
      Right (j
j, GDissectRep g c j
dg) -> (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, f c
-> GDissectRep g c j
-> Prod2 f g (GDissectRep f) (GDissectRep g) c j
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
f c -> dg c j -> Prod2 f g df dg c j
StepR f c
fc GDissectRep g c j
dg)
      Left g c
gc       -> (:*:) f g c
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (f c
fc f c -> g c -> (:*:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g c
gc)
  gStepNext c
c (StepR f c
fc GDissectRep g c j
dg) = case c -> GDissectRep g c j -> Either (g c) (j, GDissectRep g c j)
forall c j.
c -> GDissectRep g c j -> Either (g c) (j, GDissectRep g c j)
forall (rep :: * -> *) c j.
GDissect rep =>
c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
gStepNext c
c GDissectRep g c j
dg of
    Right (j
j, GDissectRep g c j
dg') -> (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. b -> Either a b
Right (j
j, f c
-> GDissectRep g c j
-> Prod2 f g (GDissectRep f) (GDissectRep g) c j
forall (f :: * -> *) (g :: * -> *) (df :: * -> * -> *)
       (dg :: * -> * -> *) c j.
f c -> dg c j -> Prod2 f g df dg c j
StepR f c
fc GDissectRep g c j
dg')
    Left g c
gc        -> (:*:) f g c
-> Either
     ((:*:) f g c) (j, Prod2 f g (GDissectRep f) (GDissectRep g) c j)
forall a b. a -> Either a b
Left (f c
fc f c -> g c -> (:*:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g c
gc)

-- | Dissection of a variable-arity list '[r]' into reversed Clowns and remaining Jokers.
--
-- Accumulates evaluated elements in reverse order for O(1) consing, reversing on completion.
data ListDissect c j = ListDissect ![c] ![j]
  deriving (ListDissect c j -> ListDissect c j -> Bool
(ListDissect c j -> ListDissect c j -> Bool)
-> (ListDissect c j -> ListDissect c j -> Bool)
-> Eq (ListDissect c j)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall c j.
(Eq c, Eq j) =>
ListDissect c j -> ListDissect c j -> Bool
$c== :: forall c j.
(Eq c, Eq j) =>
ListDissect c j -> ListDissect c j -> Bool
== :: ListDissect c j -> ListDissect c j -> Bool
$c/= :: forall c j.
(Eq c, Eq j) =>
ListDissect c j -> ListDissect c j -> Bool
/= :: ListDissect c j -> ListDissect c j -> Bool
Eq, Int -> ListDissect c j -> ShowS
[ListDissect c j] -> ShowS
ListDissect c j -> String
(Int -> ListDissect c j -> ShowS)
-> (ListDissect c j -> String)
-> ([ListDissect c j] -> ShowS)
-> Show (ListDissect c j)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall c j. (Show c, Show j) => Int -> ListDissect c j -> ShowS
forall c j. (Show c, Show j) => [ListDissect c j] -> ShowS
forall c j. (Show c, Show j) => ListDissect c j -> String
$cshowsPrec :: forall c j. (Show c, Show j) => Int -> ListDissect c j -> ShowS
showsPrec :: Int -> ListDissect c j -> ShowS
$cshow :: forall c j. (Show c, Show j) => ListDissect c j -> String
show :: ListDissect c j -> String
$cshowList :: forall c j. (Show c, Show j) => [ListDissect c j] -> ShowS
showList :: [ListDissect c j] -> ShowS
Show)

instance Functor (ListDissect c) where
  fmap :: forall a b. (a -> b) -> ListDissect c a -> ListDissect c b
fmap a -> b
g (ListDissect [c]
cs [a]
js) = [c] -> [b] -> ListDissect c b
forall c j. [c] -> [j] -> ListDissect c j
ListDissect [c]
cs ((a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
g [a]
js)

instance Bifunctor ListDissect where
  bimap :: forall a b c d.
(a -> b) -> (c -> d) -> ListDissect a c -> ListDissect b d
bimap a -> b
f c -> d
g (ListDissect [a]
cs [c]
js) = [b] -> [d] -> ListDissect b d
forall c j. [c] -> [j] -> ListDissect c j
ListDissect ((a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
cs) ((c -> d) -> [c] -> [d]
forall a b. (a -> b) -> [a] -> [b]
map c -> d
g [c]
js)

-- | Dissectable instance for standard lists '[]'.
--
-- Traverses elements from head to tail, evaluating each element in sequence.
instance Dissectable [] where
  type Dissect [] = ListDissect
  stepIn :: forall j c. [j] -> Either [c] (j, Dissect [] c j)
stepIn []       = [c] -> Either [c] (j, ListDissect c j)
forall a b. a -> Either a b
Left []
  stepIn (j
j : [j]
js) = (j, ListDissect c j) -> Either [c] (j, ListDissect c j)
forall a b. b -> Either a b
Right (j
j, [c] -> [j] -> ListDissect c j
forall c j. [c] -> [j] -> ListDissect c j
ListDissect [] [j]
js)
  stepNext :: forall c j. c -> Dissect [] c j -> Either [c] (j, Dissect [] c j)
stepNext c
c (ListDissect [c]
cs [])       = [c] -> Either [c] (j, ListDissect c j)
forall a b. a -> Either a b
Left ([c] -> [c]
forall a. [a] -> [a]
reverse (c
c c -> [c] -> [c]
forall a. a -> [a] -> [a]
: [c]
cs))
  stepNext c
c (ListDissect [c]
cs (j
j : [j]
js)) = (j, ListDissect c j) -> Either [c] (j, ListDissect c j)
forall a b. b -> Either a b
Right (j
j, [c] -> [j] -> ListDissect c j
forall c j. [c] -> [j] -> ListDissect c j
ListDissect (c
c c -> [c] -> [c]
forall a. a -> [a] -> [a]
: [c]
cs) [j]
js)