Expand description
§TensorLogic IR
Engine-agnostic Abstract Syntax Tree & Intermediate Representation for TensorLogic
Version: 0.1.0-beta.1 | Status: Production Ready
This crate provides the core data structures and operations for representing logic-as-tensor computations in the TensorLogic framework. It serves as the foundational layer that all other TensorLogic components build upon.
§Overview
TensorLogic IR enables the compilation of logical rules (predicates, quantifiers, implications) into tensor equations (einsum graphs) with a minimal DSL + IR. This approach allows for:
- Neural, symbolic, and probabilistic models within a unified tensor computation framework
- Engine-agnostic representation that can be executed on different backends
- Static analysis and optimization of logical expressions before execution
- Type-safe construction and validation of logical expressions
§Core Components
§Terms (Term)
Variables and constants that appear in logical expressions:
- Variables: Free or bound variables (e.g.,
x,y,Person) - Constants: Concrete values (e.g.,
alice,bob,42) - Typed terms: Terms with explicit type annotations
§Logical Expressions (TLExpr)
The DSL for expressing logical rules:
- Predicates: Atomic propositions like
Person(x)orknows(alice, bob) - Logical connectives: AND (∧), OR (∨), NOT (¬), Implication (→)
- Quantifiers: Existential (∃) and Universal (∀) with domain constraints
- Arithmetic: Addition, subtraction, multiplication, division
- Comparisons: Equality, less than, greater than, etc.
- Control flow: If-then-else conditionals
§Tensor Computation Graphs (EinsumGraph)
The compiled IR representing tensor operations:
- Tensors: Named tensor values with indices
- Nodes: Operations on tensors (einsum, element-wise, reductions)
- Outputs: Designated output tensors
- Validation: Comprehensive graph validation and error checking
§Features
§Type System
- Static type checking with
PredicateSignatureandTypeAnnotation - Parametric types with type constructors (
List<T>,Option<T>,Tuple<A,B>, etc.) - Type unification using Robinson’s algorithm
- Generalization and instantiation for polymorphic types
- Effect system for tracking computational effects (purity, differentiability, stochasticity)
- Effect polymorphism and inference
- Dependent types with value-dependent types (e.g.,
Vec<n, T>where n is a runtime value) - Linear types for resource management and safe in-place operations
- Refinement types for constraint-based type checking (e.g.,
{x: Int | x > 0}) - Arity validation ensures consistent predicate usage
- Type inference and compatibility checking
§Domain Constraints
- Domain management via
DomainRegistryandDomainInfo - Built-in domains: Bool, Int, Real, Nat, Probability
- Custom finite and infinite domains
- Domain validation for quantified variables
§Metadata & Provenance
- Source location tracking with
SourceLocationandSourceSpan - Provenance information via
Provenancefor rule tracking - Custom metadata support for nodes and expressions
§Serialization
- Full serde support for JSON and binary formats
- Versioned serialization with
VersionedExprandVersionedGraph - Backward compatibility checking
§Analysis & Utilities
- Free variable analysis
- Predicate extraction and counting
- Graph statistics with
GraphStats - Expression statistics with
ExprStats - Pretty printing and DOT export for visualization
- Expression and graph diffing with
diff_exprsanddiff_graphs - Profile-guided optimization (PGO) for runtime-informed optimization decisions
§Quick Start
§Creating Logical Expressions
use tensorlogic_ir::{TLExpr, Term};
// Simple predicate: Person(x)
let person = TLExpr::pred("Person", vec![Term::var("x")]);
// Logical rule: ∀x. Person(x) → Mortal(x)
let mortal = TLExpr::pred("Mortal", vec![Term::var("x")]);
let rule = TLExpr::forall("x", "Entity", TLExpr::imply(person, mortal));
// Verify no free variables (all bound by quantifier)
assert!(rule.free_vars().is_empty());§Building Computation Graphs
use tensorlogic_ir::{EinsumGraph, EinsumNode};
let mut graph = EinsumGraph::new();
// Matrix multiplication: C = A @ B
let a = graph.add_tensor("A");
let b = graph.add_tensor("B");
let c = graph.add_tensor("C");
graph.add_node(EinsumNode::einsum("ik,kj->ij", vec![a, b], vec![c])).unwrap();
graph.add_output(c).unwrap();
// Validate the graph
assert!(graph.validate().is_ok());§Domain Management
use tensorlogic_ir::{DomainRegistry, DomainInfo, TLExpr, Term};
// Create registry with built-in domains
let mut registry = DomainRegistry::with_builtins();
// Add custom domain
registry.register(DomainInfo::finite("Person", 100)).unwrap();
// Create and validate expression
let expr = TLExpr::exists("x", "Person", TLExpr::pred("P", vec![Term::var("x")]));
assert!(expr.validate_domains(®istry).is_ok());§Examples
See the examples/ directory for comprehensive demonstrations:
00_basic_expressions: Simple predicates and logical connectives01_quantifiers: Existential and universal quantifiers02_arithmetic: Arithmetic and comparison operations03_graph_construction: Building computation graphs05_serialization: JSON and binary serialization06_visualization: Pretty printing and DOT export07_parametric_types: Parametric types, unification, and polymorphic signatures08_effect_system: Effect tracking, polymorphism, and annotations09_dependent_types: Dependent types with value-dependent dimensions10_linear_types: Linear types for resource management11_refinement_types: Refinement types with predicates12_profile_guided_optimization: Profile-guided optimization with runtime profiling13_sequent_calculus: Sequent calculus and proof search14_constraint_logic_programming: Constraint satisfaction problems15_advanced_graph_algorithms: Graph analysis (SCC, cycles, critical paths)16_resolution_theorem_proving: Resolution-based automated theorem proving
§Architecture
The crate is organized into focused modules:
- term: Variables, constants, and type annotations
- expr: Logical expressions and builders
- graph: Tensor computation graphs and nodes
- domain: Domain constraints and validation
- signature: Type signatures for predicates
parametric_types: Parametric types, type constructors, and unificationeffect_system: Effect tracking, polymorphism, and annotationsdependent: Dependent types with value-dependent dimensionslinear: Linear types for resource management and multiplicity trackingrefinement: Refinement types with logical predicates- metadata: Provenance and source tracking
serialization: Versioned JSON/binary formatsutil: Pretty printing and statisticsdiff: Expression and graph comparison- graph::pgo: Profile-guided optimization with runtime profiling
- error: Comprehensive error types
§Logic-to-Tensor Mapping
| Logic Operation | Tensor Equivalent | Notes |
|---|---|---|
AND(a, b) | a * b | Hadamard product (element-wise) |
OR(a, b) | max(a, b) | Or soft variant (configurable) |
NOT(a) | 1 - a | Complement operation |
∃x. P(x) | sum(P, axis=x) | Or max for hard quantification |
∀x. P(x) | NOT(∃x. NOT(P(x))) | Dual of existential |
a → b | ReLU(b - a) | Soft implication |
§Performance
- Lazy validation: Operations are validated only when explicitly requested
- Zero-copy indices: Graph operations use tensor indices instead of cloning
- Incremental building: Graphs can be built step-by-step efficiently
- Property tests: 30 randomized tests ensure correctness
- Benchmarks: 40+ performance tests measure all operations
§See Also
- tensorlogic-compiler: Compiles TLExpr → EinsumGraph
- tensorlogic-infer: Execution and autodiff traits
- tensorlogic-scirs-backend: SciRS2-powered runtime execution
- tensorlogic-adapters: Symbol tables and axis metadata
Re-exports§
pub use dependent::DependentType;pub use dependent::DependentTypeContext;pub use dependent::DimConstraint;pub use dependent::IndexExpr;pub use diff::diff_exprs;pub use diff::diff_graphs;pub use diff::ExprDiff;pub use diff::GraphDiff;pub use diff::NodeDiff;pub use effect_system::infer_operation_effects;pub use effect_system::ComputationalEffect;pub use effect_system::Effect;pub use effect_system::EffectAnnotation;pub use effect_system::EffectScheme;pub use effect_system::EffectSet;pub use effect_system::EffectSubstitution;pub use effect_system::EffectVar;pub use effect_system::MemoryEffect;pub use effect_system::ProbabilisticEffect;pub use linear::Capability;pub use linear::LinearContext;pub use linear::LinearResource;pub use linear::LinearType;pub use linear::LinearityChecker;pub use linear::Multiplicity;pub use linear::Usage;pub use parametric_types::compose_substitutions;pub use parametric_types::generalize;pub use parametric_types::instantiate;pub use parametric_types::unify;pub use parametric_types::Kind;pub use parametric_types::ParametricType;pub use parametric_types::TypeConstructor;pub use parametric_types::TypeSubstitution;pub use refinement::LiquidTypeInference;pub use refinement::Refinement;pub use refinement::RefinementContext;pub use refinement::RefinementType;pub use resolution::Clause;pub use resolution::Literal;pub use resolution::ProofResult;pub use resolution::ProverStats;pub use resolution::ResolutionProver;pub use resolution::ResolutionStep;pub use resolution::ResolutionStrategy;pub use sequent::CutElimination;pub use sequent::InferenceRule;pub use sequent::ProofSearchEngine;pub use sequent::ProofSearchStats;pub use sequent::ProofSearchStrategy;pub use sequent::ProofTree;pub use sequent::Sequent;pub use serialization::VersionedExpr;pub use serialization::VersionedGraph;pub use serialization::FORMAT_VERSION;pub use unification::anti_unify_terms;pub use unification::are_unifiable;pub use unification::lgg_terms;pub use unification::rename_vars;pub use unification::unify_term_list;pub use unification::unify_terms;pub use unification::Substitution;pub use util::pretty_print_expr;pub use util::pretty_print_graph;pub use util::ExprStats;pub use util::GraphStats;
Modules§
- clp
- Constraint Logic Programming (CLP) Module
- dependent
- Dependent type system for value-dependent types in TensorLogic.
- diff
- IR diff tool for comparing graphs and expressions.
- effect_
system - Effect system for tracking computational effects in TensorLogic expressions.
- fuzzing
- Fuzzing and stress testing infrastructure for the IR.
- linear
- Linear type system for resource management in TensorLogic.
- parametric_
types - Parametric type system with type constructors and unification.
- refinement
- Refinement types for constraint-based type checking.
- resolution
- Resolution-Based Theorem Proving
- sequent
- Sequent Calculus for Proof-Theoretic Foundations
- serialization
- Enhanced serialization support for TensorLogic IR.
- unification
- First-Order Unification
- util
- Utility functions for the IR.
Structs§
- ACPattern
- AC pattern matching with variable bindings.
- Advanced
Rewrite System - Advanced rewrite system with sophisticated control flow.
- Complexity
Metrics - Complexity metrics for an expression.
- Conditional
Rule - A conditional rewrite rule with guards and priority.
- Confluence
Checker - Confluence checker for rewrite systems.
- Confluence
Report - Result of confluence analysis.
- Constant
Info - Information about a constant tensor.
- Constant
Propagation Result - Result of constant propagation analysis.
- Cost
Summary - Summary of graph costs.
- Credal
Set - Credal set: convex set of probability distributions.
- Critical
Pair - A critical pair representing a potential conflict between two rules.
- Critical
Path - Critical path analysis result.
- Cycle
- A cycle in the computation graph.
- Domain
Info - Represents a domain constraint for a variable.
- Domain
Registry - Registry for managing domain information.
- DotExport
Options - Options for DOT export customization.
- Einsum
Graph - Einsum
Node - Execution
Profile - Execution profile for entire graph.
- Execution
Schedule - A schedule for executing graph operations
- Expression
Profile - Characteristics of an expression used for strategy selection.
- Folding
Stats - Statistics from constant folding transformation.
- Fusion
Stats - Statistics about fusion optimizations applied
- Fuzzy
Set - Represents a fuzzy membership function over a continuous domain.
- Graph
Cost Model - Cost model for an entire graph.
- Graph
Rewrite Rule - A rewrite rule that transforms matched patterns
- Graph
Scheduler - Advanced scheduler for computation graphs
- Graph
Validation Stats - Statistics about the validated graph.
- Layout
Optimization Result - Result of layout optimization.
- Memory
Analysis - Memory optimization analysis result
- Metadata
- Metadata container that can be attached to IR nodes
- Multiset
- Multiset for AC matching.
- Node
Stats - Runtime execution statistics for a single node.
- Onnx
Export Options - Export options for ONNX format.
- Operation
Cost - Cost annotation for a single operation.
- Operator
Counts - Detailed operator counts categorized by type.
- Optimization
Metrics - Metrics collected during optimization.
- Optimization
Pipeline - Optimization pipeline that orchestrates multiple passes.
- Optimization
Stats - Parallel
Group - A group of nodes that can execute in parallel
- Parallelization
Analysis - Analysis result containing parallel execution opportunities
- Pattern
Analysis - Pattern detection results.
- Pattern
Match - Result of a successful pattern match
- Pattern
Matcher - Pattern matcher for graphs
- Pattern
Rewrite Stats - Statistics about pattern matching and rewriting
- Pipeline
Config - Configuration for the optimization pipeline.
- Predicate
Signature - Signature for a predicate: defines expected argument types
- Probability
Interval - Probability interval representing imprecise probabilities.
- Profile
Guided Optimizer - Profile-guided optimizer.
- Provenance
- Provenance information tracking the origin of an IR node
- Rewrite
Config - Configuration for the advanced rewrite system.
- Rewrite
Rule - A rewrite rule that transforms expressions matching a pattern into a template.
- Rewrite
Stats - Statistics about a rewriting session.
- Rewrite
System - A collection of rewrite rules that can be applied to expressions.
- Signature
Registry - Registry of predicate signatures
- Singleton
Fuzzy Set - Singleton fuzzy set for discrete inputs (common in fuzzy control).
- Source
Location - Source code location information
- Source
Span - Span information covering a range in source code
- Strategy
Selector - Strategy selector that recommends optimization configurations.
- Stride
Pattern - Stride pattern for a tensor.
- Strongly
Connected Component - A strongly connected component (SCC) in the computation graph.
- Temporal
Complexity - Temporal logic complexity metrics.
- Tensor
Layout - Layout configuration for a tensor.
- Tensor
Memory - Memory footprint estimate for a tensor
- Tensor
Stats - Tensor usage statistics.
- Tile
Config - Tiling configuration for a specific axis.
- Tiling
Result - Result of applying tiling transformations.
- Tiling
Strategy - Multi-dimensional tiling strategy.
- Torch
Script Export Options - Export options for TorchScript format.
- Type
Annotation - Type annotation for a term.
- Validation
Error - Validation error with severity and context.
- Validation
Report - Result of graph validation with detailed diagnostics.
- Validation
Warning - Validation warning (non-fatal issue).
Enums§
- ACOperator
- Operators that are associative and commutative.
- Aggregate
Op - Aggregation operation type.
- Defuzzification
Method - Defuzzification method selection.
- Distributive
Strategy - Strategy for applying distributive laws.
- Domain
Type - Type category of a domain.
- Fuzzy
Implication Kind - Fuzzy implication operator kinds.
- Fuzzy
Negation Kind - Fuzzy negation kinds.
- Graph
Pattern - A pattern that can match against graph structures
- IrError
- Isomorphism
Result - Graph isomorphism result.
- Layout
Strategy - Memory layout strategy for tensors.
- Modal
System - Modal logic axiom systems.
- OpType
- Operation types supported in the tensor graph
- Optimization
Hint - Optimization hint derived from profiling.
- Optimization
Level - Optimization level controlling aggressiveness of optimizations.
- Optimization
Pass - A single optimization pass.
- Pattern
- A pattern that can match against expressions.
- Rewrite
Strategy - Rewriting strategy controlling traversal order and application.
- Rule
Priority - Priority level for rewrite rules.
- Scheduling
Objective - Scheduling objective to optimize for
- TCoNorm
Kind - T-conorm (triangular conorm) kinds for fuzzy OR operations. A t-conorm is the dual of a t-norm: S(a,b) = 1 - T(1-a, 1-b)
- TLExpr
- TNorm
Kind - T-norm (triangular norm) kinds for fuzzy AND operations. A t-norm is a binary operation T: [0,1] × [0,1] → [0,1] that is:
- Temporal
Class - Classification of temporal formulas.
- Temporal
Pattern - Temporal pattern types.
- Term
- Validation
Error Kind - Types of validation errors.
- Validation
Warning Kind - Types of validation warnings.
Functions§
- ac_
equivalent - Check if two expressions are AC-equivalent.
- algebraic_
simplify - analyze_
constants - Analyze constant propagation opportunities in a graph.
- analyze_
inplace_ opportunities - Estimate memory savings from in-place operations
- analyze_
memory - Analyze memory usage patterns in a computation graph
- analyze_
parallelization - Analyze graph for parallel execution opportunities
- apply_
advanced_ ltl_ equivalences - Apply additional LTL equivalences beyond basic optimizations.
- apply_
axiom_ k - Apply axiom K to simplify modal expressions.
- apply_
axiom_ t - Apply axiom T to simplify modal expressions.
- apply_
constant_ folding - Apply constant folding transformations to a graph.
- apply_
distributive_ laws - Apply distributive laws to an expression.
- apply_
layouts - Apply the recommended layouts to a graph.
- apply_
modal_ equivalences - Apply modal logic equivalences to simplify an expression.
- apply_
multilevel_ tiling - Apply multi-level tiling (L1, L2, L3 cache hierarchy).
- apply_
register_ tiling - Apply register-level tiling for maximum register reuse.
- apply_
temporal_ equivalences - Apply temporal logic equivalences to simplify an expression.
- apply_
tiling - Apply loop tiling to einsum operations in the graph.
- are_
graphs_ equivalent - Check if two graphs are canonically equivalent.
- are_
isomorphic - Check if two graphs are isomorphic.
- are_
joinable - Check if two expressions can be joined under a rewrite system.
- auto_
annotate_ costs - Auto-annotate a graph with estimated costs.
- auto_
optimize - Convenience function to automatically select and apply the best optimization strategy.
- bisector
- Bisector of Area defuzzification.
- canonical_
hash - Compute a hash of a graph in canonical form.
- canonicalize_
graph - Canonicalize a computation graph.
- centroid
- Centroid (Center of Area) defuzzification.
- classify_
temporal_ formula - Classify a temporal formula.
- compute_
temporal_ complexity - Compute temporal complexity metrics for a formula.
- compute_
tight_ bounds - Compute tightest probability bounds for an expression using optimization.
- constant_
fold - Constant folding: evaluate constant expressions at compile time
- critical_
path_ analysis - Find the critical path in the computation graph.
- decompose_
safety_ liveness - Convert temporal formula to safety-progress decomposition.
- defuzzify
- Defuzzify a fuzzy set using the specified method.
- eliminate_
common_ subexpressions - Common Subexpression Elimination (CSE) - detects and deduplicates identical subgraphs
- eliminate_
dead_ code - Dead Code Elimination (DCE) - removes unused tensors and nodes
- estimate_
graph_ cost - Estimate the total cost of a graph given per-node costs.
- estimate_
operation_ cost - Estimate the cost of a graph operation.
- export_
to_ dot - Export an
EinsumGraphto DOT format. - export_
to_ dot_ with_ options - Export an
EinsumGraphto DOT format with custom options. - export_
to_ onnx_ text - Export EinsumGraph to ONNX text representation.
- export_
to_ onnx_ text_ with_ options - Export EinsumGraph to ONNX text representation with custom options.
- export_
to_ torchscript_ text - Export EinsumGraph to TorchScript text representation.
- export_
to_ torchscript_ text_ with_ options - Export EinsumGraph to TorchScript text representation with custom options.
- extract_
modal_ subformulas - Extract all modal subformulas from an expression.
- extract_
probabilistic_ semantics - Extract probabilistic semantics from weighted rules.
- extract_
state_ predicates - Extract state predicates (non-temporal atomic propositions).
- extract_
temporal_ subformulas - Extract all temporal subformulas from an expression.
- find_
all_ paths - Find all paths between two tensors.
- find_
cycles - Find all cycles in the computation graph.
- find_
layout_ fusion_ opportunities - Find opportunities for layout fusion (avoiding layout conversions).
- flatten_
ac - Flatten an AC expression into a list of operands.
- fold_
constants_ aggressive - Perform aggressive constant folding with all available optimizations.
- fuse_
all - Apply all fusion optimizations to a graph
- fuse_
einsum_ operations - Fuse einsum operations when possible
- fuse_
elementwise_ operations - Fuse element-wise operations that operate on the same tensors
- fuse_
map_ reduce - Fuse reduction operations with their producers when possible
- graph_
diameter - Compute graph diameter (longest shortest path).
- identify_
constant_ subgraphs - Identify constant subgraphs that can be pre-computed.
- identify_
temporal_ pattern - Identify temporal pattern in a formula.
- is_cnf
- Check if an expression is in Conjunctive Normal Form (CNF).
- is_dag
- Check if a graph is a directed acyclic graph (DAG).
- is_dnf
- Check if an expression is in Disjunctive Normal Form (DNF).
- is_
modal_ free - Check if an expression is modal-free (contains no modal operators).
- is_
temporal - Check if an expression contains temporal operators.
- is_
temporal_ nnf - Check if formula is in negation normal form for temporal operators.
- is_
theorem_ in_ system - Verify that an expression is a theorem in a given modal system.
- largest_
of_ maximum - Largest of Maximum defuzzification.
- mean_
of_ maximum - Mean of Maximum defuzzification.
- mln_
probability - Compute probability of an expression under a Markov Logic Network (MLN) semantics.
- modal_
depth - Calculate the modal depth of an expression.
- normalize
- Compute normal form of an expression (if it exists).
- normalize_
ac - Normalize an AC expression by sorting operands.
- normalize_
s5 - Normalize a modal expression according to S5 axioms.
- optimize_
expr - Apply multiple optimization passes in sequence
- optimize_
graph - Apply all optimization passes to the graph
- optimize_
layouts - Optimize tensor layouts for a graph.
- partition_
independent_ subgraphs - Partition graph into independent subgraphs for parallel execution
- propagate_
constants - propagate_
probabilities - Propagate probability intervals through a logical expression.
- recommend_
tiling_ strategy - Analyze a graph and recommend optimal tiling strategies.
- simplify_
identity_ operations - Simplify identity operations (operations that don’t transform their input)
- smallest_
of_ maximum - Smallest of Maximum defuzzification.
- strongly_
connected_ components - Find all strongly connected components using Tarjan’s algorithm.
- to_cnf
- Convert an expression to Conjunctive Normal Form (CNF).
- to_dnf
- Convert an expression to Disjunctive Normal Form (DNF).
- to_nnf
- Convert an expression to Negation Normal Form (NNF).
- topological_
sort - Perform topological sort on the computation graph.
- validate_
graph - Validate an
EinsumGraphwith comprehensive checks. - verify_
axiom_ 4 - Verify that an expression conforms to Axiom 4: □p → □□p
- verify_
axiom_ 5 - Verify that an expression conforms to Axiom 5: ◇p → □◇p
- verify_
axiom_ b - Verify that an expression conforms to Axiom B: p → □◇p
- verify_
axiom_ d - Verify that an expression conforms to Axiom D: □p → ◇p
- verify_
axiom_ k - Verify that an expression conforms to Axiom K: □(p → q) → (□p → □q)
- verify_
axiom_ t - Verify that an expression conforms to Axiom T: □p → p
- weighted_
average - Weighted Average defuzzification for singleton fuzzy sets.