Skip to content

Commit

Permalink
ENH: Operadic interface for substitution of wiring diagrams.
Browse files Browse the repository at this point in the history
Closes #40.
  • Loading branch information
epatters committed Nov 21, 2019
1 parent d08bd20 commit 803d6f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/wiring_diagrams/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export AbstractBox, Box, WiringDiagram, Wire, Ports, PortValueError, Port,
singleton_diagram, induced_subdiagram, encapsulated_subdiagram,
substitute, encapsulate,
dom, codom, id, compose, otimes, munit, braid, mcopy, delete, mmerge, create,
permute, is_permuted_equal
ocompose, permute, is_permuted_equal

using Compat
using AutoHashEquals
Expand Down Expand Up @@ -555,7 +555,8 @@ end
Performs one or more substitutions. When performing multiple substitutions, the
substitutions are simultaneous.
This operation is the operadic composition of wiring diagrams.
This operation implements the operadic composition of wiring diagrams
(`ocompose`).
"""
function substitute(d::WiringDiagram, v::Int)
substitute(d, v, box(d,v)::WiringDiagram)
Expand Down Expand Up @@ -900,6 +901,23 @@ function mmerge(A::Ports, n::Int)::WiringDiagram
return f
end

""" Operadic composition of wiring diagrams.
This generic function has two different signatures, corresponding to the two
standard definitions of an operad (Yau, 2018, Operads of Wiring Diagrams,
Definitions 2.3 and 2.10).
This operation is a simple wrapper around substitution (`substitute`).
"""
function ocompose(f::WiringDiagram, gs::Vector{WiringDiagram})
@assert length(gs) == nboxes(f)
substitute(f, box_ids(f), gs)
end
function ocompose(f::WiringDiagram, i::Int, g::WiringDiagram)
@assert 1 <= i <= nboxes(f)
substitute(f, box_ids(f)[i], g)
end

function collect_values(ob::ObExpr)::Vector
exprs = collect(ob)
@assert all(head(expr) == :generator for expr in exprs)
Expand Down
31 changes: 31 additions & 0 deletions test/wiring_diagrams/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,35 @@ X = Ports([:A])
# Unit
@test compose(otimes(id(X),create(X)), mmerge(X)) == id(X)

# Operadic composition
#---------------------

f, g, h = map([:f, :g, :h]) do sym
(i::Int) -> singleton_diagram(Box(Hom(Symbol("$sym$i"), A, A)))
end

# Identity
d = compose(f(1),f(2))
@test ocompose(g(1), 1, d) == d
@test ocompose(g(1), [d]) == d
@test ocompose(d, [f(1),f(2)]) == d
@test ocompose(d, 1, f(1)) == d
@test ocompose(d, 2, f(2)) == d

# Associativity
@test ocompose(compose(f(1),f(2)), [
ocompose(compose(g(1),g(2)), [compose(h(1),h(2)), compose(h(3),h(4))]),
ocompose(compose(g(3),g(4)), [compose(h(5),h(6)), compose(h(7),h(8))])
]) == ocompose(
ocompose(compose(f(1),f(2)), [compose(g(1),g(2)), compose(g(3),g(4))]),
[compose(h(1),h(2)), compose(h(3),h(4)), compose(h(5),h(6)), compose(h(7),h(8))]
)
@test ocompose(
ocompose(compose(f(1),f(2)), 1, compose(g(1),g(2))),
3, compose(g(3),g(4))
) == ocompose(
ocompose(compose(f(1),f(2)), 2, compose(g(3),g(4))),
1, compose(g(1),g(2))
)

end

0 comments on commit 803d6f4

Please sign in to comment.