Facts and Rules in Prolog
Facts and rules are the two fundamental clause types that make up every Prolog program. A fact, written as a predicate followed by a period such as likes(mary, wine)., asserts something unconditionally true, while a rule, written as head :- body., asserts that the head is true whenever every goal in the comma-separated body is true. Together, collections of facts and rules form a knowledge base that Prolog searches when you pose a query.
Cricket analogy: A fact is like a permanent scorecard entry — 'Sachin Tendulkar scored 100 international centuries' — that never needs re-deriving, while a rule is more like the Duckworth-Lewis method, a conditional formula applied only when rain interrupts a match.
Writing Facts
Facts follow the syntax predicate_name(argument1, argument2, ...). — lowercase predicate names (atoms) with arguments that can be atoms, numbers, variables, or compound terms, always terminated by a period. For instance, color(banana, yellow)., age(tom, 42)., and enrolled(alice, cs101, fall2026). are all valid facts, and you can assert many facts about the same predicate — such as several parent(X, Y). facts — to build a small relational database that queries can search across.
Cricket analogy: Writing multiple parent/2 facts is like entering every player's stats into a scorecard database — batsman(kohli, rcb)., batsman(gill, gt). — each one an independent, queryable row.
Writing Rules and Conjunctions
A rule's body can chain multiple goals with commas, meaning logical AND: sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y. requires all three goals to succeed for the rule to hold. You can also define multiple clauses for the same predicate to express logical OR — for example, two separate discount(Customer, 10) :- ... and discount(Customer, 20) :- ... clauses mean a customer qualifies if either condition is met — and Prolog will try each clause in the order it's written, backtracking to the next one if an earlier clause's goals fail.
Cricket analogy: Chaining goals with commas is like the ICC's criteria for 'Player of the Match' requiring runs(Player, R), R > 50, wickets(Player, W), W >= 2 — every condition must hold together, just as a Prolog rule's comma-separated body must all succeed.
% facts
parent(tom, liz).
parent(tom, bob).
parent(bob, ann).
parent(bob, pat).
male(tom).
male(bob).
female(liz).
female(ann).
female(pat).
% rule with conjunction (AND)
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
% single clause covering the general case
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
?- sibling(ann, pat).
true.
?- sibling(ann, Who).
Who = pat.By convention, Prolog predicate and atom names start with a lowercase letter (parent, sibling), while variables always start with an uppercase letter or underscore (X, Y, _Result) — this capitalization rule is how the parser distinguishes a variable from an atom, unlike most languages where case is just a style choice.
A common beginner mistake is writing X \= Y (not-equal) as X != Y, which is not valid standard Prolog syntax, or forgetting it entirely in a sibling rule — without X \= Y, sibling(ann, ann) would incorrectly succeed because Prolog would happily unify a person with themselves as their own sibling.
- A fact asserts something unconditionally true, written as predicate(args).
- A rule asserts a head is true when its body's goals all hold, written as head :- body.
- Commas in a rule body mean logical AND — every goal must succeed.
- Multiple clauses for the same predicate act as logical OR — Prolog tries each in order.
- Predicate and atom names start lowercase; variable names start uppercase or underscore.
- X \= Y checks that X and Y do not unify (are not equal), commonly used to exclude trivial matches.
- A knowledge base is simply the combined set of facts and rules Prolog searches when resolving a query.
Practice what you learned
1. What does a comma between two goals in a rule's body mean in Prolog?
2. How does Prolog know that X is a variable and parent is not?
3. In sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y., why is X \= Y necessary?
4. How does Prolog express logical OR between two possible ways a predicate can hold?
5. Which of these is a syntactically valid Prolog fact?
Was this page helpful?
You May Also Like
What Is Prolog?
An introduction to Prolog as a declarative, logic-based programming language built on facts, rules, and queries resolved through unification and backtracking.
Prolog Syntax and Terms
A tour of Prolog's core data representation: atoms, numbers, variables, compound terms, and the [Head|Tail] list notation.
Your First Prolog Program
A hands-on walkthrough of writing a family.pl file, loading it into SWI-Prolog, and querying it interactively with backtracking.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics