circuits-diagrams
Safe HaskellNone
LanguageGHC2024

Circuit.Diagram.Hyper

Description

Hypergraph normal form for the drawing-level hyper fragment of SDiagram (SWire, SSpider, multi-port SBox, SBeside, SThenD, SSwap).

A diagram is interpreted as port connectivity: every node port and every boundary port is a port reference, and a union-find over those references quotients them into wire classes. Spiders contribute no node — they simply merge the classes of all their ports, so spider fusion (and hence the bialgebra and spider laws) is automatic, and diagrams that differ only in tree shape normalise to the same value. Constructors outside the hyper fragment (cups, caps, unitors, …) are treated as opaque nodes with their natural port arities.

This is not a full graph-isomorphism check: node ports are keyed by box label, so two boxes carrying the same label are interchangeable. That is exact for the oracle suite and cheap; revisit if unlabelled node isomorphism ever matters.

Synopsis

Documentation

data HyperGraph Source #

A diagram as port connectivity: boundary arities, a sorted multiset of nodes, and the wires (equivalence classes of ports).

Constructors

HyperGraph 

Fields

Instances

Instances details
Eq HyperGraph Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Show HyperGraph Source # 
Instance details

Defined in Circuit.Diagram.Hyper

data HyperNode Source #

A box (or opaque constructor) with its port arities.

Constructors

HyperNode 

Instances

Instances details
Eq HyperNode Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Ord HyperNode Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Show HyperNode Source # 
Instance details

Defined in Circuit.Diagram.Hyper

data Wire Source #

One wire class: the boundary ends and node ports it connects, each sorted.

Constructors

Wire 

Fields

Instances

Instances details
Eq Wire Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Methods

(==) :: Wire -> Wire -> Bool #

(/=) :: Wire -> Wire -> Bool #

Ord Wire Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Methods

compare :: Wire -> Wire -> Ordering #

(<) :: Wire -> Wire -> Bool #

(<=) :: Wire -> Wire -> Bool #

(>) :: Wire -> Wire -> Bool #

(>=) :: Wire -> Wire -> Bool #

max :: Wire -> Wire -> Wire #

min :: Wire -> Wire -> Wire #

Show Wire Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Methods

showsPrec :: Int -> Wire -> ShowS #

show :: Wire -> String #

showList :: [Wire] -> ShowS #

data BoundaryEnd Source #

A diagram boundary port.

Constructors

InB Int

Input port (left boundary) at the given index.

OutB Int

Output port (right boundary) at the given index.

data PortEnd Source #

A node port: box label, direction and port index.

Constructors

PortEnd String PortDir Int 

Instances

Instances details
Eq PortEnd Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Methods

(==) :: PortEnd -> PortEnd -> Bool #

(/=) :: PortEnd -> PortEnd -> Bool #

Ord PortEnd Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Show PortEnd Source # 
Instance details

Defined in Circuit.Diagram.Hyper

data PortDir Source #

Whether a node port is an input or an output.

Constructors

In 
Out 

Instances

Instances details
Eq PortDir Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Methods

(==) :: PortDir -> PortDir -> Bool #

(/=) :: PortDir -> PortDir -> Bool #

Ord PortDir Source # 
Instance details

Defined in Circuit.Diagram.Hyper

Show PortDir Source # 
Instance details

Defined in Circuit.Diagram.Hyper

normalise :: SDiagram -> HyperGraph Source #

Interpret a diagram as its hypergraph normal form.

Assumes composable diagrams (SThenD zips the inner ports and drops any excess): the drawing syntax is untyped, so ill-formed composites degrade to dangling ports rather than an error.

STurn is involutive and reverses composition order:

>>> hyperEquiv (STurn (STurn (SBox "f" 1 1))) (SBox "f" 1 1)
True
>>> hyperEquiv (STurn (SThenD (SBox "f" 1 1) (SBox "g" 1 1))) (SThenD (STurn (SBox "g" 1 1)) (STurn (SBox "f" 1 1)))
True
>>> putStr (toMermaid (STurn (SBox "f" 1 1)))
flowchart LR
  in0(["in 0"])
  out0(["out 0"])
  n0["f†"]
  in0 --> n0
  n0 --> out0

hyperEquiv :: SDiagram -> SDiagram -> Bool Source #

Structural equality of diagrams up to hypergraph connectivity.

arity :: SDiagram -> (Int, Int) Source #

Boundary port counts of a diagram: (inputs, outputs). The unit of a unitor carries no wire, so unitors are 1 -> 1.