Prolog: define logical operator in Prolog as placeholder for other operator

↘锁芯ラ 提交于 2021-01-28 07:06:26

问题


my aim is to write a little prove assistant in prolog. My first step is to define the logical connectives as follows:

:-op(800, fx, -).

:-op(801, xfy, &).

:-op(802, xfy, v).

:-op(803, xfy, ->).

:-op(804, xfy, <->).

:-op(800, xfy, #).

The last operator # just has the meaning to be the placeholder for &, v, -> or <->. My problem is, I don't know how I it is possible to define this in prolog. I tried to solve my problem in the following way:

X # Y :- X v Y; X & Y; X -> Y; X <-> Y.

but the following definition:

proposition(X) :- atomicproposition(X).

proposition(X # Y) :- proposition(X), proposition(Y).

proposition(- X) :- proposition(X). 

with

atomicproposition(a).

gives

?- proposition(a v -a).
false

What did i wrong?


回答1:


You cannot define syntactic synonyms this way. When you define something like

X # Y :-
    X & Y.

you define semantics: "to execute X # Y, execute X & Y". But you haven't defined any way to "execute X & Y":

?- X # Y.
ERROR: Undefined procedure: (&)/2
ERROR: In:
ERROR:    [9] _2406&_2408
ERROR:    [8] _2432#_2434 at /home/isabelle/op.pl:13
ERROR:    [7] <user>

(And even if you had defined some meaning for it, it might not be what you want.)

What you are looking for instead is a way to define a notion of not only "T is a term with a binary operator", but ideally also "T's operands are X and Y". Like this:

binary_x_y(X  v  Y, X, Y).
binary_x_y(X  &  Y, X, Y).
binary_x_y(X  -> Y, X, Y).
binary_x_y(X <-> Y, X, Y).

And then, with:

proposition(X) :-
    atomicproposition(X).
proposition(Binary) :-
    binary_x_y(Binary, X, Y),
    proposition(X),
    proposition(Y).
proposition(- X) :-
    proposition(X).

atomicproposition(a).

We get:

?- proposition(a v -a).
true ;
false.

?- proposition(P).
P = a ;
P =  (a v a) ;
P =  (a v a v a) ;
P =  (a v a v a v a) ;
P =  (a v a v a v a v a) ;
P =  (a v a v a v a v a v a) .  % unfair enumeration

There are other ways of expressing the same relation with a bit less typing, for example:

binary_x_y(Binary, X, Y) :-
    Binary =.. [Op, X, Y],  % Binary is of the form Op(X, Y)
    member(Op, [v, &, ->, <->]).


来源:https://stackoverflow.com/questions/63890053/prolog-define-logical-operator-in-prolog-as-placeholder-for-other-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!