macro librarylibs/Graphs/src/Graphs.xtlm

Graphs: its macro library -- g_raph<, a graph written by its names. Imported with the functions, under one alias: "g:" u_se< "Graphs". A macro is a function from the source text written left and right of its call to the source that replaces the call, run before the program is compiled (X_eTaL MC10). Names with m: are macros; those under h: are private to this file.

source · imports t: libs/Strings/src/Strings.xtl

lowers : Char

value (private) · line 10
lowers ← "abcdefghijklmnopqrstuvwxyz"
Used in: letters, ʰn̲ame?

letters : Char

value (private) · line 11
letters ← lowers c̲at ⎕A c̲at ⎕D
Used in: ʰn̲ame?

Parsing

ʰr̲aze : Box a -> a

function (private) · line 16

The lists of a list joined into one list.

ʰr̲aze ← { b → d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ b }
Used in: ᵐg̲raph<

ʰn̲ame? : Truthy a => Char -> a

function (private) · line 20

Whether a text can be a variable's name: a lowercase letter, then letters and digits.

ʰn̲ame? ← { n → (0 < t̲ally n) ∧ ((f̲irst n) m̲ember? lowers) ∧ '∧ r̲/ n m̲ember? letters }
Used in: ᵐg̲raph<

ʰp̲aths : Char -> Box (Box Char)

function (private) · line 23

The paths of a graph text: a list per path, its stations in order.

ʰp̲aths ← { t → '{ p → '{ s → ᵗt̲rim d̲isclose s } m̲ap "-" ᵗs̲plit ᵗt̲rim d̲isclose p } m̲ap "," ᵗs̲plit t }
Used in: ᵐg̲raph<

The macro

ᵐg̲raph< : Char -> Char -> Char

macro · line 35

"town" g:g_raph< "airport-bridge-center, center-docks": the graph written by its names, paths of stations joined by "-", paths separated by ",". When the program is compiled it defines a variable per station, numbered in order of first appearance (airport := 1, bridge := 2, ...), and town as the adjacency matrix, every edge both ways, so the rest of the program speaks of stations by name (town g:l_evels center). It stands as a statement of its own. A name that cannot be a variable stops the compiler at it: error[bad-name].

ᵐg̲raph< ← { name t →
  ps ← ʰp̲aths t
  stations ← u̲nique ʰr̲aze ps
  n̲ot ʰn̲ame? name ? "bad-name left" ⎕R̲EJECT name c̲at " cannot be a variable's name: a lowercase letter, then letters and digits"
  bad ← (n̲ot '{ ʰn̲ame? d̲isclose ⍵ } e̲ach stations) r̲eplicate stations
  0 < t̲ally bad ? "bad-name right" ⎕R̲EJECT (d̲isclose f̲irst bad) c̲at " cannot be a station's name: a lowercase letter, then letters and digits"
  n ← t̲ally stations
  ⍝ each station's number, path by path; neighbors on a path are edges
  ks ← '{ p → stations i̲ndexOf d̲isclose p } m̲ap ps
  a ← ʰr̲aze '{ k → -1 d̲rop d̲isclose k } m̲ap ks
  b ← ʰr̲aze '{ k → 1 d̲rop d̲isclose k } m̲ap ks
  from ← a c̲at b                              ⍝ both ways
  to ← b c̲at a
  at ← (n × from − 1) + to
  m ← (r̲ange n × n) m̲ember? at
  defs ← "\n" ᵗj̲oin '{ i → (d̲isclose i s̲elect stations) c̲at " := " c̲at f̲ormat i } m̲ap r̲ange n
  defs c̲at "\n" c̲at name c̲at " := " c̲at (f̲ormat n) c̲at " " c̲at (f̲ormat n) c̲at " r_eshape " c̲at f̲ormat 0 + m
}
Used in: airport