nanoGentzen: Neural-Guided Automated Theorem Prover for Intuitionistic Logic
NOTE: Use v2 model
nanoGentzen is a compact, high-efficiency neural policy-value transformer model trained to guide Gentzen Sequent Calculus (LI) backward proof search.
The model evaluates intuitionistic logical sequents Ξ β’ Ξ (|Ξ| β€ 1), predicting which deduction rule to apply, which premise antecedent to select, and estimating the sound provability value of the branch in [0, 1].
Complete training code is available on GitHub
Streamlit App for 100% local testing is available on GitHub nanoGenzen_GUI
Model Architecture & Hardware Profile
| Hyperparameter / Metric | Value | Description |
|---|---|---|
| Parameters | 4,861,185 (~4.86M) | Lightweight & ultra-fast inference |
Layers (n_layer) |
6 | Bidirectional Transformer blocks |
Attention Heads (n_head) |
8 | Multi-head self-attention |
Hidden Size (n_embd) |
256 | Dense token & graph representation |
Block Size (block_size) |
256 | Max sequence context |
Rule Space (num_rules) |
11 | Complete discrete propositional LI action space |
Pivot Space (max_antecedents) |
16 | Antecedent premise index targeting |
| Training Hardware | 1Γ NVIDIA GeForce RTX 4090 (24 GB) | Native bfloat16 mixed precision |
| Peak VRAM Usage | 11.0 GB / 24.0 GB | High-throughput training with batch size 256 |
| Total Training Time | 30.5 minutes (20 epochs @ ~91.5s/epoch) | Cosine learning rate decay with linear warmup |
| Weights Format | safetensors & .pt |
Zero-copy, secure tensor serialization |
Training Dynamics Overview (20 Epochs on 200k Certified Transitions):
- Multi-Task Loss Convergence (Left): Joint optimization of Rule Policy (Cross-Entropy), Pivot Selection (Cross-Entropy), and Branch Provability Value (MSE). Shows monotonic convergence (Train: 1.042 β 0.575, Val: 0.861 β 0.655) with zero validation divergence across complex multi-branch proof states.
- Rule Action Top-1 Accuracy (Right): Discrete Gentzen rule classification rapidly climbs in epochs 1β4 and stabilizes at ~80.5% validation accuracy (82.6% train), providing over 98% Top-3 action coverage to effectively guide heuristic beam pruning during AND-OR proof search.
System 2 Engine Deep Dive: kernel.py & search.py
nanoGentzen strictly separates heuristic action ranking (neural) from logical verification (symbolic). The neural model proposes search steps; the kernel validates each transition and guarantees 100% soundness.
[ Sequent: Ξ β’ Ξ ]
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β search.py: Neural Proof Search Controller β
β 1. Tokenizes sequent and queries Policy-Value Transformer β
β 2. Ranks candidate Gentzen rules by probability β
β 3. Prunes structurally invalid rules (e.g., R_IMP without β) β
βββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β Candidate Rule & Pivot Index
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β kernel.py: Deterministic Gentzen Kernel β
β 1. apply_rule(seq, rule, idx): Decomposes goal into subgoalsβ
β 2. is_axiom(seq): Checks Identity (A β’ A) or Ex Falso β
β 3. verify_proof_tree(tree): 100% Soundness Certification β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1. kernel.py β Deterministic Gentzen LI Kernel
The symbolic foundation of the prover. Implements propositional Abstract Syntax Trees (AST), sequent manipulation, and inverse Gentzen rule decompositions.
Formula(Base AST Node): Immutable base class implemented byVar(atomic propositions),Not(~A),And(A & B),Or(A | B), andImp(A => B).Sequent(gamma: Tuple, delta: Tuple): Represents the Gentzen judgmentΞ β’ Ξ. The single-succedent restriction (|Ξ| β€ 1) enforces constructive Intuitionistic Logic (LI).Sequent.is_axiom(): Base-case check. ReturnsTrueif any antecedent matches the succedent (Ξ, A β’ A, Identity Axiom) or if Falsum appears in the premises (0, Ξ β’ Ξ, Ex Falso Quodlibet).apply_rule(seq, rule, idx=0): Backward rule applicator. Decomposes a sequent into required premises:Right Rules (
R_IMP,R_AND,R_OR_1,R_OR_2,R_NOT) decompose the succedent formula in Ξ.Left Rules (
L_IMP,L_AND,L_OR,L_NOT) decompose the antecedent premiseΞ[idx].Contraction (
L_CONTR) duplicates hypothesisΞ[idx]for multi-premise Glivenko classical proofs.verify_proof_tree(tree): Standalone proof checker. Recursively verifies that every leaf node terminates inAXIOMand all branch reductions are valid.
2. search.py β Neural-Guided AND-OR Tree Search
Combines the policy-value network predictions with search budgeting, cycle detection, and memoization.
rank_actions(seq): Runs neural inference to rank the 11 Gentzen rules (rule_logits) and 16 antecedent pivots (pivot_logits). Filters out rules whose target connectives are missing (e.g., skipsR_NOTif Ξ is not a negation).prove(seq, depth, max_depth, budget, memo, path_visited):
Axiom Check: Immediate base-case termination via
seq.is_axiom().Cycle Guard: Tracks current path in
path_visitedto prevent infinite contraction loops.Transposition Memoization: Caches intermediate results in
memoto avoid redundant sub-proof searches.AND-Branching: Recursively ensures that all sub-goals of branching rules (
R_AND,L_OR,L_IMP) close before declaring the step proven.
| Search Phase | Function / Module | Operational Role | Output |
|---|---|---|---|
| 1. Action Proposal | search.py:rank_actions() |
Neural inference + structural connective filter | Prioritized (rule, pivot) list |
| 2. Reduction | kernel.py:apply_rule() |
Backward sequent decomposition | Child sub-goal sequents |
| 3. AND-OR Search | search.py:prove() |
Recursive branch evaluation + cycle guard | Proof tree dictionary |
| 4. Certification | kernel.py:verify_proof_tree() |
Independent mathematical soundness verification | Soundness status (True/False) |
Training Convergence & Metrics
The model was trained on 200,000 certified Gentzen backward derivation transitions synthesized across multi-depth propositional logic formulas and structural constructive schemas.
Multi-Task Loss Convergence: Steady, simultaneous decline in training (1.042 β 0.575) and validation loss (0.861 β 0.655) confirming zero overfitting across complex multi-branch sequents.
Top-1 Action Accuracy: Reached 82.5% on deterministic target paths, providing >98% Top-3 coverage for neural heuristic beam ranking.
Empirical Benchmark & Validation Results
Evaluated on a balanced test distribution of certified constructive theorems, classical fallacies, and unprovable sequents:
| Metric | Result | Benchmark Target |
|---|---|---|
| Overall Accuracy | 99.00% | > 90.0% |
| Soundness (Precision) | 100.00% (0 Hallucinations) | 100.0% (Kernel Guarantee) |
| Completeness (Recall) | 98.00% (49/50 Provable Goals) | > 90.0% |
| Average Proof Latency | 9.97 ms / proof | < 25.0 ms |
| Kernel Soundness Verification | 49 / 49 (100% Sound Trees) | 100.0% |
Constructive Benchmark Suite
| Proposition / Theorem | Sequent | Status |
|---|---|---|
| Identity | P β’ P |
PROVEN |
| Modus Ponens | P, (P β Q) β’ Q |
PROVEN |
| Conjunction Intro | P, Q β’ (P β§ Q) |
PROVEN |
| Transitivity | (P β Q), (Q β R) β’ (P β R) |
PROVEN |
| Constructive De Morgan | Β¬(P β¨ Q) β’ (Β¬P β§ Β¬Q) |
PROVEN |
| Contraction Double Negation | ⒠¬¬(¬¬P β P) |
PROVEN (Requires Contr_L) |
| Law of Excluded Middle | β’ (P β¨ Β¬P) |
REFUTED (Invalid in Intuitionistic Logic LI) |
Action Space & Supported Gentzen Rules (LI)
| Rule ID | Rule Symbol | Name | Description |
|---|---|---|---|
0 |
AXIOM |
Identity Axiom | Ξ, A β’ A |
1 |
R_IMP |
Right Implication (β_R) | Ξ β’ (A β B) βΉ A, Ξ β’ B |
2 |
L_IMP |
Left Implication (β_L) | (A β B), Ξ β’ Ξ βΉ Ξ β’ A and B, Ξ β’ Ξ |
3 |
R_AND |
Right Conjunction (β§_R) | Ξ β’ (A β§ B) βΉ Ξ β’ A and Ξ β’ B |
4 |
L_AND |
Left Conjunction (β§_L) | (A β§ B), Ξ β’ Ξ βΉ A, B, Ξ β’ Ξ |
5 |
R_OR_1 |
Right Disjunction 1 (β¨_R1) | Ξ β’ (A β¨ B) βΉ Ξ β’ A |
6 |
R_OR_2 |
Right Disjunction 2 (β¨_R2) | Ξ β’ (A β¨ B) βΉ Ξ β’ B |
7 |
L_OR |
Left Disjunction (β¨_L) | (A β¨ B), Ξ β’ Ξ βΉ A, Ξ β’ Ξ and B, Ξ β’ Ξ |
8 |
R_NOT |
Right Negation (Β¬_R) | Ξ β’ Β¬A βΉ A, Ξ β’ 0 |
9 |
L_NOT |
Left Negation (Β¬_L) | Β¬A, Ξ β’ Ξ βΉ Ξ β’ A |
10 |
L_CONTR |
Left Contraction (Contr_L) | Duplicate hypothesis for multi-use premises |
|
Quickstart & Usage
1. Installation
pip install torch safetensors huggingface_hub transformers
2. Loading with Hugging Face from_pretrained
import torch
from transformers import AutoModel, AutoTokenizer
from kernel import Sequent, Imp, And, Var, verify_proof_tree
from search import NeuralProofSearch
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load model and tokenizer from Hugging Face Hub
model = AutoModel.from_pretrained("Sagicc/nanoGentzen", trust_remote_code=True).to(device)
tokenizer = AutoTokenizer.from_pretrained("Sagicc/nanoGentzen", trust_remote_code=True)
# Initialize neural proof searcher
searcher = NeuralProofSearch(model, tokenizer, device=device)
# Define a goal: Modus Ponens [ P, (P => Q) |- Q ]
P, Q = Var("P"), Var("Q")
goal = Sequent((P, Imp(P, Q)), (Q,))
# Search for certified Gentzen proof tree
proof_tree = searcher.prove(goal, max_depth=8)
if proof_tree:
print(f"Goal proved successfully: {goal.to_str()}")
print(f"Kernel Certified Sound: {verify_proof_tree(proof_tree)}")
print(f"Proof Tree: {proof_tree}")
3. Direct Loading via PyTorch / Safetensors
import json
import torch
from safetensors.torch import load_file
from configuration_nanogentzen import GentzenConfig
from modeling_nanogentzen import GentzenPolicyValueModel
from tokenization_nanogentzen import LogicTokenizerHF
# Initialize Tokenizer & Model
tokenizer = LogicTokenizerHF(vocab_file="vocab.json")
with open("config.json", "r") as f:
cfg = json.load(f)
config = GentzenConfig(**{k: v for k, v in cfg.items() if k not in ["architectures", "auto_map", "model_type", "torch_dtype"]})
model = GentzenPolicyValueModel(config)
model.load_state_dict(load_file("model.safetensors"))
model.eval()
# Forward Inference
text = "P, (P => Q) |- Q"
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
rule_logits, pivot_logits, value, _ = model(inputs["input_ids"])
print("Predicted Rule ID:", rule_logits.argmax(dim=-1).item())
print("Provability Value:", value.item())
β οΈ Scope, Mathematical Guarantees & Known Limitations
1. Deductive Validity vs. Empirical Grounding
What nanoGentzen Guarantees (100% Soundness): nanoGentzen evaluates formal deductive validity. Given a set of premises Ξ, it mathematically guarantees that conclusion Ξ follows without structural fallacies (e.g., Affirming the Consequent, circular dependencies, or invalid contraposition) with a 0.00% hallucination rate.
What Requires External Domain Axioms (Physical & Empirical Facts): nanoGentzen is a formal logical reasoner, not an empirical physics simulator or arithmetic SMT solver.
Example (The Submerged Anchor Trap): If an LLM adopts a flawed physical premise (e.g., "an anchor inside a floating boat displaces its geometric volume rather than its mass"), the internal reasoning is structurally consistent, but the physical premise violates fluid dynamics.
Example (The Mirror Reflection Trap): If an LLM constructs an argument around a false coordinate transformation (
x -> -xinstead ofz -> -z), the deduction may be formally valid, but the optical model is factually inaccurate.
2. Propositional Logic vs. First-Order Arithmetic
nanoGentzen operates over the Propositional Gentzen Sequent Calculus (LI).
Quantified First-Order Logic (βx, βy) and continuous non-linear arithmetic inequalities (
xΒ² + yΒ² β€ rΒ²) are outside the discrete propositional action space and are best complemented with SMT solvers (such as Z3).
License
This project is released under the MIT License.