Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scope of arithmetic operators

175 views
Skip to first unread message

yoxoman

unread,
Oct 27, 2010, 1:36:59 AM10/27/10
to
Hello,

When I type the following in swi-prolog :

?- 2 + 3.

I got the interpreter error :

ERROR: toplevel: Undefined procedure: (+)/2 (DWIM could not correct goal)

whereas

?- X is 2 + 3.

succeeds.

-

Returned error is the same for other arithmetic operators.

I know that the sense of the expression 2 + 3 in itself would not mean
anything to the Prolog interpreter (it is not bind to a "question"), but
why does the interpreter returns "undefined" ? Does this mean that
arithmetic operators / predicates are defined only in a subscope of the
global scope ? How is this possible ?

Thank you.

k...@kymhorsell.com

unread,
Oct 27, 2010, 3:14:52 AM10/27/10
to
yoxoman <inv...@invalid.invalid> wrote:
> Hello,
> When I type the following in swi-prolog :
> ?- 2 + 3.
> I got the interpreter error :
> ERROR: toplevel: Undefined procedure: (+)/2 (DWIM could not correct goal)
> whereas
> ?- X is 2 + 3.
> succeeds.
...

> I know that the sense of the expression 2 + 3 in itself would not mean
...

> why does the interpreter returns "undefined" ?
...

Asked and answered. My bill is in the mail.

--
R Kym Horsell <k...@kymhorsell.com>

If your ideas are any good you'll have to ram them down people's throats.
-- Howard Aiken

Chip Eastham

unread,
Oct 27, 2010, 4:39:05 AM10/27/10
to

I'm not sure "scope" is a helpful notion for
understanding the error here. The top-level
interpreter is expecting a goal to process.

SWI-Prolog is kind enough to return an error for
undefined predicates. Here, as you realized, +/2 is not
defined as a predicate though it is defined as an infix
arithmetic operator.

Some Prolog implementations simply return false in
the case of an undefined predicate. For example,
Amzi! Prolog just returns 'no' when the top-level
interpreter is given 2 + 3.

In the same case GNU-Prolog returns this error:

uncaught exception: error(existence_error(procedure,(+)/2),top_level/
0)

regards, chip

yoxoman

unread,
Oct 28, 2010, 4:56:37 PM10/28/10
to
On Wed, 27 Oct 2010 01:39:05 -0700, Chip Eastham wrote:

> I'm not sure "scope" is a helpful notion for understanding the error
> here. The top-level interpreter is expecting a goal to process.
>
> SWI-Prolog is kind enough to return an error for undefined predicates.
> Here, as you realized, +/2 is not defined as a predicate though it is
> defined as an infix arithmetic operator.
>
> Some Prolog implementations simply return false in the case of an
> undefined predicate. For example, Amzi! Prolog just returns 'no' when
> the top-level interpreter is given 2 + 3.
>
> In the same case GNU-Prolog returns this error:
>
> uncaught exception: error(existence_error(procedure,(+)/2),top_level/ 0)

I see.

Is the is/2 predicate definition accessible somewhere in Prolog terms, or
is it tied to the interpreter implementation ?

More generally, are numbers represented in a common way in Prolog (like
0, s(0), s(s(0)) ...) ?

Thank you.

Chip Eastham

unread,
Oct 29, 2010, 9:51:37 AM10/29/10
to
On Oct 28, 4:56 pm, yoxoman <inva...@invalid.invalid> wrote:

[snip]

> Is the is/2 predicate definition accessible somewhere in
> Prolog terms, or
> is it tied to the interpreter implementation ?

Hi, yoxoman:

It can be a bit tricky to post links into the SWI-Prolog
site, but the is/2 predicate definition is given here:

http://www.swi-prolog.org/pldoc/man?predicate=is/2

In case that doesn't work for some reason, the idea can
be quoted in brief. "Number is +Expr" succeeds if the
number or variable Number unifies with the evaluation
of expression Expr (the plus sign signifies that Expr
has to be a bound expression, ie. so it can be evaluated).
Usually we apply the is/2 predicate to assign the value
to a variable, e.g. X is 2+3, since equality of integer
values can be tested with "==" (exact) and approximate
equality of floating point values with "=:=" (rounding).

> More generally, are numbers represented in a common
> way in Prolog (like 0, s(0), s(s(0)) ...) ?
>
> Thank you.

None of the Prolog implementations I've seen make any
use of the successor function to represent integers
syntactically or internally.

Syntactically integers may be represented in decimal
notation, sometimes with internal limits of precision
related to the machine integer limits. [If so,
there are ISO standard "flags" that tell the range
of such signed integers.] Some Prologs provide
unlimited (very, very extended) integer precision,
and SWI-Prolog is (by default) capable of this.

You don't seem to be asking about floating point
or rational arithmetic, so I won't get distracted
by discussion of those numbers (yet!).

regards, chip

0 new messages