Prolog- find any number above 0 - prolog

I'm a bit surprised about this Prolog error. For p(X) :- X > 0:
?- p(X).
ERROR: Arguments are not sufficiently instantiated
I assumed this would unify X with some value above 0.
Could someone explain why my intuition might be off here?

Generally, arithmetic in prolog isn't reversible, but you can use something like the following code snippet from the same source:
better_add(X,Y,S) :- not(var(X)), not(var(Y)), not(var(S)),
Z is X + Y, Z=S.
better_add(X,Y,S) :- not(var(X)), not(var(Y)), var(S),
S is X + Y.
better_add(X,Y,S) :- not(var(X)), var(Y), not(var(S)),
Y is S - X.
better_add(X,Y,S) :- var(X), not(var(Y)), not(var(S)),
X is S - Y.

Related

Prolog encoded integers, less/2(X, Y) predicate

A question states the following:
In Prolog, non-negative integers can be encoded as numerals given by 0 and its successors (with, for example, the numeral s(s(s(0))) encoding 3).
numeral(0).
numeral(s(X)) :- numeral(X).
Define the predicate less/2(X, Y) that holds when X and Y are numerals encoding non-negative integers x and y such that x < y. For example,
?- less(0, s(0)).
yes.
?- less(s(s(0)), s(s(0))).
no.
I have been able to come up with a solution for this question, however, it suffers from a limitation. Here is my solution:
less(X, s(X)) :- numeral(X).
less(X, Z) :- less(X, Y), less(Y, Z).
This solution correctly outputs a yes for inputs that satisfy this predicate. However, for inputs that expect a no, this solution seems to enter an endless recursion of some sort, and the program just keeps running, rather than outputting a no.
Please help.
I would do it like this:
less(0, s(Y)) :- numeral(Y).
less(s(X), s(Y)) :- less(X, Y).
?- less(0, s(0)).
true.
?- less(s(s(0)), s(s(0))).
false.
The idea is that 0 is less than any s(Y), where Y is a numeral. If X is not 0, then X is s(X'), and X = s(X') is less than Y = s(Y') iff X' is less than Y'.
This does only work if both X and Y are numerals. If X is not a numeral then it will get stuck somewhere in the recursion and "returns" false. Same for Y, except that there need to be a test at the end if the rest of Y is a numeral.
Try this:
less2(X, s(X)) :- numeral(X).
less2(X, s(Y)) :- less2(X,Y).
Seems to work for me; your solution could recurse endlessly though, because if there exists no value of Y between X and Z it will simply try everything under the sun.

Prolog predecessor math

I have an add2 predicate which resolves like this where s(0) is the successor of 0 i.e 1
?- add2(s(0)+s(s(0)), s(s(0)), Z).
Z = s(s(s(s(s(0)))))
?- add2(0, s(0)+s(s(0)), Z).
Z = s(s(s(0)))
?- add2(s(s(0)), s(0)+s(s(0)), Z).
Z = s(s(s(s(s(0)))))
etc..
I'm trying to do add in a predecessor predicate which will work like so
?- add2(p(s(0)), s(s(0)), Z).
Z = s(s(0))
?- add2(0, s(p(0)), Z).
Z = 0
?- add2(p(0)+s(s(0)),s(s(0)),Z).
Z = s(s(s(0)))
?- add2(p(0), p(0)+s(p(0)), Z).
Z = p(p(0))
I can't seem to find a way to do this. My code is below.
numeral(0).
numeral(s(X)) :- numeral(X).
numeral(X+Y) :- numeral(X), numeral(Y).
numeral(p(X)) :- numeral(X).
add(0,X,X).
add(s(X),Y,s(Z)) :- add(X,Y,Z).
add(p(X),Y,p(Z)) :- add(X,Y,Z).
resolve(0,0).
resolve(s(X),s(Y)) :-
resolve(X,Y).
resolve(p(X),p(Y)) :-
resolve(X,Y).
resolve(X+Y,Z) :-
resolve(X,RX),
resolve(Y,RY),
add(RX,RY,Z).
add2(A,B,C) :-
resolve(A,RA),
resolve(B,RB),
add(RA,RB,C).
In general, adding with successor arithmetic means handling successor terms, which have the shape 0 or s(X) where X is also a successor term. This is addressed completely by this part of your code:
add(0,X,X).
add(s(X),Y,s(Z)) :- add(X,Y,Z).
Now you have to make a decision; you can either handle the predecessors and the addition terms here, in add/3, or you can wrap this predicate in another one that will handle them. You appear to have chosen to wrap add/3 with add2/3. In that case, you will definitely need to create a reducing term, such as you've built here with resolve/2, and I agree with your implementation of part of it:
resolve(0,0).
resolve(s(X),s(Y)) :-
resolve(X,Y).
resolve(X+Y,Z) :-
resolve(X,RX),
resolve(Y,RY),
add(RX,RY,Z).
This is all good. What you're missing now is a way to handle p(X) terms. The right way to do this is to notice that you already have a way of deducting by one, by using add/3 with s(0):
resolve(p(X), R) :-
resolve(X, X1),
add(s(0), R, X1).
In other words, instead of computing X using X = Y - 1, we are computing X using X + 1 = Y.
Provided your inputs are never negative, your add2/3 predicate will now work.

Fact formation with ceiling in prolog

Can I define a fact of the following form,
test(X, ceiling(sqrt(X))).
Where, X is related to ceiling(sqrt(X)).
I think , I could have done the following to receive the desired output,
test(X, Y) :- Y is ceiling(sqrt(X)).
You can define:test(X, ceiling(sqrt(X))).
This means that you have as fact atoms in the above form so if you query:
?- test(X, ceiling(sqrt(X))).
true.
because you defined this clause.
But note that if you query:
?- test(1.5, 2).
false.
It returns false because 2 is ceiling(sqrt(1.5)) but the predicate is waiting a syntax like ceiling(sqrt(1.5)) and not the result 2.
Another example:
?- test(1.5, Y).
Y = ceiling(sqrt(1.5)).
and
?- test(X,ceiling(sqrt(1.5))).
X = 1.5.
Also notice that :
test(X) :- X is ceiling(sqrt(X)).
is always failing for any input X (because there is no such X that equals to ceiling(sqrt(X)). )and querying test(X) will have instantiation problems due to is/2.
Maybe what you meant to write is:
test(X,X1) :- X1 is ceiling(sqrt(X)).
Sorry but I don't understand your clause
test(X) :- X is ceiling(sqrt(X)).
You're imposing the equation (not assignment: equation) " X = ceiling(sqrt(X)) ".
I think you're intention was
test(X, Y) :- Y is ceiling(sqrt(X)).
Is this what do you want?

getting out of local stack at prolog

p(0,0).
p(0,1).
p(0,2).
p(0,3).
p(0,4).
p(1,1).
p(1,2).
p(1,3).
p(1,4).
p(1,0).
p(2,0).
p(2,1).
p(2,2).
p(2,3).
p(2,4).
p(3,0).
p(3,1).
p(3,2).
p(3,3).
p(3,4).
p(4,0).
p(4,1).
p(4,2).
p(4,3).
p(4,4).
adjacent(p(X,Y),p(X,Z)) :-
p(X,Y),
p(X,Z),
Z is Y+1.
adjacent(p(X,Y),p(X,Z)) :-
p(X,Y),
p(X,Z),
Z is Y-1.
adjacent(p(X,Y),p(Z,Y)) :-
p(X,Y),
p(X,Z),
Z is X+1.
adjacent(p(X,Y),p(Z,Y)) :-
p(X,Y),
p(X,Z),
Z is X-1.
adjacentC(X,Y) :-
adjacent(X,Y).
adjacentC(X,Y) :-
adjacent(X,Z),
adjacentC(Z,Y).
I don't know why this code I wrote isn't working.
e.g.:
?- adjacentC((0,0),(4,4)). ERROR
Quick answer: The following works and terminates always using closure/3 defined elsewhere.
adjacentD(X,Y) :-
closure(adjacent,X,Y).
However, this approach is extremely slow, due to the inefficient definition of adjacent/3. Here is a better one / oh forget it, here is a more correct one, first:
adjacent2(p(X0,Y0),p(X,Y)) :-
p(X0,Y0),
( X0 = X,
p(X,Y),
abs(Y0-Y) =:= 1
; Y0 = Y,
p(X,Y),
abs(X0-X) =:= 1
).

prolog, test(X, Y, Z) :- Y is X + Z

How to get Y and Z in prolog, when I only know X?
For example:
test(X, Y, Z) :- X is Y + Z.
but error:
?- test(2, Y, Z).
ERROR: is/2: Arguments are not sufficiently instantiated
It's not possible, because you can choose Y to be anything you want and them compute Z or vice versa.
Although if you know that Y and Z are from some limited set (e.g. positive integers less than 5), you can do something like:
valid_number(1).
valid_number(2).
valid_number(3).
valid_number(4).
test(X, Y, Z) :- valid_number(Y), valid_number(Z), X is Y + Z.
You have to pass them as arguments. Prolog arithmetic (is/2) is not a magic wand, its right argument must be fully instantiated (no variables) before it can be evaluated.
If you want the predicate to work in several "directions", with multiple combinations of ground terms and variables, you'll want to use Constraint Logic Programming, but that's a rather advanced area of logic programming. In CLP on finite domains, you can say
:- use_module(library(clpfd)). % this differs between implementations
test(X,Y,Z) :- X #= Y + Z.

Resources