Leanpub Header

Skip to main content

Prolog Programming: From Fundamentals to Advanced Logic Programming

A comprehensive guide to logic programming with SWI-Prolog

Prolog Programming: From Fundamentals to Advanced Logic Programming
This book is 100% completeLast updated on 2026-08-25

Discover the power of thinking in logic. This hands-on guide takes you from your first Prolog query to advanced techniques in constraints, grammars, AI search, meta-programming and production development. With clear explanations and executable SWI-Prolog examples, you’ll learn not just how Prolog works, but how to use it well.

Minimum price

$25.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
296
Pages
About

About

About the Book

This book takes you from your first Prolog query through professional-level logic programming. Using SWI-Prolog as the primary implementation, it covers everything from facts, rules, and unification through constraint programming, definite clause grammars, tabling, meta-programming, AI search algorithms, debugging, testing, performance engineering, and production-quality application development. Every concept is explained with what it is, why it exists, how it works, when to use it, common mistakes, and best practices. All code examples are complete and executable. The book assumes basic programming familiarity but no prior Prolog or logic-programming experience.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

A comprehensive guide to logic programming with SWI-Prolog

Introduction

  1. What this book covers
  2. How to use this book
  3. What you should know before starting

Chapter 1: Getting Started with Prolog

  1. What is Prolog and Where Did It Come From
  2. Choosing and Installing a Prolog System
  3. The Interactive REPL
  4. Your First Program
  5. Source Files and Project Organization
  6. Running Queries and Reading Results

Chapter 2: The Declarative Mindset

  1. Imperative Versus Declarative Programming
  2. Relations, Not Functions
  3. First-Order Logic Foundations
  4. Horn Clauses and Logical Implication
  5. Closed-World Assumption
  6. Declarative Versus Procedural Interpretation
  7. Common Misconceptions from Imperative Backgrounds
  8. Summary

Chapter 3: Facts, Rules, and Queries

  1. Facts
  2. Rules
  3. Queries
  4. Variables in Facts, Rules, and Queries
  5. A Worked Example: Family Knowledge Base
  6. Common Early Mistakes

Chapter 4: Terms, Types, and Data Structures

  1. Atoms
  2. Variables and Anonymous Variables
  3. Numbers
  4. Strings and Character Data
  5. Compound Terms and Structures
  6. Lists
  7. Term Inspection and Comparison
  8. Summary

Chapter 5: Unification

  1. What Unification Is and Why It Matters
  2. The Unification Algorithm
  3. Repeated Variables and Constraints They Impose
  4. Occurs Check and Rational Trees
  5. Unification Failure
  6. Identity Versus Unifiability
  7. Detailed Traces of Complex Unifications
  8. Practical Debugging of Unification Problems
  9. Summary

Chapter 6: Execution Model

  1. Resolution and Goal Reduction
  2. Depth-First Search and Backtracking
  3. Clause Order and Goal Order Matter
  4. Variable Bindings and Their Scope
  5. Determinism and Nondeterminism
  6. Soundness and Completeness
  7. Summary

Chapter 7: Recursion and List Processing

  1. Why Recursion Is Central in Prolog
  2. Simple Recursive Predicates
  3. List Construction and Decomposition
  4. Classic List Relations
  5. Higher-Level List Operations
  6. Sorting and Searching
  7. Termination Analysis for Recursive Predicates
  8. Tail Recursion and Efficiency
  9. Summary

Chapter 8: Arithmetic and Numeric Computation

  1. Symbolic Terms Versus Arithmetic Values
  2. The is/2 Predicate
  3. Numeric Comparisons
  4. Integer Versus Floating-Point Arithmetic
  5. Mathematical Predicates and Functions
  6. Arithmetic in Recursive Programs
  7. Why Ordinary Arithmetic Reduces Relational Purity
  8. Summary

Chapter 9: Control Flow and Search

  1. Conjunction and Disjunction
  2. Negation as Failure
  3. If-Then-Else Constructs
  4. Cuts
  5. Once-Like Commitment
  6. Failure-Driven Loops and Repeat Constructs
  7. Higher-Order Predicates and Generators
  8. Declarative Alternatives to Excessive Procedural Control
  9. Summary

Chapter 10: Constraint Logic Programming

  1. Why Constraints Are Different from Arithmetic
  2. CLP(FD): Finite Domain Constraints
  3. Constraint Propagation
  4. Labeling and Search
  5. Combinatorial Problems with CLP(FD)
  6. Optimization with Constraints
  7. Other Constraint Domains
  8. Summary

Chapter 11: Definite Clause Grammars

  1. What DCGs Are and Why They Exist
  2. DCG Syntax and Translation
  3. Simple Grammars
  4. Recursive Grammars and Nested Structures
  5. Semantic Actions and Extra Arguments
  6. Generation as Well as Parsing
  7. Practical Applications
  8. Summary

Chapter 12: Meta-Programming and Higher-Order Techniques

  1. Programs as Data
  2. call/N and Dynamic Invocation
  3. Predicate Indicators
  4. Meta-Predicate Declarations
  5. Constructing and Inspecting Goals
  6. Meta-Interpreters
  7. Interpreters for Small Languages
  8. Term Expansion and Reflection
  9. Power and Responsibility
  10. Summary

Chapter 13: Knowledge Representation and Reasoning

  1. Modeling Relationships and Facts
  2. Taxonomies and Inheritance-Like Models
  3. Rule-Based Systems and Expert Systems
  4. Symbolic Inference and Theorem-Proving Concepts
  5. Graph-Based Knowledge Representation
  6. Explainable Reasoning and Provenance
  7. Uncertainty and Non-Classical Reasoning
  8. Summary

Chapter 14: AI and Search Algorithms in Prolog

  1. State-Space Representation
  2. Depth-First Search
  3. Breadth-First Search and Queue-Based Algorithms
  4. Depth-Limited and Iterative-Deepening Search
  5. Best-First and Heuristic Search
  6. A* Search
  7. Game Trees and Minimax
  8. Planning Concepts
  9. Why Prolog Naturally Expresses Symbolic AI
  10. Summary

Chapter 15: Advanced Language Features

  1. Tabling and Memoization
  2. Tabled Execution in Practice
  3. Attributed Variables
  4. Coroutining and Delayed Goals
  5. Rational Trees and Infinite Terms
  6. Non-Backtrackable State and Global Variables
  7. Delimited Continuations
  8. Summary of Advanced Features

Chapter 16: Modules, Libraries, and Project Organization

  1. Modules and Namespaces
  2. Importing Predicates into a Module
  3. Predicate Visibility and Encapsulation
  4. Standard and Library Predicates
  5. Multifile Projects
  6. Dynamic and Multifile Predicates
  7. Initialization and Finalization
  8. Documentation Conventions
  9. Summary

Chapter 17: Debugging, Testing, and Development Workflow

  1. Tracing Execution
  2. Breakpoints and Stack Inspection
  3. Common Error Categories
  4. Diagnosing Logical Bugs
  5. Testing with PLUnit
  6. Test Organization and CI Integration
  7. Summary

Chapter 18: Performance Engineering and Optimization

  1. Measure Before Optimizing
  2. Indexing and Argument Order
  3. Clause Ordering
  4. Determinism and Choice-Point Reduction
  5. Tail Recursion and Accumulators
  6. Data Structure Selection
  7. Algorithmic Complexity Awareness
  8. Trade-offs: Declarative Elegance versus Operational Efficiency
  9. Summary

Chapter 19: Input/Output, Communication, and Interoperability

  1. Basic Input/Output
  2. File I/O
  3. Term Serialization and Persistence
  4. JSON Support
  5. HTTP Client and Server
  6. Database Integration
  7. Foreign-Language Interfaces
  8. Process Interaction
  9. Concurrency and Threads
  10. Sockets and Network Communication
  11. Architectural Patterns for Integration
  12. Summary

Chapter 20: Professional Practices and Real-World Application Development

  1. Coding Standards
  2. Predicate Contracts and Modes
  3. Small Cohesive Predicates
  4. Declarative Design Principles
  5. Error Handling and Input Validation
  6. Documentation Practices
  7. Anti-Patterns to Avoid
  8. Case Study: Scheduling System with Constraints
  9. When to Use Prolog (and When Not To)
  10. Deployment and Maintenance
  11. Prolog Development Workflow Summary
  12. Glossary of Key Prolog Terms
  13. Final Thoughts

Conclusion

Development Workflow Checklist

Project Structure Template

Best Practices Summary

Debugging Guide

Performance Optimization Checklist

Portability Notes

References and Bibliography

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

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