Chapter 5 Prolog
Chapter 5 Prolog
PROLOG
There are five clauses in this knowledge base: two facts, and three rules.
There are three predicates in this knowledge base: happy, listens2music, and
playsAirGuitar.
Master1 AI 2024/ 2025 5
Prolog Basic Components
■ Sample of queries
■ There are terms of four kinds: atoms, numbers, variables, and compound terms.
■ Atoms: Atoms are usually strings made up of lower- and uppercase letters, digits, and the
underscore, starting with a lowercase letter. b, abcXYZ, x_123, ‘aaa’
■ Numbers: All Prolog implementations have an integer type: a sequence of digits,
optionally preceded by a - (minus). Some also support floats. 145, -10, 18,2
■ Variables: Variables are strings of letters, digits, and the underscore, starting with a
capital letter or an underscore. X, Elephant, _4711
■ Compound/complex terms: Compound terms are made up of a functor (a Prolog atom)
and a number of arguments (Prolog terms, i.e., atoms, numbers, variables, or other
compound terms) enclosed in parentheses and separated by commas.
playsAirGuitar(jody), father(X, Y)
■ We can define two predicates with the same functor but with different arity.
■ Prolog would treat this as two different predicates.
■ It is indicated with the suffix "/" followed by a number.
■ Example about the previous knowledge base:
– happy/1
– listens2music/1
– playsAirGuitar/1
■ Examples:
– parent(A, B, C)
– parent(kevin, henry, 30)
■ It will succeed with A, B, and C variables bound to kevin, henry, and 30, respectively.
– pred2(A, A, male)
– pred2(canada, cat, X)
■ The variable A cannot unify with canada and cat, so it will fail.
■ Goal Satisfaction: When you pose a query or goal in Prolog, the Prolog system
attempts to satisfy that goal by searching for a combination of rules, facts, and data
that match the query.
■ Rule Evaluation: Prolog evaluates rules and predicates in a depth-first manner. It
starts with the first available choice, executes it, and proceeds to the next subgoal or
condition within the rule.
■ Unification: During rule evaluation, Prolog uses a process called unification to match
the query or goal with the available facts and rules. Unification is the process of
finding values for variables such that the query and rule match.
■ A predicate is recursively defined if one or more rules in its definition refers to itself.
■ When writing a recursive predicate, it should always have at least two clauses: a
base clause (the clause that stops the recursion at some point), and one that
contains the recursion.
■ By unifying X with stork and Y with mosquito it obtains the following goal:
just_ate(stork,mosquito) but the knowledge base doesn’t contain the information.
■ Prolog tries to use the second rule. By unifying X with stork and Y with mosquito it
obtains the following goals: just_ate(stork,Z), is_digesting(Z,mosquito).
■ And there is such a value for Z, namely frog. It is immediate that just_ate(stork,frog).
will succeed, for this fact is listed in the knowledge base.
■ And deducing is_digesting(frog,mosquito). is almost as simple, for the first clause of
is_digesting/2 reduces this goal to deducing just_ate(frog,mosquito). and this is a
fact listed in the knowledge base.