circuits-diagrams
Safe HaskellNone
LanguageGHC2024

Circuit.Mermaid

Description

Mermaid printer for the string-diagram skeleton.

Test-only pin: emit an SDiagram as a mermaid flowchart so diagrams render anywhere markdown does (org/emacs, GitHub), before any chart-svg layout work. The printer consumes the hypergraph normal form (Circuit.Diagram.Hyper), so spiders and swaps vanish into port connectivity and structural laws show up as plain edges — a copy-then-merge round trip prints as a single wire.

Design notes:

  • One mermaid node per HyperNode; structural constructors print as symbols (, , λ, α, …).
  • Boundary ports print as stadium nodes in N / out N.
  • A wire class with several producers or consumers fans out into one mermaid edge per producer–consumer pair.
  • Edges carry port labels only when a node endpoint has arity greater than one.
  • STurn swaps node input/output arities, toggles a suffix on labels, and reverses boundary order; node identity is by label, as in the hypergraph normal form: two boxes with the same label collapse to one mermaid node.
Synopsis

Documentation

toMermaid :: SDiagram -> String Source #

Print a diagram skeleton as a mermaid flowchart.

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

Spiders are connectivity, so copy-then-merge is a single wire:

>>> putStr (toMermaid (SThenD sCopy sMerge))
flowchart LR
  in0(["in 0"])
  out0(["out 0"])
  in0 --> out0

A cup is a two-input symbol node with labelled ports:

>>> putStr (toMermaid SBend)
flowchart LR
  in0(["in 0"])
  in1(["in 1"])
  n0["∪"]
  in0 -->|i0| n0
  in1 -->|i1| n0

A trace hides the last input/output pair as a feedback loop:

>>> putStr (toMermaid (STrace (SBox "f" 2 2)))
flowchart LR
  in0(["in 0"])
  out0(["out 0"])
  n0["f"]
  n0 -->|o1:i1| n0
  in0 -->|i0| n0
  n0 -->|o0| out0