Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, June 6, 2026. Learn more…

Leanpub Header

Skip to main content

Practical AI Programming with Prolog

This book is 100% completeLast updated on 2026-05-26

The is more to AI than Large Language models. Here we explore Symbolic AI with the Prolog language.

Minimum price

$8.00

$20.00

You pay

Author earns

$
PDF
EPUB
WEB
About

About

About the Book

Practical Neuro-Symbolic AI: Combining Logic and Learning

In an era dominated by black-box statistical models and unpredictable Large Language Models, this book reveals the missing half of the modern AI equation: Prolog and Symbolic Reasoning. Learn how to build explainable, secure, and mathematically sound AI systems by bridging neural processing with logic-based constraint engines. By harnessing the bidirectional Janus bridge, you will discover how to seamlessly combine Python’s elite machine learning libraries (like scikit-learn and spaCy) and generative LLM APIs with Prolog’s unparalleled pattern matching, dynamic fact bases, and logical reasoning—unlocking a powerful new class of hybrid, neuro-symbolic AI applications.

Skip the dry academic theory and dive straight into production-ready, hands-on projects. This book walks you through building strict LLM logic guardrails that mathematically prevent hallucinations, constructing local knowledge graphs enriched dynamically by remote SPARQL queries to Wikidata, solving complex constraint optimization problems with CLP(FD), and writing meta-interpreters that explain why conclusions were reached. Whether you are a software engineer, data scientist, or AI practitioner, this book is your practical blueprint for using Prolog as a high-performance orchestration and reasoning engine for the next generation of intelligent software.

Share this book

Author

About the Author

Mark Watson

Mark Watson is a consultant specializing in LLMs, deep learning, machine learning, knowledge graphs, and general artificial intelligence software development. He uses Common Lisp, Clojure, Python, Java, Haskell, and Ruby for development.

He is the author of 20+ published books on Artificial Intelligence, Deep Learning, Java, Ruby, Machine Learning, Common LISP, Clojure, JavaScript, Semantic Web, NLP, C++, Linux, and Scheme. He has 55 US Patents.

Mark's consulting customer list includes: Google, Capital One, Olive AI, CompassLabs, Disney, Sitescout.com, Embed.ly, and Webmind Corporation.

Mark wrote ten traditional published books for McGraw Hill, Springer Verlag, J Wiley, and Morgan Kaufman publishers before adopting the LeanPub self-publishing platform.

Leanpub Podcast

Episode 253

An Interview with Mark Watson

Contents

Table of Contents

Cover Material, Copyright, and License

Preface

  1. Who This Book Is For
  2. How To Read This Book
  3. Open Source Example Programs and Manuscript Files
  4. Acknowledgments

Setting Up Your Prolog Development Environment

  1. Installing SWI-Prolog
  2. Editor Support
  3. The SWI-Prolog Interactive Top Level
  4. Installing Packs (Libraries)
  5. Cloning the Book’s GitHub Repository
  6. Project Organization and Best Practices

Prolog Tutorial

  1. Facts, Rules, and Queries
  2. Unification and Pattern Matching
  3. Backtracking and Search
  4. Lists and Recursive Data Structures
  5. Arithmetic and Comparison
  6. Input and Output
  7. Modules and Code Organization
  8. Definite Clause Grammars (DCGs) — A First Look
  9. Common SWI-Prolog Built-in Predicates

Search Algorithms in Prolog

  1. Loading Graph Data from a File
  2. Depth-First and Breadth-First Search
  3. Iterative Deepening
  4. A* Heuristic Search
  5. State-Space Search and Puzzle Solving
  6. Constraint-Based Search: The N-Queens Problem

Natural Language Processing with Definite Clause Grammars

  1. DCG Fundamentals
  2. Tokenizing and Preprocessing Text
  3. Parsing Natural Language Sentences
  4. Semantic Analysis with DCGs
  5. Named Entity Recognition
  6. Text Categorization
  7. Text Summarization

Reasoning and Inference

  1. Propositional and First-Order Logic in Prolog
  2. Forward and Backward Chaining
  3. Generating and Visualizing Proof Trees
  4. Non-Monotonic Reasoning and Defaults
  5. Case Study: A Medical Diagnosis Reasoner

Expert Systems and Rule-Based AI

  1. What Is an Expert System?
  2. Building an Expert System Shell in Prolog
  3. Knowledge Acquisition and Rule Representation
  4. Explanation Facilities
  5. Case Study: A Wine Selection Advisor
  6. Case Study: A Fault Diagnosis System

Explainable AI and Computational Law with s(CASP)

  1. What is s(CASP)?
  2. Implementing Compliance Rules
  3. Running the Compliance Check
  4. Key Design Decisions

Constraint Logic Programming

  1. Introduction to CLP
  2. CLP(FD): Constraints over Finite Domains
  3. Solving Sudoku with CLP(FD)
  4. The N-Queens Problem
  5. Scheduling and Resource Allocation
  6. CLP(R) and CLP(Q): Constraints over Reals and Rationals
  7. CLP(B): Boolean Constraints

Probabilistic Logic Programming

  1. Why Probabilistic Reasoning?
  2. ProbLog and Probabilistic Facts
  3. Learning Probabilities from Data
  4. Bayesian Networks in Prolog
  5. Practical Applications

Probability

  1. Words of Warning
  2. Glossary of Terms
  3. A SWI-Prolog Library to Explore Probability
  4. Walking Through the Bayesian Code
  5. The Correlation Module
  6. Frequentists vs. Bayesians
  7. Experimenting with Frequentist Methods
  8. Prolog-Specific Design Decisions
  9. Wrap Up

Anomaly Detection

  1. The Gaussian Approach
  2. The Training Pipeline
  3. Module Structure
  4. Loading and Subsampling
  5. Preprocessing
  6. Data Splitting
  7. Computing Statistics
  8. The Gaussian PDF
  9. Epsilon Search
  10. Putting It Together
  11. Evaluation
  12. Running the Example
  13. Prolog-Specific Design Decisions
  14. Wrap Up

Knowledge Graphs and Knowledge Representation

  1. Representing Knowledge in Prolog
  2. Building a Knowledge Graph
  3. Multi-Hop Reasoning Over Knowledge Graphs
  4. Generating RDF and Neo4j Cypher Data from Prolog
  5. Integrating with DBpedia and Wikidata

Semantic Web Tools

  1. Loading and Querying RDF Data
  2. Querying Remote SPARQL Endpoints
  3. RDFS and OWL Reasoning
  4. Practical Applications

Web Clients in Prolog

  1. HTTP GET and POST Requests
  2. Working with JSON
  3. Web Scraping
  4. Practical Applications

Client-Side Prolog with WebAssembly

  1. Architecture of a WASM Prolog Application
  2. Recommender System Logic
  3. JavaScript Integration
  4. Running the Application Locally
  5. Key Design Decisions

LLM Integration

  1. Calling LLM APIs from Prolog
  2. Structured Output from LLMs
  3. Combining LLMs with Prolog Reasoning

Cache Engine

  1. Design Overview
  2. Implementation
  3. Usage Examples
  4. Key Design Decisions
  5. Practical Applications

LLM Logic Guardrails

  1. The Neuro-Symbolic Guardrail Pattern
  2. Prolog Guardrail Rules
  3. Python Verification Harness
  4. Running the Verification Script
  5. Key Design Decisions

Daily Use REPL: Gemini with Search and Cache

  1. Design Overview
  2. Keyword Extraction
  3. Cache Context Builder
  4. Gemini API Integration
  5. The REPL Loop
  6. Running the REPL
  7. Wrap Up

The Janus Python Bridge

  1. Setting Up Janus
  2. Calling Python from Prolog
  3. Hybrid AI Pipelines
  4. Calling Prolog from Python
  5. Practical Applications

Building AI Agents with Prolog

  1. What Is an AI Agent?
  2. A Simple Reactive Agent
  3. Goal-Directed Agents
  4. Tool-Using Agents with LLM Integration
  5. Multi-Agent Communication
  6. Case Study: A Research Assistant Agent

Agent Behavior Trees

  1. Behavior Tree Node Types
  2. Implementing the BT Engine in Prolog
  3. Defining a Robot Agent
  4. Simulating the Agent
  5. Key Design Decisions

Meta-Interpreters: Prolog Reasoning About Prolog

  1. The Vanilla Meta-Interpreter
  2. Adding Proof Trees
  3. Bounded Reasoning
  4. Reasoning with Uncertainty
  5. Custom Search Strategies
  6. Debugging and Tracing Meta-Interpreters
  7. Summary

Planning and Scheduling

  1. Classical Planning in Prolog
  2. The Blocks World
  3. Planning with Constraints
  4. Partial-Order Planning
  5. Practical Job Scheduling Applications

Inductive Logic Programming with Popper

  1. How Popper Works
  2. The Grandparent Problem Setup
  3. Python Orchestrator
  4. Running the Learning Algorithm
  5. Key Design Decisions

Examples Using Scryer Prolog (The Modern Wave)

  1. Why Scryer Prolog?
  2. Installing Scryer Prolog
  3. Differences from SWI-Prolog
  4. DCG Processing of Large Text with Scryer
  5. Constraint Logic Programming in Scryer
  6. Porting SWI-Prolog Code to Scryer

Book Wrap Up

  1. Summary of What We Covered
  2. Where to Go from Here
  3. The Future of Prolog in AI

Other books by this author

Other books by this author

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

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.)

In fact, authors have earned over $15 million writing, publishing and selling 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