Abstract
We present a unified framework for semantic correctness in large language models (LLMs) that synthesizes two complementary research threads: topological detection of hallucination via homotopy groups and homology, and type-theoretic generation via certified inhabitation search in Homotopy Type Theory (HoTT). Our central construction is the detection-generation duality: detection is the problem of checking whether a global section of a semantic sheaf exists, while generation is the problem of constructing such a section. We prove these operations form an adjoint pair Generate ⊣ Detect on the semantic category Sem, and that the composite T = Detect ∘ Generate is a monad on Sem—the semantic monad—whose unit maps semantic types to their verified versions and whose multiplication collapses redundant verification. We formalize the ambient mathematical universe as an ∞-topos of semantic types, where objects are homotopy types, morphisms are certified derivations, and the internal logic is HoTT itself. Within this framework, we prove the Completeness Theorem: the Hallucination–Homotopy Correspondence (HHC) together with type-theoretic generation yields a complete system in which every hallucination is detected and generation never produces undetected hallucinations. We provide a concrete proof-of-concept architecture wrapping an LLM with a type-checking layer implementing a generate-check-abstain loop, with persistent homology for real-time topological monitoring. The framework is formalized in Haskell.
Key Theorems
The functors Generate: Claim → Sem and Detect: Sem → Claim form an adjoint pair Generate ⊣ Detect. The unit η: id_Claim → Detect ∘ Generate maps each claim to its verified version; the counit ε: Generate ∘ Detect → id_Sem is the semantic projection. A claim ĉ is hallucinated if and only if the counit component ε_ĉ is not an isomorphism.
The combined HoTT hallucination framework is complete. The following are equivalent for any claim c: (i) c is non-hallucinated; (ii) c is a fixed point of the semantic monad T = Detect ∘ Generate; (iii) the HHC obstruction class [c] ∈ H¹(K•, F) vanishes; (iv) the generate-check-abstain loop converges to c. In particular, every hallucination is detected (Detection Completeness) and generation never produces undetected hallucinations (Generation Soundness).
The semantic universe Sem_∞ of all semantic types, equipped with the Grothendieck topology from the knowledge complex K•, satisfies all axioms of an ∞-topos: it has finite limits, is locally presentable, and satisfies descent. The internal logic of Sem_∞ is Homotopy Type Theory, making HoTT the correct ambient logic for reasoning about semantic correctness.
Five-by-Five Duality Table
Each hallucination type (Part I) has a dual preventing generation principle (Part II). This correspondence is only visible when all three papers are read together.
| Detection Criterion (Part I) | Generation Principle (Part II) | Prevention Mechanism |
|---|---|---|
| π₁ ≠ 0 (Circular reasoning) | P1: Type Inhabitation | Proof search cannot produce non-contractible loops |
| π₀ disconnection | P3: Abstention on ∅ | Inhabitation fails across semantic components; system abstains |
| π₂ ≠ 0 (Inconsistent justifications) | P2: Certified Derivations | Derivation tree makes justification structure explicit |
| Hₙ ≠ 0 (Fabricated chains) | P4: Context as Fibration | Fibered context prevents locally-consistent-but-globally-unfounded chains |
| Holonomy ≠ id (Compositional drift) | P5: Attention as Weighted Limit | Categorical attention detects non-trivial transport |
Download Full Paper
The complete 28-page paper in PDF format, including all proofs, Haskell formalizations, and appendices.
Download PDF (28 pages)