pub fn ladder_grammar(width: u32) -> GraphGrammar<u32, &'static str, ()>
Expand description

Generates a graph grammar for a string with width width, also called ladder. If width = 1 it reduces to a string grammar. It has two productions:

  • ‘extend’ extends the lattice by 1 row of nodes.
  • ‘finalize’ replaces the nonterminal node by a row of terminals and therefore prohibits further expansion and makes the graph valid w.r.t. the graph grammar.
g0: a --- a\
    |     | A
    a --- a/
     
extend:    a \        a        a --- a \
           |  A  <--  |  -->   |     |  A
           a /        a        a --- a /
         
finalize:  a \        a        a --- a
           |  A  <--  |  -->   |     |
           a /        a        a --- a

GG = ( g0, {'extend', 'finalize'}, {'a'})

Examples

Start by cloning the starting graph of the graph grammar:

let ladder = graphlang::predefined::ladder_grammar(2);
let mut g = ladder.start_graph.clone();

Then the productions can be applied. Note that this specific order of applications of productions is carefully choosen and they can fail, returning None. If a random graph should be generated refer to generate_random.

ladder.productions["extend"].apply_inplace(&mut g)?;
ladder.productions["extend"].apply_inplace(&mut g)?;
ladder.productions["finalize"].apply_inplace(&mut g)?;
assert!(ladder.is_valid(&g));