gpt4 book ai didi

Prolog 程序返回命题公式中的原子

转载 作者:行者123 更新时间:2023-12-02 09:13:43 24 4
gpt4 key购买 nike

我是序言新手,正在尝试编写一个程序,以格式良好的命题公式返回原子。例如,查询 ats(and(q, imp(or(p, q), neg(p))), As). 应返回 [p,q]对于作为。下面是我的代码,它将公式返回为 As。我不知道如何分割F1中的ats中的单个F中的F2 wff 所以 wff/2 永远不会被调用。请我需要帮助才能从这里继续。谢谢。

代码

logical_atom( A ) :-
atom( A ),
atom_codes( A, [AH|_] ),
AH >= 97,
AH =< 122.

wff(A):- ground(A),
logical_atom(A).

wff(neg(A)) :- ground(A),wff(A).

wff(or(F1,F2)) :-
wff(F1),
wff(F2).
wff(and(F1,F2)) :-
wff(F1),
wff(F2).

wff(imp(F1,F2)) :-
wff(F1),
wff(F2).
ats(F, As):- wff(F), setof(F, logical_atom(F), As).

最佳答案

首先,考虑使用更清晰的表示形式:目前,您无法通过公共(public)仿函数来区分原子。因此,将它们包装在 a(Atom) 中。

其次,使用 DCG 来描述格式良好的公式与其原子列表之间的关系,例如:

wff_atoms(a(A))       --> [A].
wff_atoms(neg(F)) --> wff_atoms(F).
wff_atoms(or(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).
wff_atoms(and(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).
wff_atoms(imp(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).

示例查询及其结果:

?- phrase(wff_atoms(and(a(q), imp(or(a(p), a(q)), neg(a(p))))), As).
As = [q, p, q, p].

关于Prolog 程序返回命题公式中的原子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26544693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com