← All chapters
CHAPTER 02

Facts & Rules

Build knowledge one relationship at a time.

Use → to advance · Esc for slide overview

Facts describe relationships

parent(alex, sam).
parent(sam, pat).

Read parent(alex, sam) as “Alex is a parent of Sam.”

Rules describe implications

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

Read :- as “if”. The comma means both goals must hold.

Ask about the family

?- grandparent(alex, pat).
true.

Prolog can use the rule and the facts together.

Try to predict

Will the following query succeed using only the facts and rule above?

?- grandparent(sam, pat).

No: we have not given a child of Pat.

Keep exploring

Facts state relationships. Rules let us derive new ones.

Continue to chapter 03 →