tensorlogic-adapters 0.1.0

Symbol tables, axis metadata, and domain masks for TensorLogic
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! Integration utilities for tensorlogic-compiler.
//!
//! This module provides utilities for exporting SymbolTable data to
//! tensorlogic-compiler's CompilerContext and for bidirectional synchronization.

use anyhow::Result;
use std::collections::HashMap;

use crate::{DomainHierarchy, DomainInfo, PredicateConstraints, PredicateInfo, SymbolTable};

/// Export utilities for compiler integration.
pub struct CompilerExport;

impl CompilerExport {
    /// Export domain information as a simple map for compiler consumption.
    ///
    /// This returns a HashMap that maps domain names to their cardinalities,
    /// suitable for direct use in compiler contexts.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, DomainInfo, CompilerExport};
    ///
    /// let mut table = SymbolTable::new();
    /// table.add_domain(DomainInfo::new("Person", 100)).expect("unwrap");
    /// table.add_domain(DomainInfo::new("Location", 50)).expect("unwrap");
    ///
    /// let domain_map = CompilerExport::export_domains(&table);
    /// assert_eq!(domain_map.get("Person"), Some(&100));
    /// assert_eq!(domain_map.get("Location"), Some(&50));
    /// ```
    pub fn export_domains(table: &SymbolTable) -> HashMap<String, usize> {
        table
            .domains
            .iter()
            .map(|(name, info)| (name.clone(), info.cardinality))
            .collect()
    }

    /// Export predicate signatures for type checking.
    ///
    /// Returns a HashMap mapping predicate names to their argument domain lists.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, DomainInfo, PredicateInfo, CompilerExport};
    ///
    /// let mut table = SymbolTable::new();
    /// table.add_domain(DomainInfo::new("Person", 100)).expect("unwrap");
    /// table.add_predicate(PredicateInfo::new(
    ///     "knows",
    ///     vec!["Person".to_string(), "Person".to_string()]
    /// )).expect("unwrap");
    ///
    /// let signatures = CompilerExport::export_predicate_signatures(&table);
    /// assert_eq!(signatures.get("knows"), Some(&vec!["Person".to_string(), "Person".to_string()]));
    /// ```
    pub fn export_predicate_signatures(table: &SymbolTable) -> HashMap<String, Vec<String>> {
        table
            .predicates
            .iter()
            .map(|(name, info)| (name.clone(), info.arg_domains.clone()))
            .collect()
    }

    /// Export variable bindings for scope analysis.
    ///
    /// Returns a HashMap mapping variable names to their domain types.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, DomainInfo, CompilerExport};
    ///
    /// let mut table = SymbolTable::new();
    /// table.add_domain(DomainInfo::new("Person", 100)).expect("unwrap");
    /// table.bind_variable("x", "Person").expect("unwrap");
    /// table.bind_variable("y", "Person").expect("unwrap");
    ///
    /// let bindings = CompilerExport::export_variable_bindings(&table);
    /// assert_eq!(bindings.get("x"), Some(&"Person".to_string()));
    /// assert_eq!(bindings.get("y"), Some(&"Person".to_string()));
    /// ```
    pub fn export_variable_bindings(table: &SymbolTable) -> HashMap<String, String> {
        table
            .variables
            .iter()
            .map(|(var, domain)| (var.clone(), domain.clone()))
            .collect()
    }

    /// Create a complete export bundle for compiler initialization.
    ///
    /// Returns all three maps (domains, signatures, bindings) in a single structure.
    pub fn export_all(table: &SymbolTable) -> CompilerExportBundle {
        CompilerExportBundle {
            domains: Self::export_domains(table),
            predicate_signatures: Self::export_predicate_signatures(table),
            variable_bindings: Self::export_variable_bindings(table),
        }
    }
}

/// Complete export bundle for compiler integration.
///
/// This structure contains all information needed to initialize a compiler context
/// from a symbol table.
#[derive(Clone, Debug)]
pub struct CompilerExportBundle {
    /// Domain names mapped to cardinalities.
    pub domains: HashMap<String, usize>,
    /// Predicate names mapped to argument domain lists.
    pub predicate_signatures: HashMap<String, Vec<String>>,
    /// Variable names mapped to domain types.
    pub variable_bindings: HashMap<String, String>,
}

impl CompilerExportBundle {
    /// Create an empty export bundle.
    pub fn new() -> Self {
        Self {
            domains: HashMap::new(),
            predicate_signatures: HashMap::new(),
            variable_bindings: HashMap::new(),
        }
    }

    /// Check if the bundle is empty.
    pub fn is_empty(&self) -> bool {
        self.domains.is_empty()
            && self.predicate_signatures.is_empty()
            && self.variable_bindings.is_empty()
    }
}

impl Default for CompilerExportBundle {
    fn default() -> Self {
        Self::new()
    }
}

/// Import utilities for reverse synchronization.
pub struct CompilerImport;

impl CompilerImport {
    /// Import domain information from a compiler context back into a symbol table.
    ///
    /// This is useful for synchronizing state after compilation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, CompilerImport};
    /// use std::collections::HashMap;
    ///
    /// let mut domains = HashMap::new();
    /// domains.insert("Person".to_string(), 100);
    /// domains.insert("Location".to_string(), 50);
    ///
    /// let mut table = SymbolTable::new();
    /// CompilerImport::import_domains(&mut table, &domains).expect("unwrap");
    ///
    /// assert!(table.get_domain("Person").is_some());
    /// assert!(table.get_domain("Location").is_some());
    /// ```
    pub fn import_domains(table: &mut SymbolTable, domains: &HashMap<String, usize>) -> Result<()> {
        for (name, cardinality) in domains {
            table.add_domain(DomainInfo::new(name.clone(), *cardinality))?;
        }
        Ok(())
    }

    /// Import predicate signatures from a compiler context.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, DomainInfo, CompilerImport};
    /// use std::collections::HashMap;
    ///
    /// let mut table = SymbolTable::new();
    /// table.add_domain(DomainInfo::new("Person", 100)).expect("unwrap");
    ///
    /// let mut signatures = HashMap::new();
    /// signatures.insert("knows".to_string(), vec!["Person".to_string(), "Person".to_string()]);
    ///
    /// CompilerImport::import_predicates(&mut table, &signatures).expect("unwrap");
    ///
    /// assert!(table.get_predicate("knows").is_some());
    /// ```
    pub fn import_predicates(
        table: &mut SymbolTable,
        signatures: &HashMap<String, Vec<String>>,
    ) -> Result<()> {
        for (name, arg_domains) in signatures {
            table.add_predicate(PredicateInfo::new(name.clone(), arg_domains.clone()))?;
        }
        Ok(())
    }

    /// Import variable bindings from a compiler context.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, DomainInfo, CompilerImport};
    /// use std::collections::HashMap;
    ///
    /// let mut table = SymbolTable::new();
    /// table.add_domain(DomainInfo::new("Person", 100)).expect("unwrap");
    ///
    /// let mut bindings = HashMap::new();
    /// bindings.insert("x".to_string(), "Person".to_string());
    /// bindings.insert("y".to_string(), "Person".to_string());
    ///
    /// CompilerImport::import_variables(&mut table, &bindings).expect("unwrap");
    ///
    /// assert_eq!(table.get_variable_domain("x"), Some("Person"));
    /// assert_eq!(table.get_variable_domain("y"), Some("Person"));
    /// ```
    pub fn import_variables(
        table: &mut SymbolTable,
        bindings: &HashMap<String, String>,
    ) -> Result<()> {
        for (var, domain) in bindings {
            table.bind_variable(var, domain)?;
        }
        Ok(())
    }

    /// Import a complete bundle into a symbol table.
    pub fn import_all(table: &mut SymbolTable, bundle: &CompilerExportBundle) -> Result<()> {
        Self::import_domains(table, &bundle.domains)?;
        Self::import_predicates(table, &bundle.predicate_signatures)?;
        Self::import_variables(table, &bundle.variable_bindings)?;
        Ok(())
    }
}

/// Bidirectional synchronization utilities.
pub struct SymbolTableSync;

impl SymbolTableSync {
    /// Synchronize a symbol table with compiler data, merging information.
    ///
    /// This performs a two-way sync:
    /// 1. Exports current symbol table state
    /// 2. Imports compiler context data
    /// 3. Returns the merged export bundle
    pub fn sync_with_compiler(
        table: &mut SymbolTable,
        compiler_bundle: &CompilerExportBundle,
    ) -> Result<CompilerExportBundle> {
        // First, import compiler data into the table
        CompilerImport::import_all(table, compiler_bundle)?;

        // Then, export the merged state
        Ok(CompilerExport::export_all(table))
    }

    /// Validate that a compiler bundle is compatible with a symbol table.
    ///
    /// Checks that all referenced domains exist.
    pub fn validate_bundle(
        table: &SymbolTable,
        bundle: &CompilerExportBundle,
    ) -> Result<ValidationResult> {
        let mut errors = Vec::new();
        let mut warnings = Vec::new();

        // Check predicate signatures reference existing domains
        for (pred_name, arg_domains) in &bundle.predicate_signatures {
            for domain in arg_domains {
                if !table.domains.contains_key(domain) && !bundle.domains.contains_key(domain) {
                    errors.push(format!(
                        "Predicate '{}' references unknown domain '{}'",
                        pred_name, domain
                    ));
                }
            }
        }

        // Check variable bindings reference existing domains
        for (var_name, domain) in &bundle.variable_bindings {
            if !table.domains.contains_key(domain) && !bundle.domains.contains_key(domain) {
                errors.push(format!(
                    "Variable '{}' references unknown domain '{}'",
                    var_name, domain
                ));
            }
        }

        // Warn about unused domains
        for domain_name in bundle.domains.keys() {
            let used_in_predicates = bundle
                .predicate_signatures
                .values()
                .any(|args| args.contains(domain_name));
            let used_in_variables = bundle.variable_bindings.values().any(|d| d == domain_name);

            if !used_in_predicates && !used_in_variables {
                warnings.push(format!(
                    "Domain '{}' is defined but never used",
                    domain_name
                ));
            }
        }

        Ok(ValidationResult { errors, warnings })
    }
}

/// Result of bundle validation.
#[derive(Clone, Debug)]
pub struct ValidationResult {
    /// Validation errors (must be empty for valid bundles).
    pub errors: Vec<String>,
    /// Validation warnings (non-critical issues).
    pub warnings: Vec<String>,
}

impl ValidationResult {
    /// Check if validation passed (no errors).
    pub fn is_valid(&self) -> bool {
        self.errors.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_export_domains() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");
        table
            .add_domain(DomainInfo::new("Location", 50))
            .expect("unwrap");

        let domains = CompilerExport::export_domains(&table);
        assert_eq!(domains.get("Person"), Some(&100));
        assert_eq!(domains.get("Location"), Some(&50));
    }

    #[test]
    fn test_export_predicate_signatures() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");
        table
            .add_predicate(PredicateInfo::new(
                "knows",
                vec!["Person".to_string(), "Person".to_string()],
            ))
            .expect("unwrap");

        let signatures = CompilerExport::export_predicate_signatures(&table);
        assert_eq!(
            signatures.get("knows"),
            Some(&vec!["Person".to_string(), "Person".to_string()])
        );
    }

    #[test]
    fn test_export_all() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");
        table
            .add_predicate(PredicateInfo::new("knows", vec!["Person".to_string()]))
            .expect("unwrap");
        table.bind_variable("x", "Person").expect("unwrap");

        let bundle = CompilerExport::export_all(&table);
        assert_eq!(bundle.domains.len(), 1);
        assert_eq!(bundle.predicate_signatures.len(), 1);
        assert_eq!(bundle.variable_bindings.len(), 1);
    }

    #[test]
    fn test_import_domains() {
        let mut domains = HashMap::new();
        domains.insert("Person".to_string(), 100);

        let mut table = SymbolTable::new();
        CompilerImport::import_domains(&mut table, &domains).expect("unwrap");

        assert!(table.get_domain("Person").is_some());
    }

    #[test]
    fn test_import_predicates() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");

        let mut signatures = HashMap::new();
        signatures.insert("knows".to_string(), vec!["Person".to_string()]);

        CompilerImport::import_predicates(&mut table, &signatures).expect("unwrap");
        assert!(table.get_predicate("knows").is_some());
    }

    #[test]
    fn test_validation_invalid_domain_reference() {
        let table = SymbolTable::new();
        let mut bundle = CompilerExportBundle::new();
        bundle
            .predicate_signatures
            .insert("knows".to_string(), vec!["UnknownDomain".to_string()]);

        let result = SymbolTableSync::validate_bundle(&table, &bundle).expect("unwrap");
        assert!(!result.is_valid());
        assert!(!result.errors.is_empty());
    }

    #[test]
    fn test_validation_unused_domain_warning() {
        let table = SymbolTable::new();
        let mut bundle = CompilerExportBundle::new();
        bundle.domains.insert("UnusedDomain".to_string(), 100);

        let result = SymbolTableSync::validate_bundle(&table, &bundle).expect("unwrap");
        assert!(result.is_valid()); // Still valid, just has warnings
        assert!(!result.warnings.is_empty());
    }

    #[test]
    fn test_sync_with_compiler() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");

        let mut bundle = CompilerExportBundle::new();
        bundle.domains.insert("Location".to_string(), 50);

        let result = SymbolTableSync::sync_with_compiler(&mut table, &bundle).expect("unwrap");

        // Table should now have both domains
        assert!(table.get_domain("Person").is_some());
        assert!(table.get_domain("Location").is_some());

        // Result should include both
        assert_eq!(result.domains.len(), 2);
    }
}

/// Advanced export utilities for compiler integration with type systems.
///
/// This extends the basic CompilerExport with support for advanced type system
/// features including refinement types, dependent types, linear types, and effects.
pub struct CompilerExportAdvanced;

impl CompilerExportAdvanced {
    /// Export domain hierarchy information for subtype checking.
    ///
    /// Returns the complete subtype relationships from the hierarchy.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{DomainHierarchy, CompilerExportAdvanced};
    ///
    /// let mut hierarchy = DomainHierarchy::new();
    /// hierarchy.add_subtype("Student", "Person");
    /// hierarchy.add_subtype("Person", "Agent");
    ///
    /// let relationships = CompilerExportAdvanced::export_hierarchy(&hierarchy);
    /// assert!(relationships.contains_key("Student"));
    /// ```
    pub fn export_hierarchy(hierarchy: &DomainHierarchy) -> HashMap<String, Vec<String>> {
        let mut result = HashMap::new();

        for domain in hierarchy.all_domains() {
            // Get all ancestors (transitive closure of parent relationships)
            let ancestors = hierarchy.get_ancestors(&domain);
            if !ancestors.is_empty() {
                result.insert(domain, ancestors);
            }
        }

        result
    }

    /// Export predicate constraints for validation and optimization.
    ///
    /// Returns a map of predicate names to their constraint specifications.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_adapters::{SymbolTable, DomainInfo, PredicateInfo, PredicateConstraints, CompilerExportAdvanced};
    ///
    /// let mut table = SymbolTable::new();
    /// table.add_domain(DomainInfo::new("Person", 100)).expect("unwrap");
    ///
    /// let mut pred = PredicateInfo::new("age", vec!["Person".to_string()]);
    /// pred.constraints = Some(PredicateConstraints::new());
    /// table.add_predicate(pred).expect("unwrap");
    ///
    /// let constraints = CompilerExportAdvanced::export_constraints(&table);
    /// assert!(constraints.contains_key("age"));
    /// ```
    pub fn export_constraints(table: &SymbolTable) -> HashMap<String, PredicateConstraints> {
        table
            .predicates
            .iter()
            .filter_map(|(name, info)| info.constraints.as_ref().map(|c| (name.clone(), c.clone())))
            .collect()
    }

    /// Export refinement types for compile-time validation.
    ///
    /// Returns refinement type specifications that can be used for
    /// static analysis and runtime validation.
    pub fn export_refinement_types() -> HashMap<String, String> {
        // Export built-in refinement types
        // In a real implementation, this would be populated from a RefinementRegistry
        let mut types = HashMap::new();
        types.insert("PositiveInt".to_string(), "{ x: Int | x > 0 }".to_string());
        types.insert(
            "Probability".to_string(),
            "{ x: Float | 0.0 <= x <= 1.0 }".to_string(),
        );
        types.insert(
            "NonZeroFloat".to_string(),
            "{ x: Float | x != 0.0 }".to_string(),
        );
        types
    }

    /// Export dependent type information for dimension tracking.
    ///
    /// Returns dependent type specifications for compile-time dimension checking.
    pub fn export_dependent_types() -> HashMap<String, String> {
        // Export common dependent type patterns
        let mut types = HashMap::new();
        types.insert("Vector".to_string(), "Vector<T, n>".to_string());
        types.insert("Matrix".to_string(), "Matrix<T, m, n>".to_string());
        types.insert("Tensor3D".to_string(), "Tensor<T, i, j, k>".to_string());
        types.insert("SquareMatrix".to_string(), "Matrix<T, n, n>".to_string());
        types
    }

    /// Export linear type resources for lifetime tracking.
    ///
    /// Returns linear type specifications for resource management.
    pub fn export_linear_types() -> HashMap<String, String> {
        // Export linear type specifications
        let mut types = HashMap::new();
        types.insert("GpuTensor".to_string(), "Linear".to_string());
        types.insert("FileHandle".to_string(), "Linear".to_string());
        types.insert("NetworkSocket".to_string(), "Linear".to_string());
        types.insert("MutableReference".to_string(), "Affine".to_string());
        types.insert("LogMessage".to_string(), "Relevant".to_string());
        types
    }

    /// Export effect information for effect tracking.
    ///
    /// Returns effect specifications for compile-time effect checking.
    pub fn export_effects() -> HashMap<String, Vec<String>> {
        // Export common effect patterns
        let mut effects = HashMap::new();
        effects.insert("read_file".to_string(), vec!["IO".to_string()]);
        effects.insert(
            "allocate_gpu".to_string(),
            vec!["GPU".to_string(), "State".to_string()],
        );
        effects.insert(
            "train_model".to_string(),
            vec!["State".to_string(), "NonDet".to_string()],
        );
        effects.insert(
            "fetch_data".to_string(),
            vec!["IO".to_string(), "Exception".to_string()],
        );
        effects
    }

    /// Create a complete advanced export bundle.
    ///
    /// Returns all advanced type system information in a single structure.
    pub fn export_all_advanced(
        table: &SymbolTable,
        hierarchy: Option<&DomainHierarchy>,
    ) -> AdvancedExportBundle {
        AdvancedExportBundle {
            hierarchy: hierarchy.map(Self::export_hierarchy),
            constraints: Self::export_constraints(table),
            refinement_types: Self::export_refinement_types(),
            dependent_types: Self::export_dependent_types(),
            linear_types: Self::export_linear_types(),
            effects: Self::export_effects(),
        }
    }
}

/// Advanced export bundle containing type system information.
#[derive(Clone, Debug)]
pub struct AdvancedExportBundle {
    /// Domain hierarchy (subtype relationships).
    pub hierarchy: Option<HashMap<String, Vec<String>>>,
    /// Predicate constraints.
    pub constraints: HashMap<String, PredicateConstraints>,
    /// Refinement type specifications.
    pub refinement_types: HashMap<String, String>,
    /// Dependent type specifications.
    pub dependent_types: HashMap<String, String>,
    /// Linear type specifications.
    pub linear_types: HashMap<String, String>,
    /// Effect specifications.
    pub effects: HashMap<String, Vec<String>>,
}

impl AdvancedExportBundle {
    /// Create an empty advanced export bundle.
    pub fn new() -> Self {
        Self {
            hierarchy: None,
            constraints: HashMap::new(),
            refinement_types: HashMap::new(),
            dependent_types: HashMap::new(),
            linear_types: HashMap::new(),
            effects: HashMap::new(),
        }
    }

    /// Check if the bundle is empty.
    pub fn is_empty(&self) -> bool {
        self.hierarchy.is_none()
            && self.constraints.is_empty()
            && self.refinement_types.is_empty()
            && self.dependent_types.is_empty()
            && self.linear_types.is_empty()
            && self.effects.is_empty()
    }
}

impl Default for AdvancedExportBundle {
    fn default() -> Self {
        Self::new()
    }
}

/// Complete compiler integration bundle combining basic and advanced exports.
#[derive(Clone, Debug)]
pub struct CompleteExportBundle {
    /// Basic export bundle (domains, predicates, variables).
    pub basic: CompilerExportBundle,
    /// Advanced type system export bundle.
    pub advanced: AdvancedExportBundle,
}

impl CompleteExportBundle {
    /// Create a new complete export bundle.
    pub fn new(basic: CompilerExportBundle, advanced: AdvancedExportBundle) -> Self {
        Self { basic, advanced }
    }

    /// Export everything from a symbol table and hierarchy.
    pub fn from_symbol_table(table: &SymbolTable, hierarchy: Option<&DomainHierarchy>) -> Self {
        Self {
            basic: CompilerExport::export_all(table),
            advanced: CompilerExportAdvanced::export_all_advanced(table, hierarchy),
        }
    }

    /// Check if the bundle is completely empty.
    pub fn is_empty(&self) -> bool {
        self.basic.is_empty() && self.advanced.is_empty()
    }
}

#[cfg(test)]
mod advanced_tests {
    use super::*;

    #[test]
    fn test_export_hierarchy() {
        let mut hierarchy = DomainHierarchy::new();
        hierarchy.add_subtype("Student", "Person");
        hierarchy.add_subtype("Person", "Agent");

        let relationships = CompilerExportAdvanced::export_hierarchy(&hierarchy);

        assert!(relationships.contains_key("Student"));
        assert!(relationships.contains_key("Person"));
    }

    #[test]
    fn test_export_constraints() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");

        let mut pred = PredicateInfo::new("age", vec!["Person".to_string()]);
        pred.constraints = Some(PredicateConstraints::new());
        table.add_predicate(pred).expect("unwrap");

        let constraints = CompilerExportAdvanced::export_constraints(&table);
        assert!(constraints.contains_key("age"));
    }

    #[test]
    fn test_export_refinement_types() {
        let types = CompilerExportAdvanced::export_refinement_types();
        assert!(types.contains_key("PositiveInt"));
        assert!(types.contains_key("Probability"));
    }

    #[test]
    fn test_export_dependent_types() {
        let types = CompilerExportAdvanced::export_dependent_types();
        assert!(types.contains_key("Vector"));
        assert!(types.contains_key("Matrix"));
    }

    #[test]
    fn test_export_linear_types() {
        let types = CompilerExportAdvanced::export_linear_types();
        assert!(types.contains_key("GpuTensor"));
        assert!(types.contains_key("FileHandle"));
    }

    #[test]
    fn test_export_effects() {
        let effects = CompilerExportAdvanced::export_effects();
        assert!(effects.contains_key("read_file"));
        assert!(effects.contains_key("allocate_gpu"));
    }

    #[test]
    fn test_export_all_advanced() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");

        let mut hierarchy = DomainHierarchy::new();
        hierarchy.add_subtype("Student", "Person");

        let bundle = CompilerExportAdvanced::export_all_advanced(&table, Some(&hierarchy));

        assert!(bundle.hierarchy.is_some());
        assert!(!bundle.refinement_types.is_empty());
        assert!(!bundle.dependent_types.is_empty());
    }

    #[test]
    fn test_complete_export_bundle() {
        let mut table = SymbolTable::new();
        table
            .add_domain(DomainInfo::new("Person", 100))
            .expect("unwrap");

        let mut hierarchy = DomainHierarchy::new();
        hierarchy.add_subtype("Student", "Person");

        let bundle = CompleteExportBundle::from_symbol_table(&table, Some(&hierarchy));

        assert!(!bundle.is_empty());
        assert_eq!(bundle.basic.domains.len(), 1);
        assert!(bundle.advanced.hierarchy.is_some());
    }

    #[test]
    fn test_advanced_bundle_empty() {
        let bundle = AdvancedExportBundle::new();
        assert!(bundle.is_empty());
    }
}