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...
Bought separately
$162
Minimum price
$80.00
$120
About the Bundle
About the Books
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
insert returns a new store with a new proof, or names the axiom that blocks the factWho 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.
Your agent's LLM is fluent, and sometimes it is wrong and sure. It "remembers" Anna and Lena meeting in Berlin while its own memory has Anna in Lisbon at the same hour. A prompt cannot tell that guess from a fact. A logic layer can, and it can say why. The LLM proposes. The logic decides.
This book teaches that logic from zero, the way The Little Schemer taught recursion: one question at a time. Most pages are short question-and-answer frames: cover the answer, think, then look. Fifteen Laws of Logic Agents sum up what the frames earn, from "A fact is a rule that needs nothing. A rule is a fact that waits." to "An agent that cannot say 'no' cannot be trusted with 'yes'."
The destination is higher-order logic programming in λProlog. The last book-length introduction to it, Dale Miller and Gopalan Nadathur's Programming with Higher-Order Logic (2012), is excellent and academic. Agents changed who needs it: typed predicates, binders, scoped assumptions, and rules about rules are what the logic layer of a neurosymbolic agent is made of. Here they run in lean-elpi, an implementation of the ELPI dialect of λProlog inside Lean 4, usable from the command line and Python.
What you will learn and build
pi and => for what-if questions that leave memory untouched; meta-interpreters that print a proof as an explanation; and constraint handling rules for goals that wait for information.Every chapter also says what its technique does not give you. A logic layer checks a fact against your rules. It cannot make a consistent invention true.
Who it is for
AI and agent engineers who know Python and LLMs and have never seen Prolog, logic notation, or the λ-calculus. No mathematics background is assumed: every idea arrives as code you can run, then a picture, then notation. Bulletproof Knowledge, the author's book on proofs in Lean, is a companion, not a prerequisite.
What you get
What eats a fly? Ask the pond.
A small pond: algae, snails and flies, frogs and fish, a duck, a heron and an otter. Eleven short lines say who eats whom. Type a question, eats X fly, and a program answers: the frog, then the fish. It does not search the web or guess. It answers only from what the lines say, and it tells you when they say nothing.
That is a logic program: a list of things that are true, and a way to ask what follows. This little book teaches the idea from zero, in the question-and-answer style of The Little Schemer. Each page is two columns, a short question on the left and a short answer on the right. Cover the answers with a card, try, then look. Now and then a box states a Maxim, a short rule the questions above it have earned, from "A fact says what is true. A question asks whether it follows." to "A typing rule is a clause. A type checker is a logic program, and it runs both ways."
The language is λProlog, a younger and stranger relative of Prolog. It adds three things: types, so the computer turns away nonsense before it runs; predicates you pass around like numbers; and two small words, pi and =>, that let a program say "for any new thing" and "suppose this were true". Those three additions are what higher-order logic programming means. With them, a type checker for a little programming language fits in seven short clauses, and the same seven clauses infer types they were never told. That is where the book ends.
What you will learn
:- as "if", recursion that knows when to stop, and the search tree behind every answer.append, run three ways: join, check and split. A relation has no inputs and outputs until you ask it something.map, filter and fold.Who it is for
Students and programmers who have never taken a logic course. You need some programming in any language, a pencil, and patience. No mathematics, no Prolog and no Lean are assumed. You do not need to know what ∀ means; by Chapter 8 you will have written it. A chapter is an evening's work.
What you get
Every example runs
The engine is lean-elpi, an implementation of the ELPI dialect of λProlog written in Lean 4. pip install leanelpi gives you the whole engine with nothing to compile, on macOS, Linux and Windows with WSL; a Docker image and a build from source work too. The book's site, volland.github.io/little-elpier, has a sample of the frames.
This book explores the intersection of formal logic, type theory, and knowledge representation, arguing for a fundamental shift from traditional RDF/OWL-based approaches to type-theoretic foundations for knowledge graphs and AI systems.
We stand at a critical juncture in knowledge representation. While RDF and OWL have served as foundational technologies for the semantic web, their limitations become increasingly apparent as we build more sophisticated AI systems requiring complex reasoning, contextual knowledge, and formal guarantees. This book presents a comprehensive exploration of how dependent type theory, higher-order logic, and advanced programming language concepts can revolutionize knowledge representation.
Drawing parallels between Russell's Paradox in set theory and current limitations in knowledge graphs, we demonstrate why moving beyond first-order logic is not just beneficial but necessary for the next generation of AI systems. The book combines theoretical foundations with practical implementations, showing how languages like Twelf, Beluga, ELPI, and TypeDB point toward a more expressive and reliable future for knowledge representation.
Foundational Concepts
Programming Languages and Systems
Knowledge Representation Evolution
Advanced Applications
This book is designed for:
Readers should have familiarity with:
No prior experience with dependent types or proof assistants is required, though familiarity with these concepts will enhance understanding.
Unlike traditional treatments of knowledge representation that focus on extending existing technologies, this book advocates for a fundamental paradigm shift. It bridges the gap between theoretical computer science (type theory, formal logic) and practical AI engineering, showing how advanced programming language concepts can solve real-world knowledge representation challenges.
The book emphasizes working implementations and practical examples rather than purely theoretical discussions, demonstrating how these advanced concepts can be applied to build more reliable, expressive, and verifiable knowledge systems.
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...
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
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
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.