tensorlogic_cli/
lib.rs

1//! TensorLogic CLI Library
2//!
3//! **Version**: 0.1.0-alpha.2 | **Status**: Production Ready
4//!
5//! This library provides programmatic access to the TensorLogic CLI functionality,
6//! allowing you to use the parser, executor, optimizer, and other components
7//! directly from Rust code without shelling out to the command-line interface.
8//!
9//! # Features
10//!
11//! - **Expression Parsing**: Parse logical expressions into TensorLogic IR
12//! - **Compilation**: Compile expressions to einsum graphs with various strategies
13//! - **Execution**: Execute compiled graphs with multiple backends
14//! - **Optimization**: Apply optimization passes to improve performance
15//! - **Analysis**: Analyze graph complexity and computational costs
16//! - **Benchmarking**: Measure compilation and execution performance
17//! - **Format Conversion**: Convert between different input/output formats
18//!
19//! # Quick Start
20//!
21//! ```rust,no_run
22//! use tensorlogic_cli::{parser, CompilationContext};
23//! use tensorlogic_compiler::{compile_to_einsum_with_context, CompilationConfig};
24//!
25//! // Parse an expression
26//! let expr = parser::parse_expression("pred1(x) AND pred2(x, y)").unwrap();
27//!
28//! // Create compiler context with configuration
29//! let config = CompilationConfig::soft_differentiable();
30//! let mut ctx = CompilationContext::with_config(config);
31//! ctx.add_domain("D", 100);
32//!
33//! // Compile to einsum graph
34//! let graph = compile_to_einsum_with_context(&expr, &mut ctx).unwrap();
35//!
36//! // Execute the graph (requires appropriate setup)
37//! // let result = executor::execute_graph(&graph, &backend_config).unwrap();
38//! ```
39//!
40//! # Module Overview
41//!
42//! - [`parser`]: Parse logical expressions from strings
43//! - [`executor`]: Execute compiled einsum graphs
44//! - [`optimize`]: Apply optimization passes
45//! - [`benchmark`]: Performance benchmarking utilities
46//! - [`analysis`]: Graph analysis and metrics
47//! - [`conversion`]: Format conversion utilities
48//! - `config`: Configuration management (internal)
49//! - [`output`]: Output formatting and colors
50//!
51//! # Library Mode Benefits
52//!
53//! Using TensorLogic as a library instead of a CLI provides:
54//!
55//! - **Better Performance**: No process spawning overhead
56//! - **Type Safety**: Compile-time error checking
57//! - **Integration**: Direct embedding in Rust applications
58//! - **Flexibility**: Fine-grained control over compilation and execution
59//! - **Testing**: Easier unit and integration testing
60//!
61//! # Example: Full Compilation Pipeline
62//!
63//! ```rust,no_run
64//! use tensorlogic_cli::{parser, analysis, CompilationContext};
65//! use tensorlogic_compiler::{compile_to_einsum_with_context, CompilationConfig};
66//!
67//! // Parse expression
68//! let expr = parser::parse_expression(
69//!     "EXISTS x IN Person. (pred1(x) AND pred2(x, y))"
70//! ).unwrap();
71//!
72//! // Setup compilation context
73//! let config = CompilationConfig::soft_differentiable();
74//! let mut ctx = CompilationContext::with_config(config);
75//! ctx.add_domain("Person", 100);
76//! ctx.add_domain("D", 100);
77//!
78//! // Compile
79//! let graph = compile_to_einsum_with_context(&expr, &mut ctx).unwrap();
80//!
81//! // Analyze complexity
82//! let metrics = analysis::GraphMetrics::analyze(&graph);
83//! println!("Graph has {} tensors and {} nodes", metrics.tensor_count, metrics.node_count);
84//! println!("Estimated FLOPs: {}", metrics.estimated_flops);
85//! ```
86
87// Re-export core TensorLogic types for convenience
88pub use tensorlogic_adapters;
89pub use tensorlogic_compiler;
90pub use tensorlogic_infer;
91pub use tensorlogic_ir;
92pub use tensorlogic_scirs_backend;
93
94// Export public modules
95pub mod analysis;
96pub mod benchmark;
97pub mod cache;
98pub mod conversion;
99pub mod error_suggestions;
100pub mod executor;
101pub mod ffi;
102pub mod macros;
103pub mod optimize;
104pub mod output;
105pub mod parser;
106pub mod simplify;
107pub mod snapshot;
108
109// Re-export config types (but keep internal config logic private)
110pub use config::{CacheConfig, Config, ReplConfig, WatchConfig};
111
112// Internal modules (not part of public API)
113#[allow(dead_code)]
114mod batch;
115#[allow(dead_code)]
116mod cli;
117pub(crate) mod completion;
118pub(crate) mod config;
119#[allow(dead_code)]
120pub(crate) mod profile;
121#[allow(dead_code)]
122pub(crate) mod repl;
123#[allow(dead_code)]
124pub(crate) mod watch;
125
126/// Type alias for compilation context used throughout the library
127pub type CompilationContext = tensorlogic_compiler::CompilerContext;
128
129/// Type alias for einsum graphs
130pub type EinsumGraph = tensorlogic_ir::EinsumGraph;
131
132/// Type alias for TensorLogic expressions
133pub type TLExpr = tensorlogic_ir::TLExpr;
134
135/// Library result type
136pub type Result<T> = anyhow::Result<T>;
137
138/// Library version
139pub const VERSION: &str = env!("CARGO_PKG_VERSION");
140
141/// Library name
142pub const NAME: &str = env!("CARGO_PKG_NAME");
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    #[test]
149    fn test_version_const() {
150        // VERSION is a compile-time constant from CARGO_PKG_VERSION
151        assert!(VERSION.contains('.'), "Version should be in semver format");
152        assert_eq!(NAME, "tensorlogic-cli");
153    }
154
155    #[test]
156    fn test_parse_and_compile() {
157        let expr = parser::parse_expression("pred(x, y)").unwrap();
158        assert!(matches!(expr, TLExpr::Pred { .. }));
159    }
160
161    #[test]
162    fn test_format_conversion() {
163        let expr = parser::parse_expression("AND(a, b)").unwrap();
164        let formatted = conversion::format_expression(&expr, false);
165        assert!(formatted.contains("AND"));
166    }
167}