I am trying to write the following formula into swi prolog but get an error,
Here is the code/query I am typing
[X, Y, Z] ins 0 .. 4, X #= Y + 1.
and here is the error I am getting
ERROR: Syntax error: Operator expected
ERROR: [X, Y, Z]
ERROR: ** here **
ERROR: ins 0..4, X #= Y + 1 .
Could someone let me know what I did wrong.
The problem was that I forgot to import the library [library(clpfd)] so basically had to use the following statement use_module(library(clpfd)). This was spotted by #CapelliC
Related
double(X, Y) :-
X is Y/2, Y is X*2.
I'm trying to execute this but its given error always
Arguments are not sufficiently instantiated
In:
[2] 4 is _1604/2
[1] double(4,_1662) at line 2
how can I get double of two variables.
You are trying to make a bidirectional procedure, where at least one of the parameters is instantiated.
You may use CLP(fd) like this:
double(X, Y):- Y #= X*2.
Note this will only work with integer values, so for example
?- double(2, Y).
Y = 4.
?- double(X, 4).
X = 2.
but
?- double(2.5, Y).
ERROR: Domain error: `clpfd_expression' expected, found `2.5'
ERROR: In:
ERROR: [14] throw(error(domain_error(clpfd_expression,2.5),_2162))
ERROR: [11] clpfd:parse_clpfd(2.5*2,_2200) at c:/swi/swi8/library/clp/clpfd.pl:7359
ERROR: [9] clpfd:clpfd_equal(_2236,2.5*2) at c:/swi/swi8/library/clp/clpfd.pl:2795
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
?- double(X, 5).
false.
or if you want to use is/2 then you should make sure that the right hand side of is/2 is bound to an number, for example like this:
double(X, Y) :-
( number(X)
-> Y is X*2
; number(Y)
-> X is Y/2
).
The procedure using CLP(fd) is clearly more powerful as it does not require the right hand arithmetic expression to be instantiated prior to issue the constraint, and thus allows queries like double(X,Y) to succeed (giving the remaining constraints upon querying).
You may also try CLP(r) which works over reals:
:- use_module(library(clpr)).
double(X, Y):- { Y=X*2 }.
sample runs:
?- double(2, Y).
Y = 4.
?- double(X, 4).
X = 2.
?- double(2.5, Y).
Y = 5.0.
?- double(X, 5).
X = 2.5.
Dear Stackoverflow Community,
I just wanted to test out the constrained logic programming library (clpfd) for Prolog.
So I am including the library by calling
:- use_module(library(clpfd)).
Then I want to do sth like the following.
[X,Y] :: [1..2], X #\= Y, X+Y #\= 3.
But I always get the answer that
ERROR: Syntax error: Operator expected
ERROR: [X,Y]
ERROR: ** here **
ERROR: :: [1..2], X #\= Y, X+Y #\= 3 .
The same happens when executing the following example
? member(X,[42,1,17]), [X,Y] :: [0..20].
ERROR: Syntax error: Operator expected
ERROR: member(X,[42,1,17]), [X,Y]
ERROR: ** here **
ERROR: :: [0..20] .
Seems like Prolog does not recognise the :: operator properly.
Any help is appreciated
As far as I know, there is no (::)/2 predicate in the clpfd library. You are probably looking for the ins/2 predicate. For example:
?- [X,Y] ins 1..2, X #\= Y, X+Y #\= 3, label([X,Y]).
false.
?- [X,Y] ins 1..3, X #\= Y, X+Y #\= 3, label([X,Y]).
X = 1,
Y = 3 ;
X = 2,
Y = 3 ;
X = 3,
Y = 1 .
So if X and Y are in 1..2, then there is no solution, since your first constraint says X should be different from Y, and the second constraint says that X + Y should be different from 3.
In case we add 3 to the result, then there are solutions.
We can here use ins/2 to filter as well:
?- member(X,[42,1,17]), [X,Y] ins 0..20.
X = 1,
Y in 0..20 ;
X = 17,
Y in 0..20.
I made the following program, which is this:
eval([],_,_).
eval([(U, V)| Tail], X, Y):-
Y + evaluate([Tail], X, Y), Y is U * (X ** V).
it returns false, and I dont know why. How can I fix it?
So this eval([(4,3), 4, X) should return 256.
and eval([(4,3),(1,0)], 4, X). should return 257.
Now I get this error"
ERROR: Undefined procedure: (+)/2
ERROR: In:
ERROR: [9] _5562+eval([...],4,_5572)
ERROR: [8] eval([(4,3),...],4,_5606) at c:/users/parya lotfi/desktop/exe2.pl:2
ERROR: [7] <user>
eval([], _,0).
eval([(U,V)|UVs], X, Y0) :-
eval(UVs, X, Y1),
Y0 is Y1 + U*X^V.
?- eval([(4,3),(1,0)], 4, X).
X = 257.
I'm trying to define a rule to convert natural numbers to integers as follows:
?- assert(nat_to_int(0, 0)).
true.
?- assert(nat_to_int(s(X), Y):- nat_to_int(X, Z), Y is Z + 1).
ERROR: Uninstantiated argument expected, found _5706 is _5712+1 (2-nd argument)
ERROR: In:
ERROR: [8] assert((nat_to_int(...,_5772):-nat_to_int(_5776,_5778)),_5782 is _5788+1)
ERROR: [7] <user>
I understand that Z has to be instantiated before it can be used in the expression Y is Z + 1. However, I don't understand why it wouldn't be instantiated. Doesn't nat(X, Z) instantiate Z?
I think the problem is that X itself might not be instantiated. Hence, it wouldn't be able to figure out what Z should be. How do I fix this problem?
I have this fact at PROLOG (swi)
pos(X,[K|T],Z) :- findall(X, (member(X, [K|T])), Z) .
and then I write this:
pos (5 , [1,4,5] , Z ).
and I get the following error.
ERROR: Syntax error: Operator expected
ERROR: pos
ERROR: ** here **
ERROR: (5 , [1,4,5] , Z ) .
I am new to prolog so I cannot understand what the interpreter tells me about and secondly what I am doing wrong
Thx in advance.