logicaffeine_language/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3//! # logicaffeine-language
4//!
5//! Natural language to first-order logic transpilation pipeline.
6//!
7//! This crate provides a complete system for parsing English sentences and
8//! producing formal logical representations in various notations.
9//!
10//! ## Quick Start
11//!
12//! ```rust
13//! use logicaffeine_language::compile;
14//!
15//! let fol = compile("Every philosopher is wise.").unwrap();
16//! assert!(fol.contains("∀"));
17//! ```
18//!
19//! ## Architecture
20//!
21//! The pipeline consists of several stages:
22//!
23//! 1. **Lexer** ([`lexer`]) - Tokenizes natural language input into a stream of
24//! [`Token`]s, handling vocabulary lookup and morphological analysis.
25//!
26//! 2. **Parser** ([`parser`]) - Constructs a logical AST with discourse tracking
27//! via Discourse Representation Structures ([`drs`]).
28//!
29//! 3. **Semantics** ([`semantics`]) - Applies axiom expansion, Kripke lowering
30//! for modal logic, and intensional readings.
31//!
32//! 4. **Transpiler** ([`transpile`]) - Renders the AST to Unicode, LaTeX, or
33//! ASCII first-order logic notation.
34//!
35//! ## Output Formats
36//!
37//! | Format | Example | Use Case |
38//! |--------|---------|----------|
39//! | Unicode | `∀x(P(x) → Q(x))` | Terminal display |
40//! | LaTeX | `\forall x(P(x) \to Q(x))` | Academic papers |
41//! | SimpleFOL | `Ax(P(x) -> Q(x))` | ASCII-only environments |
42//! | Kripke | Explicit world quantification | Modal logic analysis |
43//!
44//! ## Multi-Sentence Discourse
45//!
46//! For pronoun resolution and anaphora across sentences, use [`Session`]:
47//!
48//! ```rust
49//! use logicaffeine_language::Session;
50//!
51//! let mut session = Session::new();
52//! session.eval("A man walked in.").unwrap();
53//! session.eval("He sat down.").unwrap(); // "He" resolves to "a man"
54//! ```
55//!
56//! ## Ambiguity Handling
57//!
58//! Natural language is inherently ambiguous. The crate provides several
59//! strategies:
60//!
61//! - [`compile_forest`] - Returns all valid parse readings for lexical and
62//! structural ambiguity (noun/verb, PP attachment).
63//!
64//! - [`compile_all_scopes`] - Returns all quantifier scope permutations
65//! ("Every woman loves a man" → surface and inverse readings).
66//!
67//! ## Feature Flags
68//!
69//! - `dynamic-lexicon` - Enable runtime lexicon loading via the
70//! `runtime_lexicon` module (when enabled)
71
72// Re-export base types for internal use and consumers
73pub use logicaffeine_base::{Arena, Interner, Symbol, SymbolEq, Span as BaseSpan};
74
75// Provide an `intern` module alias for internal code that uses `crate::intern::*`
76pub mod intern {
77 pub use logicaffeine_base::{Interner, Symbol, SymbolEq};
78}
79
80// Provide an `arena` module alias for internal code that uses `crate::arena::*`
81pub mod arena {
82 pub use logicaffeine_base::Arena;
83}
84
85// Core modules
86pub mod token;
87pub mod lexer;
88pub mod lexicon;
89pub mod drs;
90pub mod error;
91
92// Parser and AST
93pub mod parser;
94pub mod ast;
95
96// Semantic analysis
97pub mod semantics;
98pub mod lambda;
99pub mod transpile;
100
101// Compile API
102pub mod compile;
103
104// Support modules
105pub mod analysis;
106pub mod arena_ctx;
107pub mod formatter;
108pub mod mwe;
109pub mod ontology;
110pub mod pragmatics;
111pub mod registry;
112pub mod scope;
113pub mod session;
114pub mod suggest;
115pub mod symbol_dict;
116pub mod view;
117pub mod visitor;
118pub mod debug;
119pub mod style;
120
121// Proof conversion: bridges language AST to proof engine
122pub mod proof_convert;
123pub use proof_convert::{logic_expr_to_proof_expr, term_to_proof_term};
124
125// Re-export key types at crate root
126pub use token::{BlockType, FocusKind, MeasureKind, PresupKind, Span, Token, TokenType};
127pub use lexer::{Lexer, LineLexer, LineToken};
128pub use parser::{Parser, ParserMode, NegativeScopeMode, QuantifierParsing};
129pub use error::{ParseError, ParseErrorKind, socratic_explanation};
130pub use drs::{Drs, BoxType, WorldState, Gender, Number, Case};
131pub use analysis::TypeRegistry;
132pub use registry::SymbolRegistry;
133pub use arena_ctx::AstContext;
134pub use session::Session;
135
136// Compile API re-exports
137pub use compile::{
138 compile, compile_simple, compile_kripke, compile_with_options,
139 compile_with_world_state, compile_with_world_state_options,
140 compile_with_discourse, compile_with_world_state_interner_options,
141 compile_all_scopes, compile_all_scopes_with_options,
142 compile_forest, compile_forest_with_options, MAX_FOREST_READINGS,
143 compile_discourse, compile_discourse_with_options,
144 compile_ambiguous, compile_ambiguous_with_options,
145 compile_theorem,
146};
147
148// Runtime lexicon re-export (when dynamic-lexicon feature is enabled)
149#[cfg(feature = "dynamic-lexicon")]
150pub use logicaffeine_lexicon::runtime as runtime_lexicon;
151
152// Output format configuration
153#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
154pub enum OutputFormat {
155 #[default]
156 Unicode,
157 LaTeX,
158 SimpleFOL,
159 /// Kripke semantics output: modals lowered to explicit world quantification.
160 Kripke,
161}
162
163// Transpile context
164pub struct TranspileContext<'a> {
165 pub registry: &'a mut SymbolRegistry,
166 pub interner: &'a Interner,
167}
168
169impl<'a> TranspileContext<'a> {
170 pub fn new(registry: &'a mut SymbolRegistry, interner: &'a Interner) -> Self {
171 TranspileContext { registry, interner }
172 }
173}
174
175#[derive(Debug, Clone, Copy)]
176pub struct CompileOptions {
177 pub format: OutputFormat,
178}
179
180impl Default for CompileOptions {
181 fn default() -> Self {
182 CompileOptions {
183 format: OutputFormat::Unicode,
184 }
185 }
186}