Leanpub Header

Skip to main content

Logic for Agents: Building Neurosymbolic AI with Elpi

Logic for Agents: Building Neurosymbolic AI with Elpi

> Higher-order logic programming for AI engineers, taught the way *The Little Reasoner* taught recursion. > Your agent's LLM can guess. Can it tell a guess from a fact? > An agent that cannot say "no" cannot be trusted with "yes".

Minimum price

$35.00

$50.00

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

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

  • Part I, The Little Logician. What neurosymbolic AI is and where logic sits in an agent. First-order logic through facts, rules, and questions. Prolog and Datalog in a nutshell: unification, backtracking, search trees, negation as failure, and why Datalog always finishes.
  • Part II, The Seasoned Logician. λProlog with lean-elpi: types and modes that refuse an ill-formed fact before it loads; predicates as values; lambdas and binders, so queries become data the agent can store and pass; 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.
  • Part III, The Logic Layer. LF proof certificates checked by Lean's kernel. The logic layer in Python: propose, check, commit. An LLM that writes λProlog, guarded by the parser, the type checker, the mode checker, a fuel limit, and a consistency check. And a complete little agent that refuses the phantom, explains why, answers a what-if, and lets a rule call the LLM back, with the answer still checked.

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

  • 13 short chapters in three parts, about 330 print pages, 22 diagrams
  • Hundreds of question-and-answer frames and the fifteen Laws, collected on one page
  • Exercises graded ★ to ★★★ in every chapter from Chapter 2, with tested solutions
  • λProlog at a Glance (a two-page cheat sheet), a glossary, and a bibliography
  • The Apache-2.0 companion code at github.com/Volland/logic-for-agents, where CI runs every example and solution on lean-elpi v0.4. The agent uses the Anthropic SDK and replays recorded responses by default, so you need no API key.

Share this book

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. The Book That Was Missing
  2. Why a Little Book
  3. How This Book Came to Be
  4. Thanks

How to Read This Book

  1. Frames
  2. Laws, Interludes, and Asides
  3. What You Need
  4. Running a Program
  5. Coming from Prolog
  6. Conventions
  7. The Running Example

Part I: The Little Logician

The Little Agent

  1. The Phantom
  2. Interlude: Two Kinds of Mind
  3. Propose, Check, Commit
  4. A First Rule
  5. The Logic Ladder
  6. Interlude: Why Climb Higher
  7. What We Learned
  8. Further Reading

Facts, Rules, Questions

  1. Facts
  2. Questions
  3. Rules
  4. Interlude: Memory That Answers
  5. Rules That Call Themselves
  6. What First-Order Logic Cannot Say
  7. What We Learned
  8. Exercises
  9. Further Reading

Prolog and Datalog in a Nutshell

  1. Unification
  2. Backtracking and Search Trees
  3. Lists, Numbers, Cut, and Not
  4. The Question That Never Came Back
  5. Datalog in a Nutshell
  6. Interlude: Three Languages, One Family
  7. Interlude: Why Not Z3?
  8. What We Learned
  9. Exercises
  10. Further Reading

Part II: The Seasoned Logician

Types and Modes

  1. A Fact Upside Down
  2. Kinds, Types, and Predicates
  3. Lists, Pairs, and Options
  4. Modes: Who Supplies, Who Receives
  5. Interlude: A Type Checker at the Gate
  6. What Types Do Not Catch
  7. What We Learned
  8. Exercises
  9. Further Reading

Predicates as Values

  1. The Same Rule Three Times
  2. A Relation as an Argument
  3. The Library’s Higher-Order Predicates
  4. Relations from Relations
  5. Policies: Rules as Data
  6. Interlude: For All Relations
  7. What We Learned
  8. Exercises
  9. Further Reading

Lambdas and Binders

  1. A Term with a Hole
  2. Filling the Hole
  3. Claims with Holes
  4. When Is a Claim True?
  5. Substitution for Free
  6. Interlude: Why Agents Want Binders
  7. Taking Claims Apart
  8. What Binders Do Not Give Us
  9. What We Learned
  10. Exercises
  11. Further Reading

pi and =>

  1. One Goal, One Assumption
  2. What If Lena Had Joined the Kickoff?
  3. Interlude: Questions That Leave No Trace
  4. Checking Before Committing
  5. Fresh Names
  6. Assumed Rules Need Their Own pi
  7. Walking a Formula
  8. What pi and => Do Not Give Us
  9. What We Learned
  10. Exercises
  11. Further Reading

Rules about Rules

  1. Rules as Data
  2. The Little Interpreter
  3. Proof Trees
  4. Interlude: Explanations for People
  5. How Deep, How Many
  6. Rules That Make Rules
  7. Which Rules May Answer
  8. What Meta-Interpretation Costs
  9. What We Learned
  10. Exercises
  11. Further Reading

Waiting Goals

  1. Not No, Not Yet
  2. Delayed Goals
  3. What the Answer Waits For
  4. Interlude: Time from Language Models
  5. Rules for the Store
  6. A Contradiction the Moment It Appears
  7. No Bilocation as a Rule
  8. What CHR Gives and What It Costs
  9. What We Learned
  10. Exercises
  11. Further Reading

Part III: The Logic Layer

Proofs You Can Check

  1. Who Checks the Searcher?
  2. Judgments as Types
  3. Reading a Proof Term
  4. Interlude: Answers That Carry Their Reasons
  5. The Checker We Call
  6. Modes, Totality, and Strategy
  7. What a Certificate Does Not Give Us
  8. What We Learned
  9. Exercises
  10. Further Reading

The Logic Layer

  1. The Phantom, as JSON
  2. Interlude: Where the Logic Sits
  3. A Program from Python
  4. A Commit Is a New Program
  5. Rejections That Name the Rule
  6. From JSON to a Clause
  7. Logic That Calls Python
  8. Derive and Explain
  9. What the Logic Layer Does Not Give Us
  10. What We Learned
  11. Exercises
  12. Further Reading

The LLM Speaks λProlog

  1. Autoformalization
  2. The Signature Is the Contract
  3. Six Guards and a Commit
  4. One Mistake per Guard
  5. Feedback and a Second Try
  6. What a Guarded Call Costs
  7. What the Guards Do Not Catch
  8. What We Learned
  9. Exercises
  10. Further Reading

A Little Agent

  1. The Session, Turn by Turn
  2. Inside the Little Agent
  3. Logic Calls the LLM
  4. Answers That Show Their Work
  5. Measuring What the Logic Catches
  6. What the Logic Cannot Catch
  7. Where to Go Next
  8. The Phantom, One Last Time
  9. What We Learned
  10. Exercises
  11. Further Reading

The Laws of Logic Agents

Appendix B: λProlog at a Glance

  1. Declarations
  2. Clauses and goals
  3. Higher-order
  4. Waiting goals (Chapter 9)
  5. The standard library you meet in this book
  6. Running
  7. From the Wire to the Guard

Appendix A: Solutions

  1. Chapter 2: Facts, Rules, Questions
  2. Chapter 3: Prolog and Datalog in a Nutshell
  3. Chapter 4: Types and Modes
  4. Chapter 5: Predicates as Values
  5. Chapter 6: Lambdas and Binders
  6. Chapter 7: pi and =>
  7. Chapter 8: Rules about Rules
  8. Chapter 9: Waiting Goals
  9. Chapter 10: Proofs You Can Check
  10. Chapter 11: The Logic Layer
  11. Chapter 12: The LLM Speaks λProlog
  12. Chapter 13: A Little Agent

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