I want to represent edges as pairs using the hyphen operator, i.e., From-To.
I want to represent nodes as pairs as well (denoting FCA concepts), i.e., Extent-Intent.
So I get something like the following:
([1]-[a])-([2,3]-[b])
The brackets make sure the expression is properly disambiguated: the single outer hyphen denotes the edge operator, the two inner hyphens denote the node operator.
Here is my problem:
?- X = (a-b)-(c-d), X = Y1-Y2-Y3.
X = a-b- (c-d),
Y1 = a,
Y2 = b,
Y3 = c-d
I was expecting this to fail instead...
My questions:
Why are the brackets removed in the (displayed) binding of X?
Why is the first hyphen interpreted in a different way than the third one?
Possibly relevant, precedence rules for the hyphen operator:
?- current_op(P,T,-).
P = 200,
T = fy ;
P = 500,
T = yfx.
(This is in SWI-Prolog developer branch, but I doubt this will be SWI-specific.)
Usually, write_canonical is very useful in understanding how Prolog interprets operators.
?- write_canonical(a-b-c).
-(-(a,b),c)
true.
?- write_canonical(a-(b-c)).
-(a,-(b,c))
true.
?- write_canonical(a-b-c-d).
-(-(-(a,b),c),d)
true.
?- write_canonical(a-b-(c-d)).
-(-(a,b),-(c,d))
true.
And then, this sort of makes sense, assuming that all hyphens here are binary operators with the right hand argument having the strictly lower precedence:
?- X = (a-b)-(c-d), X = Y1-(Y2-Y3).
X = a-b- (c-d),
Y1 = a-b,
Y2 = c,
Y3 = d.
?- X = (a-b)-(c-d), X = (Y1-Y2)-Y3.
X = a-b- (c-d),
Y1 = a,
Y2 = b,
Y3 = c-d.
The first brackets are not strictly necessary to display the nested term -(-(a,b),-(c,d)). The second pair of brackets is necessary, otherwise it would mean -(-(-(a,b),c),d).
PS. Since I am not good enough to always keep operator precedence rules in my head, I try to avoid operators. There is nothing wrong with explicit functors. The hyphen of course is very useful for predicates like keysort/2 and libraries like library(ugraphs).
Related
I started to learn Prolog and want to solve this task. But I am not quite shure whether I am right or not. Pls, help me
pilots(A,london) = pilots(london, paris)
point(X,Y,Z) = point(X1,Y1,Z1)
letter(C) = word(letter)
noun(alpha) = alpha
'vicar' = vicar
I think that everything will give "no", but the last one will be yes. Z will be equal b, X=Z=b
Just try them in prolog.
Expression
Result
Explanation
pilots(A,london) = pilots(london,paris)
false
Variable A unifies with the atom london, but the atoms london and paris do not.
point(X,Y,Z) = point(X1,Y1,Z1)
true
The variable pairs X and X1, Y and Y1, and Z and Z1 each unify and become the same variable.
letter(C) = word(letter)
false
Although letter/1 and word/1 have the same arity, they have different functors and so do not unify.
noun(alpha) = alpha
false
The structure/term noun(alpha) with functor noun and arity 1 (noun/1) cannot unify with the atom alpha
'vicar' = vicar
true
Text delimited by single quotes (e.g., 'vicar') denotes an atom; the bare word vicar likewise denotes an atom, and, in fact, the exact, same atom as vicar: they unify (because they are identical.
Consider the following example program in Prolog:
p(0).
p(1).
b1(T) :-
T = tri(X, Y, Z), p(X), p(Y), c(T), !, p(Z).
c(tri(X, X, _Z)).
SWI Prolog gives some interesting answers for certain queries:\
?- b1(tri(0, Y, Z)).
Y = Z, Z = 0 ;
Y = 0,
Z = 1.
Note that it did break the line in the second answer, but not in the first one.
This makes me wonder, what are the exact rules of line-breaking? When does SWI Prolog break the lines and when does it not? What does this depend on?
All bindings (Var = Value) appear on their own line, except when two or more variables are bound to the same value. In that case it uses the following syntax on a single line.
V1 = V2, V2 = V3, ..., Vn-1 = Vn, Vn = value.
It does this because it is valuable to know two variables have the same value. The answer in SWI-Prolog is printed as a valid Prolog program. There are no further promises and the layout, ordering, etc. may change without notice between versions. If you want a machine to read results, do not use the toplevel.
append([],U,U).
append([X|U1],U2,[W|U3]) :- **W = X** , append(U1,[X|U2],[I|Quyruk]) ,
**W = I**, U3 = Quyruk .
This code appends first two lists when I delete "W is X".
This code has unnecessary variables like "W is X" but they are about my question.
When I set any value to "W" between ":-" and ",append..." like "W is X" or "W = 3" or "W = 6" -- returns false.
Why can't I set any value to the W at that position in code but I CAN set "W = I" at the end of the code?
The query is append([1,2],[3],U). I want to get [2,1,3] at this code
at append([1,2,3],[4,5,6],U). I want to get [3,2,1,4,5,6].
append([1],[2,3],U). returns [1,2,3] , when I take the length of first list "1" (when first list only has one element) the code is perfect ; but when I take the length of first list >1 (when first list has more than one element) the code returns false.
In prolog, you can't assign variables, and then reassign them. Variables are unified and instantiated. Once instantiated, they cannot be re-instantiated inside of a clause. So if you have this inside of a clause:
W = X,
...
W = I,
Then first W is unified with X (=/2 is the unification operator). That means they either both now have the same value instantiated (if at least one was instantiated before), or their values will be forced to be identical instantiation later in the clause. When W = I is encountered later, then I must be unifiable with W or the clause will fail. If I has a specific value instantiated that is different from the instantiation of W (and, therefore, X), the clause will necessarily fail.
Let's see it happen (note I changed the name to my_append since Prolog rejects redefining the built-in predicate, append):
my_append([],U,U).
my_append([X|U1], U2, [W|U3]) :-
W = X,
my_append(U1, [X|U2], [I|Quyruk]),
write('I = '), write(I), write('; W = '), write(W), nl,
W = I,
U3 = Quyruk.
If we run:
?- my_append([1], [1,2], L).
I = 1; W = 1
L = [1,2,3]
yes
Life is good. Now let's try:
| ?- my_append([1,2], [3,4], L).
I = 2; W = 2 % This will be OK
I = 2; W = 1 % Uh oh... trouble
no
Prolog cannot unify 1 and 2, as I described above. They are two different values. So the predicate fails due to the W = I statement.
The solution is a little simpler than what you're attempting (although you are very close):
% Append empty to list gives the same list
my_append([], U, U).
% Append of [X|U1] and U2 is just append U1 and [X|U2]
% Or, thought of another way, you are moving elements of the first list
% over to the head of the second one at a time
my_append([X|U1], U2, U3) :-
my_append(U1, [X|U2], U3).
| ?- my_append([1,2,3],[4,5,6],L).
L = [3,2,1,4,5,6]
yes
The essence of this was in your code. Those other variables were just getting in the way (as C.B. pointed out). :)
The is operator is specifically used to compare or unify integers. W = I Is attempting to unify W with I (regardless of type). When you Unify W with X (assuming X is an integer), you have already unified W, and if X\=I (doesn't unify) you will return false.
In your example, W unifies with 1, but then you try to unify it with 2.
You have a lot of unnecessary variables, here is a very simple implementation of append:
append([],XS,XS).
append([X|XS],YS,[X|ZS]):- append(XS,YS,ZS).
To understand whats going wrong with your code, lets walk through it
append([],U,U).
append([X|U1],U2,[W|U3]) :- W is X , append(U1,[X|U2],[I|Quyruk]) , W = I, U3 = Quyruk .
?-append([1,2,3],[4,5,6],U).
I will use X1,X2,... to differentiate between different bindings.
In the first call, X unifies with 1, U1 unifies with [2,3] and U2 unifies with [4,5,6]. W and U3 are not yet bound before going into the horn clause.
W is X unifies W with 1.
append(U1,[X|U2],[I|Quyruk]) is calling append([2,3],[1,4,5,6],[I|Quyruk]). Already you should see that your recursion isn't working correctly.
To illustrate the difference between 'is' and '=', next example is given in my Prolog course:
?- X is 2+3
X = 5.
?- X = 2+3.
X = 2+3.
However, both Y is 3 and Y = 3 seem to do the same. Is there a difference? And if not, is there a convention not to use one of the two in Prolog programs?
In Prolog, =/2 and is/2 serve very different purposes. is/2 is used to assign a value from an arithmetic expression. The right hand side must be fully instantiated (all variables bound) and it will compute the expression and unify it with the single variable on the left. For example:
Y = 3,
X is log(Y+7)/2.
X = 1.151292546497023
Y = 3
The = is used to unify terms on each side of the =. So when you say:
X = log(Y+7)/2.
That is unifying the term X with the term log(Y+7)/2 (or, technically, '/'(log('+'(Y,7),2)) which gives you X = log(Y+7)/2. It doesn't compute log(Y+7)/2. because that's not the job of =. That's a job for is/2.
With = you can also say things like:
foo(X, _) = foo(3, blah).
And you will get X = 3 since it can unify both terms by setting X to 3.
In the simplest case, these operators appear to be the same because X is 3 evaluates the expression 3 and assigns it (binds it to) X, and X = 3 unifies X with 3. Both results are the same in this case.
I try to check the correctness of student mathematical expression using Prolog (SWI-Prolog). So, for example if the student were asked to add three variable x, y, and z, and there's a rule that the first two variable that must be added are: x and y (in any order), and the last variable that must be added is z then I expect that prolog can give me true value if the student's answer is any of these:
x+y+z
(x+y)+ z
z+(x+y)
z+x+y
y+x+z
and many other possibilities.
I use the following rule for this checking:
addData :-
assert(variable(v1)),
assert(variable(v2)),
assert(variable(v3)),
assert(varName(v1,x)),
assert(varName(v2,y)),
assert(varName(v3,z)),
assert(varExpr(v1,x)),
assert(varExpr(v2,y)),
assert(varExpr(v3,z)).
add(A,B,R) :- R = A + B.
removeAll :- retractall(variable(X)),
retractall(varName(X,_)),
retractall(varExpr(X,_)).
checkExpr :-
% The first two variable must be x and y, in any combination
( (varExpr(v1,AExpr), varExpr(v2,BExpr));
(varExpr(v2,AExpr), varExpr(v1,BExpr))
),
add(AExpr, BExpr, R1),
% store the expression result as another variable, say v4
retractall(variable(v4)),
retractall(varName(v4, _)),
retractall(varExpr(v4, _)),
assert(variable(v4)),
assert(varName(v4, result)),
assert(varExpr(v4, R1)),
% add the result from prev addition with Z (in any combination)
( (varExpr(v3,CExpr), varExpr(v4,DExpr));
(varExpr(v4,CExpr), varExpr(v3,DExpr))
),
add(CExpr, DExpr, R2),
R2 = z + x + y. % will give me false
% R2 = z + (x + y). % will give me true
% Expected: both should give me true
checkCorrect :- removeAll,
addData,
checkExpr.
You should try to specify a grammar and write a parser for your expressions.
Avoid assert/retract, that make the program much more difficult to understand, and attempt instead to master the declarative model of Prolog.
Expressions are recursive data structures, using operators with known precedence and associativity to compose, and parenthesis to change specified precedence where required.
See this answer for a parser and evaluator, that accepts input from text. In your question you show expressions from code. Then you are using Prolog' parser to do the dirty work, and can simply express your requirements on the resulting syntax tree:
expression(A + B) :-
expression(A),
expression(B).
expression(A * B) :-
expression(A),
expression(B).
expression(V) :-
memberchk(V, [x,y,z]).
?- expression(x+y+(x+z*y)).
true .
edit: we can provide a template of what we want and let Prolog work out the details by means of unification:
% enumerate acceptable expressions
checkExpr(E) :-
member(E, [F = A + D, F = D + A]),
F = f,
A = c * N,
N = 1.8,
D = d.
And so on...
Test:
?- checkExpr(f=(c*1.8)+d).
true.
?- checkExpr(f=(c*1.8)+e).
false.
?- checkExpr(f=d+c*1.8).
true.