Prolog Data Types: Atoms and Numbers
Prolog Data Types: Atoms and Numbers
Prolog's single data type is the term. Terms are either: atoms, numbers, variables
or compound terms (structures). The figure below presents a classification
of the data types in Prolog:
Numbers can be integers or real numbers (not used very much in typical
Prolog programming).
Variables
Compound Terms
1 Figure taken from Ivan Bratko – Prolog Programming for Artificial Intelligence
A compound term is composed of an atom called a "functor" and a number
of "arguments", which are again terms. Compound terms are ordinarily written as
a functor followed by a comma-separated list of argument terms, which is
contained in parentheses. The number of arguments is called the term's arity. An
atom can be regarded as a compound term with arity zero.
Examples of compound terms are: truck_year('Mazda', 1986) and
'Person_Friends'(zelda,[tom,jim]). Users can declare arbitrary functors as operators
with different precedence to allow for domain-specific notations. The notation f/n
is commonly used to denote a term with functor f and arity n.
• Strings
Prolog Unification
• Atoms unify if and only if they are the same atom.
• Numbers unify if and only if they are the same number.
• Strings unify if and only if they are the same string.
• Lists unify if and only if
1. their heads unify, and
2. their tails unify.
• Structures unify if and only if
1. their names unify,
2. they have the same number of arguments, and
3. their arguments unify.
• Variable V unifies with Term T just in case one of the following conditions is
satisfied:
1. V is an instantiated variable.
If T is not a variable, then V and T unify if and only if the term instantiated
on V unifies with T.
If T is an instantiated variable, then V and T unify if and only if the term
instantiated on V unifies with the term instantiated on T.
If T is an uninstantiated variable, then V and T are unified by instantiating
on T the term that is instantiated on V.
2. V is an uninstantiated variable.
If T is not a variable, then V and T are unified by instantiating T on V.
If T is an instantiated variable, then V and T are unified by instantiating on
V the term that is instantiated on T.
If T is an uninstantiated variable, then V and T unify and become
synonyms for the same variable.
4.1.2 Terms
4.1.3 Compound Terms
4.8.1.2 Unification
EXERCISES
Execute the following unification queries. Explain the results in a text file:.
a. ?- a = a.
b. ?- a = b.
c. ?- 1 = 2.
d. ?- ‘ana’ = ‘Ana’.
e. ?- X = 1, Y = X.
f. ?- X = 3, Y = 2, X = Y.
g. ?- X = 3, X = Y, Y = 2.
h. ?- X = ana.
i. ?- X = ana, Y = ‘ana’, X = Y.
j. ?- a(b,c) = a(X,Y).
k. ?- a(X,c(d,X)) = a(2,c(d,Y)).
l. ?- a(X,Y) = a(b(c,Y),Z).
m. ?- tree(left, root, Right) = tree(left, root, tree(a, b, tree(c, d, e))).
n. ?- k(s(g),t(k)) = k(X,t(Y)).
o. ?- father(X) = X.
p. ?- loves(X,X) = loves(marsellus,mia).