From c20b1fe88b0ab1469300bd53be14faa6a5b917a0 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Thu, 6 Nov 2025 07:04:52 +0100 Subject: initial commit --- morphology/examples/demo.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 morphology/examples/demo.rs (limited to 'morphology/examples/demo.rs') 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 = (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)); +} -- cgit v1.2.3