| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Circuit.Tensor
Description
Tensor action and braiding for traced categories.
This module collects the cartesian and cocartesian structure over the
standard tensors (,) and Either, plus the tensor action on morphisms.
The goal is to keep the core Trace syntax and eval fold
independent of these structural details.
Note: the monomorphic assocL and assocR helpers below reassociate
leftward and rightward respectively — the opposite direction to
assoc and assoc'.
Synopsis
- superpose :: forall t (arr :: Type -> Type -> Type) a b c d. (Tensor t arr, Traced t arr) => Trace t arr a b -> Trace t arr c d -> Trace t arr (t a c) (t b d)
- data Bias
- type family Unit (t :: k -> k -> k) :: k
- class Category arr => Unital (t :: k -> k -> k) (arr :: k -> k -> Type) where
- class Unital t arr => Tensor (t :: k -> k -> k) (arr :: k -> k -> Type) where
- tensor :: forall (a :: k) (b :: k) (c :: k) (d :: k). arr a b -> arr c d -> arr (t a c) (t b d)
- class Tensor t arr => Action (t :: k -> k -> k) (arr :: k -> k -> Type) where
- braid :: forall (a :: k) (b :: k). arr (t a b) (t b a)
- class TensorSeed (t :: Type -> Type -> Type) where
- seedPair :: a -> b -> t a b
- class (Tensor d arr, Tensor t arr) => Distributive (d :: k -> k -> k) (t :: k -> k -> k) (arr :: k -> k -> Type) where
- distl :: forall (a :: k) (b :: k) (c :: k). arr (d a (t b c)) (t (d a b) (d a c))
- distl' :: forall (a :: k) (b :: k) (c :: k). arr (t (d a b) (d a c)) (d a (t b c))
- distr :: forall (a :: k) (b :: k) (c :: k). arr (d (t a b) c) (t (d a c) (d b c))
- distr' :: forall (a :: k) (c :: k) (b :: k). arr (t (d a c) (d b c)) (d (t a b) c)
- annih :: forall (a :: k). arr (d a (Unit t)) (Unit t)
- annih' :: forall (a :: k). arr (Unit t) (d a (Unit t))
- assocL :: (a, (b, c)) -> ((a, b), c)
- assocR :: ((a, b), c) -> (a, (b, c))
- coassoc :: Either a (Either b c) -> Either (Either a b) c
- coassoc' :: Either (Either a b) c -> Either a (Either b c)
- coseed :: s -> Either a b -> Either (s, a) (s, b)
- coabsorbL :: (t -> s -> s') -> Either (s, (t, a)) b -> Either (s', a) b
- coabsorbR :: (t -> s -> s') -> Either a (s, (t, b)) -> Either a (s', b)
- coreleaseL :: (s -> (s', t)) -> Either (s, a) b -> Either (s', (t, a)) b
- coreleaseR :: (s -> (s', t)) -> Either a (s, b) -> Either a (s', (t, b))
Fused parallel composition
superpose :: forall t (arr :: Type -> Type -> Type) a b c d. (Tensor t arr, Traced t arr) => Trace t arr a b -> Trace t arr c d -> Trace t arr (t a c) (t b d) Source #
Fused parallel composition for Trace when the feedback tensor matches.
Two yanks in parallel superpose into one yank over a paired channel,
satisfying the superposing axiom of traced monoidal categories:
superpose (trace f) (trace g) = trace (pre . tensor f g . post)
where pre and post rearrange the paired channel via associators
and braiding. This preserves sharing for recursive circuits; the lawful
Tensor instance falls back to independent evaluation.
>>>let k1 = yank (base (\(ns, _) -> (1 : ns, take 3 ns))) :: Trace (,) (->) [Int] [Int]>>>let k2 = yank (base (\(ns, _) -> (2 : ns, take 3 ns)))>>>eval (superpose k1 k2) ([], [])([1,1,1],[2,2,2])
The same fusion works for K, preserving sharing across the
recursive channels under MonadFix.
>>>let k1 = yank (base (K $ \(ns, _) -> pure (1 : ns, take 3 ns))) :: Trace (,) (K Identity) [Int] [Int]>>>let k2 = yank (base (K $ \(ns, _) -> pure (2 : ns, take 3 ns)))>>>runK (eval (superpose k1 k2)) ([], [])Identity ([1,1,1],[2,2,2])
Schedule bias (also used by additive disjunction)
Bias for ordered choice in scheduling and additive disjunction.
LeftFirst and RightFirst are used by shared-medium fusion in
Circuit.Shared and by additive disjunction in Circuit.Poles.
Constructors
| LeftFirst | |
| RightFirst |
Channel product on base arrows
type family Unit (t :: k -> k -> k) :: k Source #
The unit object for a tensor t.
t is an object-level bifunctor (Either, (,), type-level (+), …)
with kind k -> k -> k, not a morphism tensor.
class Category arr => Unital (t :: k -> k -> k) (arr :: k -> k -> Type) where Source #
The unit object structure of a tensor t on a category arr.
unitl and unitr witness that the tensor has a unit object. This is
the planar fragment without the morphism-level tensor product: arrows
that are merely unital can introduce and eliminate the unit, but cannot
parallel-compose two arbitrary morphisms.
Splitting this out from Tensor matters for premonoidal arrows such as
Circuit.Prob: the unitors are deterministic and embed cleanly, while
the general tensor product tensor is not canonical.
Kind-polymorphic: t and arr share object kind (inferred via PolyKinds).
Methods
unitl :: forall (a :: k). arr (t (Unit t) a) a Source #
Left unitor: I ⊗ a -> a.
unitl' :: forall (a :: k). arr a (t (Unit t) a) Source #
Inverse left unitor: a -> I ⊗ a.
unitr :: forall (a :: k). arr (t a (Unit t)) a Source #
Right unitor: a ⊗ I -> a.
unitr' :: forall (a :: k). arr a (t a (Unit t)) Source #
Inverse right unitor: a -> a ⊗ I.
Instances
| Unital (,) Process Source # | |
| Unital (,) Pullback Source # | Parallel composition pairs pullbacks independently;
|
| Monad m => Unital Either (K m :: Type -> Type -> Type) Source # | Coproduct unit structure on |
| Unital Either (->) Source # | Coproduct unit structure on functions. Laws: |
| Monad m => Unital These (K m :: Type -> Type -> Type) Source # | Inclusive unit structure on |
| Unital These (->) Source # | Inclusive unit structure on functions. Laws: |
| Monad m => Unital (,) (K m :: Type -> Type -> Type) Source # | Cartesian unit structure on |
| Unital (,) (->) Source # | Cartesian unit structure on functions. Laws: |
| Unital t arr => Unital (t :: Type -> Type -> Type) (Trace t' arr :: Type -> Type -> Type) Source # | |
| Unital w arr => Unital (w :: Type -> Type -> Type) (SMC w arr :: Type -> Type -> Type) Source # | |
| Unital t arr => Unital (t :: k -> k -> k) (Dagger arr :: k -> k -> Type) Source # | |
class Unital t arr => Tensor (t :: k -> k -> k) (arr :: k -> k -> Type) where Source #
The tensor action of t on a category arr, without braiding.
tensor is the tensor product of morphisms (parallel composition on
disjoint wires). The unitors live in the Unital superclass.
Kind-polymorphic: t and arr share object kind (inferred via PolyKinds).
Methods
tensor :: forall (a :: k) (b :: k) (c :: k) (d :: k). arr a b -> arr c d -> arr (t a c) (t b d) Source #
Parallel composition: run two arrows on disjoint wires.
>>>tensor ((+1) :: Int -> Int) ((*2) :: Int -> Int) (3, 4)(4,8)
Instances
| Tensor (,) Process Source # | |
| Tensor (,) Pullback Source # | |
| Monad m => Tensor Either (K m :: Type -> Type -> Type) Source # | Coproduct tensor action on
|
| Tensor Either (->) Source # | Coproduct tensor action on functions.
|
| Monad m => Tensor These (K m :: Type -> Type -> Type) Source # | Inclusive tensor action on |
| Tensor These (->) Source # | Inclusive tensor action on functions. |
| Monad m => Tensor (,) (K m :: Type -> Type -> Type) Source # | Cartesian tensor on |
| Tensor (,) (->) Source # | Cartesian tensor action on functions. |
Defined in Circuit.Tensor | |
| (Tensor t arr, Traced t' arr) => Tensor (t :: Type -> Type -> Type) (Trace t' arr :: Type -> Type -> Type) Source # | Lift This is the single lawful instance: it evaluates each |
| Tensor w arr => Tensor (w :: Type -> Type -> Type) (SMC w arr :: Type -> Type -> Type) Source # | |
| Tensor t arr => Tensor (t :: k -> k -> k) (Dagger arr :: k -> k -> Type) Source # | |
class Tensor t arr => Action (t :: k -> k -> k) (arr :: k -> k -> Type) where Source #
The action of a tensor t on a category arr, extended with a
symmetric braiding.
This is the self-action of a symmetric monoidal category: t acts on
arr by taking morphisms to morphisms over paired objects, and braid
provides the symmetry.
Methods
braid :: forall (a :: k) (b :: k). arr (t a b) (t b a) Source #
Symmetric braiding.
>>>braid (3, 4) :: (Int, Int)(4,3)
Instances
| Action (,) Process Source # | |
Defined in Circuit.Process | |
| Action (,) Pullback Source # | |
Defined in Circuit.Pullback | |
| Monad m => Action Either (K m :: Type -> Type -> Type) Source # | Coproduct symmetry on |
| Action Either (->) Source # | Coproduct symmetry on functions.
|
| Monad m => Action These (K m :: Type -> Type -> Type) Source # | Inclusive symmetry on |
| Action These (->) Source # | Inclusive symmetry on functions. |
| Monad m => Action (,) (K m :: Type -> Type -> Type) Source # | |
Defined in Circuit.Tensor | |
| Action (,) (->) Source # | Cartesian symmetry on functions. |
Defined in Circuit.Tensor | |
| (Action t arr, Traced t' arr) => Action (t :: Type -> Type -> Type) (Trace t' arr :: Type -> Type -> Type) Source # | |
Defined in Circuit.Tensor | |
| Action w arr => Action (w :: Type -> Type -> Type) (SMC w arr :: Type -> Type -> Type) Source # | |
Defined in Circuit.SMC | |
| Action t arr => Action (t :: k -> k -> k) (Dagger arr :: k -> k -> Type) Source # | |
Defined in Circuit.Dagger | |
class TensorSeed (t :: Type -> Type -> Type) where Source #
Value-level pairing for a tensor t.
Tensor gives the action of t on morphisms; TensorSeed names the
canonical way to combine two values into a value of type t a b. It is
needed by constructions (such as SomeBody) that store a
concrete channel value alongside a body.
Not every tensor has a canonical pairing: (,) has the pair constructor,
but Either has no unbiased way to combine a and b into
Either a b. Consequently TensorSeed is a separate class.
Instances
| TensorSeed (,) Source # | Cartesian pairing. |
Defined in Circuit.Tensor | |
Distributivity of two tensors (multiplicative over additive)
class (Tensor d arr, Tensor t arr) => Distributive (d :: k -> k -> k) (t :: k -> k -> k) (arr :: k -> k -> Type) where Source #
Distributivity of a multiplicative tensor d over an additive tensor t.
In a distributive monoidal category the product distributes over the sum:
d a (t b c) ≅ t (d a b) (d a c) and d (t a b) c ≅ t (d a c) (d b c),
and the additive unit is annihilated: d a (Unit t) ≅ Unit t.
For d = (,) and t = Either this is the ordinary distributivity of
cartesian product over coproduct, with (a, Void) ≅ Void.
Methods
distl :: forall (a :: k) (b :: k) (c :: k). arr (d a (t b c)) (t (d a b) (d a c)) Source #
Left distributor: d a (t b c) -> t (d a b) (d a c).
distl' :: forall (a :: k) (b :: k) (c :: k). arr (t (d a b) (d a c)) (d a (t b c)) Source #
Inverse left distributor.
distr :: forall (a :: k) (b :: k) (c :: k). arr (d (t a b) c) (t (d a c) (d b c)) Source #
Right distributor: d (t a b) c -> t (d a c) (d b c).
distr' :: forall (a :: k) (c :: k) (b :: k). arr (t (d a c) (d b c)) (d (t a b) c) Source #
Inverse right distributor.
annih :: forall (a :: k). arr (d a (Unit t)) (Unit t) Source #
Left annihilator: d a (Unit t) -> Unit t.
annih' :: forall (a :: k). arr (Unit t) (d a (Unit t)) Source #
Inverse left annihilator.
Instances
| Monad m => Distributive (,) Either (K m :: Type -> Type -> Type) Source # | Distributivity of |
Defined in Circuit.Tensor Methods distl :: K m (a, Either b c) (Either (a, b) (a, c)) Source # distl' :: K m (Either (a, b) (a, c)) (a, Either b c) Source # distr :: K m (Either a b, c) (Either (a, c) (b, c)) Source # distr' :: K m (Either (a, c) (b, c)) (Either a b, c) Source # | |
| Distributive (,) Either (->) Source # | Distributivity of
|
Defined in Circuit.Tensor | |
Cartesian / cocartesian associators
coassoc :: Either a (Either b c) -> Either (Either a b) c Source #
Coassociator for sums.
>>>coassoc (Left 1 :: Either Int (Either Bool Char))Left (Left 1)
coassoc' :: Either (Either a b) c -> Either a (Either b c) Source #
Inverse coassociator.
>>>coassoc' (Left (Left 1) :: Either (Either Int Bool) Char)Left 1
coseed :: s -> Either a b -> Either (s, a) (s, b) Source #
Tag a channel value onto whichever branch of the sum is active.
>>>coseed "st" (Left 42 :: Either Int Char)Left ("st",42)
coabsorbL :: (t -> s -> s') -> Either (s, (t, a)) b -> Either (s', a) b Source #
If the left branch is taken, move a value from the payload into the channel wire.
>>>coabsorbL (+) (Left (10, (3, 7)) :: Either (Int, (Int, Int)) Bool)Left (13,7)
coabsorbR :: (t -> s -> s') -> Either a (s, (t, b)) -> Either a (s', b) Source #
If the right branch is taken, move a value from the payload into the channel wire.
>>>coabsorbR (+) (Right (10, (3, 7)) :: Either Bool (Int, (Int, Int)))Right (13,7)
coreleaseL :: (s -> (s', t)) -> Either (s, a) b -> Either (s', (t, a)) b Source #
If the left branch is taken, move a value from the channel wire into the payload.
>>>coreleaseL (\s -> (s+1, s*2)) (Left (5, 99) :: Either (Int, Int) Char)Left (6,(10,99))
coreleaseR :: (s -> (s', t)) -> Either a (s, b) -> Either a (s', (t, b)) Source #
If the right branch is taken, move a value from the channel wire into the payload.
>>>coreleaseR (\s -> (s+1, s*2)) (Right (5, 99) :: Either Char (Int, Int))Right (6,(10,99))