A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Logic Programming Basics (Prolog)

Graduate Depth 83 in the knowledge graph ☐ I know this ☆ Set as goal
399prerequisites beneath it
See this on the map →
First-Order Logic for AIRecursion Basics→
logic-programming prolog backward-chaining unification

Core Idea

Logic programming (Prolog) uses first-order logic clauses and backward chaining to solve problems through unification and goal reduction. Prolog naturally expresses relational and constraint problems, though performance requires careful formulation to avoid inefficient search and the lack of type checking can lead to subtle errors.

How It's Best Learned

Write Prolog programs for relational problems (family relations, graph traversal) and understand how backtracking explores the proof space.

Explainer

Logic programming flips the way you think about writing programs. In conventional programming, you write step-by-step instructions telling the computer *how* to compute something. In logic programming, you declare *what* is true — facts and rules expressed in first-order logic — and let the system figure out how to derive answers. You already know how first-order logic represents knowledge with predicates, quantifiers, and logical connectives. Prolog takes that formalism and turns it into a programming language: you state logical clauses, and the interpreter uses them as a program.

A Prolog program consists of facts and rules. A fact like `parent(alice, bob).` asserts that Alice is a parent of Bob. A rule like `grandparent(X, Z) :- parent(X, Y), parent(Y, Z).` says X is a grandparent of Z if there exists some Y where X is a parent of Y and Y is a parent of Z. When you pose a query — `?- grandparent(alice, W).` — Prolog tries to prove it by searching through the facts and rules using backward chaining: it starts from the goal and works backward, trying to find facts that satisfy each sub-goal. This is where your understanding of recursion becomes essential, because rules can refer to themselves, and Prolog naturally handles recursive definitions by repeatedly applying rules until it reaches base-case facts.

The engine that makes this work is unification. When Prolog tries to match a goal against a rule head, it finds a substitution of variables that makes them identical. For `grandparent(alice, W)`, it unifies with the rule head `grandparent(X, Z)` by binding X to alice and Z to W, then attempts to prove `parent(alice, Y)` and `parent(Y, W)`. If the first sub-goal succeeds with Y = bob, it then tries `parent(bob, W)`. If that succeeds with W = charlie, the query returns `W = charlie`. If at any point a sub-goal fails, Prolog backtracks — it undoes the most recent variable binding and tries alternative matches. This systematic exploration of the proof space is what gives Prolog its power for search and constraint problems.

The elegance of logic programming comes with practical tradeoffs. Because Prolog searches depth-first by default, the order of clauses matters: a poorly ordered recursive rule can send the interpreter into an infinite loop. There is no type system catching errors at compile time, so a misspelled predicate name simply fails silently rather than raising an error. And while Prolog excels at relational reasoning, graph traversal, and symbolic AI tasks like natural language parsing, it struggles with numerical computation and problems that require fine-grained control over execution order. Understanding these strengths and limitations helps you recognize when logic programming is the right tool — typically when your problem is naturally expressed as relationships and constraints rather than sequential transformations.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → First-Order Logic for AI → Logic Programming Basics (Prolog)

Longest path: 84 steps · 399 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.