Trash
Trash

Reputation: 15

swiprolog map false to zero and true to one

How can i map in swi-prolog true and false to 1's and 0's respectively? i need to evaluate ones and zeroes like true and falses (like in truth tables). i am having some difficulties since whenever i try to use 1 as logical value swi-prolog dosent take it kindly and returns erros.

I would like to try something like this

0 :- false.
1 :- true.


and when i query my logical "and" operation, it should return logical and operation applied to both values.

? my_and_operation(1, 0).
false

? my_and_operation(1, 1).
true

Upvotes: 0

Views: 552

Answers (4)

Fred Foo
Fred Foo

Reputation: 363627

The really simple solution:

my_and_operation(1, 1).

Upvotes: 0

m09
m09

Reputation: 7493

Alternatively you could do something like :

map(1, true).
map(0, false).

boolAnd(X, Y) :-
    maplist(map, [X, Y], [A, B]),
    (A, B).

boolOr(X, Y) :-
    maplist(map, [X, Y], [A, B]),
    (A; B).

Upvotes: 1

CapelliC
CapelliC

Reputation: 60024

The simpler way:

my_and_operation(X, Y) :-
 X == 1, Y == 1.

Upvotes: 0

Simple way to do what you need is just determine some isTrue/1 and isFalse/1 predicates.

isTrue( 1 ).
isFalse( 0 ).

booleanAnd( X, Y ) :-
    isTrue( X ),
    isTrue( Y ).

Usage.

?- booleanAnd( 1, 0 ).
false.

?- booleanAnd( 1, 1 ).
true.

Upvotes: 3

Related Questions