# 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 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::add_value(value) -> Result ValidSet::add_value_with_dependencies(value) -> ValidSet ValidSet::is_valid() -> bool ValidSet::contains(value) -> bool ValidSet::size() -> usize generate_valid_sets(max_size) -> Vec generate_valid_sets_of_size(size) -> Vec ``` **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 generate_all_sequences(length) -> Vec 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