Is there a way to check if an element is boolean in prolog?

Viewed 592

For example in scheme there is the function "boolean?" which checks if an element is boolean.

2 Answers

There are no standardized values for true and false (in contrast to a list). We can however use true/0 and false/0, and thus use:

boolean(true).
boolean(false).

or check this without unification with:

is_boolean(X) :-
    X == true;
    X == false.

As an addendum, the predicate must_be/2 (SWI-Prolog only?) is as good as it gets in deciding whether something is a boolean:

?- must_be(boolean,true).
true.

?- must_be(boolean,false).
true.

?- must_be(boolean,foo).
ERROR: Type error: `boolean' expected, found `foo' (an atom)

Both true and false are just atoms:

?- atom(true).
true.

?- atom(false).
true.

(Even Mercury doesn't have a dedicated "boolean" type.)

However, you can actually call the atoms true and false, whereby they are interpreted as 0-ary predicates and perform as expected, either succeeding the query or failing the query (that would be called "de-reifying the truth value", eh?)

?- true.
true.

?- false.
false.
Related