Clingo interpretation - sudoku

I am trying to interpret a clingo code for a sudoku and the last two lines confuse a lot. Is there anyone experienced with that, who can explain to me what I see?
%same_line(X1,X2) :- value(X1), value(X2), (X1-1)/3 == (X2-1)/3.
%:- sudoku(X1,Y1,N), sudoku(X2,Y2,N), same_line(X1,X2), same_line(Y1,Y2), X1 != X2, Y1 != Y2.
What I think I understand is that in the first line it is described how the values are being distributed in every line and in the second line I believe it is a constrain rule that would delete a row, column or subgrid with the same number.
The X1 != X2 and Y1 != Y2 is also abit confusing

Related

Pythagorean theorem for calculating distance between two points. Prolog

We all know that the Pythagorean theorem formula is: a2 + b2 = c2. I wanted to implement this formula, for calculating the distance between two points. This is my data of the coordinates of the cities (in km):
city(amsterdam, 121.813/487.362).
city(breda, 112.095/398.291).
city(eindhoven, 161.871/382.839).
city(groningen, 233.871/582.030).
city(haarlem, 103.690/488.416).
city(hertogenbosch, 149.225/412.119).
city(leeuwarden, 182.605/583.855).
city(maastricht, 176.830/318.793).
city(rotterdam, 92.548/437.734).
city(utrecht, 135.923/456.419).
city(zwolle, 203.252/503.130).
I implemented a program for this cause, but it doesn't seem to work:
estimate(State/NextState, Estimate) :-
city(State, X/Y),
city(NextState, X1/Y1),
X2 is X1 - X,
Y2 is Y1 - Y,
Estimate is X2^2 + Y2^2,
DifferentVar is sqrt(Estimate),
estimate(State/NextState, DifferentVar).
If a query something like this it returns false:
?- estimate(amsterdam/utrecht, X).
false.
?- estimate(utrecht/amsterdam, X).
false.
I also tried this, but it doesn't work:
estimate(State/NextState, Estimate) :-
city(State, X/Y),
city(NextState, X1/Y1),
Estimate is sqrt((X1 - X)^2 + sqrt(Y1 - Y)^2).
I have checked each 'subgoal' and I can't find the mistake or the wrong implementation. In my eyes it seems to me that each 'subgoal' has been reached, but it still returns false. I would really appreciate it if somebody could help me further!
The estimation rule should be:
estimate(State/NextState, Estimate) :-
city(State, X/Y),
city(NextState, X1/Y1),
X2 is X1 - X,
Y2 is Y1 - Y,
Estimate is sqrt(X2^2 + Y2^2).
Note that only the last line changed (and the next 2 lines were deleted).

A logic circuit is given two 2-bit binary numbers A and Bas its inputs. The circuit consists of two outputs Y1 and Y2

A logic circuit is given two 2-bit binary numbers A and Bas its inputs. The circuit consists of two outputs Y1 and Y2. The output values of Y1 and Y2 are obtained as follows:
If A<B, then Y1 and Y2 will be equal to A-B. Else Y1 and Y2 will be equal to A.
How To Determinate truth table for this
There are two inputs A[0:1] and B[0:1], 4 inputs in total. Your truth table will have 16 inputs(rows).Are Y1 and Y2 2-bit outputs and is it magnitude of A-B? If yes, The left two columns can be A and next two will be B. For 6 of these rows from 16 cases, A<B in 6 cases ([A,B] = {[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]}). these six rows will have Y1 = Y2 = [B-A]. All other rows will have Y1 = Y1 = A input. Seems straightforward, but I may be missing something here.

Prolog - findall returns a list of uninstantiated variables rahter than values

I am writing a Checkers game in Prolog, and I want to write a predicate to print all possible moves.
I have a predicate who checks all the legal moves than can be made -
is_move_legal(Board,p(X1,Y1),p(X2,Y2),DoEat, Player):-
Player = w, % making sure the "right" player is playing here - white
get(Board,p(X1,Y1),w),
(
get(Board,p(X2,Y2),1); % make sure no player is in the dest cell
get(Board,p(X2,Y2),0) % make sure no player is in the dest cell
),
between(1,8,X1),between(1,8,X2),between(1,8,Y1),between(1,8,Y2),
(
(DoEat = 0, X2 is X1-1,Y2 is Y1-1); % we don't eat
(DoEat = 0, X2 is X1-1,Y2 is Y1+1);
(DoEat = 1, X2 is X1-2, Y2 is Y1-2, X3 is X1-1, Y3 is Y1-1, (get(Board,p(X3,Y3),b);get(Board,p(X3,Y3),bk)),remove(Board,p(X3,Y3))); % eat the black soldier
(DoEat = 1, X2 is X1-2, Y2 is Y1+2, X3 is X1-1, Y3 is Y1+1, (get(Board,p(X3,Y3),b);get(Board,p(X3,Y3),bk)),remove(Board,p(X3,Y3))) % eat the black soldier
).
I have similair predicates for the black soldiers and for "kings" soldiers.
This is the findall predicate -
% find all possible moves
moves(Board,Moves):-
findall((X->Y),is_move_legal(Board,P1,P2,_,b),Moves).
It seems that it does find the moves, however this it the output I get -
[(_8090->_8092),(_8078->_8080),(_8066->_8068),(_8054->_8056),(_8042->_8044),(_8030->_8032),(_8018->_8020),(_8006->_8008)]
What I am trying to do, is to satisfy the p(X1,Y1), p(X2,Y2) arguments in the is_move_legal predicate.
EDIT:
From a comment here I realized the mistake -Rather then (X->Y), write -
findall((P1->P2),is_move_legal(Board,P1,P2,_,b),Moves).
Your help is much appreciated.
Thank you!

How does the power function work

This is My First Logic Programming Language course so this is a really Dumb Question But I cannot for the life of me figure out how does this power predicate work I've tried making a search tree to trace it But I still cannot understand how is it working
mult(_ , 0 ,0).
mult(X , Y, Z):-
Y > 0,
Y1 is Y - 1,
mult(X,Y1,Z1),
Z is Z1 + X.
exp2(_ ,0 , 1).
exp2(X,Y,Z):-
Y > 0,
Y1 is Y - 1,
exp2(X , Y1 , Z1),
mult(X,Z1,Z).
I so far get that I'm going to call the exp2 predicate till I reach the point where the Y is going to be Zero then I'm going to start multiplying from there, but At the last call when it's at exp2(2 , 1 , Z) what is the Z value and how does the predicate work from there?
Thank you very much =)
EDIT: I'm really sorry for the Late reply I had some problems and couldn't access my PC
I'll walk through mult/3 in more detail here, but I'll leave exp2/3 to you as an exercise. It's similar..
As I mentioned in my comment, you want to read a Prolog predicate as a rule.
mult(_ , 0 ,0).
This rule says 0 is the result of multiplying anything (_) by 0. The variable _ is an anonymous variable, meaning it is not only a variable, but you don't care what its value is.
mult(X, Y, Z) :-
This says, Z is the result of multiplying X by Y if....
Y > 0,
Establish that Y is greater than 0.
Y1 is Y - 1,
And that Y1 has the value of Y minus 1.
mult(X, Y1, Z1),
And that Z1 is the result of multiplying X by Y1.
Z is Z1 + X.
And Z is the value of Z1 plus X.
Or reading the mult(X, Y, Z) rule altogether:
Z is the result of multiplying X by Y if Y is greater than 0, and Y1 is Y-1, and Z1 is the result of multiplying X by Y1, and Z is the result of adding Z1 to X.
Now digging a little deeper, you can see this is a recursive definition, as in the multiplication of two numbers is being defined by another multiplication. But what is being multiplied is important. Mathematically, it's using the fact that x * y is equal to x * (y - 1) + x. So it keeps reducing the second multiplicand by 1 and calling itself on the slightly reduced problem. When does this recursive reduction finally end? Well, as shown above, the second rule says Y must be greater than 0. If Y is 0, then the first rule, mult(_, 0, 0) applies and the recursion finally comes back with a 0.
If you are not sure how recursion works or are unfamiliar with it, I highly recommend Googling it to understand it. That is, indeed, a concept that applies to many computer languages. But you need to be careful about learning Prolog via comparison with other languages. Prolog is fundamentally different in it's behavior from procedural/imperative languages like Java, Python, C, C++, etc. It's best to get used to interpreting Prolog rules and facts as I have described above.
Say you want to compute 2^3 as assign result to R.
For that you will call exp2(2, 3, R).
It will recursively call exp2(2, 2, R1) and then exp2(2, 1, R2) and finally exp(2, 0, R3).
At this point exp(_, 0, 1) will match and R3 will be assigned to 1.
Then when call stack unfolds 1 will be multiplied by 2 three times.
In Java this logic would be encoded as follows. Execution would go pretty much the same route.
public static int Exp2(int X, int Y) {
if (Y == 0) { // exp2(_, 0, 1).
return 1;
}
if (Y > 0) { // Y > 0
int Y1 = Y - 1; // Y1 is Y - 1
int Z1 = Exp2(X, Y1); // exp2(X, Y1, Z1);
return X * Z1; // mult(X, Z1, Z).
}
return -1; // this should never happen.
}

Prolog verify predicates in a loop

I have a some predicates inside the recursion verif(X1,Y1,F). The first predicate is gen(X1,Y1,X2,Y2) which receives X1 and Y1 and generates the numbers X2 and Y2. The other predicates are the ones that I want to verify. If one of this predicates returns F=1 the loop should be restarted with verif(X2,Y2,F), but I dont know how to do this. If all the predicates return F=0 the recursion ends.
Here is the example:
verif(X1,Y1,0).
verif(X1,Y1,F):-
gen(X1,Y1,X2,Y2),
pred1(X2,Y2,A,B,F),
pred2(X2,Y2,C,D,F),
pred3(X2,Y2,E,G,F),
verif(X2,Y2,F).
The problem is when the 3 predicates return diferent values for F it will fail.
One way would be to use the predicate repeat until none of the predicates fails, but this way the predicate gen(X1, Y1, X2, Y2) would always generate the same X2 and Y2 because it would allways receive the same X1 and Y1.
I think you can use different variables, and then test the values
verif(X1,Y1,F):-
gen(X1,Y1,X2,Y2),
pred1(X2,Y2,A,B,F1),
pred2(X2,Y2,C,D,F2),
pred3(X2,Y2,E,G,F3),
(( F1 == 0, F2 == 0, F3 == 0 ) -> true ; verif(X2,Y2,F)).
or simpler
verif(X1,Y1,F):-
gen(X1,Y1,X2,Y2),
pred1(X2,Y2,A,B,F1),
pred2(X2,Y2,C,D,F2),
pred3(X2,Y2,E,G,F3),
(F1 + F3 + F3 =:= 0 -> true ; verif(X2,Y2,F)).

Resources