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.
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.
I am very new to Prolog. I need to find all the paths from Source to Destination in a maze.
I wrote the following clause so that I do not have to write all the edge relations explicitly:
neighbour(X,Y) :-
( X =:= Y+1
; Y =:= X+1
; X =:= Y+6
; Y =:= X+6
).
Next, for finding path I do the following:
path(Source,Source,_).
path(Source,Destination,PathList) :-
neighbour(Source,Z), % find a neighbour of Source
not(member(Z,PathList)), % that is not already in the list
% and see if there is a path from Z to Destination
path(Z,Destination,[Source|PathList]).
While running it I queried the following:
?- path(1,2,[]). % "Is there a path from 1 to 2?"
This gives me the following error:
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [10] 1=:=_26424+1
ERROR: [9] neighbour(1,_26452) at /Users/sujitkumar/Desktop/this sem/PL/2.pl:3
ERROR: [8] path(1,2,[]) at /Users/sujitkumar/Desktop/this sem/PL/2.pl:30
ERROR: [7] <user>
I do not understand why the expression is not getting evaluated.
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 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
I want to write a SWI-Prolog program, which tests if the result is lower than 4.0.
Therefore I pass a list like this [Name, Subject, Result], the subject and the name for the return value.
third([Head|Tail], Z) :-
Z = Tail.
second([Head|Tail], Z) :-
Z = Head.
test([Head|Tail], Subject, Z) :-
second(Tail, Subject2),
Subject2==Subject,
third(Tail,Result),
Result=<4.0,
Z=[Head|Tail].
Now if I execute with the following parameters:
test([smith, mu, 2.4], mu, R).
There is an error occuring:
ERROR: =</2: Type error: `character' expected, found `2.4'
I just can't find a way to fix it.
Replace third(Tail,Result) with third(Tail,[Result]):
test([Head|Tail], Subject, Z):-
second(Tail, Subject2),
Subject2==Subject,
third(Tail,[Result]),
Result =< 4.0,
Z=[Head|Tail].