discopy
discopy copied to clipboard
YAML parsing as DisCoPy types
See https://github.com/discopy/discopy/issues/211#issuecomment-1706237680.
The goal is to transform the YAML representation graph into DisCoPy definitions:
- https://yaml.org/spec/1.2.2/#321-representation-graph.
The initial goal is to support only the structure and not tags and other features.
Copying your code snippet and comment from the previous discussion:
def from_yaml(data: yaml.Node) -> Layer:
"""
Convert a Python object to a representation node.
"""
match data:
case ScalarNode(value=value):
return Hypergraph.id(Ty(value))
case SequenceNode(value=values):
h = Hypergraph.id()
for value in values:
h @= from_yaml(value)
return h
case MappingNode(value=key_values):
h = Hypergraph.id()
for key, value in key_values:
h @= from_yaml(key) @ from_yaml(value)
return h
I'm having some difficulties understand boxes. I'm looking at Cup and Cap but also at Eval. I'm reconsidering going back to simpler abstractions as well, such as the original suggestion of capturing signatures.
I'm not sure where the difficulty comes from, a box is just a triple of a name and a pair of types for domain and codomain. Cup, Cap, Eval, Swap, etc. are all special cases of boxes.
If you want a YAML syntax for string diagrams, you will need to define the syntax for boxes before you can start composing them together.
I think this makes much more sense :)
from discopy.closed import Ty, Box, Id
f = Ty('f')
x = Ty('x')
g = Ty('g')
y = Ty('y')
z = Ty('z')
first = Box("f: x\ng: y", f @ g, x @ y)
second = Box("x: z", x, z)
result = first >> (second @ Id(y))
result.draw()
I think I'm picking up speed with using this library so I hope to gain independence.
I'm working on two things:
- yaml to digraph: https://github.com/yaml-programming/nx_yaml
- digraph to signature:
- ob = digraph nodes
- ar = digraph nodes
- dom = predecessors
- cod = successors
I'm focusing on the first one to deliver value to a large community while I continue researching signatures. I hope the signature above will be very friendly to work with.
I'm also working towards QPL 2024! This is a really exciting and ambitious challenge but I hope I can give a tool session. I'm a local BCS and I have a unique opportunity to participate :)
- https://github.com/colltoaction/qpl2024