-- | The walking bimonoid, the PROP for monoids and comonoids satisfying the
-- bimonoid compatibility laws. Objects count wires and tensor adds their counts.
-- An arrow records the paths between its inputs and outputs, in copy order at
-- each input and merge order at each output.
module Proarrow.Category.Instance.Bimonoid
  ( BIPATHS (..)
  , Bimonoid
  , Wire
  , fromOrderedPaths
  , orderedPaths
  ) where

import Control.Monad (guard)
import Data.Bifunctor (first)
import Data.Fin (Fin, weakenLeft, weakenRight)
import Data.List (sort)
import Data.Map.Strict qualified as M
import Data.Proxy (Proxy (..))
import Data.Type.Nat (Nat0, Nat1, Plus, snat, snatToNat)
import Data.Typeable (eqT)
import Data.Vec.Lazy qualified as V
import Prelude hiding (id, (**), (.))
import Prelude qualified as P

import Proarrow.Category.Enriched.Dagger (DaggerProfunctor (..))
import Proarrow.Category.Instance.Simplex (IsNat, Nat)
import Proarrow.Category.Monoidal (Monoidal (..), MonoidalProfunctor (..), SymMonoidal (swap), associatorDefault)
import Proarrow.Category.Monoidal.Hypergraph (Sized (..))
import Proarrow.Colimit.Initial (HasInitialObject (..))
import Proarrow.Core (CAT, CategoryOf (..), Profunctor (..), Promonad (..), UN, WrappedOb, dimapDefault)
import Proarrow.Limit.Terminal (HasTerminalObject (..))
import Proarrow.Monoid qualified as Mon
import Proarrow.Optic.Iso (DecidableIso (..), isoFromEquality)

type data BIPATHS = BP Nat

type Wire :: BIPATHS
type Wire = BP Nat1

type Bimonoid :: CAT BIPATHS
data Bimonoid a b where
  Bimonoid :: (IsNat a, IsNat b) => V.Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)

deriving instance Eq (Bimonoid a b)
deriving instance Show (Bimonoid a b)

-- | Build an arrow from one list per output, in merge order. Each pair gives
-- the source input and its zero-based copy position. For each input the copy
-- positions must be consecutive starting at zero, with no repetitions.
--
-- >>> import Data.Fin (fin0)
-- >>> let crossed = [(fin0 :: Fin Nat1, 1), (fin0, 0)] V.::: V.VNil
-- >>> fmap orderedPaths (fromOrderedPaths crossed) == Just crossed
-- True
fromOrderedPaths :: (IsNat a, IsNat b) => V.Vec b [(Fin a, Int)] -> Maybe (Bimonoid (BP a) (BP b))
fromOrderedPaths :: forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Maybe (Bimonoid (BP a) (BP b))
fromOrderedPaths Vec b [(Fin a, Int)]
paths = do
  let positions :: Map (Fin a) [Int]
positions = ([Int] -> [Int] -> [Int]) -> [(Fin a, [Int])] -> Map (Fin a) [Int]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
(++) [(Fin a
input, [Int
position]) | (Fin a
input, Int
position) <- Vec b [(Fin a, Int)] -> [(Fin a, Int)]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat Vec b [(Fin a, Int)]
paths]
  Bool -> Maybe ()
forall (f :: Type -> Type). Alternative f => Bool -> f ()
guard (([Int] -> Bool) -> Map (Fin a) [Int] -> Bool
forall (t :: Type -> Type) a.
Foldable t =>
(a -> Bool) -> t a -> Bool
all (\[Int]
ps -> [Int] -> [Int]
forall a. Ord a => [a] -> [a]
sort [Int]
ps [Int] -> [Int] -> Bool
forall a. Eq a => a -> a -> Bool
== [Int
0 .. [Int] -> Int
forall a. [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [Int]
ps Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]) Map (Fin a) [Int]
positions)
  Bimonoid (BP a) (BP b) -> Maybe (Bimonoid (BP a) (BP b))
forall a. a -> Maybe a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid Vec b [(Fin a, Int)]
paths)

-- | The paths arriving at each output, in merge order.
orderedPaths :: Bimonoid (BP a) (BP b) -> V.Vec b [(Fin a, Int)]
orderedPaths :: forall (a :: Nat) (b :: Nat).
Bimonoid (BP a) (BP b) -> Vec b [(Fin a, Int)]
orderedPaths (Bimonoid Vec b [(Fin a, Int)]
paths) = Vec b [(Fin a, Int)]
Vec b [(Fin a, Int)]
paths

permutationArrow :: (IsNat a, IsNat b) => V.Vec b (Fin a) -> Bimonoid (BP a) (BP b)
permutationArrow :: forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b (Fin a) -> Bimonoid (BP a) (BP b)
permutationArrow = Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid (Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b))
-> (Vec b (Fin a) -> Vec b [(Fin a, Int)])
-> Vec b (Fin a)
-> Bimonoid (BP a) (BP b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
P.. (Fin a -> [(Fin a, Int)]) -> Vec b (Fin a) -> Vec b [(Fin a, Int)]
forall a b. (a -> b) -> Vec b a -> Vec b b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Fin a
i -> [(Fin a
i, Int
0)])

instance CategoryOf BIPATHS where
  type (~>) = Bimonoid
  type Ob a = WrappedOb BP a

instance Profunctor Bimonoid where
  dimap :: forall (c :: BIPATHS) (a :: BIPATHS) (b :: BIPATHS) (d :: BIPATHS).
(c ~> a) -> (b ~> d) -> Bimonoid a b -> Bimonoid c d
dimap = (c ~> a) -> (b ~> d) -> Bimonoid a b -> Bimonoid c d
Bimonoid c a -> Bimonoid b d -> Bimonoid a b -> Bimonoid c d
forall {k} (p :: CAT k) (c :: k) (a :: k) (b :: k) (d :: k).
Promonad p =>
p c a -> p b d -> p a b -> p c d
dimapDefault
  (Ob a, Ob b) => r
r \\ :: forall (a :: BIPATHS) (b :: BIPATHS) r.
((Ob a, Ob b) => r) -> Bimonoid a b -> r
\\ Bimonoid{} = r
(Ob a, Ob b) => r
r

instance Promonad Bimonoid where
  id :: forall (a :: BIPATHS). Ob a => Bimonoid a a
id = Vec (UN BP a) (Fin (UN BP a))
-> Bimonoid (BP (UN BP a)) (BP (UN BP a))
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b (Fin a) -> Bimonoid (BP a) (BP b)
permutationArrow Vec (UN BP a) (Fin (UN BP a))
forall (n :: Nat). SNatI n => Vec n (Fin n)
V.universe
  Bimonoid Vec b [(Fin a, Int)]
g . :: forall (b :: BIPATHS) (c :: BIPATHS) (a :: BIPATHS).
Bimonoid b c -> Bimonoid a b -> Bimonoid a c
. Bimonoid Vec b [(Fin a, Int)]
f =
    Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid (Vec b [(Fin a, Int, Int)] -> Vec b [(Fin a, Int)]
forall i (n :: Nat).
Ord i =>
Vec n [(i, Int, Int)] -> Vec n [(i, Int)]
rankPaths (([(Fin b, Int)] -> [(Fin a, Int, Int)])
-> Vec b [(Fin b, Int)] -> Vec b [(Fin a, Int, Int)]
forall a b. (a -> b) -> Vec b a -> Vec b b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Fin b, Int) -> [(Fin a, Int, Int)])
-> [(Fin b, Int)] -> [(Fin a, Int, Int)]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> [b]) -> t a -> [b]
concatMap (\(Fin b
j, Int
q) -> [(Fin a
i, Int
p, Int
q) | (Fin a
i, Int
p) <- Vec b [(Fin a, Int)]
f Vec b [(Fin a, Int)] -> Fin b -> [(Fin a, Int)]
forall (n :: Nat) a. Vec n a -> Fin n -> a
V.! Fin b
j])) Vec b [(Fin a, Int)]
Vec b [(Fin b, Int)]
g))

-- Copy order is lexicographic in the upstream and downstream copy positions.
rankPaths :: (Ord i) => V.Vec n [(i, Int, Int)] -> V.Vec n [(i, Int)]
rankPaths :: forall i (n :: Nat).
Ord i =>
Vec n [(i, Int, Int)] -> Vec n [(i, Int)]
rankPaths Vec n [(i, Int, Int)]
expanded = ([(i, Int, Int)] -> [(i, Int)])
-> Vec n [(i, Int, Int)] -> Vec n [(i, Int)]
forall a b. (a -> b) -> Vec n a -> Vec n b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (((i, Int, Int) -> (i, Int)) -> [(i, Int, Int)] -> [(i, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (\key :: (i, Int, Int)
key@(i
i, Int
_, Int
_) -> (i
i, Map (i, Int, Int) Int
positions Map (i, Int, Int) Int -> (i, Int, Int) -> Int
forall k a. Ord k => Map k a -> k -> a
M.! (i, Int, Int)
key))) Vec n [(i, Int, Int)]
expanded
  where
    byInput :: Map i [(Int, Int)]
byInput = ([(Int, Int)] -> [(Int, Int)] -> [(Int, Int)])
-> [(i, [(Int, Int)])] -> Map i [(Int, Int)]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
forall a. [a] -> [a] -> [a]
(++) [(i
i, [(Int
p, Int
q)]) | (i
i, Int
p, Int
q) <- Vec n [(i, Int, Int)] -> [(i, Int, Int)]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat Vec n [(i, Int, Int)]
expanded]
    positions :: Map (i, Int, Int) Int
positions =
      [((i, Int, Int), Int)] -> Map (i, Int, Int) Int
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
        [ ((i
i, Int
p, Int
q), Int
pos)
        | (i
i, [(Int, Int)]
copies) <- Map i [(Int, Int)] -> [(i, [(Int, Int)])]
forall k a. Map k a -> [(k, a)]
M.toList Map i [(Int, Int)]
byInput
        , (Int
pos, (Int
p, Int
q)) <- [Int] -> [(Int, Int)] -> [(Int, (Int, Int))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] ([(Int, Int)] -> [(Int, Int)]
forall a. Ord a => [a] -> [a]
sort [(Int, Int)]
copies)
        ]

instance MonoidalProfunctor Bimonoid where
  one :: Bimonoid Unit Unit
one = Vec Nat0 [(Fin Nat0, Int)] -> Bimonoid (BP Nat0) (BP Nat0)
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid Vec Nat0 [(Fin Nat0, Int)]
forall a. Vec Nat0 a
V.VNil
  Bimonoid @a @b Vec b [(Fin a, Int)]
left ** :: forall (x1 :: BIPATHS) (x2 :: BIPATHS) (y1 :: BIPATHS)
       (y2 :: BIPATHS).
Bimonoid x1 x2 -> Bimonoid y1 y2 -> Bimonoid (x1 ** y1) (x2 ** y2)
** Bimonoid @c @d Vec b [(Fin a, Int)]
right =
    forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @Nat @a @c (forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @Nat @b @d (Vec (Plus b b) [(Fin (Plus a a), Int)]
-> Bimonoid (BP (Plus a a)) (BP (Plus b b))
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid (Vec b [(Fin (Plus a a), Int)]
left' Vec b [(Fin (Plus a a), Int)]
-> Vec b [(Fin (Plus a a), Int)]
-> Vec (Plus b b) [(Fin (Plus a a), Int)]
forall (n :: Nat) a (m :: Nat).
Vec n a -> Vec m a -> Vec (Plus n m) a
V.++ Vec b [(Fin (Plus a a), Int)]
right')))
    where
      left' :: Vec b [(Fin (Plus a a), Int)]
left' = ([(Fin a, Int)] -> [(Fin (Plus a a), Int)])
-> Vec b [(Fin a, Int)] -> Vec b [(Fin (Plus a a), Int)]
forall a b. (a -> b) -> Vec b a -> Vec b b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Fin a, Int) -> (Fin (Plus a a), Int))
-> [(Fin a, Int)] -> [(Fin (Plus a a), Int)]
forall a b. (a -> b) -> [a] -> [b]
map ((Fin a -> Fin (Plus a a)) -> (Fin a, Int) -> (Fin (Plus a a), Int)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: Type -> Type -> Type) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Proxy a -> Fin a -> Fin (Plus a a)
forall (n :: Nat) (m :: Nat).
SNatI n =>
Proxy m -> Fin n -> Fin (Plus n m)
weakenLeft (forall {k} (t :: k). Proxy t
forall (t :: Nat). Proxy t
Proxy @c)))) Vec b [(Fin a, Int)]
left
      right' :: Vec b [(Fin (Plus a a), Int)]
right' = ([(Fin a, Int)] -> [(Fin (Plus a a), Int)])
-> Vec b [(Fin a, Int)] -> Vec b [(Fin (Plus a a), Int)]
forall a b. (a -> b) -> Vec b a -> Vec b b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Fin a, Int) -> (Fin (Plus a a), Int))
-> [(Fin a, Int)] -> [(Fin (Plus a a), Int)]
forall a b. (a -> b) -> [a] -> [b]
map ((Fin a -> Fin (Plus a a)) -> (Fin a, Int) -> (Fin (Plus a a), Int)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: Type -> Type -> Type) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Proxy a -> Fin a -> Fin (Plus a a)
forall (n :: Nat) (m :: Nat).
SNatI n =>
Proxy n -> Fin m -> Fin (Plus n m)
weakenRight (forall {k} (t :: k). Proxy t
forall (t :: Nat). Proxy t
Proxy @a)))) Vec b [(Fin a, Int)]
right

instance Monoidal BIPATHS where
  type Unit = BP Nat0
  type a ** b = BP (Plus (UN BP a) (UN BP b))
  withOb2 :: forall (a :: BIPATHS) (b :: BIPATHS) r.
(Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @(BP a) @(BP b) Ob (a ** b) => r
result = forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @Nat @a @b r
Ob (UN BP a ** UN BP b) => r
Ob (a ** b) => r
result
  associator :: forall (a :: BIPATHS) (b :: BIPATHS) (c :: BIPATHS).
(Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
associator @a @b @c = forall {k} (a :: k) (b :: k) (c :: k).
(Strictly a, Monoidal k, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
forall (a :: BIPATHS) (b :: BIPATHS) (c :: BIPATHS).
(Strictly a, Monoidal BIPATHS, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
associatorDefault @a @b @c
  associatorInv :: forall (a :: BIPATHS) (b :: BIPATHS) (c :: BIPATHS).
(Ob a, Ob b, Ob c) =>
(a ** (b ** c)) ~> ((a ** b) ** c)
associatorInv @a @b @c = forall {k} (a :: k) (b :: k) (c :: k).
(Strictly a, Monoidal k, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
forall (a :: BIPATHS) (b :: BIPATHS) (c :: BIPATHS).
(Strictly a, Monoidal BIPATHS, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
associatorDefault @a @b @c

instance SymMonoidal BIPATHS where
  swap :: forall (a :: BIPATHS) (b :: BIPATHS).
(Ob a, Ob b) =>
(a ** b) ~> (b ** a)
swap @(BP a) @(BP b) =
    forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @Nat @a @b (forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @Nat @b @a (Vec (Plus (UN BP b) (UN BP a)) (Fin (Plus (UN BP a) (UN BP b)))
-> Bimonoid
     (BP (Plus (UN BP a) (UN BP b))) (BP (Plus (UN BP b) (UN BP a)))
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b (Fin a) -> Bimonoid (BP a) (BP b)
permutationArrow (Vec (UN BP b) (Fin (Plus (UN BP a) (UN BP b)))
right Vec (UN BP b) (Fin (Plus (UN BP a) (UN BP b)))
-> Vec (UN BP a) (Fin (Plus (UN BP a) (UN BP b)))
-> Vec (Plus (UN BP b) (UN BP a)) (Fin (Plus (UN BP a) (UN BP b)))
forall (n :: Nat) a (m :: Nat).
Vec n a -> Vec m a -> Vec (Plus n m) a
V.++ Vec (UN BP a) (Fin (Plus (UN BP a) (UN BP b)))
left)))
    where
      right :: Vec (UN BP b) (Fin (Plus (UN BP a) (UN BP b)))
right = (Fin (UN BP b) -> Fin (Plus (UN BP a) (UN BP b)))
-> Vec (UN BP b) (Fin (UN BP b))
-> Vec (UN BP b) (Fin (Plus (UN BP a) (UN BP b)))
forall a b. (a -> b) -> Vec (UN BP b) a -> Vec (UN BP b) b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Proxy (UN BP a) -> Fin (UN BP b) -> Fin (Plus (UN BP a) (UN BP b))
forall (n :: Nat) (m :: Nat).
SNatI n =>
Proxy n -> Fin m -> Fin (Plus n m)
weakenRight (forall {k} (t :: k). Proxy t
forall (t :: Nat). Proxy t
Proxy @a)) (forall (n :: Nat). SNatI n => Vec n (Fin n)
V.universe @b)
      left :: Vec (UN BP a) (Fin (Plus (UN BP a) (UN BP b)))
left = (Fin (UN BP a) -> Fin (Plus (UN BP a) (UN BP b)))
-> Vec (UN BP a) (Fin (UN BP a))
-> Vec (UN BP a) (Fin (Plus (UN BP a) (UN BP b)))
forall a b. (a -> b) -> Vec (UN BP a) a -> Vec (UN BP a) b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Proxy (UN BP b) -> Fin (UN BP a) -> Fin (Plus (UN BP a) (UN BP b))
forall (n :: Nat) (m :: Nat).
SNatI n =>
Proxy m -> Fin n -> Fin (Plus n m)
weakenLeft (forall {k} (t :: k). Proxy t
forall (t :: Nat). Proxy t
Proxy @b)) (forall (n :: Nat). SNatI n => Vec n (Fin n)
V.universe @a)

instance DaggerProfunctor Bimonoid where
  dagger :: forall (a :: BIPATHS) (b :: BIPATHS). Bimonoid a b -> Bimonoid b a
dagger (Bimonoid @_ @b Vec b [(Fin a, Int)]
fibres) = Vec a [(Fin b, Int)] -> Bimonoid (BP b) (BP a)
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid ((Fin a -> [(Fin b, Int)]) -> Vec a [(Fin b, Int)]
forall (n :: Nat) a. SNatI n => (Fin n -> a) -> Vec n a
V.tabulate (\Fin a
i -> ((Int, (Fin b, Int)) -> (Fin b, Int))
-> [(Int, (Fin b, Int))] -> [(Fin b, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, (Fin b, Int)) -> (Fin b, Int)
forall a b. (a, b) -> b
snd ([(Int, (Fin b, Int))] -> [(Int, (Fin b, Int))]
forall a. Ord a => [a] -> [a]
sort ([(Int, (Fin b, Int))]
-> Fin a
-> Map (Fin a) [(Int, (Fin b, Int))]
-> [(Int, (Fin b, Int))]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Fin a
i Map (Fin a) [(Int, (Fin b, Int))]
byInput))))
    where
      byInput :: Map (Fin a) [(Int, (Fin b, Int))]
byInput =
        ([(Int, (Fin b, Int))]
 -> [(Int, (Fin b, Int))] -> [(Int, (Fin b, Int))])
-> [(Fin a, [(Int, (Fin b, Int))])]
-> Map (Fin a) [(Int, (Fin b, Int))]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith
          [(Int, (Fin b, Int))]
-> [(Int, (Fin b, Int))] -> [(Int, (Fin b, Int))]
forall a. [a] -> [a] -> [a]
(++)
          [ (Fin a
i, [(Int
copyPosition, (Fin b
j, Int
mergePosition))])
          | (Fin b
j, [(Fin a, Int)]
fibre) <- [Fin b] -> [[(Fin a, Int)]] -> [(Fin b, [(Fin a, Int)])]
forall a b. [a] -> [b] -> [(a, b)]
zip (Vec b (Fin b) -> [Fin b]
forall (n :: Nat) a. Vec n a -> [a]
V.toList (forall (n :: Nat). SNatI n => Vec n (Fin n)
V.universe @b)) (Vec b [(Fin a, Int)] -> [[(Fin a, Int)]]
forall (n :: Nat) a. Vec n a -> [a]
V.toList Vec b [(Fin a, Int)]
fibres)
          , (Int
mergePosition, (Fin a
i, Int
copyPosition)) <- [Int] -> [(Fin a, Int)] -> [(Int, (Fin a, Int))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [(Fin a, Int)]
fibre
          ]

instance HasInitialObject BIPATHS where
  type InitialObject = BP Nat0
  initiate :: forall (a :: BIPATHS). Ob a => InitialObject ~> a
initiate = Vec (UN BP a) [(Fin Nat0, Int)]
-> Bimonoid (BP Nat0) (BP (UN BP a))
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid ([(Fin Nat0, Int)] -> Vec (UN BP a) [(Fin Nat0, Int)]
forall (n :: Nat) x. SNatI n => x -> Vec n x
V.repeat [])

instance HasTerminalObject BIPATHS where
  type TerminalObject = BP Nat0
  terminate :: forall (a :: BIPATHS). Ob a => a ~> TerminalObject
terminate = Vec Nat0 [(Fin (UN BP a), Int)]
-> Bimonoid (BP (UN BP a)) (BP Nat0)
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid Vec Nat0 [(Fin (UN BP a), Int)]
forall a. Vec Nat0 a
V.VNil

-- Tensor powers carry the componentwise multiplication of the generating wire.
instance (Ob (a :: BIPATHS)) => Mon.Monoid a where
  mempty :: Unit ~> a
mempty = Unit ~> a
InitialObject ~> BP (UN BP a)
forall k (a :: k). (HasInitialObject k, Ob a) => InitialObject ~> a
forall (a :: BIPATHS). Ob a => InitialObject ~> a
initiate
  mappend :: (a ** a) ~> a
mappend = forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @Nat @(UN BP a) @(UN BP a) (Vec (UN BP a) [(Fin (Plus (UN BP a) (UN BP a)), Int)]
-> Bimonoid (BP (Plus (UN BP a) (UN BP a))) (BP (UN BP a))
forall (a :: Nat) (b :: Nat).
(IsNat a, IsNat b) =>
Vec b [(Fin a, Int)] -> Bimonoid (BP a) (BP b)
Bimonoid ((Fin (UN BP a) -> [(Fin (Plus (UN BP a) (UN BP a)), Int)])
-> Vec (UN BP a) (Fin (UN BP a))
-> Vec (UN BP a) [(Fin (Plus (UN BP a) (UN BP a)), Int)]
forall a b. (a -> b) -> Vec (UN BP a) a -> Vec (UN BP a) b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap Fin (UN BP a) -> [(Fin (Plus (UN BP a) (UN BP a)), Int)]
pair Vec (UN BP a) (Fin (UN BP a))
forall (n :: Nat). SNatI n => Vec n (Fin n)
V.universe))
    where
      n :: Proxy (UN BP a)
n = forall {k} (t :: k). Proxy t
forall (t :: Nat). Proxy t
Proxy @(UN BP a)
      pair :: Fin (UN BP a) -> [(Fin (Plus (UN BP a) (UN BP a)), Int)]
      pair :: Fin (UN BP a) -> [(Fin (Plus (UN BP a) (UN BP a)), Int)]
pair Fin (UN BP a)
i = [(Proxy (UN BP a) -> Fin (UN BP a) -> Fin (Plus (UN BP a) (UN BP a))
forall (n :: Nat) (m :: Nat).
SNatI n =>
Proxy m -> Fin n -> Fin (Plus n m)
weakenLeft Proxy (UN BP a)
n Fin (UN BP a)
i, Int
0), (Proxy (UN BP a) -> Fin (UN BP a) -> Fin (Plus (UN BP a) (UN BP a))
forall (n :: Nat) (m :: Nat).
SNatI n =>
Proxy n -> Fin m -> Fin (Plus n m)
weakenRight Proxy (UN BP a)
n Fin (UN BP a)
i, Int
0)]

instance (Ob (a :: BIPATHS)) => Mon.Comonoid a where
  counit :: a ~> Unit
counit = a ~> Unit
BP (UN BP a) ~> TerminalObject
forall k (a :: k).
(HasTerminalObject k, Ob a) =>
a ~> TerminalObject
forall (a :: BIPATHS). Ob a => a ~> TerminalObject
terminate
  comult :: a ~> (a ** a)
comult = Bimonoid (BP (Plus (UN BP a) (UN BP a))) (BP (UN BP a))
-> Bimonoid (BP (UN BP a)) (BP (Plus (UN BP a) (UN BP a)))
forall k (p :: k +-> k) (a :: k) (b :: k).
DaggerProfunctor p =>
p a b -> p b a
forall (a :: BIPATHS) (b :: BIPATHS). Bimonoid a b -> Bimonoid b a
dagger (BP (UN BP a) ** BP (UN BP a)) ~> BP (UN BP a)
Bimonoid (BP (Plus (UN BP a) (UN BP a))) (BP (UN BP a))
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Mon.mappend

instance DecidableIso BIPATHS where
  isoOf :: forall (c :: (BIPATHS +-> BIPATHS) -> Constraint) (a :: BIPATHS)
       (b :: BIPATHS).
(Ob a, Ob b) =>
Maybe (Optic c a a b b)
isoOf @_ @a @b = Maybe (a :~: b) -> Maybe (Optic c a a b b)
forall {k} (c :: (k +-> k) -> Constraint) (a :: k) (b :: k).
(CategoryOf k, Ob a) =>
Maybe (a :~: b) -> Maybe (Optic c a a b b)
isoFromEquality (forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
forall (a :: BIPATHS) (b :: BIPATHS).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT @a @b)

instance Sized BIPATHS where
  sizeOf :: forall (a :: BIPATHS). Ob a => Int
sizeOf @(BP n) = Nat -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (SNat (UN BP a) -> Nat
forall (n :: Nat). SNat n -> Nat
snatToNat (forall (n :: Nat). SNatI n => SNat n
snat @n))