| Crates.io | tensorlogic-adapters |
| lib.rs | tensorlogic-adapters |
| version | 0.1.0-alpha.2 |
| created_at | 2025-11-07 22:23:40.2456+00 |
| updated_at | 2026-01-03 21:03:09.273517+00 |
| description | Symbol tables, axis metadata, and domain masks for TensorLogic |
| homepage | https://github.com/cool-japan/tensorlogic |
| repository | https://github.com/cool-japan/tensorlogic |
| max_upload_size | |
| id | 1922253 |
| size | 1,399,492 |
Adapter utilities for the Tensorlogic ecosystem: symbol tables, axis metadata, domain management, and schema validation.
The tensorlogic-adapters crate provides the bridge between logical expressions and tensor execution by managing:
Person <: Agent)T: Comparable)Add to your Cargo.toml:
[dependencies]
tensorlogic-adapters = "0.1.0-alpha.2"
use tensorlogic_adapters::{SymbolTable, DomainInfo, PredicateInfo};
// Create a symbol table
let mut table = SymbolTable::new();
// Add a domain
table.add_domain(DomainInfo::new("Person", 100))?;
// Add a predicate
let knows = PredicateInfo::new(
"knows",
vec!["Person".to_string(), "Person".to_string()]
);
table.add_predicate(knows)?;
// Bind a variable
table.bind_variable("x", "Person")?;
use tensorlogic_adapters::SchemaBuilder;
// Build a schema with fluent API
let table = SchemaBuilder::new()
.domain("Person", 100)
.domain("Location", 50)
.predicate("at", vec!["Person", "Location"])
.predicate("knows", vec!["Person", "Person"])
.variable("x", "Person")
.build()?;
use tensorlogic_adapters::DomainHierarchy;
let mut hierarchy = DomainHierarchy::new();
// Define a type hierarchy
hierarchy.add_subtype("Student", "Person");
hierarchy.add_subtype("Teacher", "Person");
hierarchy.add_subtype("Person", "Agent");
// Check relationships
assert!(hierarchy.is_subtype("Student", "Agent")); // Transitive
assert!(hierarchy.is_subtype("Teacher", "Person"));
// Find common supertype
let common = hierarchy.least_common_supertype("Student", "Teacher");
assert_eq!(common, Some("Person".to_string()));
use tensorlogic_adapters::{ParametricType, TypeParameter};
// Create List<Person>
let list_person = ParametricType::list(
TypeParameter::concrete("Person")
);
println!("{}", list_person); // "List<Person>"
// Create Map<String, Int>
let map_type = ParametricType::map(
TypeParameter::concrete("String"),
TypeParameter::concrete("Int")
);
println!("{}", map_type); // "Map<String, Int>"
// Nested types: List<Option<Person>>
let nested = ParametricType::list(
TypeParameter::parametric(
ParametricType::option(TypeParameter::concrete("Person"))
)
);
println!("{}", nested); // "List<Option<Person>>"
use tensorlogic_adapters::{
CompositePredicate, CompositeRegistry, PredicateBody
};
// Define a composite predicate
let friend = CompositePredicate::new(
"friend",
vec!["x".to_string(), "y".to_string()],
PredicateBody::And(vec![
PredicateBody::Reference {
name: "knows".to_string(),
args: vec!["x".to_string(), "y".to_string()],
},
PredicateBody::Reference {
name: "trusts".to_string(),
args: vec!["x".to_string(), "y".to_string()],
},
])
);
// Register in a registry
let mut registry = CompositeRegistry::new();
registry.register(friend)?;
// Expand with concrete arguments
let expanded = registry.expand(
"friend",
&["alice".to_string(), "bob".to_string()]
)?;
use tensorlogic_adapters::{DomainInfo, Metadata, Provenance, Documentation};
// Create domain with rich metadata
let mut meta = Metadata::new();
// Add provenance
meta.provenance = Some(
Provenance::new("Alice", "2025-01-15T10:30:00Z")
.with_source("schema.yaml", Some(42))
);
// Add tags
meta.add_tag("experimental");
meta.add_tag("reasoning");
// Add custom attributes
meta.set_attribute("complexity", "O(n^2)");
// Add documentation
let mut doc = Documentation::new("Domain for persons in the knowledge base");
doc.add_note("This domain supports reasoning about human entities");
meta.documentation = Some(doc);
// Attach to domain
let domain = DomainInfo::new("Person", 100)
.with_metadata(meta);
use tensorlogic_adapters::{SymbolTable, SchemaValidator};
let mut table = SymbolTable::new();
// ... populate table ...
// Validate schema
let validator = SchemaValidator::new(&table);
let report = validator.validate()?;
// Check for issues
if !report.errors.is_empty() {
for error in &report.errors {
eprintln!("Error: {}", error);
}
}
if !report.warnings.is_empty() {
for warning in &report.warnings {
println!("Warning: {}", warning);
}
}
use tensorlogic_adapters::SymbolTable;
// Export to JSON
let json = table.to_json()?;
std::fs::write("schema.json", json)?;
// Import from JSON
let json = std::fs::read_to_string("schema.json")?;
let table = SymbolTable::from_json(&json)?;
// Export to YAML
let yaml = table.to_yaml()?;
std::fs::write("schema.yaml", yaml)?;
// Import from YAML
let yaml = std::fs::read_to_string("schema.yaml")?;
let table = SymbolTable::from_yaml(&yaml)?;
The adapters integrate seamlessly with tensorlogic-compiler:
use tensorlogic_adapters::SymbolTable;
use tensorlogic_compiler::CompilerContext;
use tensorlogic_compiler::passes::symbol_integration;
let mut table = SymbolTable::new();
// ... define schema ...
// Sync with compiler context
let mut ctx = CompilerContext::new();
symbol_integration::sync_context_with_symbol_table(&mut ctx, &table)?;
// Build signature registry for type checking
let registry = symbol_integration::build_signature_registry(&table);
tensorlogic-adapters/
├── axis.rs # Axis metadata for einsum notation
├── compact.rs # Compact schema representation
├── compiler_integration.rs # Export/import for compiler
├── composition.rs # Predicate composition system
├── constraint.rs # Predicate constraints and properties
├── diff.rs # Schema diff and compatibility
├── domain.rs # Domain information and management
├── error.rs # Error types
├── hierarchy.rs # Domain hierarchy and subtyping
├── mask.rs # Domain masks for filtering
├── metadata.rs # Rich metadata with provenance/tags
├── parametric.rs # Parametric types (List<T>, etc.)
├── performance.rs # String interning and caching
├── predicate.rs # Predicate metadata
├── schema_analysis.rs # Schema statistics and analysis
├── signature_matcher.rs # Fast predicate lookups
├── symbol_table.rs # Central symbol table
├── validation.rs # Schema validation
└── bin/
├── schema_validate.rs # CLI validation tool
└── schema_migrate.rs # CLI migration tool
Use domain hierarchies and predicate constraints to model ontologies and validate RDF*/SHACL schemas.
Parametric types and composition enable defining complex feature transformations and model architectures.
Rich metadata and provenance tracking support explainable AI and audit trails.
Schema validation ensures logical expressions are well-typed before compilation.
The crate includes two command-line tools:
Validate, analyze, and get statistics for schema files:
# Basic validation
cargo run --bin schema_validate schema.yaml
# With analysis and statistics
cargo run --bin schema_validate --analyze --stats schema.yaml
# Validate JSON schema
cargo run --bin schema_validate --format json schema.json
Convert, merge, diff, and check compatibility:
# Convert JSON to YAML
cargo run --bin schema_migrate convert schema.json schema.yaml
# Merge multiple schemas
cargo run --bin schema_migrate merge schema1.yaml schema2.yaml merged.yaml
# Show diff between versions
cargo run --bin schema_migrate diff old.yaml new.yaml
# Check compatibility
cargo run --bin schema_migrate check old.yaml new.yaml
use tensorlogic_adapters::incremental_validation::{
ChangeTracker, IncrementalValidator
};
let mut table = SymbolTable::new();
let mut tracker = ChangeTracker::new();
// Add domain and track change
table.add_domain(DomainInfo::new("Person", 100))?;
tracker.record_domain_addition("Person");
// Incremental validation (only validates changed components)
let mut validator = IncrementalValidator::new(&table, &tracker);
let report = validator.validate_incremental()?;
println!("Validated {} components, {} cached",
report.components_validated,
report.components_cached);
use tensorlogic_adapters::query_planner::{QueryPlanner, PredicateQuery};
let mut planner = QueryPlanner::new(&table);
// Query with automatic optimization
let query = PredicateQuery::by_signature(
vec!["Person".to_string(), "Person".to_string()]
);
let results = planner.execute(&query)?;
// View statistics
println!("Query stats: {:?}", planner.statistics().top_queries(5));
use tensorlogic_adapters::evolution::EvolutionAnalyzer;
let analyzer = EvolutionAnalyzer::new(&old_schema, &new_schema);
let report = analyzer.analyze()?;
if report.has_breaking_changes() {
for change in &report.breaking_changes {
println!("⚠️ {}: {}", change.impact, change.description);
if let Some(hint) = &change.migration_hint {
println!(" Migration: {}", hint);
}
}
}
println!("Suggested version bump: {}", report.suggested_version_bump());
println!("Migration steps: {}", report.migration_plan.steps.len());
Run the test suite:
cargo nextest run -p tensorlogic-adapters
Current Test Stats: 447 tests (all passing, zero warnings) ⬆️ +18 new tests
Lines of Code: ~23,000 (production code)
Examples: 13 complete examples in examples/
Benchmarks: 6 comprehensive benchmark suites
Recent Enhancements v0.1.0-alpha.2:
See TODO.md for the complete roadmap. Upcoming features include:
Contributions are welcome! See CONTRIBUTING.md for guidelines.
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Status: 🎉 Production Ready (v0.1.0-alpha.2) Last Updated: 2025-12-16 (Alpha.2 Release) Tests: 447/447 passing (100%) ⬆️ +18 new tests (8 subtyping + 10 database property tests) Lines of Code: ~23,000 (production code) Examples: 13 comprehensive examples Benchmarks: 6 comprehensive benchmark suites (250+ benchmark functions) Completion: ~99% (Advanced production features) CLI Tools: 2 production-ready binaries with integration tests Code Quality: Zero warnings, zero clippy issues New in 0.1.0-alpha.2: