| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Proarrow.Tools.Optic
Description
Compile resource-aware SMC programs of optics to forward/backward computations.
A wire '(a, da) carries its forward and backward objects. optic and opticLegs
expand each primitive to two boxes joined by its residual; runOptics connects them
by graph substitution and reads the computation into the target symmetric monoidal category.
No trace or Frobenius structure is required of the target.
For example, an SMC program on Wires '[ '(Double, Double)] can compose the
primitive opticLegs @Double (x -> (x, x*x)) ((x, dy) -> 2*x*dy) with
opticLegs @Double (x -> (cos x, sin x)) (uncurry (*)). Applying runOptics
gives (x, dy) -> (sin (x*x), 2*x*cos (x*x)*dy). A primitive with several output
wires supports an ordinary SMC tuple pattern. An explicit duplication optic can
copy a forward value and add its two cotangents on the backward pass.
This is the optic translation in §10 of Wilson and Zanasi, Data-Parallel Algorithms for String Diagrams. The source program may compose, tensor and permute its wires. Copying, discarding, merging and feedback are rejected when their translated wiring needs stronger structure.
Synopsis
- type OPTICS k = OPENHG (k, k) (OpticPrim k)
- type family Forward (xs :: [(k, k)]) :: [k] where ...
- type family Backward (xs :: [(k, k)]) :: [k] where ...
- optic :: forall {k} (c :: (k -> k -> Type) -> Constraint) (xs :: [(k, k)]) (ys :: [(k, k)]). (SymMonoidal k, SortList xs, SortList ys, (Ob (Fold (Forward ys)), Ob (Fold (Backward ys))) => c (ExOptic (ActFl (Tensor :: k -> (k, k) -> Type)) (Fold (Forward ys)) (Fold (Backward ys)))) => Optic c (Fold (Forward xs)) (Fold (Backward xs)) (Fold (Forward ys)) (Fold (Backward ys)) -> (Wires xs :: OPENHG (k, k) (OpticPrim k)) ~> (Wires ys :: OPENHG (k, k) (OpticPrim k))
- opticLegs :: forall {k} (m :: k) (xs :: [(k, k)]) (ys :: [(k, k)]). (SymMonoidal k, Ob m, SortList xs, SortList ys) => (Fold (Forward xs) ~> (m ** Fold (Forward ys))) -> ((m ** Fold (Backward ys)) ~> Fold (Backward xs)) -> (Wires xs :: OPENHG (k, k) (OpticPrim k)) ~> (Wires ys :: OPENHG (k, k) (OpticPrim k))
- runOptics :: forall {k} (xs :: [(k, k)]) (ys :: [(k, k)]). SymMonoidal k => ((Wires xs :: OPENHG (k, k) (OpticPrim k)) ~> (Wires ys :: OPENHG (k, k) (OpticPrim k))) -> Either String ((Fold (Forward xs) ** Fold (Backward ys)) ~> (Fold (Forward ys) ** Fold (Backward xs)))
Documentation
type family Forward (xs :: [(k, k)]) :: [k] where ... Source Github #
The forward objects of a list of optic wires, in boundary order.
type family Backward (xs :: [(k, k)]) :: [k] where ... Source Github #
The backward objects of a list of optic wires, in boundary order.
optic :: forall {k} (c :: (k -> k -> Type) -> Constraint) (xs :: [(k, k)]) (ys :: [(k, k)]). (SymMonoidal k, SortList xs, SortList ys, (Ob (Fold (Forward ys)), Ob (Fold (Backward ys))) => c (ExOptic (ActFl (Tensor :: k -> (k, k) -> Type)) (Fold (Forward ys)) (Fold (Backward ys)))) => Optic c (Fold (Forward xs)) (Fold (Backward xs)) (Fold (Forward ys)) (Fold (Backward ys)) -> (Wires xs :: OPENHG (k, k) (OpticPrim k)) ~> (Wires ys :: OPENHG (k, k) (OpticPrim k)) Source Github #
A primitive from an existing tensor-action optic, in any encoding supported by withLegs.
opticLegs :: forall {k} (m :: k) (xs :: [(k, k)]) (ys :: [(k, k)]). (SymMonoidal k, Ob m, SortList xs, SortList ys) => (Fold (Forward xs) ~> (m ** Fold (Forward ys))) -> ((m ** Fold (Backward ys)) ~> Fold (Backward xs)) -> (Wires xs :: OPENHG (k, k) (OpticPrim k)) ~> (Wires ys :: OPENHG (k, k) (OpticPrim k)) Source Github #
A primitive from its two legs, with residual m. The residual is produced once by
the forward leg and consumed by the backward leg; it needs no comonoid structure.
The forward and backward lists of ports are folded with the target's tensor.
runOptics :: forall {k} (xs :: [(k, k)]) (ys :: [(k, k)]). SymMonoidal k => ((Wires xs :: OPENHG (k, k) (OpticPrim k)) ~> (Wires ys :: OPENHG (k, k) (OpticPrim k))) -> Either String ((Fold (Forward xs) ** Fold (Backward ys)) ~> (Fold (Forward ys) ** Fold (Backward xs))) Source Github #
Expand the primitives, move the external backward wires to the other boundary, and
read back using permutations. The inputs are the forward source and backward target;
the outputs are the forward target and backward source. Empty boundaries fold to the unit.