Course contentsShow
← Computer Science
Lesson 2158 of 2,87248. Programming ParadigmsFree lesson

Prolog Syntax: Facts and Rules

Learn the basic syntax of Prolog: writing facts as assertions and rules as logical implications.

Prolog Syntax: Facts and Rules

What you'll learn: You'll understand how to express knowledge in Prolog using facts (simple assertions) and rules (logical implications).

The Building Blocks of Logic Programming

In Prolog, you describe what is true rather than how to compute. This knowledge comes in two forms: facts and rules.

Facts: Simple Assertions

A fact is a basic statement that declares something unconditionally true. Think of it as a database entry or an axiom.

The syntax is straightforward:

parent(tom, bob).
parent(tom, liz).
likes(mary, chocolate).

Each fact is a predicate (like parent or likes) followed by arguments in parentheses, ending with a period. Read parent(tom, bob) as "Tom is a parent of Bob."

Rules: Logical Implications

A rule defines a relationship based on conditions. It says "X is true if Y is true." Rules use the :- operator (read as "if").

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

This reads: "X is a grandparent of Z if X is a parent of Y and Y is a parent of Z."

The left side (head) is what you're defining. The right side (body) lists conditions separated by commas (meaning "and").

How They Work Together

Facts provide ground truth. Rules build new relationships from existing ones. Prolog queries these to find solutions through logical inference—matching facts and applying rules automatically.

Key Takeaway: Prolog programs consist of facts (unconditional truths ending with .) and rules (conditional truths using :-), letting you declare relationships rather than step-by-step procedures.