summaryrefslogtreecommitdiff
path: root/morphology/examples
diff options
context:
space:
mode:
authorjacksonfu14 <jackson@fashionunited.com>2025-11-06 07:04:52 +0100
committerJackson Moore <jacksonmoore@tuta.io>2025-11-06 07:04:52 +0100
commitc20b1fe88b0ab1469300bd53be14faa6a5b917a0 (patch)
tree9e41975e6f6852146216a05ba4cef09436688aa2 /morphology/examples
initial commit
Diffstat (limited to 'morphology/examples')
-rw-r--r--morphology/examples/demo.rs121
-rw-r--r--morphology/examples/demo_full.rs220
2 files changed, 341 insertions, 0 deletions
diff --git a/morphology/examples/demo.rs b/morphology/examples/demo.rs
new file mode 100644
index 0000000..bdaddf4
--- /dev/null
+++ b/morphology/examples/demo.rs
@@ -0,0 +1,121 @@
+//! Demonstration of the meta_affine library
+//!
+//! Compile and run:
+//! rustc examples/demo.rs -L . --extern meta_affine=libmeta_affine.rlib -o demo
+//! ./demo
+
+extern crate meta_affine;
+
+use meta_affine::{Value, ValidSet};
+use meta_affine::value::get_minimum_derivation_path;
+use meta_affine::set::generate_valid_sets;
+
+fn main() {
+ println!("\n{}", "=".repeat(70));
+ println!("Meta-Affine Library Demonstration - Phase 1 & 2");
+ println!("{}\n", "=".repeat(70));
+
+ // ========================================================================
+ // Phase 1: Core Data Structures
+ // ========================================================================
+
+ println!("PHASE 1: Core Data Structures (Value)\n");
+
+ // Create values
+ let v1 = Value::new(2, 3);
+ let v2 = Value::origin();
+
+ println!("1. Created values:");
+ println!(" v1 = {}", v1);
+ println!(" v2 = {} (origin)", v2);
+
+ // Compute derivation path
+ println!("\n2. Minimum derivation path for {}:", v1);
+ let path = get_minimum_derivation_path(v1);
+ print!(" ");
+ for (i, value) in path.iter().enumerate() {
+ print!("{}", value);
+ if i < path.len() - 1 {
+ print!(" → ");
+ }
+ }
+ println!("\n Path length: {}", path.len());
+
+ // ========================================================================
+ // Phase 2: Valid Set Generation
+ // ========================================================================
+
+ println!("\n{}", "=".repeat(70));
+ println!("PHASE 2: Valid Set Generation\n");
+
+ // Create a valid set manually
+ println!("3. Creating valid sets:");
+ let set1 = ValidSet::origin_set();
+ println!(" Origin set: {:?}", set1.values_sorted());
+
+ let set2 = set1.add_value_with_dependencies(Value::new(2, 3));
+ println!(" After adding (2,3) with dependencies:");
+ println!(" {:?}", set2.values_sorted());
+ println!(" Size: {}", set2.size());
+ println!(" Valid: {}", set2.is_valid());
+
+ // Generate all valid sets up to size 3
+ println!("\n4. Generating all valid sets up to size 3:");
+ let all_sets = generate_valid_sets(3);
+ println!(" Total number of valid sets: {}", all_sets.len());
+
+ // Show sets by size
+ for size in 0..=3 {
+ let sets_of_size: Vec<_> = all_sets
+ .iter()
+ .filter(|s| s.size() == size)
+ .collect();
+ println!(" Sets of size {}: {}", size, sets_of_size.len());
+ }
+
+ // Show some example sets
+ println!("\n5. Example valid sets of size 3:");
+ let size_3_sets: Vec<_> = all_sets
+ .iter()
+ .filter(|s| s.size() == 3)
+ .take(5)
+ .collect();
+
+ for (i, set) in size_3_sets.iter().enumerate() {
+ println!(" Set {}: {:?}", i + 1, set.values_sorted());
+ }
+
+ // Demonstrate validation
+ println!("\n6. Set validation:");
+ let valid_values: std::collections::HashSet<_> = vec![
+ Value::new(0, 0),
+ Value::new(1, 0),
+ Value::new(1, 1),
+ ].into_iter().collect();
+ let valid_set = ValidSet::from_values(valid_values).unwrap();
+ println!(" {{(0,0), (1,0), (1,1)}} is valid: {}", valid_set.is_valid());
+
+ let invalid_values: std::collections::HashSet<_> = vec![
+ Value::new(0, 0),
+ Value::new(2, 0), // Missing (1,0)
+ ].into_iter().collect();
+ let invalid_result = ValidSet::from_values(invalid_values);
+ println!(" {{(0,0), (2,0)}} is valid: {}", invalid_result.is_ok());
+
+ // Statistics
+ println!("\n7. Statistics for sets up to size 5:");
+ let large_set = generate_valid_sets(5);
+ println!(" Total valid sets: {}", large_set.len());
+
+ let by_size: Vec<usize> = (0..=5)
+ .map(|size| large_set.iter().filter(|s| s.size() == size).count())
+ .collect();
+
+ for (size, count) in by_size.iter().enumerate() {
+ println!(" Size {}: {} sets", size, count);
+ }
+
+ println!("\n{}", "=".repeat(70));
+ println!("Demonstration complete!");
+ println!("{}\n", "=".repeat(70));
+}
diff --git a/morphology/examples/demo_full.rs b/morphology/examples/demo_full.rs
new file mode 100644
index 0000000..fcb4d82
--- /dev/null
+++ b/morphology/examples/demo_full.rs
@@ -0,0 +1,220 @@
+//! Comprehensive demonstration of the meta_affine library - All 3 Phases
+//!
+//! Compile and run:
+//! rustc examples/demo_full.rs -L . --extern meta_affine=libmeta_affine.rlib -o demo_full
+//! ./demo_full
+
+extern crate meta_affine;
+
+use meta_affine::{Value, ValidSet, Sequence};
+use meta_affine::value::get_minimum_derivation_path;
+use meta_affine::set::generate_valid_sets;
+use meta_affine::sequence::{generate_sequences, generate_all_sequences};
+
+fn main() {
+ println!("\n{}", "=".repeat(70));
+ println!("Meta-Affine Library - Complete Demonstration (Phases 1-3)");
+ println!("{}\n", "=".repeat(70));
+
+ // ========================================================================
+ // Phase 1: Core Data Structures
+ // ========================================================================
+
+ println!("PHASE 1: Core Data Structures (Value)\n");
+
+ let v1 = Value::new(2, 3);
+ println!("1. Value: {}", v1);
+
+ let path = get_minimum_derivation_path(v1);
+ print!(" Derivation path: ");
+ for (i, value) in path.iter().enumerate() {
+ print!("{}", value);
+ if i < path.len() - 1 {
+ print!(" → ");
+ }
+ }
+ println!("\n Length: {}\n", path.len());
+
+ // ========================================================================
+ // Phase 2: Valid Set Generation
+ // ========================================================================
+
+ println!("{}", "=".repeat(70));
+ println!("PHASE 2: Valid Set Generation\n");
+
+ let set1 = ValidSet::origin_set();
+ println!("2. Origin set: {:?}", set1.values_sorted());
+
+ let set2 = set1.add_value_with_dependencies(Value::new(1, 1));
+ println!(" Extended set: {:?}", set2.values_sorted());
+ println!(" Size: {}, Valid: {}\n", set2.size(), set2.is_valid());
+
+ println!("3. All valid sets up to size 4:");
+ let all_sets = generate_valid_sets(4);
+ for size in 0..=4 {
+ let count = all_sets.iter().filter(|s| s.size() == size).count();
+ println!(" Size {}: {} sets", size, count);
+ }
+ println!();
+
+ // ========================================================================
+ // Phase 3: Valid Sequence Generation
+ // ========================================================================
+
+ println!("{}", "=".repeat(70));
+ println!("PHASE 3: Valid Sequence Generation\n");
+
+ // Example 1: Simple set, length 1
+ println!("4. Sequences from {{(0,0)}} with length 1:");
+ let simple_set = ValidSet::origin_set();
+ let seqs1 = generate_sequences(&simple_set, 1);
+ println!(" Count: {}", seqs1.len());
+ for seq in &seqs1 {
+ print!(" [");
+ for (i, v) in seq.values().iter().enumerate() {
+ print!("{}", v);
+ if i < seq.values().len() - 1 {
+ print!(", ");
+ }
+ }
+ println!("]");
+ }
+ println!();
+
+ // Example 2: Two-element set, length 2
+ println!("5. Sequences from {{(0,0), (1,0)}} with length 2:");
+ let mut set_vals = std::collections::HashSet::new();
+ set_vals.insert(Value::new(0, 0));
+ set_vals.insert(Value::new(1, 0));
+ let two_set = ValidSet::from_values(set_vals).unwrap();
+ let seqs2 = generate_sequences(&two_set, 2);
+ println!(" Count: {}", seqs2.len());
+ for seq in &seqs2 {
+ print!(" [");
+ for (i, v) in seq.values().iter().enumerate() {
+ print!("{}", v);
+ if i < seq.values().len() - 1 {
+ print!(", ");
+ }
+ }
+ println!("]");
+ }
+ println!();
+
+ // Example 3: Complex set with inner values
+ println!("6. Sequences from {{(0,0), (1,0), (1,1)}} with length 3:");
+ let mut set_vals = std::collections::HashSet::new();
+ set_vals.insert(Value::new(0, 0));
+ set_vals.insert(Value::new(1, 0));
+ set_vals.insert(Value::new(1, 1));
+ let complex_set = ValidSet::from_values(set_vals).unwrap();
+ let seqs3 = generate_sequences(&complex_set, 3);
+ println!(" Count: {}", seqs3.len());
+ if seqs3.len() <= 10 {
+ for seq in &seqs3 {
+ print!(" [");
+ for (i, v) in seq.values().iter().enumerate() {
+ print!("{}", v);
+ if i < seq.values().len() - 1 {
+ print!(", ");
+ }
+ }
+ println!("]");
+ }
+ } else {
+ for seq in seqs3.iter().take(5) {
+ print!(" [");
+ for (i, v) in seq.values().iter().enumerate() {
+ print!("{}", v);
+ if i < seq.values().len() - 1 {
+ print!(", ");
+ }
+ }
+ println!("]");
+ }
+ println!(" ... and {} more", seqs3.len() - 5);
+ }
+ println!();
+
+ // Example 4: All sequences of a given length
+ println!("7. All valid sequences of length 2 (from all possible sets):");
+ let all_seqs2 = generate_all_sequences(2);
+ println!(" Total count: {}", all_seqs2.len());
+ println!(" Sample sequences:");
+ for seq in all_seqs2.iter().take(5) {
+ print!(" [");
+ for (i, v) in seq.values().iter().enumerate() {
+ print!("{}", v);
+ if i < seq.values().len() - 1 {
+ print!(", ");
+ }
+ }
+ println!("]");
+ }
+ if all_seqs2.len() > 5 {
+ println!(" ... and {} more", all_seqs2.len() - 5);
+ }
+ println!();
+
+ // ========================================================================
+ // Constraint Demonstration
+ // ========================================================================
+
+ println!("{}", "=".repeat(70));
+ println!("CONSTRAINT VALIDATION DEMONSTRATION\n");
+
+ use meta_affine::sequence::*;
+
+ println!("8. Constraint checking examples:");
+
+ // Valid sequence
+ let valid_seq = Sequence::new(vec![Value::new(1, 0), Value::new(0, 0)]);
+ println!(" Sequence: [(1,0), (0,0)]");
+ println!(" - Coverage: {}", check_coverage(&valid_seq, &two_set));
+ println!(" - No consecutive duplicates: {}", check_no_consecutive_duplicates(&valid_seq));
+ println!(" - Terminal (0,0): {}", check_terminal(&valid_seq));
+ println!(" - Outer constraint: {}", check_outer_value_constraint(&valid_seq));
+ println!(" Overall valid: {}", valid_seq.is_valid(&two_set));
+ println!();
+
+ // Invalid sequence (doesn't end with (0,0))
+ let invalid_seq1 = Sequence::new(vec![Value::new(0, 0), Value::new(1, 0)]);
+ println!(" Sequence: [(0,0), (1,0)]");
+ println!(" - Terminal (0,0): {}", check_terminal(&invalid_seq1));
+ println!(" Overall valid: {}", invalid_seq1.is_valid(&two_set));
+ println!();
+
+ // Invalid sequence (consecutive duplicates)
+ let invalid_seq2 = Sequence::new(vec![Value::new(0, 0), Value::new(0, 0)]);
+ println!(" Sequence: [(0,0), (0,0)]");
+ println!(" - No consecutive duplicates: {}", check_no_consecutive_duplicates(&invalid_seq2));
+ println!(" Overall valid: {}", invalid_seq2.is_valid(&simple_set));
+ println!();
+
+ // ========================================================================
+ // Statistics
+ // ========================================================================
+
+ println!("{}", "=".repeat(70));
+ println!("STATISTICS\n");
+
+ println!("9. Sequence counts by length:");
+ for length in 1..=4 {
+ let count = generate_all_sequences(length).len();
+ println!(" Length {}: {} sequences", length, count);
+ }
+ println!();
+
+ println!("10. Growth comparison:");
+ println!(" Sets up to size 3: {} valid sets", generate_valid_sets(3).len());
+ println!(" Sets up to size 4: {} valid sets", generate_valid_sets(4).len());
+ println!(" Sets up to size 5: {} valid sets", generate_valid_sets(5).len());
+ println!();
+
+ println!("{}", "=".repeat(70));
+ println!("COMPLETE! All three phases demonstrated.");
+ println!("- Phase 1: Core data structures (Values and paths)");
+ println!("- Phase 2: Valid set generation with downward-closure");
+ println!("- Phase 3: Valid sequence generation with 4 constraints");
+ println!("{}\n", "=".repeat(70));
+}