0 | module Syntax.StringDiagram.Symmetric
  1 |
  2 | import public Control.Category.Core
  3 | import public Control.Category.Functor
  4 | import public Control.Category.Monoidal
  5 | import public Control.Category.Braided
  6 | import public Data.Vect
  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 | ||| Expand shortened string diagram notation into a full expression.
 18 | |||
 19 | ||| This takes two additional parameters: `obj`, which is a wildcard
 20 | ||| expression used when passing objects as parameters, and `impl`,
 21 | ||| which is the `Braided` implementation the expansion will use.
 22 | export
 23 | stringImpl : (obj,impl : TTImp) -> TTImp -> Elab TTImp
 24 | stringImpl obj impl t = do
 25 |   MkSDiagram inp steps out <- parseDiagram t
 26 |   let Just inp' = the (Maybe _) $ sequence inp
 27 |     | Nothing => fail "Strings cannot be discarded"
 28 |   sym <- genSym "sym"
 29 |   brd <- genSym "brd"
 30 |   mon <- genSym "mon"
 31 |   cat <- genSym "cat"
 32 |   ts <- stringImpl' [<] (IVar EmptyFC sym) (IVar EmptyFC mon) inp' out steps
 33 |   pure $ ILet EmptyFC EmptyFC MW sym (Implicit EmptyFC True) impl
 34 |     `(let Control.Category.Braided.MkMonSymmetric @{~(IBindVar EmptyFC brd)} {} = ~(IVar EmptyFC sym)
 35 |           Control.Category.Braided.MkBraided @{~(IBindVar EmptyFC mon)} {} = ~(IVar EmptyFC brd)
 36 |           Control.Category.Monoidal.MkMonoidal @{~(IBindVar EmptyFC cat)} {} = ~(IVar EmptyFC mon)
 37 |       in ~(composeImp (IVar EmptyFC cat) ts))
 38 |   where
 39 |     listImp : Nat -> TTImp
 40 |     listImp Z = `(Prelude.Nil)
 41 |     listImp (S n) = `(Prelude.(::) ~obj ~(listImp n))
 42 |
 43 |     permutation : List Integer -> TTImp
 44 |     permutation [] = `(Data.Permutation.Nil)
 45 |     permutation (x :: xs) =
 46 |       `(Data.Permutation.(::) (Data.Fin.fromInteger ~(IPrimVal EmptyFC (BI x))) ~(permutation xs))
 47 |
 48 |     stringsToBack : Eq a => SnocList Integer -> Integer -> List a -> List a -> TTImp
 49 |     stringsToBack xs _ _ [] = permutation (xs <>> [])
 50 |     stringsToBack xs r inp (str :: strings) =
 51 |       case findIndex (==str) inp of
 52 |         Just i => stringsToBack (xs :< r + finToInteger i) r (filter (/=str) inp) strings
 53 |         Nothing => stringsToBack (xs :< 0) (r - 1) inp strings
 54 |
 55 |
 56 |     stringImpl' : SnocList TTImp -> TTImp -> TTImp ->
 57 |                   List String -> List String -> List SDiagramStep -> Elab (SnocList TTImp)
 58 |     stringImpl' ts sym mon strings out (step :: steps) = do
 59 |       let Just o = the (Maybe _) $ sequence step.outputs
 60 |         | Nothing => fail "Strings cannot be discarded"
 61 |       let rest = filter (\n => not $ elem n step.inputs) strings
 62 |       let False = any (`elem` rest) o
 63 |         | True => fail "Cannot shadow string name"
 64 |       let perm = stringsToBack [<] (natToInteger $ length rest) step.inputs strings
 65 |       stringImpl' (ts :<
 66 |         `(Control.Category.Braided.permute @{~sym} {xs = ~(listImp $ length strings)} ~perm) :<
 67 |         `(Control.Category.Monoidal.applyAssoc @{~mon}
 68 |           {xs = ~(listImp $ length rest), ys = ~(listImp $ length step.inputs),
 69 |            ys' = ~(listImp $ length o), zs = Prelude.Nil} ~(step.mor)))
 70 |         sym mon (rest ++ o) out steps
 71 |     stringImpl' ts sym _ strings out [] = do
 72 |       perm <- do
 73 |         let Just is = for out $ \n =>
 74 |                       findIndex (== n) strings
 75 |           | Nothing => fail "Unrecognized string name"
 76 |         let Just p = fromPermSeq =<< toVect (length strings) is
 77 |           | Nothing => fail "Return strings violate linearity"
 78 |         pure $ permutation $ natToInteger <$> toList p
 79 |       pure $ ts :< `(Control.Category.Braided.permute @{~sym}
 80 |                       {xs = ~(listImp $ length strings)} ~perm)
 81 |
 82 |     composeImp : TTImp -> SnocList TTImp -> TTImp
 83 |     composeImp cat [<] = `(Control.Category.Core.id {a = ~obj})
 84 |     composeImp cat [<t] = t
 85 |     composeImp cat (ts :< t) =
 86 |       `(Control.Category.Core.(.) @{~cat}
 87 |         {a = ~obj, b = ~obj, c = ~obj}
 88 |         ~t ~(composeImp cat ts))
 89 |
 90 | ||| Enter string diagram notation (for `MonSymmetric`).
 91 | |||
 92 | ||| This elaboration script must be specifically invoked with
 93 | ||| `%runElab`. The category is inferred from the return type, but
 94 | ||| the tensor product must be passed as an explicit argument.
 95 | export
 96 | string : {obj : _} -> {0 cat : Hom obj} -> (0 ten : obj -> obj -> obj) -> {0 i,a,b : obj} ->
 97 |          MonSymmetric cat ten i => TTImp -> Elab (cat a b)
 98 | string {obj = obj@(Wrap0 _)} _ @{impl} t = check !(stringImpl `(W0 _) !(quote impl) t)
 99 | string {obj = _} @{impl} _ t = check !(stringImpl `(_) !(quote impl) t)
100 |