gcrsim 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. gcrsim-0.1.0/CHANGELOG.md +34 -0
  2. gcrsim-0.1.0/LICENSE +21 -0
  3. gcrsim-0.1.0/PKG-INFO +143 -0
  4. gcrsim-0.1.0/README.md +124 -0
  5. gcrsim-0.1.0/SPEC.md +93 -0
  6. gcrsim-0.1.0/docs/checking.md +38 -0
  7. gcrsim-0.1.0/docs/cli.md +278 -0
  8. gcrsim-0.1.0/docs/configuration.md +216 -0
  9. gcrsim-0.1.0/docs/grammar-types.md +124 -0
  10. gcrsim-0.1.0/docs/grammars.md +1356 -0
  11. gcrsim-0.1.0/docs/interactive.md +108 -0
  12. gcrsim-0.1.0/docs/marks-and-derivations.md +159 -0
  13. gcrsim-0.1.0/docs/migration.md +37 -0
  14. gcrsim-0.1.0/docs/model-syntax.md +196 -0
  15. gcrsim-0.1.0/docs/python.md +237 -0
  16. gcrsim-0.1.0/docs/semantics.md +72 -0
  17. gcrsim-0.1.0/examples/README.md +35 -0
  18. gcrsim-0.1.0/examples/cookbook/01_context_free.txt +14 -0
  19. gcrsim-0.1.0/examples/cookbook/01_monotone_anbncn.txt +24 -0
  20. gcrsim-0.1.0/examples/cookbook/01_type0_powers_of_two.txt +33 -0
  21. gcrsim-0.1.0/examples/cookbook/02_insdel.txt +14 -0
  22. gcrsim-0.1.0/examples/cookbook/03_marcus_internal.txt +13 -0
  23. gcrsim-0.1.0/examples/cookbook/04_marcus_external.txt +18 -0
  24. gcrsim-0.1.0/examples/cookbook/05_evolutionary_processors.txt +17 -0
  25. gcrsim-0.1.0/examples/cookbook/06_network_of_processors.txt +16 -0
  26. gcrsim-0.1.0/examples/cookbook/07_graph_controlled.txt +16 -0
  27. gcrsim-0.1.0/examples/cookbook/07_graph_controlled_appearance_checking.txt +43 -0
  28. gcrsim-0.1.0/examples/cookbook/08_matrix.txt +33 -0
  29. gcrsim-0.1.0/examples/cookbook/08_matrix_appearance_checking.txt +76 -0
  30. gcrsim-0.1.0/examples/cookbook/08_matrix_direct.txt +29 -0
  31. gcrsim-0.1.0/examples/cookbook/09_programmed.txt +21 -0
  32. gcrsim-0.1.0/examples/cookbook/10_time_varying.txt +19 -0
  33. gcrsim-0.1.0/examples/cookbook/11_regular_control.txt +20 -0
  34. gcrsim-0.1.0/examples/cookbook/12_random_context.txt +28 -0
  35. gcrsim-0.1.0/examples/cookbook/13_semi_conditional.txt +16 -0
  36. gcrsim-0.1.0/examples/cookbook/14_context_conditional.txt +12 -0
  37. gcrsim-0.1.0/examples/cookbook/15_ordered.txt +16 -0
  38. gcrsim-0.1.0/examples/cookbook/16_bounded_index_classes.txt +31 -0
  39. gcrsim-0.1.0/examples/cookbook/16_finite_index.txt +24 -0
  40. gcrsim-0.1.0/examples/cookbook/17_counter_machine.txt +22 -0
  41. gcrsim-0.1.0/examples/cookbook/18_scattered_context.txt +23 -0
  42. gcrsim-0.1.0/examples/cookbook/18_scattered_context_choices.txt +14 -0
  43. gcrsim-0.1.0/examples/cookbook/19_post_left_right.txt +25 -0
  44. gcrsim-0.1.0/examples/cookbook/19_post_tag_system.txt +22 -0
  45. gcrsim-0.1.0/examples/cookbook/20_valence.txt +30 -0
  46. gcrsim-0.1.0/examples/features/checking_an_bn.txt +17 -0
  47. gcrsim-0.1.0/examples/features/overlapping_symbols.txt +23 -0
  48. gcrsim-0.1.0/examples/features/symbols_and_classes.txt +17 -0
  49. gcrsim-0.1.0/examples/python/anbn_with_mistake.txt +16 -0
  50. gcrsim-0.1.0/examples/python/beam.py +12 -0
  51. gcrsim-0.1.0/examples/python/check_from_file.py +7 -0
  52. gcrsim-0.1.0/examples/python/check_infinite.py +12 -0
  53. gcrsim-0.1.0/examples/python/expected_anbn.txt +21 -0
  54. gcrsim-0.1.0/examples/python/mark_what_is_cut.py +11 -0
  55. gcrsim-0.1.0/examples/python/valence.py +7 -0
  56. gcrsim-0.1.0/examples/python/valence.txt +23 -0
  57. gcrsim-0.1.0/examples/python/valence_keep.py +10 -0
  58. gcrsim-0.1.0/examples/real/LR/LR.txt +79 -0
  59. gcrsim-0.1.0/examples/real/LR/LR_ab-c.txt +79 -0
  60. gcrsim-0.1.0/examples/real/LR/LR_erase.txt +67 -0
  61. gcrsim-0.1.0/examples/real/LR/LR_rot.txt +70 -0
  62. gcrsim-0.1.0/examples/real/ins/ins222.txt +196 -0
  63. gcrsim-0.1.0/examples/real/mat_insdel/mat_insdel_110110a.txt +94 -0
  64. gcrsim-0.1.0/examples/real/mat_insdel/mat_insdel_110200a.txt +33 -0
  65. gcrsim-0.1.0/examples/real/mat_insdel/mat_insdel_110200b.txt +72 -0
  66. gcrsim-0.1.0/examples/real/mat_insdel/mat_insdel_200110a.txt +48 -0
  67. gcrsim-0.1.0/examples/real/noncf110110/noncf110110.txt +165 -0
  68. gcrsim-0.1.0/examples/real/rcanbncn/rcanbncn.txt +27 -0
  69. gcrsim-0.1.0/gcrsim/__init__.py +40 -0
  70. gcrsim-0.1.0/gcrsim/__main__.py +7 -0
  71. gcrsim-0.1.0/gcrsim/_meta.py +6 -0
  72. gcrsim-0.1.0/gcrsim/_reference.py +155 -0
  73. gcrsim-0.1.0/gcrsim/cli.py +478 -0
  74. gcrsim-0.1.0/gcrsim/convert.py +606 -0
  75. gcrsim-0.1.0/gcrsim/engine.py +699 -0
  76. gcrsim-0.1.0/gcrsim/errors.py +61 -0
  77. gcrsim-0.1.0/gcrsim/filters.py +167 -0
  78. gcrsim-0.1.0/gcrsim/history.py +34 -0
  79. gcrsim-0.1.0/gcrsim/lexicon.py +445 -0
  80. gcrsim-0.1.0/gcrsim/listing.py +93 -0
  81. gcrsim-0.1.0/gcrsim/model.py +439 -0
  82. gcrsim-0.1.0/gcrsim/output.py +124 -0
  83. gcrsim-0.1.0/gcrsim/pattern.py +153 -0
  84. gcrsim-0.1.0/gcrsim/retention.py +199 -0
  85. gcrsim-0.1.0/gcrsim/rules.py +523 -0
  86. gcrsim-0.1.0/gcrsim/sections.py +90 -0
  87. gcrsim-0.1.0/gcrsim/settings.py +285 -0
  88. gcrsim-0.1.0/gcrsim/stepper.py +175 -0
  89. gcrsim-0.1.0/gcrsim/verify.py +108 -0
  90. gcrsim-0.1.0/pyproject.toml +49 -0
  91. gcrsim-0.1.0/tests/conftest.py +276 -0
  92. gcrsim-0.1.0/tests/fixtures/examples/an_bn_2comp.txt +11 -0
  93. gcrsim-0.1.0/tests/fixtures/examples/an_bn_check.txt +15 -0
  94. gcrsim-0.1.0/tests/fixtures/examples/an_bn_check_conf.py +16 -0
  95. gcrsim-0.1.0/tests/fixtures/examples/an_bn_marks.txt +11 -0
  96. gcrsim-0.1.0/tests/fixtures/examples/an_bn_marks_conf.py +12 -0
  97. gcrsim-0.1.0/tests/fixtures/examples/ins_222.txt +121 -0
  98. gcrsim-0.1.0/tests/fixtures/examples/ins_222_conf.py +26 -0
  99. gcrsim-0.1.0/tests/fixtures/examples/ins_222_declarative.py +14 -0
  100. gcrsim-0.1.0/tests/fixtures/examples/ins_a_110200.txt +41 -0
  101. gcrsim-0.1.0/tests/fixtures/examples/ins_a_110200_conf.py +25 -0
  102. gcrsim-0.1.0/tests/fixtures/examples/ins_a_110200_declarative.py +15 -0
  103. gcrsim-0.1.0/tests/fixtures/examples/ins_b_110200.txt +26 -0
  104. gcrsim-0.1.0/tests/fixtures/examples/ins_b_110200_conf.py +26 -0
  105. gcrsim-0.1.0/tests/fixtures/examples/ins_b_110200_declarative.py +14 -0
  106. gcrsim-0.1.0/tests/fixtures/examples/ins_b_110200_declarative.toml +16 -0
  107. gcrsim-0.1.0/tests/fixtures/examples/ins_c_211110.txt +40 -0
  108. gcrsim-0.1.0/tests/fixtures/examples/ins_c_211110_conf.py +27 -0
  109. gcrsim-0.1.0/tests/fixtures/examples/ins_c_211110_declarative.py +12 -0
  110. gcrsim-0.1.0/tests/fixtures/examples/rc_anbncn.txt +23 -0
  111. gcrsim-0.1.0/tests/fixtures/perl/gcrsim.pl +360 -0
  112. gcrsim-0.1.0/tests/fixtures/perl/ins_222.cfg +48 -0
  113. gcrsim-0.1.0/tests/fixtures/perl/ins_222.txt +134 -0
  114. gcrsim-0.1.0/tests/fixtures/perl/ins_a_110200.cfg +50 -0
  115. gcrsim-0.1.0/tests/fixtures/perl/ins_a_110200.txt +46 -0
  116. gcrsim-0.1.0/tests/fixtures/perl/ins_b_110200.cfg +49 -0
  117. gcrsim-0.1.0/tests/fixtures/perl/ins_b_110200.txt +30 -0
  118. gcrsim-0.1.0/tests/fixtures/perl/ins_c_211110.cfg +49 -0
  119. gcrsim-0.1.0/tests/fixtures/perl/ins_c_211110.txt +45 -0
  120. gcrsim-0.1.0/tests/test_adoptions.py +246 -0
  121. gcrsim-0.1.0/tests/test_cli.py +568 -0
  122. gcrsim-0.1.0/tests/test_context_symbols.py +396 -0
  123. gcrsim-0.1.0/tests/test_convert_cli.py +122 -0
  124. gcrsim-0.1.0/tests/test_cookbook.py +334 -0
  125. gcrsim-0.1.0/tests/test_doc_links.py +45 -0
  126. gcrsim-0.1.0/tests/test_docs_blocks.py +42 -0
  127. gcrsim-0.1.0/tests/test_engine.py +515 -0
  128. gcrsim-0.1.0/tests/test_layers.py +178 -0
  129. gcrsim-0.1.0/tests/test_lexicon.py +337 -0
  130. gcrsim-0.1.0/tests/test_marks.py +238 -0
  131. gcrsim-0.1.0/tests/test_overlap.py +73 -0
  132. gcrsim-0.1.0/tests/test_packaging.py +71 -0
  133. gcrsim-0.1.0/tests/test_parser.py +441 -0
  134. gcrsim-0.1.0/tests/test_pattern.py +96 -0
  135. gcrsim-0.1.0/tests/test_perl_models.py +175 -0
  136. gcrsim-0.1.0/tests/test_python_examples.py +89 -0
  137. gcrsim-0.1.0/tests/test_real_examples.py +92 -0
  138. gcrsim-0.1.0/tests/test_reference.py +204 -0
  139. gcrsim-0.1.0/tests/test_reference_examples.py +106 -0
  140. gcrsim-0.1.0/tests/test_reference_surface.py +127 -0
  141. gcrsim-0.1.0/tests/test_review2.py +276 -0
  142. gcrsim-0.1.0/tests/test_review3.py +230 -0
  143. gcrsim-0.1.0/tests/test_review4.py +425 -0
  144. gcrsim-0.1.0/tests/test_review_fixes.py +421 -0
  145. gcrsim-0.1.0/tests/test_rules.py +127 -0
  146. gcrsim-0.1.0/tests/test_toml_config.py +66 -0
  147. gcrsim-0.1.0/tests/test_trace.py +282 -0
  148. gcrsim-0.1.0/tests/test_unicode.py +124 -0
  149. gcrsim-0.1.0/tests/test_verification.py +174 -0
  150. gcrsim-0.1.0/tests/test_verify.py +58 -0
@@ -0,0 +1,34 @@
1
+ # Changelog
2
+
3
+ All notable changes of each released version are listed here, newest first. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the versions follow [Semantic Versioning](https://semver.org/).
5
+
6
+ ## [0.1.0] - 2026-11-04
7
+
8
+ First release of the Python version of gcrsim. It continues the [Perl simulator](https://github.com/sverlan/gcrsim-perl) with a
9
+ new, declarative model notation; models of the Perl version can be converted with `gcrsim-convert`
10
+ ([docs/migration.md](docs/migration.md)).
11
+
12
+ ### Added
13
+
14
+ * **The simulator.** Graph-controlled string rewriting: numbered components that hold strings, rules that rewrite a string and send
15
+ the result to a component, insertion-deletion systems (`ins`, `del`, `sub`), plain rules `x->y`, regular-expression rules
16
+ `re(...)`, and `send()`; conditions on a rule (`;permit:`, `;forbid:`, `;permit-any:`), classes of symbols, multi-character symbols,
17
+ and the alphabet. The meaning of a step is in [docs/semantics.md](docs/semantics.md), and the exact specification of the plain
18
+ fragment in [SPEC.md](SPEC.md).
19
+ * **The model file.** A TOML document with the rules in their own blocks: `[symbols]`, `[model]`, `[run]`, `[axioms]`, `[rules]`,
20
+ `[rules.N]` ([docs/model-syntax.md](docs/model-syntax.md), [docs/configuration.md](docs/configuration.md)).
21
+ * **Filters and marks.** Per-component filters (`forbid`, `require`, `max_count` with symbols, classes and patterns), marks with
22
+ expected strings or expected patterns, and derivations of marked strings
23
+ ([docs/marks-and-derivations.md](docs/marks-and-derivations.md)).
24
+ * **Python.** A Python file (`--python-filter`) with a filter function `keep`, functions in the filters and marks of the components,
25
+ and `pure(...)` for the functions that `--streaming` can use; the importable package `gcrsim`
26
+ ([docs/python.md](docs/python.md)).
27
+ * **The command line.** Output formats `text`, `perl`, `csv` and `jsonl`, a step-by-step mode (`-i`), `--progress`, `--streaming`,
28
+ limits on the size of a step, retention of the steps in memory or in a file, and `--cross-check`, which checks the plain
29
+ fragment against a reference interpreter ([docs/cli.md](docs/cli.md), [docs/interactive.md](docs/interactive.md),
30
+ [docs/checking.md](docs/checking.md)).
31
+ * **Tools.** `gcrsim-convert` (models of the Perl version to the new notation) and `gcrsim-retention` (summaries of kept steps).
32
+ * **Examples.** A cookbook with one small model for each kind of grammar, examples of single features, models with a Python file,
33
+ and the research models in [`examples/`](examples/) ([docs/grammars.md](docs/grammars.md),
34
+ [docs/grammar-types.md](docs/grammar-types.md)).
gcrsim-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2026 Sergey Verlan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
gcrsim-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.4
2
+ Name: gcrsim
3
+ Version: 0.1.0
4
+ Summary: Graph-controlled string rewriting simulator (insertion-deletion systems and other regulated rewriting)
5
+ Keywords: rewriting,formal languages,insertion-deletion,graph-controlled grammars,simulator
6
+ Author: Sergey Verlan
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Topic :: Scientific/Engineering
15
+ Classifier: Intended Audience :: Science/Research
16
+ Requires-Python: >=3.11
17
+ Project-URL: Perl version, https://github.com/sverlan/gcrsim-perl
18
+ Description-Content-Type: text/markdown
19
+
20
+ # gcrsim
21
+
22
+ Graph Controlled string Rewriting SIMulator. It simulates the derivation of regulated rewriting systems, in particular
23
+ insertion-deletion systems with graph control: components (states) hold sets of strings, rules rewrite a string in one component
24
+ and send the result to another one, and the simulator advances all components synchronously, step by step.
25
+
26
+ **Main features**
27
+
28
+ * **Simulation of many variants of regulated rewriting and of insertion-deletion systems.** Graph-controlled, matrix, programmed,
29
+ time-varying, random-context and ordered grammars, Marcus contextual grammars, evolutionary processors, networks of processors and
30
+ insertion-deletion systems are all written with the same few rule forms (see [docs/grammar-types.md](docs/grammar-types.md)).
31
+ * **Catching wrong derivations and the stage where they happen.** *Marks* flag the strings you care about, for example every terminal
32
+ string that is not in the language you intended, at the step and component where they appear. The *traceback* then prints the full
33
+ derivation, rule by rule from the axiom, of any marked string or of any string you name (see
34
+ [docs/marks-and-derivations.md](docs/marks-and-derivations.md)).
35
+
36
+ gcrsim is a complete rewrite, in Python, of the earlier Perl simulator of the same name, and its evolution: the model language is
37
+ new, the semantics are written down and checked against an independent reference interpreter, and it adds marks, derivation
38
+ sequences and resource limits. The Perl version is kept at [github.com/sverlan/gcrsim-perl](https://github.com/sverlan/gcrsim-perl);
39
+ models written for it are migrated with `gcrsim-convert`, see [docs/migration.md](docs/migration.md).
40
+
41
+ * a small package, standard library only, Python 3.11+;
42
+ * rule forms `ins`, `del`, `sub`, plain rules, `send()` and regular-expression rules, permitting / forbidding words, multi-character
43
+ symbols and symbol classes, UTF-8;
44
+ * for the part of the language without regular expressions, a written specification, an independent reference interpreter,
45
+ differential tests and `--cross-check` (see [docs/checking.md](docs/checking.md));
46
+ * for long experiments: declarative component conditions, resource limits, progress reporting, `csv` and `jsonl` output.
47
+
48
+ ## Installation
49
+
50
+ Python 3.11 or later; there are no other dependencies. The package installs three programs: `gcrsim` (the simulator),
51
+ `gcrsim-convert` (migration of Perl-era models) and `gcrsim-retention` (look into a retention file).
52
+
53
+ ```
54
+ pip install gcrsim # into the current environment
55
+ pipx install gcrsim # as an isolated command-line tool
56
+ uv tool install gcrsim # the same, with uv
57
+ uvx gcrsim model.txt 10 # run the simulator once, without installing
58
+ ```
59
+
60
+ ## Running
61
+
62
+ ```
63
+ gcrsim model.txt [steps]
64
+ python -m gcrsim model.txt [steps] # the same, from a checkout of the repository without installing
65
+ ```
66
+
67
+ With `steps` (on the command line or in the configuration) the simulator runs that many steps and exits; with `-i` it waits for a command after each step (Enter for the next step, `q` to quit, more commands in [docs/interactive.md](docs/interactive.md)).
68
+
69
+ ## A first model
70
+
71
+ A matrix grammar for aⁿbⁿcⁿ, a language that needs several components. A matrix is a chain of rules applied one after the other, so
72
+ each rule gets its own component; the first rules of all the matrices are in component 1, and the last rule of `m2` returns to it:
73
+
74
+ ```
75
+ # matrix m1 = (S -> ABC), m2 = (A -> aA, B -> bB, C -> cC), m3 = (A -> a, B -> b, C -> c)
76
+ [axioms]
77
+ 1 = ["S"]
78
+
79
+ [rules.1]
80
+ # m1 (one rule), and the first rules of m2 and m3
81
+ S -> A B C, 1
82
+ A -> a A, 2
83
+ A -> a, 4
84
+ # m2: B -> bB in component 2, C -> cC in component 3, then back to component 1
85
+ [rules.2]
86
+ B -> b B, 3
87
+ [rules.3]
88
+ C -> c C, 1
89
+ # m3: B -> b in component 4, C -> c in component 5, and the word is in component 6
90
+ [rules.4]
91
+ B -> b, 5
92
+ [rules.5]
93
+ C -> c, 6
94
+ ```
95
+
96
+ The model is the file [`examples/cookbook/08_matrix_direct.txt`](examples/cookbook/08_matrix_direct.txt); `gcrsim examples/cookbook/08_matrix_direct.txt 12` prints, at every step, the strings of each component; `abc`, `aabbcc`, ... appear
97
+ in component 6.
98
+
99
+ ## Documentation
100
+
101
+ | Document | Content |
102
+ | --- | --- |
103
+ | [docs/model-syntax.md](docs/model-syntax.md) | the model language: sections, rule forms, conditions, symbols and classes |
104
+ | [docs/semantics.md](docs/semantics.md) | what a step does, in plain words (exact version: [SPEC.md](SPEC.md)) |
105
+ | [docs/grammars.md](docs/grammars.md) | cookbook: one example for each kind of grammar, in classical notation and as a model |
106
+ | [docs/grammar-types.md](docs/grammar-types.md) | the types of grammars and systems that are supported, and how each is written |
107
+ | [docs/configuration.md](docs/configuration.md) | settings, configuration files, the filters of the components |
108
+ | [docs/python.md](docs/python.md) | the Python interface: the filter file, callbacks, marks from Python, the package API |
109
+ | [docs/marks-and-derivations.md](docs/marks-and-derivations.md) | marking strings and getting the derivation sequence of a string |
110
+ | [docs/cli.md](docs/cli.md) | command line options, output, limits, exit statuses |
111
+ | [docs/interactive.md](docs/interactive.md) | the step-by-step mode (`-i`): looking at the strings, searching, tracing, marking |
112
+ | [docs/checking.md](docs/checking.md) | how the results are checked, `--cross-check` |
113
+ | [docs/migration.md](docs/migration.md) | moving from the Perl simulator, `gcrsim-convert` |
114
+ | [examples/](examples/README.md) | the cookbook models and the research models |
115
+
116
+ ## Supported grammars
117
+
118
+ A summary; the full table, with one sentence on how each grammar is written, is in [docs/grammar-types.md](docs/grammar-types.md), and
119
+ [docs/grammars.md](docs/grammars.md) gives an example of each in the classical notation and as a model.
120
+
121
+ | Grammars and systems | How it is written |
122
+ | --- | --- |
123
+ | unrestricted, monotone, context-free and pure grammars; internal Marcus contextual grammars; insertion-deletion systems; evolutionary processors; graph-controlled grammars; random-context and semi-conditional grammars; grammars with derivations of bounded index | directly, with the rule forms |
124
+ | external Marcus contextual grammars; networks of evolutionary processors; matrix grammars and matrix insertion-deletion systems; appearance checking (failure fields) in graph-controlled and matrix grammars; programmed grammars; time-varying grammars; regular control; ordered grammars; counter and register machines | with a small encoding (components and `send()`) |
125
+ | scattered-context grammars | in part, with `re(...)`: exact when each symbol occurs once in the word |
126
+ | valence grammars | with a Python filter |
127
+ | parallel rewriting (L systems, ET0L), splicing and communicating grammar systems, leftmost and rightmost derivations, control by a pushdown automaton or a Petri net | not supported |
128
+
129
+ ## How it is developed
130
+
131
+ The development is AI-assisted, and the work is shared between the human author and the AI. The plan is discussed and validated by
132
+ the human author. The AI implements the code and writes the tests; the human rereads the code. The examples are written by the AI
133
+ and the human together, and the results are verified by the human. Independent AI reviews of the plan and of the code were used as
134
+ an additional check; they do not replace the human steps. What the automatic checks do and do not guarantee is described in
135
+ [docs/checking.md](docs/checking.md).
136
+
137
+ ## Development
138
+
139
+ ```
140
+ uv run pytest # fast tests, plus the Perl comparisons when perl is on PATH
141
+ ```
142
+
143
+ The Perl comparison tests use the original models and configurations in `tests/fixtures/perl`.
gcrsim-0.1.0/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # gcrsim
2
+
3
+ Graph Controlled string Rewriting SIMulator. It simulates the derivation of regulated rewriting systems, in particular
4
+ insertion-deletion systems with graph control: components (states) hold sets of strings, rules rewrite a string in one component
5
+ and send the result to another one, and the simulator advances all components synchronously, step by step.
6
+
7
+ **Main features**
8
+
9
+ * **Simulation of many variants of regulated rewriting and of insertion-deletion systems.** Graph-controlled, matrix, programmed,
10
+ time-varying, random-context and ordered grammars, Marcus contextual grammars, evolutionary processors, networks of processors and
11
+ insertion-deletion systems are all written with the same few rule forms (see [docs/grammar-types.md](docs/grammar-types.md)).
12
+ * **Catching wrong derivations and the stage where they happen.** *Marks* flag the strings you care about, for example every terminal
13
+ string that is not in the language you intended, at the step and component where they appear. The *traceback* then prints the full
14
+ derivation, rule by rule from the axiom, of any marked string or of any string you name (see
15
+ [docs/marks-and-derivations.md](docs/marks-and-derivations.md)).
16
+
17
+ gcrsim is a complete rewrite, in Python, of the earlier Perl simulator of the same name, and its evolution: the model language is
18
+ new, the semantics are written down and checked against an independent reference interpreter, and it adds marks, derivation
19
+ sequences and resource limits. The Perl version is kept at [github.com/sverlan/gcrsim-perl](https://github.com/sverlan/gcrsim-perl);
20
+ models written for it are migrated with `gcrsim-convert`, see [docs/migration.md](docs/migration.md).
21
+
22
+ * a small package, standard library only, Python 3.11+;
23
+ * rule forms `ins`, `del`, `sub`, plain rules, `send()` and regular-expression rules, permitting / forbidding words, multi-character
24
+ symbols and symbol classes, UTF-8;
25
+ * for the part of the language without regular expressions, a written specification, an independent reference interpreter,
26
+ differential tests and `--cross-check` (see [docs/checking.md](docs/checking.md));
27
+ * for long experiments: declarative component conditions, resource limits, progress reporting, `csv` and `jsonl` output.
28
+
29
+ ## Installation
30
+
31
+ Python 3.11 or later; there are no other dependencies. The package installs three programs: `gcrsim` (the simulator),
32
+ `gcrsim-convert` (migration of Perl-era models) and `gcrsim-retention` (look into a retention file).
33
+
34
+ ```
35
+ pip install gcrsim # into the current environment
36
+ pipx install gcrsim # as an isolated command-line tool
37
+ uv tool install gcrsim # the same, with uv
38
+ uvx gcrsim model.txt 10 # run the simulator once, without installing
39
+ ```
40
+
41
+ ## Running
42
+
43
+ ```
44
+ gcrsim model.txt [steps]
45
+ python -m gcrsim model.txt [steps] # the same, from a checkout of the repository without installing
46
+ ```
47
+
48
+ With `steps` (on the command line or in the configuration) the simulator runs that many steps and exits; with `-i` it waits for a command after each step (Enter for the next step, `q` to quit, more commands in [docs/interactive.md](docs/interactive.md)).
49
+
50
+ ## A first model
51
+
52
+ A matrix grammar for aⁿbⁿcⁿ, a language that needs several components. A matrix is a chain of rules applied one after the other, so
53
+ each rule gets its own component; the first rules of all the matrices are in component 1, and the last rule of `m2` returns to it:
54
+
55
+ ```
56
+ # matrix m1 = (S -> ABC), m2 = (A -> aA, B -> bB, C -> cC), m3 = (A -> a, B -> b, C -> c)
57
+ [axioms]
58
+ 1 = ["S"]
59
+
60
+ [rules.1]
61
+ # m1 (one rule), and the first rules of m2 and m3
62
+ S -> A B C, 1
63
+ A -> a A, 2
64
+ A -> a, 4
65
+ # m2: B -> bB in component 2, C -> cC in component 3, then back to component 1
66
+ [rules.2]
67
+ B -> b B, 3
68
+ [rules.3]
69
+ C -> c C, 1
70
+ # m3: B -> b in component 4, C -> c in component 5, and the word is in component 6
71
+ [rules.4]
72
+ B -> b, 5
73
+ [rules.5]
74
+ C -> c, 6
75
+ ```
76
+
77
+ The model is the file [`examples/cookbook/08_matrix_direct.txt`](examples/cookbook/08_matrix_direct.txt); `gcrsim examples/cookbook/08_matrix_direct.txt 12` prints, at every step, the strings of each component; `abc`, `aabbcc`, ... appear
78
+ in component 6.
79
+
80
+ ## Documentation
81
+
82
+ | Document | Content |
83
+ | --- | --- |
84
+ | [docs/model-syntax.md](docs/model-syntax.md) | the model language: sections, rule forms, conditions, symbols and classes |
85
+ | [docs/semantics.md](docs/semantics.md) | what a step does, in plain words (exact version: [SPEC.md](SPEC.md)) |
86
+ | [docs/grammars.md](docs/grammars.md) | cookbook: one example for each kind of grammar, in classical notation and as a model |
87
+ | [docs/grammar-types.md](docs/grammar-types.md) | the types of grammars and systems that are supported, and how each is written |
88
+ | [docs/configuration.md](docs/configuration.md) | settings, configuration files, the filters of the components |
89
+ | [docs/python.md](docs/python.md) | the Python interface: the filter file, callbacks, marks from Python, the package API |
90
+ | [docs/marks-and-derivations.md](docs/marks-and-derivations.md) | marking strings and getting the derivation sequence of a string |
91
+ | [docs/cli.md](docs/cli.md) | command line options, output, limits, exit statuses |
92
+ | [docs/interactive.md](docs/interactive.md) | the step-by-step mode (`-i`): looking at the strings, searching, tracing, marking |
93
+ | [docs/checking.md](docs/checking.md) | how the results are checked, `--cross-check` |
94
+ | [docs/migration.md](docs/migration.md) | moving from the Perl simulator, `gcrsim-convert` |
95
+ | [examples/](examples/README.md) | the cookbook models and the research models |
96
+
97
+ ## Supported grammars
98
+
99
+ A summary; the full table, with one sentence on how each grammar is written, is in [docs/grammar-types.md](docs/grammar-types.md), and
100
+ [docs/grammars.md](docs/grammars.md) gives an example of each in the classical notation and as a model.
101
+
102
+ | Grammars and systems | How it is written |
103
+ | --- | --- |
104
+ | unrestricted, monotone, context-free and pure grammars; internal Marcus contextual grammars; insertion-deletion systems; evolutionary processors; graph-controlled grammars; random-context and semi-conditional grammars; grammars with derivations of bounded index | directly, with the rule forms |
105
+ | external Marcus contextual grammars; networks of evolutionary processors; matrix grammars and matrix insertion-deletion systems; appearance checking (failure fields) in graph-controlled and matrix grammars; programmed grammars; time-varying grammars; regular control; ordered grammars; counter and register machines | with a small encoding (components and `send()`) |
106
+ | scattered-context grammars | in part, with `re(...)`: exact when each symbol occurs once in the word |
107
+ | valence grammars | with a Python filter |
108
+ | parallel rewriting (L systems, ET0L), splicing and communicating grammar systems, leftmost and rightmost derivations, control by a pushdown automaton or a Petri net | not supported |
109
+
110
+ ## How it is developed
111
+
112
+ The development is AI-assisted, and the work is shared between the human author and the AI. The plan is discussed and validated by
113
+ the human author. The AI implements the code and writes the tests; the human rereads the code. The examples are written by the AI
114
+ and the human together, and the results are verified by the human. Independent AI reviews of the plan and of the code were used as
115
+ an additional check; they do not replace the human steps. What the automatic checks do and do not guarantee is described in
116
+ [docs/checking.md](docs/checking.md).
117
+
118
+ ## Development
119
+
120
+ ```
121
+ uv run pytest # fast tests, plus the Perl comparisons when perl is on PATH
122
+ ```
123
+
124
+ The Perl comparison tests use the original models and configurations in `tests/fixtures/perl`.
gcrsim-0.1.0/SPEC.md ADDED
@@ -0,0 +1,93 @@
1
+ # Semantics of the plain fragment
2
+
3
+ This is the precise specification of what gcrsim computes for the *plain fragment*: rules without
4
+ regular expressions (`plain`, `ins`, `del`, `sub`, `send`), `permit`/`forbid` words, components, the
5
+ history/duplicate filter and the length filter. It is written so that it can be implemented without
6
+ looking at the simulator. Anything not stated here is not part of the fragment.
7
+
8
+ ## 1. Objects
9
+
10
+ * A **symbol** is a non-empty Python `str` of any length (a class, being a `frozenset`, is therefore never a symbol). A **string** is a
11
+ finite sequence of symbols (a Python `tuple` of symbols). The empty string is `()`.
12
+ * A **component** is a number `1 .. n`, as in the models. A **content** is an ordered list of strings (duplicates possible
13
+ only in the axioms).
14
+ * An **item** is one of: a symbol `a` (matches exactly that symbol); a **class** (a `frozenset` of symbols,
15
+ matches one symbol that is a member); `ANY` (matches one arbitrary symbol); `START` and `END` (match the
16
+ empty sequence at position 0, respectively at position `len(s)`).
17
+ * A **rule** has a `source` component, a `target` component, `permit`, `permit_any` and `forbid` (each a tuple of
18
+ non-empty strings), and a **kind**:
19
+ * `sub` with four fields `u`, `x`, `v` (sequences of items) and `y` (a string of symbols): the items `x` are replaced by `y`;
20
+ * `send` (no further fields).
21
+ The model forms map to `sub`: a plain rule `x -> y` is the `sub` with `u=()`, `v=()` and the same `x` and `y`; `ins(u,y,v)` is
22
+ `x=()`; `del(u,x,v)` is `y=()`; `sub(u,x,y,v)` is itself.
23
+
24
+ ## 2. Edits of a rule on a string `s`
25
+
26
+ Let `n = len(s)`. A *match attempt at position i* (`0 <= i <= n`) matches the item sequence
27
+ `W = u ++ x ++ v` against `s` starting at `i`, item by item, left to right, with a cursor `pos` that starts at `i`:
28
+
29
+ * a symbol item matches if `pos < n` and `s[pos]` equals it; then `pos += 1`;
30
+ * a class item matches if `pos < n` and `s[pos]` is a member; then `pos += 1`;
31
+ * `ANY` matches if `pos < n`; then `pos += 1`;
32
+ * `START` matches if `pos == 0` (the cursor does not move); `END` matches if `pos == n`.
33
+
34
+ If every item matches, the attempt succeeds. Let `p` be the cursor after the items of `u`, and `q` the cursor
35
+ after the items of `x`. The attempt yields the **edit** `(p, q)` with replacement `y`; the **candidate** is
36
+ `s[:p] + y + s[q:]`. (Boundary items consume nothing, so `p` and `q` may be smaller than `i + len(u)`.)
37
+
38
+ `edits(rule, s)` is the list of the edits of all successful attempts, in increasing order of `i`. Properties:
39
+ overlapping occurrences all count; the same edit cannot come from two different `i`; there are at most `n + 1`
40
+ edits. The contexts `u` and `v` are not changed. A candidate is never searched again by the same rule.
41
+
42
+ For kind `send` there is exactly one edit, `(0, 0)` with the empty replacement; its candidate is `s` itself.
43
+
44
+ **Guards.** A rule is *enabled* on `s` if every word of `permit` occurs in `s` as a contiguous subsequence, at least one
45
+ word of `permit_any` occurs in `s` (when `permit_any` is not empty), and no word of `forbid` occurs in `s`. If it is not enabled, `edits` is empty. Words are plain symbol sequences
46
+ (no classes, no boundaries); an empty word is invalid input.
47
+
48
+ ## 3. One step
49
+
50
+ State: for every component `c` the **content** `F[c]` (list of strings), and, when **history** is on, a set
51
+ `H[c]` of the strings that have ever been *seen as a candidate* in `c`, including candidates later discarded by the length filter (initially the set of the axioms of `c`). Parameter: `max_len`
52
+ (an integer, or none for no limit).
53
+
54
+ 1. **Generation.** Create empty lists `C[1..n]` of *raw candidates*. For every component `c` in increasing order,
55
+ for every string `s` in `F[c]` in order, for every rule `r` with `r.source == c` in declaration order, append
56
+ the candidates of `edits(r, s)`, in order, to `C[r.target]`. (Nothing is removed during generation.)
57
+ 2. **Filtering.** For every target `t` in increasing order, with `seen = H[t]` if history is on and a fresh
58
+ empty set (per target, per step) otherwise: for every candidate `w` of `C[t]` in order:
59
+ * if `w` is in `seen`, discard it;
60
+ * otherwise add `w` to `seen`; then, if `max_len` is not none and `len(w) > max_len`, discard it (it stays in
61
+ `seen`); otherwise append `w` to the new content `N[t]`.
62
+ 3. The new contents `N[1..n]` replace `F`. The number of raw candidates of the step is `sum(len(C[t]))`.
63
+
64
+ `len` counts symbols. The first content `F` is the list of axioms of each component, in the given order, with
65
+ duplicates kept. A run of `k` steps repeats the step `k` times; the result of step `j` is the list of contents.
66
+
67
+ ## 4. Worked examples (acceptance tests)
68
+
69
+ Notation: strings are written as words of single-letter symbols; contents as lists.
70
+
71
+ 1. One component, rules `aa -> b`. `F = ["aaa"]`, history off: raw candidates `["ba", "ab"]`; new content `["ba", "ab"]`.
72
+ 2. `ins(a, X, b)` on `"abab"` gives edits at `p = 1` and `p = 3`: candidates `"aXbab"`, `"abaXb"`.
73
+ 3. `ins(, X, )` on `"ab"`: edits `p = 0, 1, 2`: `"Xab"`, `"aXb"`, `"abX"`.
74
+ 4. `del(a, b, )` on `"ab"` gives `"a"`; `del(, , )` (deleting the empty string) on `"ab"` gives `"ab"` three times.
75
+ 5. `sub(a, {any}, X, b)` on `"abab"`: attempts at `i = 0..`: `W = a, ANY, b`; success at `i = 0`? `s[0]='a'`,
76
+ `s[1]='b'` is ANY, `s[2]='a'` is not `b`: fails; no match at `i = 2` either. With `"aab"`: success at `i = 0` gives edit `(1, 2)` and candidate `"aXb"`.
77
+ 6. `ins({start}, X, a)` on `"aa"`: only `i = 0` succeeds (START needs `pos == 0`): candidate `"Xaa"`.
78
+ 7. `ins(a, X, {end})` on `"aba"`: `W = a, END`: success at `i = 2` (`a` at position 2, then pos 3 == n): candidate `"abaX"`.
79
+ 8. `send()` with `permit: ((a,),)` on `"ab"` gives `"ab"`; on `"b"` gives nothing.
80
+ 9. History: one component, rule `a -> a`, axiom `"a"`, history on, step 1: raw candidate `"a"`, already in `H`, so the new content is `[]`. With history off: new content `["a"]`.
81
+ 10. Length filter: `max_len = 1`, rule `a -> aa`, axiom `"a"`: raw `"aa"` is marked seen but discarded; new content `[]`; if a later step produces `"aa"` again (history on) it is discarded as a duplicate.
82
+
83
+ ## 5. What the tests compare
84
+
85
+ For a model given as data (axioms, rules, classes, `history`, `max_len`) and `k` steps, an implementation must
86
+ produce, for each step, the list of contents (lists of tuples of symbols, in order) and the raw candidate
87
+ count. Order matters.
88
+
89
+ ## 6. Out of scope of this specification
90
+
91
+ How model *text* becomes symbol sequences (lexemes, the alphabet, longest-match tokenisation, the internal
92
+ private-use characters) is part of the model language, not of this fragment: the specification starts from
93
+ sequences of symbols. The engine's tokenisation is tested separately (`tests/test_lexicon.py`).
@@ -0,0 +1,38 @@
1
+ # How the results are checked
2
+
3
+ The *plain fragment* is the part of the language without regular expressions: plain rules, `ins`/`del`/`sub`, `send()`,
4
+ `permit`/`forbid`/`permit-any` words, components, the history and the length filter. It has a precise specification, [SPEC.md](../SPEC.md).
5
+ The specification starts from sequences of symbols; how model *text* becomes symbols (lexemes, classes, the alphabet, longest-match
6
+ tokenisation) is outside it and is tested separately.
7
+
8
+ ## What exists
9
+
10
+ * **An independent reference interpreter** (`gcrsim/_reference.py`), written from SPEC.md alone by a different author without access
11
+ to the engine. It uses no regular expressions and works on tuples of symbols.
12
+ * **Differential tests.** The engine is compared with it on: every `sub` rule with operands of up to one item over a small alphabet
13
+ (one lexeme, two classes, `{any}`, `{start}`, `{end}`) on every string up to length four, edit by edit and candidate by candidate;
14
+ hand-picked two-item operand shapes; guards and `send()`; and seeded random multi-component models step by step (contents in
15
+ order, candidate counts, history on and off, length limit), written in every surface form (`plain`, `ins`, `del`, `sub`, numbered
16
+ and general `[rules]` sections, optional prefixes and targets) and run in the default, the streaming and the candidate-limit
17
+ paths. Overlapping lexemes are checked against a separate tokeniser.
18
+ * **`--cross-check`.** `gcrsim model.txt 20 --cross-check` runs the reference next to the engine on your model, compares the
19
+ contents and the number of candidates after every step, and stops with exit status 5 at the first difference. It refuses models
20
+ outside the fragment (regular-expression rules, `reject_strings`, `filters`, filter functions, `--no-mark-rejected`).
21
+ * **The cookbook and the real examples.** `tests/test_cookbook.py` checks that each cookbook model generates the language its header
22
+ states; `tests/test_real_examples.py` checks that each research model, rebuilt from its `# old:` comments, gives the same
23
+ contents as the ins/del/sub version, and runs `--cross-check` on those without regular expressions.
24
+
25
+ ## What this means
26
+
27
+ These are evidence, not a proof of correctness for all models: they show agreement between two independent readings of the
28
+ specification on the cases they cover, and on the run `--cross-check` executes.
29
+
30
+ Not covered by the reference: regular-expression rules (ordinary tests only), marks, the detailed rule trace, `trace_many` and the
31
+ printed output. `--cross-check` re-executes the rules the engine parsed; it does not read the model text a second time, so a parsing
32
+ mistake is not caught by it (the parser has its own tests).
33
+
34
+ The setting `hash_history` (off by default) replaces the exact duplicate test by a test on 128-bit digests; a run that uses it is
35
+ outside the guarantee above (a collision is extremely unlikely, not impossible).
36
+
37
+ Regular expressions have a known cost: a pathological pattern can take very long. Use `--max-step-candidates` and the other limits when
38
+ experimenting.