Prolog - 命题逻辑中的公式
我正在尝试创建一个谓词来验证给定的输入是否代表一个公式。
我只能使用命题原子,如 p、q、r、s、t 等。 我必须测试的公式如下:
neg(X) - represents the negation of X
and(X, Y) - represents X and Y
or(X, Y) - represents X or Y
imp(X, Y) - represents X implies Y
我创建了谓词 wff
,如果给定结构是公式,则返回 true,否则返回 false。另外,我不必在公式中使用变量,只需使用下面提到的命题原子。
logical_atom( A ) :-
atom( A ),
atom_codes( A, [AH|_] ),
AH >= 97,
AH =< 122.
wff(A):-
\+ ground(A),
!,
fail.
wff(and(A, B)):-
wff(A),
wff(B).
wff(neg(A)):-
wff(A).
wff(or(A, B)):-
wff(A),
wff(B).
wff(imp(A, B)):-
wff(A),
wff(B).
wff(A):-
ground(A),
logical_atom(A),
!.
当我介绍这样的测试时, wff(and(q, imp(or(p, q), neg(p)))).
,调用同时返回 true
和 false< /代码>值。你能告诉我为什么会这样吗?
I am trying to make a predicate in order to validate if a given input represents a formula.
I am allowed to use only to propositional atoms like p, q, r, s, t, etc.
The formulas which I have to test are the following:
neg(X) - represents the negation of X
and(X, Y) - represents X and Y
or(X, Y) - represents X or Y
imp(X, Y) - represents X implies Y
I have made the predicate wff
which returns true if a given structure is a formula and false the otherwise. Also, I don't have to use variables inside the formula, only propositional atoms as mentioned bellow.
logical_atom( A ) :-
atom( A ),
atom_codes( A, [AH|_] ),
AH >= 97,
AH =< 122.
wff(A):-
\+ ground(A),
!,
fail.
wff(and(A, B)):-
wff(A),
wff(B).
wff(neg(A)):-
wff(A).
wff(or(A, B)):-
wff(A),
wff(B).
wff(imp(A, B)):-
wff(A),
wff(B).
wff(A):-
ground(A),
logical_atom(A),
!.
When i introduce a test like this one,wff(and(q, imp(or(p, q), neg(p)))).
, the call returns both the true
and false
values. Can you please tell me why it happens like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您选择表示公式的数据结构称为“默认”,因为您需要一个“默认”情况来测试原子标识符:不上述内容的所有内容(和、或、负、 imp) 并满足逻辑原子/1 是一个逻辑原子(在您的表示中)。解释器无法通过函子来区分这些情况以应用第一个参数索引。与和/或/等类似,也为原子变量配备其专用函子,例如“atom(...)”,这要干净得多。 wff/1 可以读取:
您的查询现在是确定性的:
如果您的公式最初不是以这种方式表示的,那么最好先将它们转换为这样的形式,然后使用这种非默认的表示形式以便进一步加工。
另一个优点是,您现在不仅可以轻松测试,还可以枚举格式正确的公式,其代码如下:
以及查询:
Yielding:
作为其输出。
The data structure you chose to represent formulae is called "defaulty", because you need a "default" case to test for atomic identifiers: Everything that is not something of the above (and, or, neg, imp) and satisfies logical_atom/1 is a logical atom (in your representation). The interpreter cannot distinguish these cases by their functor to apply first-argument indexing. It is much cleaner to, in analogy to and/or/etc., also equip atomic variables with their dedicated functor, say "atom(...)". wff/1 could then read:
Your query is now deterministic as desired:
If your formulae are initially not represented in this way, it is still better to first convert them to such a form, and then to use such a representation which is not defaulty for further processing.
An additional advantage is that you can now easily not only test but also enumerate well-formed formulae with code like:
And a query like:
Yielding:
as its output.