# Using Formal Grammars as Musical Genome
## 2 Melodies and Genomics
### Representational Models
#### 2.1.2. Tonal Music
This sub-section allows the possibility to define rules for the inclusion of roles; different types of dependencies between them; the compositional units where they are forced, allowed or prohibited to appear in; and general evolution of the presence of instruments. There are a few new operators to improve harmony management, some of them with a more complex interpretation process and dependencies with other operators and global parameters, like the operator \(M\) to create chords (see development example below). Since we enforce a strict hierarchical structure, it is useful to distinguish the symbols that represent compositional elements that will be rewritten during the development from those that have the same specific meaning at any given moment. For this reason we use an encoding based on deterministic context-free grammar, defined as \((V,\Sigma,S,P)\), where \(V\) is the set of non-terminal symbols, \(\Sigma\) is the set of terminal symbols (the alphabet), \(S\in V\) is the axiom or starting symbol, \(P\subseteq V\times\{V\cup\Sigma\}^{*}\) is the set of production rules. The symbols in the set \(V\) can be identified with the structural units: composition, periods, phrases and ideas, while the symbols in \(\Sigma\) represent notes and music operators, such as modulators of pitch, duration, current harmonic root or current chord. In our implementation, on the right hand side of the production rules, there are only terminal symbols or non-terminal symbols from the following level in the hierarchy in a decreasing order. To illustrate the rewriting process, let us introduce some of the reserved terminal symbols and their interpretation:  
\(N\) increases the counters pitch and harmonic root in one unit. \(n\) decreases the counters pitch and harmonic root in one unit. \([\) saves in a stack the current value of pitch, harmonic root and duration. \(]\) restores from the stack the last value of pitch, harmonic root and duration. \(<\) saves in a stack the current time position, value of pitch, harmonic root and duration. \(>\) restores from the stack the last saved time position, value of pitch, harmonic root and duration. \(W4.0\) applies the macro-dynamic mezzo-forte. \(M0.0.0.0\) makes the next symbol linked to an instrument to play the root note of the current chord, instead of the current pitch. Let us define \(Gt\), a simple and handmade grammar to illustrate the development using this new model:\[ \begin{array}{l}Gt=(V,\Sigma,S,P)\\ V=\{Z,A,B,C,D,E,F\}\\ \Sigma=\{N,n,[,],<,>,W4.0,a,b,s,M0.0.0.0\}\\ S=\{Z\}\\ \end{array} \]  
And \(P\) consisting of the following rules:  
\[ \begin{array}{l}P=\{\\ Z\to W4.0[ANNNNNNNNA]B\\ A\to CC\\ B\to D\\ C\to ENEnE\\ D\to FFNF\\ E\to<anaNNsa>M0.0.0.0 bM0.0.0.0 bsM0.0.0.0b\\ F\to as[NNNa]a\\ \}\\ \end{array} \]  
Iteration 0 (composition)  
String: \(Z\)  
The axiom \(Z\) represents the highest structural level, the composition. Iteration 1 (periods)  
String: \(W4.0[ANNNNNNA]B\)  
The composition develops into two identical musical units (periods), represented by the symbol \(A\), separated by seven steps in the pitch dimension and then followed by the period \(B\). Iteration 2 (phrases)  
String: \(W4.0[CCNNNNNNNC]D\)  
The two types of periods \(A\) and \(B\) would develop into two simple sequences of phrases, \(CC\) and \(D\) respectively. Iteration 3 (musical ideas)  
String: \(W4.0[ENEnEENENENNNNNNN