Leanpub Header

Skip to main content

Bulletproof Knowledge

Building Hallucination-Resistant Neurosymbolic Agents with Lean and Logic Programming

Bulletproof Knowledge

> No proof, no entry. No proof, no answer. Alternatives: > Your agent's memory believes two impossible things at once. Make it refuse the second one. > The LLM proposes. Lean decides. > A hallucination in chat is said once. A hallucination in memory is said forever.

Minimum price

$30.00

$45.00

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

An agent with a memory has a new way to be wrong. A chatbot that hallucinates says something false once. An agent that hallucinates into its memory says it again tomorrow, builds on it, and answers every later question on top of a contradiction nothing in the pipeline noticed. Retrieval cannot tell which of two contradictory notes is right, and asking a second model "is this consistent?" only moves the hallucination from the fact to the verdict.

This book builds the missing piece: a small, fixed checker that decides what an agent may remember and what it may claim. A fact enters memory only with a proof that the memory stays consistent, and an answer leaves it only with a proof from what is stored. The agent's ontology of people, places, organizations and events is written as types in Lean 4, its rules about the world as propositions, and every accepted fact carries a certificate checked by Lean's kernel. Rules and search come from λProlog, running inside Lean through lean-elpi, and when a search finds an answer, the kernel checks that too. The LLM proposes, Lean decides, and a rejection goes back to the model with the name of the broken rule.

The promise is narrow on purpose. A checker guarantees consistency with your axioms, not truth about the world, and every chapter says what its technique does not give you. You do not need to know Lean, Prolog, dependent types or formal logic, and you do not need to be deep in mathematics: both languages are taught from zero, with code first, then a picture, then notation. Every snippet is cut from a public repository that compiles, and every output and error message in the book is the one Lean actually prints.

What you will build

  • A typed ontology for agent memory in which an ill-typed fact does not compile
  • A certified store whose insert returns a new store with a new proof, or names the axiom that blocks the fact
  • Hierarchies of places and events with type classes, subtypes and coercions
  • Proofs about the ontology itself: invariants, safe updates and schema migrations
  • Rules over the memory in λProlog, hypothetical questions that leave no trace, and search results turned into kernel-checked certificates
  • An agent, written entirely in Lean, that turns away six kinds of hallucination, feeds each rejection back to the LLM, and answers only with certificates

Who it is for

Engineers who build AI agents: you know Python, you have built with LLMs, and you know roughly what RAG and a knowledge graph are. Logicians and type theorists will find familiar ideas applied to an unfamiliar problem, with reading lists to the deeper sources.

What you get

12 chapters in three parts, 33 diagrams, exercises with compiled solutions, a 60-term glossary, and the Apache-2.0 companion code at github.com/Volland/bulletproof-knowledge: Lean 4 v4.35.0 and lean-elpi v0.3.3, no Mathlib, one lake build.


Author

About the Author

Volodymyr Pavlyshyn

Hey I am Volodymyr 

Seasoned Developer's Journey from COBOL to Web 3.0, SSI, Privacy First Edge AI, and Beyond

 As a seasoned developer with over 20 years of experience, I have worked with various programming languages, including some that are considered "dead," such as COBOL and Smalltalk. However, my passion for innovation and embracing cutting-edge technology has led me to focus on the emerging fields of Web 5.0, Self-Sovereign Identity (SSI),AI Agents, Knowledge Graphs, Agentiic memory systems, and the architecture of a decentralized world that empowers data democratization.

A firm believer in the potential of agent systems and the concept of a "soft" internet, I am dedicated to exploring and promoting these transformative ideas. In addition to writing, I also enjoy sharing my knowledge and insights through videoblogging. Most of my Medium posts serve as supplementary content to the videos on my YouTube channel, which you can explore here: https://www.youtube.com/c/VolodymyrPavlyshyn. 

Join me on this exciting journey as we delve into the future of technology and the possibilities it holds.

Contents

Table of Contents

Preface

  1. Why This Book Exists
  2. Who This Book Is For
  3. How This Book Came to Be
  4. What You Will Be Able to Do

How to Read This Book

  1. The Three Parts
  2. If You Are in a Hurry
  3. Setting Up the Code
  4. How the Snippets Relate to the Repository
  5. Asides
  6. The Running Example
  7. Reading Lean in Ten Minutes
  8. Typographic Conventions

Neurosymbolic AI and the Case for Provable Reasoning

  1. What Neurosymbolic AI Means
  2. Why LLMs Hallucinate
  3. Why Agents Need Reasoning
  4. Why Reasoning Must Be Provable
  5. Logic as the Symbolic Layer
  6. Data That Carries Proofs
  7. What This Does Not Give Us
  8. Summary
  9. Further Reading

Higher-Order Logic and Logic Programming for Agents

  1. The Ladder of Logics
  2. Why First-Order Is Not Enough for Agents
  3. Logic Programming: Search as Proof
  4. From Prolog to λProlog
  5. Where This Book Sits Among Neurosymbolic Systems
  6. What This Does Not Give Us
  7. Summary
  8. Further Reading

Knowledge Graphs, Metagraphs and Higher-Order Structure

  1. The Idea in Plain Words
  2. Two Ways to Draw a Memory
  3. Where Flat Graphs Stop, and the Ways Out
  4. A Metagraph in Lean
  5. Asking the Metagraph: What Holds, and Who Says So
  6. Following Chains: Reachability
  7. What This Does Not Give Us
  8. Summary
  9. Further Reading

Ontologies as Types

  1. The Idea in Plain Words
  2. What an Ontology Is
  3. The Core Move: Classes Are Types, Individuals Are Terms
  4. Propositions as Types
  5. Subclasses as Subtypes
  6. Working with an Ontologist
  7. What This Does Not Give Us
  8. Summary
  9. Further Reading

Closed and Open Worlds

  1. Two Answers to One Question
  2. Why the Closed World Suits Agent Memory
  3. The Closed World in Lean
  4. The Open World in Lean, for Contrast
  5. Where the Open World Still Matters
  6. The Boundary Pattern
  7. What This Does Not Give Us
  8. Summary
  9. Further Reading

Lean 4 Essentials: Types, Propositions and Proofs

  1. First Steps: A Scratch File, Values, and Functions
  2. Data: Inductive Types, Structures, and Matching
  3. Dependent Types: When Types Mention Values
  4. Propositions and Proofs
  5. Tactics and the Goal View
  6. Reading Errors and Knowing the Limits
  7. Summary
  8. Exercises
  9. Further Reading

Certified Ontologies in Lean

  1. From Pydantic Models to Dependent Types
  2. The Parts of an Ontology in Lean
  3. Axioms Over the Whole Store
  4. The Certified Store
  5. Extending the Ontology
  6. What This Does Not Give Us
  7. Summary
  8. Exercises
  9. Further Reading

Hierarchies with Type Classes

  1. Subtypes and Coercions
  2. Type Classes as Class Hierarchies
  3. The Place Hierarchy
  4. An Event Taxonomy
  5. Choosing a Construct
  6. Pitfalls and Limits
  7. Summary
  8. Exercises
  9. Further Reading

Proving the Ontology

  1. Two Kinds of Proof
  2. Invariants of the Certified Store
  3. Proving Updates
  4. Schema Evolution
  5. Fighting Hallucinations with Proofs
  6. Trust Hygiene
  7. Summary
  8. Exercises
  9. Further Reading

lean-elpi: Logic Programming Inside Lean

  1. Lean Checks, λProlog Searches
  2. λProlog in Twenty Minutes
  3. The Type Bridge
  4. Rules over the Agent’s Memory
  5. Calling ELPI from Lean
  6. What This Does Not Give Us
  7. Summary
  8. Exercises
  9. Further Reading

Beyond First-Order Logic

  1. Relations as Arguments
  2. Binders as Data: λ-Terms and HOAS
  3. Hypothetical Queries with pi and =>
  4. Reasoning over the Metagraph
  5. Certificates: From Whiteboard to Ledger
  6. What This Does Not Give Us
  7. Summary
  8. Exercises
  9. Further Reading

A Hallucination-Resistant Agent

  1. The Write Loop
  2. The Read Loop
  3. The Loop, Replayed
  4. Measuring Hallucination Resistance
  5. Fitting into Your Agent Stack
  6. Limits and Where to Go Next
  7. Summary
  8. Exercises
  9. Further Reading

Glossary

Bibliography

About the Author

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub