I'm having trouble using the round/1
and floor/1
built-ins in SWI-Prolog. When using them in my code, they aren't recognized and when submitting them as a query; for instance ?- round(1.6).
, Prolog will tell me that the procedure is not recognized. Am I doing something wrong? I tried it on both the online Swish version and my own windows installed version, but getting the same error on both.
1 Answers
round/1
and floor/1
are no builtin predicates, round/2
and floor/2
are.
Prolog works with predicates. That means that a predicate can only be true
or false
(or error). Furthermore it can unify variables (further).
So the only means to calculate the floor
of 1.6, is to use two variables, and make the second one the floor of the first one. For example:
?- round(1.6,X).
X = 2.
?- floor(1.6,X).
X = 1.
Since it is sometimes cumbersome to write predicates that way. Prolog has defined some functors, these functors can be interpreted with the is/2
predicate. round/1
and floor/1
are functors with semantics in the is/2
predicate:
?- X is round(1.6).
X = 2.
?- X is floor(1.6).
X = 1.
is
can work with more advanced expression trees as well, like:
?- X is floor(0.4+0.4+0.4).
X = 1.
But note that is
is actually a predicate as well. We have written:
is(X, floor(0.4+0.4+0.4)).
behind the curtains, and the is/2
predicate will call floor/2
. Mind however that you can not simply inject your own predicate that way. You can not simply write is(X, foo(2))
and expect Prolog to call foo(2.X)
.