0 | module Syntax.StringDiagram.Symmetric
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
8 | import public Language.Reflection
9 | import public Syntax.StringDiagram.Util
12 | %language ElabReflection
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"
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))
39 | listImp : Nat -> TTImp
40 | listImp Z = `(Prelude.Nil
)
41 | listImp (S n) = `(Prelude.(::)
~obj ~(listImp n))
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))
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
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
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
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)
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))
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)