Prolog:在 Prolog 中定义逻辑运算符作为其他运算符的占位符 [英] Prolog: define logical operator in Prolog as placeholder for other operator
问题描述
我的目标是在 prolog 中编写一个小证明助手.我的第一步是定义逻辑连接词如下:
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, #).
最后一个操作符 # 只是作为 &
、v
、->
或 < 的占位符;->
.我的问题是,我不知道如何在序言中定义它.我尝试通过以下方式解决我的问题:
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.
但是下面的定义:
proposition(X) :- atomicproposition(X).
proposition(X # Y) :- proposition(X), proposition(Y).
proposition(- X) :- proposition(X).
与
atomicproposition(a).
给予
?- proposition(a v -a).
false
我做错了什么?
推荐答案
您不能以这种方式定义句法同义词.当你定义类似
You cannot define syntactic synonyms this way. When you define something like
X # Y :-
X & Y.
你定义语义:执行X#Y
,执行X&是
".但是您还没有定义任何方式来执行 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.)
您正在寻找的是一种不仅可以定义T
是带有二元运算符的术语",而且理想情况下还可以定义T
的操作数是 X
和 Y
".像这样:
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).
然后,用:
proposition(X) :-
atomicproposition(X).
proposition(Binary) :-
binary_x_y(Binary, X, Y),
proposition(X),
proposition(Y).
proposition(- X) :-
proposition(X).
atomicproposition(a).
我们得到:
?- 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, &, ->, <->]).
这篇关于Prolog:在 Prolog 中定义逻辑运算符作为其他运算符的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!