Prolog : Evaluating logical expression w/o backtracking with AND/OR op/2? - prolog

I'm trying to implement logical expressions evaluation w/o backtracking behavior.
After some experimentation I got it working, here goes :
:- op(80, xfy, and).
:- op(80, xfy, or).
%check/evaluate logical expressions
check(true) :- !.
check(Cond and Conds) :- !, (check(Cond) , check(Conds)).
check(Cond or Conds) :- !, (check(Cond) ; check(Conds)), !. %% <- weird
check(Cond) :- !, call(Cond).
?- check(true and true).
true.
?- check(true and false).
false.
?- check(true or false).
true.
?- check(true or (false and true)).
true.
?- check((true or false) and (false and true)).
false.
?- X = 1 , Y = 2 , check((X = 1) and (Y > 1)).
X = 1,
Y = 2.
So the code works.
My question is why it works, especially the need of two cuts! in the OR expression.
Does this look like a proper Prolog implementation ?
How would you have done it yourself ?

Related

Prolog meta-interpreters and single sided unification

I tried this vanilla interpreter:
solve(true) :- !, true.
solve(X is E) :- !, X is E.
solve((A,B)) :- !, solve(A), solve(B).
solve(H) :- clause(H,B), solve(B).
Can we use it to meta-interpret some code? I tried this code,
requires SWI-Prolog 8.3.19, which runs fine normally:
sumlist([X|Y], R) => sumlist(Y, H), R is X+H.
sumlist([], R) => R is 0.
?- sumlist([1,2,3],X).
X = 6.
?- sumlist(X,Y).
ERROR: No rule matches sumlist(_21604,_21606)
But meta-interpretation goes wrong. The reason is that clause/2
doesn’t know about rules that use single sided unification:
?- clause(sumlist(A,B),C).
A = [_22728|_22730],
C = (sumlist(_22730, _22736), B is _22728+_22736) ;
A = [],
C = (B is 0).
?- solve(sumlist([1,2,3],X)).
X = 6.
?- solve(sumlist(X,Y)).
SWI-Prolog wurde unerwartet beendet.
Is there a solution for meta-interpreters and single sided unification?
One way out of the dilemma and stay inside the ISO core standard, is to translate single sided unfication to a combination of nonvar/1, (=)/2 and (==)/2, like here:
?- clause(sumlist(X,Y),Z), write((sumlist(X,Y):-Z)), nl, fail; true.
sumlist(_A, _B) :- nonvar(_A), _A = [_C|_D], sumlist(_D, _E), _B is _C+_E
sumlist(_A, _B) :- nonvar(_A), _A = [], _B is 0
Of course we need to add the built-ins nonvar/1, (=)/2 and (==)/2 as well to the meta interpreter:
solve(true) :- !.
solve(X is E) :- !, X is E.
solve(nonvar(X)) :- !, nonvar(X).
solve(X == Y) :- !, X == Y.
solve(X = Y) :- !, X = Y.
solve((A, B)) :- !, solve(A), solve(B).
solve(H) :- clause(H, B), solve(B).
Meta-interpreting sumlist/2 now works fine:
?- solve(sumlist([1,2,3],X)).
X = 6
?- solve(sumlist(X,Y)).
No
But the translator might challenge a Prolog system concering clause indexing. It moves away the functors from the head into the body. So the Prolog system would need some body front indexing as pioneered by YAP and found in Jekejeke Prolog.
Open Source:
Yet Another Pattern Matcher
https://gist.github.com/jburse/a3517410a28b759ef44f72584f89aaf8#file-picat3-pl
Vanilla Interpreter, Expansion Solution
https://gist.github.com/jburse/a3517410a28b759ef44f72584f89aaf8#file-vanilla4-pl

Simulating occurs_check=error in SICStus Prolog

It turns out that SICStus Prolog doesn't have an occurs_check
Prolog flag. At least we couldn't find one, and this here
gives an error message:
/* SICStus 4.6.0 (x86_64-win32-nt-4) */
?- set_prolog_flag(occurs_check, true).
Domain error in argument 1 of set_prolog_flag/2
It seems the value "true" is not so much a problem, the
crictical unifications can be realized via the existing
built-in unify_with_occurs_check/2. An interesting value
of an occurs_check Prolog flag is the value "error".
How would one implement a predicate unify_with_occurs_check_and_error/2 ?
Please note, the solution for unify_with_occurs_check_and_error/2
should behave like unify_with_occurs_check/2, i.e. not trigger
attributed variables.
Here is an example usage of the Prolog flag where present:
?- set_prolog_flag(occurs_check, error).
true.
?- X = f(X).
ERROR: ...
And this is what one would do in SICStus Prolog:
?- unify_with_occurs_check_and_error(X, f(X)).
ERROR: ...
Was adapting the code from here and got the following solution:
unify_with_error(X, Y) :- var(X), var(Y), !, X = Y.
unify_with_error(X, Y) :- var(X), !, must_notin(X, Y), X = Y.
unify_with_error(X, Y) :- var(Y), !, must_notin(Y, X), X = Y.
unify_with_error(X, Y) :- functor(X, F, A), functor(Y, G, B),
F/A = G/B,
X =.. [_|L],
Y =.. [_|R],
maplist(unify_with_error, L, R).
must_notin(X, Y) :-
term_variables(Y, L),
maplist(\==(X), L), !.
must_notin(X, Y) :-
throw(error(occurs_check(X, Y),_)).
Seems to work and no interference with attributed variables:
/* SICStus 4.6.0 (x86_64-win32-nt-4) */
?- unify_with_error(X, f(X)).
error(occurs_check(_413,f(_413)),_409)
?- freeze(X, throw(ball)), unify_with_error(X, f(X)).
error(occurs_check(_413,f(_413)),_409)

Pure Prolog Peano Number Apartness

Lets assume there is pure_2 Prolog with dif/2 and pure_1 Prolog without dif/2. Can we realize
Peano apartness for values, i.e. Peano numbers, without using dif/2? Thus lets assume we have Peano apartness like this in pure_2 Prolog:
/* pure_2 Prolog */
neq(X, Y) :- dif(X, Y).
Can we replace neq(X,Y) by a more pure definition, namely from pure_1 Prolog that doesn't use dif/2? So that we have a terminating neq/2 predicate that can decide inequality for Peano numbers? So what would be its definition?
/* pure_1 Prolog */
neq(X, Y) :- ??
Using less from this comment:
less(0, s(_)).
less(s(X), s(Y)) :- less(X, Y).
neq(X, Y) :- less(X, Y); less(Y, X).
I had something else in mind, which is derived from two of the Peano Axioms, which is also part of Robinson Arithmetic. The first axiom is already a Horn clause talking about apartness:
∀x(0 ≠ S(x))
∀x∀y(S(x) = S(y) ⇒ x = y)
Applying contraposition to the second axiom gives.
The axiom is now a Horn clause talking about apartness:
∀x∀y(x ≠ y ⇒ S(x) ≠ S(y))
Now we have everything to write some Prolog code.
Adding some symmetry we get:
neq(0, s(_)).
neq(s(_), 0).
neq(s(X), s(Y)) :- neq(X, Y).
Here are some example queries. Whether the predicate leaves a choice
point depends on the Prolog system. I get:
SWI-Prolog 8.3.15 (some choice point):
?- neq(s(s(0)), s(s(0))).
false.
?- neq(s(s(0)), s(0)).
true ;
false.
Jekejeke Prolog 1.4.6 (no choice point):
?- neq(s(s(0)), s(s(0))).
No
?- neq(s(s(0)), s(0)).
Yes
Just removing the unwanted choicepoint (in swi-prolog) from user502187's answer:
neq(0, s(_)).
neq(s(N), M) :-
% Switch args, to use first-arg indexing
neq_(M, s(N)).
neq_(0, s(_)).
neq_(s(N), s(M)) :-
% Switch args back, to fix choicepoint
neq(M, N).
Results in swi-prolog:
?- neq(s(s(0)), s(0)).
true.
?- neq(s(0), s(s(0))).
true.
?- neq(N, M).
N = 0,
M = s(_) ;
N = s(_),
M = 0 ;
N = s(s(_)),
M = s(0) ;
N = s(0),
M = s(s(_)) ;
N = s(s(0)),
M = s(s(s(_))) ;
N = s(s(s(_))),
M = s(s(0)) ;

IF-THEN in canonical form?

defining IF like this :
dynamic(if/1).
op(200, fx, if).
op(150, xfx, then).
op(100, xfy, and).
op(100, xfy, or).
generates the following canonical form :
?- write_canonical(if x then y).
if(then(x,y))
?- write_canonical(if x and z then y).
if(then(and(x,z),y))
?- write_canonical(if x and z or t then y).
if(then(and(x,or(z,t)),y))
Is there a way to generate :
if( conds, then(actions) ).
OR even better :
if( conds, (actions) ).
like this :
if(x,y)
if(x, then(y))
if( and(x,or(z,t)), then(y))
if( and(x,or(z,t)), (y))
one possible alternative I can see :)
?- op(200, xfy, ==>).
?- write_canonical(x ==> y).
==>(x,y)
?- write_canonical(x and z ==> y).
==>(and(x,z),y)
I found better solution to generate normal clauses. Instead of "then" I can just use ":-"
?- write_canonical(if x and z :- y ).
:-(if(and(x,z)),y)
?- assert(if x and z :- write(axz) ).
?- if x and z.
axz

Prolog: How to check if a predicate exists?

How can I check if a predicate exists in a Prolog program? That would be an exists/1, like:
?- exists(some_predicate).
false.
?- assert(some_predicate).
true.
?- exists(some_predicate).
true.
You can use current_predicate/1, current_predicate/2 or predicate_property/2 (for the last you will probably need functor/3):
?- current_predicate(a/1).
false.
?- functor(A,a,1),predicate_property(A,visible).
false.
?- functor(A,a,1),current_predicate(_,A).
false.
?- assert(a(42)).
true.
?- current_predicate(a/1).
true.
?- functor(A,a,1),predicate_property(A,visible).
A = a(_G136).
?- functor(A,a,1),current_predicate(_,A).
A = a(_G122).
current_predicate/2 and predicate_property/2 (with visible) succeeds if the predicate can be autoloaded while currrent_predicate/1 fails
the 'old fashioned way', but accepted in ISO, is clause/2. You could encounter it while reusing/browsing some of the older examples.
example:
?- [user].
|: app([], Y, Y).
|: app([X|Xs], Y, [X|Zs]) :- app(Xs, Y, Zs).
|: % user://1 compiled 0,15 sec, 17 clauses
true.
?- clause(app(X,Y,Z),Body).
X = [],
Y = Z,
Body = true ;
X = [_G338|_G339],
Z = [_G338|_G342],
Body = app(_G339, Y, _G342).

Resources