Is_integer predicate?

I’m using: SWI-Prolog version 8.0.2.

Is there an is_integer predicate in SWI-Prolog? The problem with integer/1 is that it will convert a rational number to an integer. I want to know if a value is natively an integer. Bonus points if there is a system predicate that will check if a value is a positive, non-zero integer. If not, I’ll just write my own.

1 Like

I think is_of_type(integer, X) is what you want. See library(error) for more information on that predicate & the various type names it knows about.

Yes

Of course.

I note a few ways to do this in Wiki: Bug hunting toolbox

The one you probably want is

is_of_type(+Type, @Term)

and for the Type value see the list in must_be/2

?- is_of_type(positive_integer,-1).
false.

?- is_of_type(positive_integer,0).
false.

?- is_of_type(positive_integer,1).
true.

?- is_of_type(positive_integer,0.1).
false.

?- is_of_type(integer,-1).
true.

?- is_of_type(integer,0).
true.

?- is_of_type(integer,1).
true.

?- is_of_type(nonneg,-1).
false.

?- is_of_type(nonneg,0).
true.

?- is_of_type(nonneg,1).
true.
1 Like

There are 3 pages on integer/1. One of them says that it is the same as round/1, which indicates that it will succeed if given a rational number, a behavior I didn’t want so I made this post:

https://www.swi-prolog.org/pldoc/doc_for?object=f(integer/1)

However I tried that in the console and it true. integer(1.1) returns false so you are correct in that I can use integer/1 for my purposes::

25 ?- integer(1.1).
false.

Apparently that doc page is incorrect since it does not function similar to round/1? I couldn’t test this because round/1 does not appear to be defined, at least not be default:


26 ?- round(1.1).
Correct to: "ground(1.1)"? no
ERROR: Undefined procedure: round/1

Wow! I never knew that and that’s really important, especially when looking things up in the docs.

Unfortunately true. The online docs say

Availability: Arithmetic function (see is/2)”

Which was supposed to give a clue …

1 Like

While the docs are my first stop for info, baring that I look for examples in the SWI-Prolog source code at GitHub as also noted by Jan.

At GitHub to search the code you have to go into a repository, e.g. swip-devel, swish or one of the others.

Once in a repository then use search at the upper left of the page. I typically search just swip-devel.

Searching for round returns many hits

Not all of the hits are Prolog source code but it is real working code.

That is how I learned to use setup_call_cleanup

1 Like