compare - Why does Prolog does not backtrack on comparison?

I would expect that the following should always be true if a comparison backtrack, right? unless it goes into an infinite loop! ?- Y=2 , random:random(1,3,X), X =\= Y. Y = 2, X = 1. ?- Y=2 , random:random(1,3,X), X =\= Y. false. but I got false!In general, my question is why doesn't comparison backtrack? Thanks for all the answers. My confusion seemed to come primarily from my expectation of random keep generating new-random numbers, so I confused that comparison was not backtracking, instead, the reason was that random does its thing only once...Read more

prolog - CLP: Constraints on structured variables?

Lets have the following hypothetical scenario ... a grid with 5x5 and let say 3 figures.We want to define constraint on the positions. In CLP we normally define the constraints with integers, so this is one way of doing it : ... Fig1X #\= 2, Fig1Y #\= 3, ....i.e. we have separate variable for every X and Y position. Is there a way to define constraint on structured-variable which is built on top of integers. For the sake of example : .... Fig1 #\= (2,4) ...The scenario is just for illustration.I'm interested mainly in how do you handle structu...Read more

artificial intelligence - Implementing a model written in a Predicate Calculus into ProLog, how do I start?

I have four sets of algorithms that I want to set up as modules but I need all algorithms executed at the same time within each module, I'm a complete noob and have no programming experience. I do however, know how to prove my models are decidable and have already done so (I know Applied Logic).The models are sensory parsers. I know how to create the state-spaces for the modules but I don't know how to program driver access into ProLog for my web cam (I have a Toshiba Satellite Laptop with a built in web cam). I also don't know how to link t...Read more

prolog - Finding if a point is on a rectangle

Hi I am trying to figure out if a point is on a rectangle using Prolog. Where (ULX,ULY) is the upper left point of the rectangle and (LRX,LRY) is lower right point of the rectangle and (X,Y) is the point I am using to check if it is on the shape:I tried this but I kept getting the error(=:=/2: Arguments are not sufficiently instantiated)on(point2d(X,Y), rectangle(point2d(ULX, ULY), point2d(LRX, LRY))):-URX is LRX,URY is ULY,LLX is ULX,LLY is LRY,%Checks if point is in area 1(Y =:= ULY, X > ULX, X < URX); %OR Checks if point is in area 2...Read more

prolog - Most general higher-order constraint describing a sequence of integers ordered with respect to a relation

In CLP(FD), we frequently need to state: "This is a list of integers and finite domain variables in (sometimes: strictly) ascending/descending order."Is there any CLP(FD) system that provides a general (parametrisable) built-in constraint for this task?SWI-Prolog provides a constraint called chain/2, which is similar to what I am looking for. However, the name is slightly too specific to encompass all relations that the constraint can describe (example: #< is not a partial order but admissible in chain/2, leading to the sequence — take...Read more

prolog - ECLiPSe CLP puzzle: perfect rectangle fitting

I'm working on a puzzle known as 'divide-by-box'. In essence, it's a form of perfect rectangle fitting, based on given clues. The rules are:Some grid cells contain numbers (this is known input data)The task is to partition the grid area into rectangular rooms satisfying following constraints: each room contains exactly one number and the total area of the room is equal to the number in itE.g.:4 _ 2_ _ __ 3 _has solution:+-----------+| 4 . | 2 || . . | . ||------+----+| . 3 . |+-----------+I have written constraints and a small finite do...Read more

prolog - ECLiPSe clp : getting all adjacent cells on grid

So my problem is the following: I want, given some point with X,Y-coordinates in a grid, to return all of its adjacent cells.( Note: in the following examples, I'm using the notation UL = upper-left, UM = upper-middle, UR = upper-right, L = left, R = right, BL = bottom-left, BM = bottom-middle and BR = bottom-right. )E.g.: For element x at (3,3) on a 5x5 grid _ _ _ _ _ _ UL UM UR _ _ L x R _ _ BL BM BR _ _ _ _ _ _ For element x at (1,1) on a 5x5 grid x R _ _ _ BM BR _ _ _ _ _ _ _ _ _ ...Read more

prolog - Duplicate constraints in CLP(FD) and with dif/2

In SWI-Prolog, the following query gives this result:?- X mod 2 #= 0, X mod 2 #= 0.X mod 2#=0,X mod 2#=0.While correct, there is obviously no need for the second constraintSimilarly:?- dif(X,0), dif(X,0).dif(X, 0),dif(X, 0).Is there no way to avoid such duplicate constraints? (Obviously the most correct way would be to not write code that leads to that situation, but it is not always that easy)....Read more

puzzle - How to make path restrictions using Prolog CLP FD?

I'm trying to use restrictions programming through Prolog CLP FD to solve a puzzle that was proposed. This puzzle consists in the next simple rules:Now, in my code, I already cover the restrictions for the 2x2 grid AND that one piece MUST be connected to AT LEAST one of the same color.The problem is that I cannot find a way to build the restriction that says that one piece MUST have a PATH (be connected) to all the other pieces of the same color, without passing through pieces of the opposite color, so I'm getting this kind of outputs:0 0 0 00 ...Read more

CLP(B) weighted sat_count/3 in Prolog

For the CLP(B) library of SWI-Prolog,I want to implement a weighted version of sat_count/2sat_count(Sat0, N) :- catch((parse_sat(Sat0, Sat), sat_bdd(Sat, BDD), sat_roots(Sat, Roots), roots_and(Roots, _-BDD, _-BDD1), % we mark variables that occur in Sat0 as visited ... term_variables(Sat0, Vs), maplist(put_visited, Vs), % ... so that they do not appear in Vs1 ... bdd_variables(BDD1, Vs1), partition(universal_var, ...Read more

SWI Prolog vs. GNU Prolog - CLP(FD) issues under SWI

I wrote a quick predicate in Prolog trying out CLP(FD) and its ability to solve systems of equations.problem(A, B) :- A-B #= 320, A #= 21*B.When I call it in SWI, I get:?- problem(A,B).320+B#=A,21*B#=A.Whereas in GNU, I get the correct answer of:| ?- problem(A,B).A = 336B = 16What's going on here? Ideally I'd like to get the correct results in SWI as it's a much more robust environment....Read more

In writing a purely relational prolog program, is it ok to use a carefully placed cut?

I am trying my hand in writing a relational prolog program that manages a key, value store. The initial code is taken from some lecture slides i found on the internet (http://people.eng.unimelb.edu.au/pstuckey/book/course.html -- see: using data structure slides).newdic([]).addkey(D0,K,I,D) :- D = [p(K,I)|D0].delkey([],_,[]).delkey([p(K,_)|D],K,D).delkey([p(K0,I)|D0],K,[p(K0,I)|D]) :- dif(K, K0), delkey(D0,K,D).This code allows adding more than one value with the same key -- which, is fine with me. However, it also adds the same key, value p...Read more

prolog - Representing logical disjunctions in Constraint Handling Rules

I am writing a constraint solver in Prolog that implements a simple logical formula:"(alive(A) and animal(A)) iff (awake(A) or asleep(A))".I found one way to implement it in Constraint Handling Rules, but it is much more verbose than the original formula::- use_module(library(chr)).:- chr_constraint is_true/1.is_true(A) \ is_true(A) <=> true.is_true(alive(A)),is_true(animal(A)) ==> is_true(awake(A));is_true(asleep(A)).is_true(awake(A)) ==> is_true(animal(A)),is_true(alive(A)).is_true(asleep(A)) ==> is_true(animal(A)),is_true(aliv...Read more

prolog - Count solutions in a CSP

I'm using prolog and I have this code::- use_module(library(clpb)).fun(A, B, C, D, E) :- sat(A + B + C + D), sat(E), labeling([A, B, C, D, E]).If I want count all the solutions how can I do it? I've read about sat_count(+Expr, -Count) used in clpb but I'm not able to implement it without errors...Read more

cygwin - SWI-Prolog Guitracer X-server

The following error pops up when I try to use the guitracer to trace my SWI-Prolog code using a cygwin terminal: [PCE fatal: @display/display: Failed to connect to X-server at `': no DISPLAY environment variable You MUST be running the X11 Windowing environment. If you are, * check the setting of your DISPLAY environment variable as well * the access rights to your X11 server. See xauth(1) and xhost(1). * in: <No exception goal> ] Host stack: [23] pce_principal:send(@3221888617/prolog_debugger, icon(resource(de...Read more