summaryrefslogtreecommitdiff
path: root/morphology/examples/demo.rs
blob: bdaddf4b4ba12e83a5d9c3cf3daaa61ac607ea8e (plain)
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
//! 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));
}