{-# LANGUAGE AllowAmbiguousTypes #-}
module Proarrow.Tools.Optic (OPTICS, Forward, Backward, optic, opticLegs, runOptics) where
import Data.Map.Strict qualified as M
import Prelude hiding (id, (**), (.))
import Proarrow.Category.Instance.DecoratedCospan (DECCOSPAN (..), DecCospan (..))
import Proarrow.Category.Instance.FinHask (FinHask (..))
import Proarrow.Category.Instance.OpenHypergraph
( Box (..)
, Boxes (..)
, OPENHG
, Port (..)
, Prim
, SomeHypergraph (..)
, SomeSort
, SortList
, SortOb
, Sorts
, WireSorts
, Wires
, box
, permutations
, reifySorts
, simplify
, someArrow
, sortList
, sorts
, unsafeOpenHypergraph
, unsafePrim
, unsafeSubstituteWith
)
import Proarrow.Category.Instance.Product (Fst, Snd)
import Proarrow.Category.Instance.Sub (Sub (..))
import Proarrow.Category.Monoidal (Monoidal (..), MonoidalProfunctor (..), SymMonoidal, Tensor)
import Proarrow.Category.Monoidal.Strictified
( Fold
, Strictified (..)
, concatMany
, splitMany
, withIsListOf
, type (++)
)
import Proarrow.Core (CategoryOf (..), Kind, Promonad (..), obj)
import Proarrow.Functor (FunctorForRep (type (@)))
import Proarrow.Object (ListOf (..), SomeOf (..), withKnownAppend, withKnownListOf, pattern Objs)
import Proarrow.Optic (ExOptic, Optic, withLegs)
import Proarrow.Optic.Action (ActFl (..))
type OPTICS :: Kind -> Kind
type OPTICS k = OPENHG (k, k) (OpticPrim k)
newtype OpticPrim k = OpticPrim (SomeHypergraph k (Prim k))
type Forward :: [(k, k)] -> [k]
type family Forward xs where
Forward '[] = '[]
Forward ('(a, da) ': xs) = a ': Forward xs
type Backward :: [(k, k)] -> [k]
type family Backward xs where
Backward '[] = '[]
Backward ('(a, da) ': xs) = da ': Backward xs
optic
:: forall {k} c (xs :: [(k, k)]) ys
. ( SymMonoidal k
, SortList xs
, SortList ys
, (Ob (Fold (Forward ys)), Ob (Fold (Backward ys))) => c (ExOptic (ActFl Tensor) (Fold (Forward ys)) (Fold (Backward ys)))
)
=> Optic c (Fold (Forward xs)) (Fold (Backward xs)) (Fold (Forward ys)) (Fold (Backward ys))
-> (Wires xs :: OPTICS k) ~> Wires ys
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) (Fold (Forward ys)) (Fold (Backward ys)))) =>
Optic
c
(Fold (Forward xs))
(Fold (Backward xs))
(Fold (Forward ys))
(Fold (Backward ys))
-> Wires xs ~> Wires ys
optic o :: Optic
c
(Fold (Forward xs))
(Fold (Backward xs))
(Fold (Forward ys))
(Fold (Backward ys))
o@Optic
c
(Fold (Forward xs))
(Fold (Backward xs))
(Fold (Forward ys))
(Fold (Backward ys))
Objs =
forall {j} {k} (w :: FLAVOR j k)
(c :: (k -> j -> Type) -> Constraint) (s :: k) (t :: j) (a :: k)
(b :: j) r.
(CategoryOf j, CategoryOf k, Flavor w,
(Ob a, Ob b) => c (ExOptic w a b)) =>
Optic c s t a b
-> (forall (p :: k +-> k) (q :: j +-> j).
(w p q, Profunctor p, Profunctor q) =>
p s a -> q b t -> r)
-> r
forall (w :: FLAVOR k k) (c :: (k +-> k) -> Constraint) (s :: k)
(t :: k) (a :: k) (b :: k) r.
(CategoryOf k, CategoryOf k, Flavor w,
(Ob a, Ob b) => c (ExOptic w a b)) =>
Optic c s t a b
-> (forall (p :: k +-> k) (q :: k +-> k).
(w p q, Profunctor p, Profunctor q) =>
p s a -> q b t -> r)
-> r
withLegs @(ActFl Tensor) Optic
c
(Fold (Forward xs))
(Fold (Backward xs))
(Fold (Forward ys))
(Fold (Backward ys))
o \p (Fold (Forward xs)) (Fold (Forward ys))
l q (Fold (Backward ys)) (Fold (Backward xs))
r -> forall {m} {k} (act :: (m, k) +-> k) (p :: k +-> k) (q :: k +-> k)
(s :: k) (a :: k) (b :: k) (t :: k) r.
ActFl act p q =>
p s a
-> q b t
-> (forall (x :: m).
Ob x =>
(s ~> Act act x a) -> (Act act x b ~> t) -> r)
-> r
forall (act :: (k, k) +-> k) (p :: k +-> k) (q :: k +-> k) (s :: k)
(a :: k) (b :: k) (t :: k) r.
ActFl act p q =>
p s a
-> q b t
-> (forall (x :: k).
Ob x =>
(s ~> Act act x a) -> (Act act x b ~> t) -> r)
-> r
withActP @Tensor p (Fold (Forward xs)) (Fold (Forward ys))
l q (Fold (Backward ys)) (Fold (Backward xs))
r \ @m Fold (Forward xs) ~> Act Tensor x (Fold (Forward ys))
f Act Tensor x (Fold (Backward ys)) ~> Fold (Backward xs)
g -> forall (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 ~> Wires ys
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 ~> Wires ys
opticLegs @m @xs @ys Fold (Forward xs) ~> Act Tensor x (Fold (Forward ys))
Fold (Forward xs) ~> (x ** Fold (Forward ys))
f Act Tensor x (Fold (Backward ys)) ~> Fold (Backward xs)
(x ** Fold (Backward ys)) ~> Fold (Backward xs)
g
opticLegs
:: forall {k} (m :: k) (xs :: [(k, k)]) ys
. (SymMonoidal k, Ob m, SortList xs, SortList ys)
=> (Fold (Forward xs) ~> m ** Fold (Forward ys))
-> (m ** Fold (Backward ys) ~> Fold (Backward xs))
-> (Wires xs :: OPTICS k) ~> Wires ys
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 ~> Wires ys
opticLegs Fold (Forward xs) ~> (m ** Fold (Forward ys))
f (m ** Fold (Backward ys)) ~> Fold (Backward xs)
g = Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
Wires xs ~> Wires ys)
-> Wires xs ~> Wires ys
forall {k} (xs :: [(k, k)]) r.
CategoryOf k =>
Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
r)
-> r
withHalves (forall s (xs :: [s]). SortList xs => Sorts xs
sorts @_ @xs) (((SortList (Forward xs), SortList (Backward xs), Ob (Forward xs),
Ob (Backward xs)) =>
Wires xs ~> Wires ys)
-> Wires xs ~> Wires ys)
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
Wires xs ~> Wires ys)
-> Wires xs ~> Wires ys
forall a b. (a -> b) -> a -> b
$
Sorts ys
-> ((SortList (Forward ys), SortList (Backward ys),
Ob (Forward ys), Ob (Backward ys)) =>
Wires xs ~> Wires ys)
-> Wires xs ~> Wires ys
forall {k} (xs :: [(k, k)]) r.
CategoryOf k =>
Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
r)
-> r
withHalves (forall s (xs :: [s]). SortList xs => Sorts xs
sorts @_ @ys) (((SortList (Forward ys), SortList (Backward ys), Ob (Forward ys),
Ob (Backward ys)) =>
Wires xs ~> Wires ys)
-> Wires xs ~> Wires ys)
-> ((SortList (Forward ys), SortList (Backward ys),
Ob (Forward ys), Ob (Backward ys)) =>
Wires xs ~> Wires ys)
-> Wires xs ~> Wires ys
forall a b. (a -> b) -> a -> b
$
case (forall (as :: [k]). (Ob as, Monoidal k) => as ~> '[Fold as]
forall {k} (as :: [k]). (Ob as, Monoidal k) => as ~> '[Fold as]
concatMany @(Forward xs), forall (as :: [k]). (Ob as, Monoidal k) => '[Fold as] ~> as
forall {k} (as :: [k]). (Ob as, Monoidal k) => '[Fold as] ~> as
splitMany @(Forward ys), forall (as :: [k]). (Ob as, Monoidal k) => as ~> '[Fold as]
forall {k} (as :: [k]). (Ob as, Monoidal k) => as ~> '[Fold as]
concatMany @(Backward ys), forall (as :: [k]). (Ob as, Monoidal k) => '[Fold as] ~> as
forall {k} (as :: [k]). (Ob as, Monoidal k) => '[Fold as] ~> as
splitMany @(Backward xs)) of
(joinForward :: Strictified (Forward xs) '[Fold (Forward xs)]
joinForward@Strictified (Forward xs) '[Fold (Forward xs)]
Objs, splitForward :: Strictified '[Fold (Forward ys)] (Forward ys)
splitForward@Strictified '[Fold (Forward ys)] (Forward ys)
Objs, joinBackward :: Strictified (Backward ys) '[Fold (Backward ys)]
joinBackward@Strictified (Backward ys) '[Fold (Backward ys)]
Objs, splitBackward :: Strictified '[Fold (Backward xs)] (Backward xs)
splitBackward@Strictified '[Fold (Backward xs)] (Backward xs)
Objs) ->
let inputs :: [SomeSort k]
inputs = (SomeSort (k, k) -> [SomeSort k])
-> [SomeSort (k, k)] -> [SomeSort k]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> [b]) -> t a -> [b]
concatMap SomeSort (k, k) -> [SomeSort k]
forall k. CategoryOf k => SomeSort (k, k) -> [SomeSort k]
expand (forall s (xs :: [s]). SortList xs => [SomeSort s]
sortList @_ @xs)
outputs :: [SomeSort k]
outputs = (SomeSort (k, k) -> [SomeSort k])
-> [SomeSort (k, k)] -> [SomeSort k]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> [b]) -> t a -> [b]
concatMap SomeSort (k, k) -> [SomeSort k]
forall k. CategoryOf k => SomeSort (k, k) -> [SomeSort k]
expand (forall s (xs :: [s]). SortList xs => [SomeSort s]
sortList @_ @ys)
ni :: Int
ni = [SomeSort k] -> Int
forall a. [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [SomeSort k]
inputs
no :: Int
no = [SomeSort k] -> Int
forall a. [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [SomeSort k]
outputs
residual :: Int
residual = Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
no
forward :: Strictified (Forward xs) ('[m] ++ Forward ys)
forward =
(forall (a :: [k]). (CategoryOf [k], Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @'[m] Strictified '[m] '[m]
-> Strictified '[Fold (Forward ys)] (Forward ys)
-> Strictified ('[m] ** '[Fold (Forward ys)]) ('[m] ** Forward ys)
forall (x1 :: [k]) (x2 :: [k]) (y1 :: [k]) (y2 :: [k]).
Strictified x1 x2
-> Strictified y1 y2 -> Strictified (x1 ** y1) (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
(y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** Strictified '[Fold (Forward ys)] (Forward ys)
splitForward) Strictified ('[m] ++ '[Fold (Forward ys)]) ('[m] ++ Forward ys)
-> Strictified (Forward xs) ('[m] ++ '[Fold (Forward ys)])
-> Strictified (Forward xs) ('[m] ++ Forward ys)
forall (b :: [k]) (c :: [k]) (a :: [k]).
Strictified b c -> Strictified a b -> Strictified a c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. forall (as :: [k]) (bs :: [k]).
(Ob as, Ob bs) =>
(Fold as ~> Fold bs) -> Strictified as bs
forall {k} (as :: [k]) (bs :: [k]).
(Ob as, Ob bs) =>
(Fold as ~> Fold bs) -> Strictified as bs
Str @'[Fold (Forward xs)] @'[m, Fold (Forward ys)] Fold '[Fold (Forward xs)] ~> Fold '[m, Fold (Forward ys)]
Fold (Forward xs) ~> (m ** Fold (Forward ys))
f Strictified '[Fold (Forward xs)] ('[m] ++ '[Fold (Forward ys)])
-> Strictified (Forward xs) '[Fold (Forward xs)]
-> Strictified (Forward xs) ('[m] ++ '[Fold (Forward ys)])
forall (b :: [k]) (c :: [k]) (a :: [k]).
Strictified b c -> Strictified a b -> Strictified a c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Strictified (Forward xs) '[Fold (Forward xs)]
joinForward
backward :: Strictified ('[m] ++ Backward ys) (Backward xs)
backward =
Strictified '[Fold (Backward xs)] (Backward xs)
splitBackward
Strictified '[Fold (Backward xs)] (Backward xs)
-> Strictified ('[m] ++ Backward ys) '[Fold (Backward xs)]
-> Strictified ('[m] ++ Backward ys) (Backward xs)
forall (b :: [k]) (c :: [k]) (a :: [k]).
Strictified b c -> Strictified a b -> Strictified a c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. forall (as :: [k]) (bs :: [k]).
(Ob as, Ob bs) =>
(Fold as ~> Fold bs) -> Strictified as bs
forall {k} (as :: [k]) (bs :: [k]).
(Ob as, Ob bs) =>
(Fold as ~> Fold bs) -> Strictified as bs
Str @'[m, Fold (Backward ys)] @'[Fold (Backward xs)] (m ** Fold (Backward ys)) ~> Fold (Backward xs)
Fold '[m, Fold (Backward ys)] ~> Fold '[Fold (Backward xs)]
g
Strictified '[m, Fold (Backward ys)] '[Fold (Backward xs)]
-> Strictified ('[m] ++ Backward ys) '[m, Fold (Backward ys)]
-> Strictified ('[m] ++ Backward ys) '[Fold (Backward xs)]
forall (b :: [k]) (c :: [k]) (a :: [k]).
Strictified b c -> Strictified a b -> Strictified a c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. (forall (a :: [k]). (CategoryOf [k], Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @'[m] Strictified '[m] '[m]
-> Strictified (Backward ys) '[Fold (Backward ys)]
-> Strictified
('[m] ** Backward ys) ('[m] ** '[Fold (Backward ys)])
forall (x1 :: [k]) (x2 :: [k]) (y1 :: [k]) (y2 :: [k]).
Strictified x1 x2
-> Strictified y1 y2 -> Strictified (x1 ** y1) (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
(y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** Strictified (Backward ys) '[Fold (Backward ys)]
joinBackward)
boxes :: [Box (Prim k) Int]
boxes =
[ Prim k -> [Int] -> [Int] -> Box (Prim k) Int
forall l n. l -> [n] -> [n] -> Box l n
Box (SomeArrow k -> Prim k
forall k. SomeArrow k -> Prim k
unsafePrim ((Forward xs ~> (m : Forward ys)) -> SomeArrow k
forall {s} (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
(as ~> bs) -> SomeArrow s
someArrow Forward xs ~> (m : Forward ys)
Strictified (Forward xs) ('[m] ++ Forward ys)
forward)) [Int
0, Int
2 .. Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] (Int
residual Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int
ni, Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 .. Int
residual Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1])
, Prim k -> [Int] -> [Int] -> Box (Prim k) Int
forall l n. l -> [n] -> [n] -> Box l n
Box (SomeArrow k -> Prim k
forall k. SomeArrow k -> Prim k
unsafePrim (((m : Backward ys) ~> Backward xs) -> SomeArrow k
forall {s} (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
(as ~> bs) -> SomeArrow s
someArrow (m : Backward ys) ~> Backward xs
Strictified ('[m] ++ Backward ys) (Backward xs)
backward)) (Int
residual Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3 .. Int
residual Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]) [Int
1, Int
3 .. Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
]
replacement :: SomeHypergraph k (Prim k)
replacement = [SomeSort k]
-> (forall (xs :: [k]). SortList xs => SomeHypergraph k (Prim k))
-> SomeHypergraph k (Prim k)
forall s r.
[SomeSort s] -> (forall (xs :: [s]). SortList xs => r) -> r
reifySorts [SomeSort k]
inputs \ @as -> [SomeSort k]
-> (forall (xs :: [k]). SortList xs => SomeHypergraph k (Prim k))
-> SomeHypergraph k (Prim k)
forall s r.
[SomeSort s] -> (forall (xs :: [s]). SortList xs => r) -> r
reifySorts [SomeSort k]
outputs \ @bs ->
(Wires xs ~> Wires xs) -> SomeHypergraph k (Prim k)
forall s l (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> SomeHypergraph s l
SomeHypergraph
(forall l (as :: [k]) (bs :: [k]).
(SortList as, SortList bs) =>
[SomeSort k]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
forall {s} l (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph @(Prim k) @as @bs ([SomeSort k]
inputs [SomeSort k] -> [SomeSort k] -> [SomeSort k]
forall a. [a] -> [a] -> [a]
++ [SomeSort k]
outputs [SomeSort k] -> [SomeSort k] -> [SomeSort k]
forall a. [a] -> [a] -> [a]
++ [forall (x :: k). SortOb x => SomeSort k
forall {k} {c :: OB k} (x :: k). c x => SomeOf c
Some @m]) [Int
0 .. Int
ni Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] [Int
ni .. Int
residual Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] [Box (Prim k) Int]
boxes)
in OpticPrim k -> Wires xs ~> Wires ys
forall {s} l (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
l -> Wires as ~> Wires bs
box (SomeHypergraph k (Prim k) -> OpticPrim k
forall k. SomeHypergraph k (Prim k) -> OpticPrim k
OpticPrim SomeHypergraph k (Prim k)
replacement)
runOptics
:: forall {k} (xs :: [(k, k)]) ys
. (SymMonoidal k)
=> (Wires xs :: OPTICS k) ~> Wires ys
-> Either String ((Fold (Forward xs) ** Fold (Backward ys)) ~> (Fold (Forward ys) ** Fold (Backward xs)))
runOptics :: forall {k} (xs :: [(k, k)]) (ys :: [(k, k)]).
SymMonoidal k =>
(Wires xs ~> Wires ys)
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
runOptics graph :: Wires xs ~> Wires ys
graph@Wires xs ~> Wires ys
DecCospan (Wires xs) (Wires ys)
Objs =
Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
forall {k} (xs :: [(k, k)]) r.
CategoryOf k =>
Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
r)
-> r
withHalves (forall s (xs :: [s]). SortList xs => Sorts xs
sorts @_ @xs) (((SortList (Forward xs), SortList (Backward xs), Ob (Forward xs),
Ob (Backward xs)) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
forall a b. (a -> b) -> a -> b
$
Sorts ys
-> ((SortList (Forward ys), SortList (Backward ys),
Ob (Forward ys), Ob (Backward ys)) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
forall {k} (xs :: [(k, k)]) r.
CategoryOf k =>
Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
r)
-> r
withHalves (forall s (xs :: [s]). SortList xs => Sorts xs
sorts @_ @ys) (((SortList (Forward ys), SortList (Backward ys), Ob (Forward ys),
Ob (Backward ys)) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> ((SortList (Forward ys), SortList (Backward ys),
Ob (Forward ys), Ob (Backward ys)) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
forall a b. (a -> b) -> a -> b
$
forall {k} (c :: OB k) (as :: [k]) (bs :: [k]) r.
(KnownListOf c as, KnownListOf c bs) =>
(KnownListOf c (as ++ bs) => r) -> r
forall (c :: OB k) (as :: [k]) (bs :: [k]) r.
(KnownListOf c as, KnownListOf c bs) =>
(KnownListOf c (as ++ bs) => r) -> r
withKnownAppend @SortOb @(Forward xs) @(Backward ys) ((KnownListOf SortOb (Forward xs ++ Backward ys) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> (KnownListOf SortOb (Forward xs ++ Backward ys) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
forall a b. (a -> b) -> a -> b
$
forall {k} (c :: OB k) (as :: [k]) (bs :: [k]) r.
(KnownListOf c as, KnownListOf c bs) =>
(KnownListOf c (as ++ bs) => r) -> r
forall (c :: OB k) (as :: [k]) (bs :: [k]) r.
(KnownListOf c as, KnownListOf c bs) =>
(KnownListOf c (as ++ bs) => r) -> r
withKnownAppend @SortOb @(Forward ys) @(Backward xs) ((KnownListOf SortOb (Forward ys ++ Backward xs) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> (KnownListOf SortOb (Forward ys ++ Backward xs) =>
Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs))))
-> Either
String
((Fold (Forward xs) ** Fold (Backward ys))
~> (Fold (Forward ys) ** Fold (Backward xs)))
forall a b. (a -> b) -> a -> b
$ do
SomeHypergraph (DecCospan @c (Sub (FinHask ins)) (Sub (FinHask outs)) (Boxes boxes)) <-
(SomeSort (k, k) -> [SomeSort k])
-> (OpticPrim k -> SomeHypergraph k (Prim k))
-> (Wires xs ~> Wires ys)
-> Either String (SomeHypergraph k (Prim k))
forall {s} {t} l m (a :: OPENHG s l) (b :: OPENHG s l).
(SomeSort s -> [SomeSort t])
-> (l -> SomeHypergraph t m)
-> (a ~> b)
-> Either String (SomeHypergraph t m)
unsafeSubstituteWith SomeSort (k, k) -> [SomeSort k]
forall k. CategoryOf k => SomeSort (k, k) -> [SomeSort k]
expand (\(OpticPrim SomeHypergraph k (Prim k)
g) -> SomeHypergraph k (Prim k)
g) Wires xs ~> Wires ys
graph
let unPort (Port Int
i) = Int
i
inputs = (Port (PortSorts b1) -> Int) -> [Port (PortSorts b1)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Port (PortSorts b1) -> Int
forall {s} {xs :: [s]}. Port xs -> Int
unPort (Map a1 (Port (PortSorts b1)) -> [Port (PortSorts b1)]
forall k a. Map k a -> [a]
M.elems Map a1 b1
Map a1 (Port (PortSorts b1))
ins)
outputs = (Port (PortSorts b1) -> Int) -> [Port (PortSorts b1)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Port (PortSorts b1) -> Int
forall {s} {xs :: [s]}. Port xs -> Int
unPort (Map a1 (Port (PortSorts b1)) -> [Port (PortSorts b1)]
forall k a. Map k a -> [a]
M.elems Map a1 b1
Map a1 (Port (PortSorts b1))
outs)
evenPorts = [a] -> [a]
forall {a}. [a] -> [a]
everyOther
oddPorts = [a] -> [a]
forall {a}. [a] -> [a]
everyOther ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
1
computation =
forall l (as :: [k]) (bs :: [k]).
(SortList as, SortList bs) =>
[SomeSort k]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
forall {s} l (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph @(Prim k)
@(Forward xs ++ Backward ys)
@(Forward ys ++ Backward xs)
(forall s (xs :: [s]). SortList xs => [SomeSort s]
sortList @_ @(WireSorts (DC c)))
([Int] -> [Int]
forall {a}. [a] -> [a]
evenPorts [Int]
inputs [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int] -> [Int]
forall {a}. [a] -> [a]
oddPorts [Int]
outputs)
([Int] -> [Int]
forall {a}. [a] -> [a]
evenPorts [Int]
outputs [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int] -> [Int]
forall {a}. [a] -> [a]
oddPorts [Int]
inputs)
((Box (Prim k) (Port (PortSorts b1)) -> Box (Prim k) Int)
-> [Box (Prim k) (Port (PortSorts b1))] -> [Box (Prim k) Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Port (PortSorts b1) -> Int)
-> Box (Prim k) (Port (PortSorts b1)) -> Box (Prim k) Int
forall a b. (a -> b) -> Box (Prim k) a -> Box (Prim k) b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap Port (PortSorts b1) -> Int
forall {s} {xs :: [s]}. Port xs -> Int
unPort) [Box (Prim k) n]
[Box (Prim k) (Port (PortSorts b1))]
boxes)
result <- simplify permutations computation
pure $
unStr
((concatMany @(Forward ys) ** concatMany @(Backward xs)) . result . (splitMany @(Forward xs) ** splitMany @(Backward ys)))
where
everyOther :: [a] -> [a]
everyOther [] = []
everyOther (a
x : [a]
rest) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
everyOther (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
1 [a]
rest)
expand :: (CategoryOf k) => SomeSort (k, k) -> [SomeSort k]
expand :: forall k. CategoryOf k => SomeSort (k, k) -> [SomeSort k]
expand (Some @p) = [forall (x :: k). SortOb x => SomeSort k
forall {k} {c :: OB k} (x :: k). c x => SomeOf c
Some @(Fst @ p), forall (x :: k). SortOb x => SomeSort k
forall {k} {c :: OB k} (x :: k). c x => SomeOf c
Some @(Snd @ p)]
withHalves
:: forall {k} xs r
. (CategoryOf k)
=> Sorts (xs :: [(k, k)])
-> ( ( SortList (Forward xs)
, SortList (Backward xs)
, Ob (Forward xs)
, Ob (Backward xs)
)
=> r
)
-> r
withHalves :: forall {k} (xs :: [(k, k)]) r.
CategoryOf k =>
Sorts xs
-> ((SortList (Forward xs), SortList (Backward xs),
Ob (Forward xs), Ob (Backward xs)) =>
r)
-> r
withHalves Sorts xs
xs (SortList (Forward xs), SortList (Backward xs), Ob (Forward xs),
Ob (Backward xs)) =>
r
k = Sorts xs -> (Sorts (Forward xs) -> Sorts (Backward xs) -> r) -> r
forall (zs :: [(k, k)]).
Sorts zs -> (Sorts (Forward zs) -> Sorts (Backward zs) -> r) -> r
go Sorts xs
xs \Sorts (Forward xs)
as Sorts (Backward xs)
ds ->
Sorts (Forward xs) -> (SortList (Forward xs) => r) -> r
forall {k} (c :: OB k) (xs :: [k]) r.
ListOf c xs -> (KnownListOf c xs => r) -> r
withKnownListOf Sorts (Forward xs)
as ((SortList (Forward xs) => r) -> r)
-> (SortList (Forward xs) => r) -> r
forall a b. (a -> b) -> a -> b
$
Sorts (Backward xs) -> (SortList (Backward xs) => r) -> r
forall {k} (c :: OB k) (xs :: [k]) r.
ListOf c xs -> (KnownListOf c xs => r) -> r
withKnownListOf Sorts (Backward xs)
ds ((SortList (Backward xs) => r) -> r)
-> (SortList (Backward xs) => r) -> r
forall a b. (a -> b) -> a -> b
$
Sorts (Forward xs) -> (IsList (Forward xs) => r) -> r
forall {k} (c :: k -> Constraint) (as :: [k]) r.
(CategoryOf k, forall (x :: k). c x => Ob' x) =>
ListOf c as -> (IsList as => r) -> r
withIsListOf Sorts (Forward xs)
as ((IsList (Forward xs) => r) -> r)
-> (IsList (Forward xs) => r) -> r
forall a b. (a -> b) -> a -> b
$
Sorts (Backward xs) -> (IsList (Backward xs) => r) -> r
forall {k} (c :: k -> Constraint) (as :: [k]) r.
(CategoryOf k, forall (x :: k). c x => Ob' x) =>
ListOf c as -> (IsList as => r) -> r
withIsListOf Sorts (Backward xs)
ds r
(SortList (Forward xs), SortList (Backward xs), Ob (Forward xs),
Ob (Backward xs)) =>
r
IsList (Backward xs) => r
k
where
go :: Sorts (zs :: [(k, k)]) -> (Sorts (Forward zs) -> Sorts (Backward zs) -> r) -> r
go :: forall (zs :: [(k, k)]).
Sorts zs -> (Sorts (Forward zs) -> Sorts (Backward zs) -> r) -> r
go ListOf SortOb zs
Nil Sorts (Forward zs) -> Sorts (Backward zs) -> r
next = Sorts (Forward zs) -> Sorts (Backward zs) -> r
next ListOf SortOb '[]
Sorts (Forward zs)
forall {k} (c :: OB k). ListOf c '[]
Nil ListOf SortOb '[]
Sorts (Backward zs)
forall {k} (c :: OB k). ListOf c '[]
Nil
go (Cons @p ListOf SortOb xs1
rest) Sorts (Forward zs) -> Sorts (Backward zs) -> r
next = ListOf SortOb xs1
-> (Sorts (Forward xs1) -> Sorts (Backward xs1) -> r) -> r
forall (zs :: [(k, k)]).
Sorts zs -> (Sorts (Forward zs) -> Sorts (Backward zs) -> r) -> r
go ListOf SortOb xs1
rest \Sorts (Forward xs1)
as Sorts (Backward xs1)
ds -> Sorts (Forward zs) -> Sorts (Backward zs) -> r
next (forall (x :: k) (xs1 :: [k]).
SortOb x =>
ListOf SortOb xs1 -> ListOf SortOb (x : xs1)
forall {k} {c :: OB k} (x :: k) (xs1 :: [k]).
c x =>
ListOf c xs1 -> ListOf c (x : xs1)
Cons @(Fst @ p) Sorts (Forward xs1)
as) (forall (x :: k) (xs1 :: [k]).
SortOb x =>
ListOf SortOb xs1 -> ListOf SortOb (x : xs1)
forall {k} {c :: OB k} (x :: k) (xs1 :: [k]).
c x =>
ListOf c xs1 -> ListOf c (x : xs1)
Cons @(Snd @ p) Sorts (Backward xs1)
ds)