1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# Meta-Affine Sequence Generator
A Rust library for generating and validating meta-affine sequences.
## Project Structure
```
morphology/
├── meta_affine/ # Library source code
│ ├── lib.rs # Public API and module exports
│ ├── value.rs # Phase 1: Core data structures
│ └── set.rs # Phase 2: Valid set generation
├── tests/ # Test suite (separate from source)
│ ├── value.rs # Tests for value module (16 tests)
│ └── set.rs # Tests for set module (30 tests)
├── examples/ # Example programs
│ └── demo.rs # Comprehensive demonstration
├── libmeta_affine.rlib # Compiled library
└── README.md # This file
```
**Module:** `value.rs`
Implements the foundation of meta-affine space:
- Value struct representing (outer, inner) tuples
- Compute minimum derivation path from (0,0) to any value
- Five derivation rules for generating values
**API:**
```rust
Value::new(outer, inner) -> Value
Value::origin() -> Value
Value::is_origin() -> bool
get_minimum_derivation_path(value) -> Vec<Value>
path_length(value) -> usize
```
**Module:** `set.rs`
Implements downward-closed sets in meta-affine space:
- ValidSet struct with validation
- Set generation
- Immutable set operations
**API:**
```rust
ValidSet::new() -> ValidSet
ValidSet::origin_set() -> ValidSet
ValidSet::from_values(values) -> Result<ValidSet>
ValidSet::add_value(value) -> Result<ValidSet>
ValidSet::add_value_with_dependencies(value) -> ValidSet
ValidSet::is_valid() -> bool
ValidSet::contains(value) -> bool
ValidSet::size() -> usize
generate_valid_sets(max_size) -> Vec<ValidSet>
generate_valid_sets_of_size(size) -> Vec<ValidSet>
```
**Module:** `sequence.rs`
Implements sequence generation and sorting:
- Sequence struct containing ordered values
- Sequence generation from valid sets
- Sorting with customizable precedence
**API:**
```rust
Sequence::new(values) -> Sequence
Sequence::length() -> usize
Sequence::values() -> &[Value]
Sequence::is_valid(&set) -> bool
generate_sequences(&set, length) -> Vec<Sequence>
generate_all_sequences(length) -> Vec<Sequence>
count_sequences(&set, length) -> usize
// Sorting
PrecedenceOrder::IncreasingNegativeFirst // [0, -1, 1, -2, 2, ...]
PrecedenceOrder::IncreasingPositiveFirst // [0, 1, -1, 2, -2, ...]
sort_sequences(&mut sequences, outer_order, inner_order)
```
## Building and Testing
### Compile the Library
```bash
cd morphology
rustc --crate-type=lib --crate-name=meta_affine --edition=2021 \
meta_affine/lib.rs --out-dir .
```
### Run Tests
```bash
# Compile value tests
rustc --test --edition=2021 tests/value.rs -L . \
--extern meta_affine=libmeta_affine.rlib -o test_value
# Compile set tests
rustc --test --edition=2021 tests/set.rs -L . \
--extern meta_affine=libmeta_affine.rlib -o test_set
# Run tests
./test_value # 16 tests
./test_set # 30 tests
```
### Run Demonstration
```bash
rustc examples/demo.rs -L . --extern meta_affine=libmeta_affine.rlib -o demo
./demo
```
## Usage Examples
### Core Data Structures
```rust
use meta_affine::{Value, value::get_minimum_derivation_path};
// Create a value
let v = Value::new(2, 3);
// Get its minimum derivation path from origin
let path = get_minimum_derivation_path(v);
// [(0,0), (1,0), (2,0), (2,1), (2,2), (2,3)]
// Check if it's the origin
assert!(!v.is_origin());
assert!(Value::origin().is_origin());
```
### Valid Set Generation
```rust
use meta_affine::{ValidSet, Value};
use meta_affine::set::generate_valid_sets;
// Create a set with automatic dependency inclusion
let set = ValidSet::new()
.add_value_with_dependencies(Value::new(2, 3));
// Contains: (0,0), (1,0), (2,0), (2,1), (2,2), (2,3)
// Validate a set
assert!(set.is_valid());
assert_eq!(set.size(), 6);
// Generate all valid sets up to size 5
let all_sets = generate_valid_sets(5);
// Returns 210 valid sets
```
### Sequence Generation and Sorting
```rust
use meta_affine::{ValidSet, Value, Sequence};
use meta_affine::sequence::generate_sequences;
use meta_affine::{generate_all_sequences, sort_sequences, PrecedenceOrder};
use std::collections::HashMap;
// Create a valid set
let set = ValidSet::new()
.add_value_with_dependencies(Value::new(1, 0));
// Generate all valid sequences of length 3 from this set
let mut sequences = generate_sequences(&set, 3);
// Returns sequences like [(0,0), (1,0), (0,0)] and [(1,0), (0,0), (1,0)]
// Sort sequences with default precedence (IncreasingPositiveFirst for both)
sort_sequences(&mut sequences, None, None);
// Sort with custom precedence orders
sort_sequences(
&mut sequences,
Some(PrecedenceOrder::IncreasingNegativeFirst), // outer: [0, -1, 1, -2, 2, ...]
Some(PrecedenceOrder::IncreasingPositiveFirst), // inner: [0, 1, -1, 2, -2, ...]
);
// Generate all sequences from all valid sets with sorting options
let mut options = HashMap::new();
options.insert("outerPrecedenceOrder".to_string(), PrecedenceOrder::IncreasingNegativeFirst);
options.insert("innerPrecedenceOrder".to_string(), PrecedenceOrder::IncreasingPositiveFirst);
let all_sequences = generate_all_sequences(3, options);
// Returns all valid sequences of length 3, sorted by the specified precedence
```
## Mathematical Background
### Meta-Affine Space
A meta-affine space consists of 2-tuple values (outer, inner) satisfying:
1. (0,0) is in the space
2. If (n,0) is in, then (n+1,0) is in (increment outer)
3. If (n,0) is in, then (n-1,0) is in (decrement outer)
4. If (m,n) is in, then (m,n+1) is in (increment inner)
5. If (m,n) is in, then (m,n-1) is in (decrement inner)
### Valid Sets
- For every value v ∈ S, all values in the minimum derivation path from (0,0) to v must be in S
This creates a dependency structure ensuring sets are "closed" under derivation.
### Valid Sequences
A sequence is valid if it satisfies four constraints:
1. Every value in the generating set appears at least once
2. No value appears next to itself
3. Last value is (0,0)
4. For each outer value n, the last occurrence of any (n,m) has m=0
|