logicaffeine-language
Natural language to first-order logic transpilation pipeline.
This crate provides a complete system for parsing English sentences and producing formal logical representations in various notations. It handles quantifier scope, pronoun resolution, modal logic, and ambiguity in natural language.
Part of the Logicaffeine project.
Quick Start
use compile;
let fol = compile.unwrap;
// → "∀x(Philosopher(x) → Wise(x))"
let fol = compile.unwrap;
// → "Philosopher(socrates)"
Output Formats
The crate supports multiple output formats for different contexts:
| Format | Function | Example | Use Case |
|---|---|---|---|
| Unicode | compile() |
∀x(P(x) → Q(x)) |
Terminal, documentation |
| LaTeX | compile_with_options() |
\forall x(P(x) \to Q(x)) |
Academic papers |
| SimpleFOL | compile_simple() |
Ax(P(x) -> Q(x)) |
ASCII-only environments |
| Kripke | compile_kripke() |
Explicit world quantification | Modal logic analysis |
use ;
use ;
// ASCII output
let ascii = compile_simple.unwrap;
// → "Ax(Cat(x) -> Ez(Sleep(z) & Agent(z, x)))"
// LaTeX output
let opts = CompileOptions ;
let latex = compile_with_options.unwrap;
// Modal logic with explicit worlds
let kripke = compile_kripke.unwrap;
Multi-Sentence Discourse
For pronoun resolution and anaphora tracking across multiple sentences, use Session:
use Session;
let mut session = new;
session.eval.unwrap;
session.eval.unwrap; // "He" resolves to "a man"
// Access the accumulated logical form
let result = session.result;
For finer control over discourse state, use the lower-level API:
use ;
let mut world_state = new;
let mut interner = new;
let fol1 = compile_with_discourse.unwrap;
let fol2 = compile_with_discourse.unwrap;
Handling Ambiguity
Natural language is inherently ambiguous. This crate provides strategies for each type:
Structural & Lexical Ambiguity
compile_forest returns all valid parse readings for lexical ambiguity (noun/verb)
and structural ambiguity (PP attachment):
use compile_forest;
let readings = compile_forest.unwrap;
// Returns both: "saw using telescope" and "man has telescope"
Quantifier Scope Ambiguity
compile_all_scopes returns all quantifier scope permutations:
use compile_all_scopes;
let scopes = compile_all_scopes.unwrap;
// Surface scope: ∀x(Woman(x) → ∃y(Man(y) ∧ Loves(x, y)))
// Inverse scope: ∃y(Man(y) ∧ ∀x(Woman(x) → Loves(x, y)))
Combined Ambiguity
compile_ambiguous handles both structural and scope ambiguity together:
use compile_ambiguous;
let all_readings = compile_ambiguous.unwrap;
Core API Reference
Single Sentence
| Function | Description |
|---|---|
compile(input) |
Parse and transpile to Unicode FOL |
compile_simple(input) |
Parse and transpile to ASCII FOL |
compile_kripke(input) |
Modal logic with explicit world quantification |
compile_with_options(input, opts) |
Custom output format |
compile_theorem(input) |
Parse as a theorem for proof engine |
Ambiguity
| Function | Description |
|---|---|
compile_forest(input) |
All parse readings (lexical/structural) |
compile_all_scopes(input) |
All quantifier scope permutations |
compile_ambiguous(input) |
All readings × all scopes |
Discourse
| Function | Description |
|---|---|
compile_with_discourse(input, world, interner) |
Single sentence with DRS tracking |
compile_discourse(sentences) |
Multiple sentences with shared context |
Session::new() |
REPL-style incremental evaluation |
Architecture
The pipeline consists of four stages:
Input → Lexer → Parser → Semantics → Transpiler → FOL Output
-
Lexer (
lexer) - Tokenizes natural language input, handles vocabulary lookup and morphological analysis via the lexicon. -
Parser (
parser) - Constructs a logical AST with discourse tracking via Discourse Representation Structures (drs). Uses arena allocation for efficiency. -
Semantics (
semantics) - Applies axiom expansion, Kripke lowering for modal logic, and intensional readings. Neo-Davidsonian event semantics. -
Transpiler (
transpile) - Renders the AST to the target notation format.
Key Types
Token- Lexical tokens with spans and semantic featuresParser- Configurable recursive descent parserDrs/WorldState- Discourse representation for pronoun trackingSession- Stateful multi-sentence evaluationOutputFormat- Unicode, LaTeX, SimpleFOL, or Kripke
Feature Flags
[]
= "0.1"
# Or with dynamic lexicon loading
= { = "0.1", = ["dynamic-lexicon"] }
| Feature | Description |
|---|---|
dynamic-lexicon |
Runtime lexicon loading via runtime_lexicon module |
Dependencies
This crate builds on:
logicaffeine-base- Arena allocation, symbol interninglogicaffeine-lexicon- Vocabulary database with verb frames, noun features
Re-exported Types
For convenience, key types from dependencies are re-exported:
use ;
License
Business Source License 1.1 (BUSL-1.1)
- Free for individuals and organizations with <25 employees
- Commercial license required for organizations with 25+ employees offering Logic Services
- Converts to MIT on December 24, 2029
See LICENSE for full terms.