Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean assignment in Prolog

Tags:

prolog

clpfd

all.

I want to assign a boolean value to a variable.

I've tried stuff like.

Diagonal is (XPiece = XFinal)
Diagonal is (XPiece =:= XFinal)
Diagonal is (XPiece is XFinal)

None work...

Any solutions?

like image 876
F. P. Avatar asked Sep 28 '11 16:09

F. P.


3 Answers

Prolog's built-in predicate is/2 evaluates the right-hand side of the expression as an arithmetic expression and unifies the result with the left-hand side.

Also, prolog doesn't have a boolean type. Prolog's built-in types are

  • integer
  • float
  • atom
  • unbound variable
  • compound term

You could elect to represent a boolean value as the atoms true/false (useful for readability), or you could represent a boolean value as the integer values 1/0 (useful for computation). The way most procedural languages, like C, evaluate arithmetic values as booleans is broken WRT formal logic, though: falsity is single-valued (0) and truth multi-valued (non-zero), meaning that which is not false. In formal logic, truth is single-valued and falsity is defined as that which is not true.

So you might want to consider the semantics of your representation and build some predicates to manipulate your booleans, possibly adding some operators to "extend" prolog a bit.

like image 190
Nicholas Carey Avatar answered Sep 26 '22 21:09

Nicholas Carey


Use an if-then-else:

(XPiece = XFinal ->
    Diagonal = true
;
    Diagonal = false
)

or use 1/0, or whatever you want. Alternatively, use CLP(FD), that supports the idiom you want:

use_module(library(clpfd)).

diag(XPiece, XFinal, Diagonal) :-
    Diagonal #= (XPiece #= XFinal).
like image 22
Fred Foo Avatar answered Sep 22 '22 21:09

Fred Foo


What about

diagonal(XPiece, XFinal) :- XPiece = XFinal.
like image 32
Jiri Kriz Avatar answered Sep 25 '22 21:09

Jiri Kriz