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
//! TLExpr → EinsumGraph compiler (planning only).
//!
//! **Version**: 0.1.0-rc.1 | **Status**: Production Ready
//!
//! This crate compiles logical expressions into tensor computation graphs
//! represented as einsum operations. It provides a bridge between symbolic
//! logic and numeric tensor computations.
//!
//! # Overview
//!
//! The tensorlogic-compiler translates high-level logical expressions (predicates,
//! quantifiers, implications) into low-level tensor operations that can be executed
//! efficiently on various backends (CPU, GPU, etc.).
//!
//! **Key Features:**
//! - Logic-to-tensor mapping with configurable strategies
//! - Type checking and scope analysis
//! - Optimization passes (negation, CSE, einsum optimization)
//! - Enhanced diagnostics with helpful error messages
//! - Support for arithmetic, comparison, and conditional expressions
//!
//! # Quick Start
//!
//! ```rust
//! use tensorlogic_compiler::{compile_to_einsum_with_context, CompilerContext};
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let mut ctx = CompilerContext::new();
//! ctx.add_domain("Person", 100);
//!
//! // Define a logic rule: ∃y. knows(x, y)
//! // "Find all persons x who know someone"
//! let rule = TLExpr::exists(
//! "y",
//! "Person",
//! TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
//! );
//!
//! // Compile to tensor operations
//! let graph = compile_to_einsum_with_context(&rule, &mut ctx).unwrap();
//! ```
//!
//! # Compilation Pipeline
//!
//! The compiler follows a multi-stage pipeline:
//!
//! 1. **Pre-compilation passes**:
//! - Scope analysis (detect unbound variables)
//! - Type checking (validate predicate arity and types)
//! - Expression optimization (negation optimization, CSE)
//!
//! 2. **Compilation**:
//! - Variable axis assignment
//! - Logic-to-tensor mapping (using configurable strategies)
//! - Einsum graph construction
//!
//! 3. **Post-compilation passes**:
//! - Dead code elimination
//! - Einsum operation merging
//! - Identity elimination
//!
//! # Modules
//!
//! - [`config`]: Compilation configuration and strategy selection
//! - [`optimize`]: Expression-level optimization passes
//! - [`passes`]: Analysis and validation passes
//!
//! # Examples
//!
//! ## Basic Predicate Compilation
//!
//! ```rust
//! use tensorlogic_compiler::compile_to_einsum;
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
//! let graph = compile_to_einsum(&expr).unwrap();
//! ```
//!
//! ## Compilation with Context
//!
//! ```rust
//! use tensorlogic_compiler::{compile_to_einsum_with_context, CompilerContext};
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let mut ctx = CompilerContext::new();
//! ctx.add_domain("Person", 100);
//!
//! let expr = TLExpr::exists(
//! "y",
//! "Person",
//! TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
//! );
//!
//! let graph = compile_to_einsum_with_context(&expr, &mut ctx).unwrap();
//! ```
//!
//! ## Using Optimization Passes
//!
//! ### Unified Pipeline (Recommended)
//!
//! ```rust
//! use tensorlogic_compiler::optimize::{OptimizationPipeline, PipelineConfig};
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let pipeline = OptimizationPipeline::new();
//! let expr = TLExpr::add(
//! TLExpr::mul(TLExpr::Constant(2.0), TLExpr::Constant(3.0)),
//! TLExpr::Constant(0.0)
//! );
//! let (optimized, stats) = pipeline.optimize(&expr);
//! println!("Applied {} optimizations", stats.total_optimizations());
//! ```
//!
//! ### Individual Passes
//!
//! ```rust
//! use tensorlogic_compiler::optimize::optimize_negations;
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let expr = TLExpr::negate(TLExpr::negate(
//! TLExpr::pred("p", vec![Term::var("x")])
//! ));
//!
//! let (optimized, stats) = optimize_negations(&expr);
//! assert_eq!(stats.double_negations_eliminated, 1);
//! ```
use Result;
use ;
pub use ;
pub use ;
pub use ;
// Re-export adapter types for convenience
pub use validate_arity;
pub use ;
use ;
/// Compile a TLExpr into an EinsumGraph with an empty context.
///
/// This is the simplest entry point for compilation. It creates a new
/// compiler context automatically and infers domains where possible.
///
/// # Example
///
/// ```
/// use tensorlogic_compiler::compile_to_einsum;
/// use tensorlogic_ir::{TLExpr, Term};
///
/// let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
/// let graph = compile_to_einsum(&expr).unwrap();
/// ```
/// Compile a TLExpr into an EinsumGraph with a custom compilation configuration.
///
/// This allows you to control how logical operations are compiled to tensor operations,
/// using different strategies for AND, OR, NOT, quantifiers, and other logic operators.
///
/// # Arguments
///
/// * `expr` - The logical expression to compile
/// * `config` - Compilation configuration specifying strategies
///
/// # Returns
///
/// An `EinsumGraph` representing the compiled tensor computation.
///
/// # Example
///
/// ```
/// use tensorlogic_compiler::{compile_to_einsum_with_config, CompilationConfig};
/// use tensorlogic_ir::{TLExpr, Term};
///
/// // Use Łukasiewicz fuzzy logic
/// let config = CompilationConfig::fuzzy_lukasiewicz();
/// let expr = TLExpr::and(
/// TLExpr::pred("P", vec![Term::var("x")]),
/// TLExpr::pred("Q", vec![Term::var("x")]),
/// );
/// let graph = compile_to_einsum_with_config(&expr, &config).unwrap();
///
/// // Use hard Boolean logic
/// let config = CompilationConfig::hard_boolean();
/// let graph = compile_to_einsum_with_config(&expr, &config).unwrap();
///
/// // Use probabilistic logic
/// let config = CompilationConfig::probabilistic();
/// let graph = compile_to_einsum_with_config(&expr, &config).unwrap();
/// ```
/// Compile a TLExpr into an EinsumGraph with an existing context.
///
/// Use this when you need fine-grained control over domains, variable bindings,
/// or when compiling multiple related expressions with shared context.
///
/// # Example
///
/// ```
/// use tensorlogic_compiler::{compile_to_einsum_with_context, CompilerContext};
/// use tensorlogic_ir::{TLExpr, Term};
///
/// let mut ctx = CompilerContext::new();
/// ctx.add_domain("Person", 100);
///
/// let expr = TLExpr::exists(
/// "y",
/// "Person",
/// TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
/// );
///
/// let graph = compile_to_einsum_with_context(&expr, &mut ctx).unwrap();
/// ```