A comprehensive guide to logic programming with SWI-Prolog
Introduction
- What this book covers
- How to use this book
- What you should know before starting
Chapter 1: Getting Started with Prolog
- What is Prolog and Where Did It Come From
- Choosing and Installing a Prolog System
- The Interactive REPL
- Your First Program
- Source Files and Project Organization
- Running Queries and Reading Results
Chapter 2: The Declarative Mindset
- Imperative Versus Declarative Programming
- Relations, Not Functions
- First-Order Logic Foundations
- Horn Clauses and Logical Implication
- Closed-World Assumption
- Declarative Versus Procedural Interpretation
- Common Misconceptions from Imperative Backgrounds
- Summary
Chapter 3: Facts, Rules, and Queries
- Facts
- Rules
- Queries
- Variables in Facts, Rules, and Queries
- A Worked Example: Family Knowledge Base
- Common Early Mistakes
Chapter 4: Terms, Types, and Data Structures
- Atoms
- Variables and Anonymous Variables
- Numbers
- Strings and Character Data
- Compound Terms and Structures
- Lists
- Term Inspection and Comparison
- Summary
Chapter 5: Unification
- What Unification Is and Why It Matters
- The Unification Algorithm
- Repeated Variables and Constraints They Impose
- Occurs Check and Rational Trees
- Unification Failure
- Identity Versus Unifiability
- Detailed Traces of Complex Unifications
- Practical Debugging of Unification Problems
- Summary
Chapter 6: Execution Model
- Resolution and Goal Reduction
- Depth-First Search and Backtracking
- Clause Order and Goal Order Matter
- Variable Bindings and Their Scope
- Determinism and Nondeterminism
- Soundness and Completeness
- Summary
Chapter 7: Recursion and List Processing
- Why Recursion Is Central in Prolog
- Simple Recursive Predicates
- List Construction and Decomposition
- Classic List Relations
- Higher-Level List Operations
- Sorting and Searching
- Termination Analysis for Recursive Predicates
- Tail Recursion and Efficiency
- Summary
Chapter 8: Arithmetic and Numeric Computation
- Symbolic Terms Versus Arithmetic Values
- The is/2 Predicate
- Numeric Comparisons
- Integer Versus Floating-Point Arithmetic
- Mathematical Predicates and Functions
- Arithmetic in Recursive Programs
- Why Ordinary Arithmetic Reduces Relational Purity
- Summary
Chapter 9: Control Flow and Search
- Conjunction and Disjunction
- Negation as Failure
- If-Then-Else Constructs
- Cuts
- Once-Like Commitment
- Failure-Driven Loops and Repeat Constructs
- Higher-Order Predicates and Generators
- Declarative Alternatives to Excessive Procedural Control
- Summary
Chapter 10: Constraint Logic Programming
- Why Constraints Are Different from Arithmetic
- CLP(FD): Finite Domain Constraints
- Constraint Propagation
- Labeling and Search
- Combinatorial Problems with CLP(FD)
- Optimization with Constraints
- Other Constraint Domains
- Summary
Chapter 11: Definite Clause Grammars
- What DCGs Are and Why They Exist
- DCG Syntax and Translation
- Simple Grammars
- Recursive Grammars and Nested Structures
- Semantic Actions and Extra Arguments
- Generation as Well as Parsing
- Practical Applications
- Summary
Chapter 12: Meta-Programming and Higher-Order Techniques
- Programs as Data
- call/N and Dynamic Invocation
- Predicate Indicators
- Meta-Predicate Declarations
- Constructing and Inspecting Goals
- Meta-Interpreters
- Interpreters for Small Languages
- Term Expansion and Reflection
- Power and Responsibility
- Summary
Chapter 13: Knowledge Representation and Reasoning
- Modeling Relationships and Facts
- Taxonomies and Inheritance-Like Models
- Rule-Based Systems and Expert Systems
- Symbolic Inference and Theorem-Proving Concepts
- Graph-Based Knowledge Representation
- Explainable Reasoning and Provenance
- Uncertainty and Non-Classical Reasoning
- Summary
Chapter 14: AI and Search Algorithms in Prolog
- State-Space Representation
- Depth-First Search
- Breadth-First Search and Queue-Based Algorithms
- Depth-Limited and Iterative-Deepening Search
- Best-First and Heuristic Search
- A* Search
- Game Trees and Minimax
- Planning Concepts
- Why Prolog Naturally Expresses Symbolic AI
- Summary
Chapter 15: Advanced Language Features
- Tabling and Memoization
- Tabled Execution in Practice
- Attributed Variables
- Coroutining and Delayed Goals
- Rational Trees and Infinite Terms
- Non-Backtrackable State and Global Variables
- Delimited Continuations
- Summary of Advanced Features
Chapter 16: Modules, Libraries, and Project Organization
- Modules and Namespaces
- Importing Predicates into a Module
- Predicate Visibility and Encapsulation
- Standard and Library Predicates
- Multifile Projects
- Dynamic and Multifile Predicates
- Initialization and Finalization
- Documentation Conventions
- Summary
Chapter 17: Debugging, Testing, and Development Workflow
- Tracing Execution
- Breakpoints and Stack Inspection
- Common Error Categories
- Diagnosing Logical Bugs
- Testing with PLUnit
- Test Organization and CI Integration
- Summary
Chapter 18: Performance Engineering and Optimization
- Measure Before Optimizing
- Indexing and Argument Order
- Clause Ordering
- Determinism and Choice-Point Reduction
- Tail Recursion and Accumulators
- Data Structure Selection
- Algorithmic Complexity Awareness
- Trade-offs: Declarative Elegance versus Operational Efficiency
- Summary
Chapter 19: Input/Output, Communication, and Interoperability
- Basic Input/Output
- File I/O
- Term Serialization and Persistence
- JSON Support
- HTTP Client and Server
- Database Integration
- Foreign-Language Interfaces
- Process Interaction
- Concurrency and Threads
- Sockets and Network Communication
- Architectural Patterns for Integration
- Summary
Chapter 20: Professional Practices and Real-World Application Development
- Coding Standards
- Predicate Contracts and Modes
- Small Cohesive Predicates
- Declarative Design Principles
- Error Handling and Input Validation
- Documentation Practices
- Anti-Patterns to Avoid
- Case Study: Scheduling System with Constraints
- When to Use Prolog (and When Not To)
- Deployment and Maintenance
- Prolog Development Workflow Summary
- Glossary of Key Prolog Terms
- Final Thoughts