Download An Introductory Course on Constraint Logic Programming

Transcript
4.4. TYPE PREDICATES
73
4.4 Type Predicates
The introduction of extra-logical predicates (such as, for example, the arithmetical ones just
presented, and others we will see later), causes the need of testing the type of terms at
runtime: we may want to check whether an argument is a number or not, to react accordingly
at runtime. This is a feature not taken into account in formal logic, since the type of an object
has, actually, no sense at all: all data in formal logic are Herbrand terms, and have no specic
meaning per se. But, for practical reasons, it is often advantageous knowing when a variable
has been bound to a number, or to a constant, or to a complex structure, or when it is still
free. There are a number of unary predicates which deal with the types of terms
Name
integer(X)
float(X)
number(X)
atom(X)
atomic(X)
Meaning
X
X
X
X
X
is an integer
is a oating point number
is a number
is a constant other than a number
is a constant
Table 4.3: Predicates checking types of terms
These predicates behave approximately as if they where dened via an innite set of facts.
The dierence is that they do not enumerate, as facts would have done: when handed down
a free variable, they fail (as they should, because a free variable is not a constant):
?- integer(3).
yes
?- float(3).
no
?- float(3.0).
yes
?- atom(3).
no
?- atom(logo).
yes
?- atomic(logo).
yes
?- atom(X).
no
?- atomic(X).