100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Facts and Rules in Prolog

How Prolog knowledge bases are built from facts and rules, including how conjunctions (AND) and multiple clauses (OR) combine to express logic.

FoundationsBeginner9 min readJul 10, 2026
Analogies

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.

prolog
% 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

Was this page helpful?

Topics covered

#Programming#PrologStudyNotes#FactsAndRulesInProlog#Facts#Rules#Prolog#Writing#StudyNotes#SkillVeris#ExamPrep