0 | module Syntax.StringDiagram.Cartesian
  1 |
  2 | import public Control.Category.Core
  3 | import public Control.Category.Functor
  4 | import public Control.Category.Monoidal
  5 | import public Control.Category.Cartesian
  6 | import public Data.Fin
  7 | import Data.Wrap0
  8 | import public Language.Reflection
  9 | import public Syntax.StringDiagram.Util
 10 |
 11 | %default total
 12 | %language ElabReflection
 13 |
 14 | export infix 0 -<
 15 | export prefix 0 =<
 16 |
 17 | usedInDiagram : List SDiagramStep -> List String -> String -> Bool
 18 | usedInDiagram [] out n = elem n out
 19 | usedInDiagram (step :: steps) out n =
 20 |   elem n step.inputs ||
 21 |     (not (elem (Just n) step.outputs) && usedInDiagram steps out n)
 22 |
 23 | ||| Expand shortened string diagram notation into a full expression.
 24 | |||
 25 | ||| This takes two additional parameters: `obj`, which is a wildcard
 26 | ||| expression used when passing objects as parameters, and `impl`,
 27 | ||| which is the `Cartesian` implementation the expansion will use.
 28 | export
 29 | stringImpl : (obj,impl : TTImp) -> TTImp -> Elab TTImp
 30 | stringImpl obj impl t = do
 31 |   MkSDiagram inp steps out <- parseDiagram t
 32 |   crt <- genSym "crt"
 33 |   mon <- genSym "mon"
 34 |   cat <- genSym "cat"
 35 |   ts <- stringImpl' [<] (IVar EmptyFC crt) (IVar EmptyFC mon) inp out steps
 36 |   pure $ ILet EmptyFC EmptyFC MW crt (Implicit EmptyFC True) impl
 37 |     `(let Control.Category.Cartesian.MkCartesian @{~(IBindVar EmptyFC mon)} {} = ~(IVar EmptyFC crt)
 38 |           Control.Category.Monoidal.MkMonoidal @{~(IBindVar EmptyFC cat)} {} = ~(IVar EmptyFC mon)
 39 |       in ~(composeImp (IVar EmptyFC cat) ts))
 40 |   where
 41 |     listImp : Nat -> TTImp
 42 |     listImp Z = `(Prelude.Nil)
 43 |     listImp (S n) = `(Prelude.(::) ~obj ~(listImp n))
 44 |
 45 |     toFinList : List Integer -> TTImp
 46 |     toFinList [] = `(Prelude.Nil)
 47 |     toFinList (n :: ns) =
 48 |       `(Prelude.(::) (Data.Fin.fromInteger ~(IPrimVal EmptyFC (BI n))) ~(toFinList ns))
 49 |
 50 |     stringImpl' : SnocList TTImp -> TTImp -> TTImp ->
 51 |                   List (Maybe String) -> List String -> List SDiagramStep -> Elab (SnocList TTImp)
 52 |     stringImpl' ts crt mon strings out (step :: steps) = do
 53 |       let (us, used) = unzip $
 54 |                         filter (\(_,name) => not (elem (Just name) step.outputs) &&
 55 |                                               usedInDiagram steps out name) $
 56 |                         mapMaybe (\(i,n) => (i,) <$> n) $
 57 |                         zip [0..natToInteger (length strings) - 1] strings
 58 |       let Just is = for step.inputs $ \n =>
 59 |                       finToInteger <$> findIndex (== Just n) strings
 60 |         | Nothing => fail "Unrecognized string name"
 61 |       let sw = toFinList (us ++ is)
 62 |       stringImpl'
 63 |         (ts :<
 64 |         `(Control.Category.Cartesian.swizzle @{~crt}
 65 |         {xs = ~(listImp $ length strings)} ~sw) :<
 66 |         `(Control.Category.Monoidal.applyAssoc @{~mon}
 67 |           {xs = ~(listImp $ length us), ys = ~(listImp $ length step.inputs),
 68 |            ys' = ~(listImp $ length step.outputs), zs = Prelude.Nil} ~(step.mor)))
 69 |         crt mon
 70 |         (map Just used ++ step.outputs)
 71 |         out steps
 72 |     stringImpl' ts crt _ strings out [] = do
 73 |       let Just is = for out $ \n =>
 74 |                       finToInteger <$> findIndex (== Just n) strings
 75 |         | Nothing => fail "Unrecognized string name"
 76 |       let sw = toFinList is
 77 |       pure (ts :<
 78 |         `(Control.Category.Cartesian.swizzle @{~crt}
 79 |           {xs = ~(listImp $ length strings)} ~sw))
 80 |       
 81 |     composeImp : TTImp -> SnocList TTImp -> TTImp
 82 |     composeImp cat [<] = `(Control.Category.Core.id {a = ~obj})
 83 |     composeImp cat [<t] = t
 84 |     composeImp cat (ts :< t) =
 85 |       `(Control.Category.Core.(.) @{~cat}
 86 |         {a = ~obj, b = ~obj, c = ~obj}
 87 |         ~t ~(composeImp cat ts))
 88 |
 89 | ||| Enter string diagram notation (for `Cartesian`).
 90 | |||
 91 | ||| This elaboration script must be specifically invoked with
 92 | ||| `%runElab`. The category is inferred from the return type, but
 93 | ||| the tensor product must be passed as an explicit argument.
 94 | export
 95 | string : {obj : _} -> {0 cat : Hom obj} -> (0 ten : obj -> obj -> obj) -> {0 i,a,b : obj} ->
 96 |          Cartesian cat ten i => TTImp -> Elab (cat a b)
 97 | string {obj = obj@(Wrap0 _)} _ @{impl} t = check !(stringImpl `(W0 _) !(quote impl) t)
 98 | string {obj = _} @{impl} _ t = check !(stringImpl `(_) !(quote impl) t)
 99 |
100 |