Prolog根据输入决定选择某个规则

时间:2013-02-23 23:25:27

标签: dynamic prolog dcg prolog-assert

我有这个prolog问题我无法解决。我想要实现的是断言FACT A,当我输入时收回事实B:当我输入put时,取得并断言事实B并收回事实A.

即:

:- dynamic s/2.
:- dynamic s/3.

s(P0, s(V, NP)) --> v(P1, V), np(P2, NP), {P0 is P1*P2*0.35}.

s(P0, s(V, NP, PP)) --> v(P1, V), np(P2, NP), pp(P3, PP), {P0 is P1*P2*P3*0.65}.
s(P0, s(V, NP)) --> v(P1, V), np(P2, NP), {V == take -> P0 is P1*P2*0.35; P0 is 0}.
s(P0, s(V, NP, PP)) --> v(P1, V), np(P2, NP), pp(P3, PP), {V == put -> P0 is P1*P2*P3*0.65; P0 is 0}.

np(P0, np(D, N)) --> det(P1, D), n(P2, N), {P0 is P1*P2*0.36}.
np(P0, np(D, A, N)) --> det(P1, D), a(P2, A), n(P3, N), {P0 is P1*P2*P3*0.46}.
np(P0, np(D, N, PP)) --> det(P1, D), n(P2, N), pp(P3, PP), {P0 is P1*P2*P3*0.13}.
np(P0, np(D, A, N, PP)) --> det(P1, D), a(P2, A), n(P3, N), pp(P4, PP), {P0 is P1*P2*P3*P4*0.05}.

pp(P0, pp(P, NP)) --> p(P1, P), np(P2, NP), {P0 is P1*P2*1.0}.

v(0.65, v(put)) --> {retract(s(V, NP))}, [put].
v(0.35, v(take)) --> {retract(s(V, NP, PP))}, [take].

n(0.23, n(block)) --> [block].
n(0.25, n(circle)) --> [circle].
n(0.15, n(cone)) --> [cone].
n(0.12, n(cube)) --> [cube].
n(0.25, n(square)) --> [square].

a(0.56, a(blue)) --> [blue].
a(0.27, a(green)) --> [green].
a(0.17, a(red)) --> [red].

det(1.0, det(the)) --> [the].

p(1.0, p(on)) --> [on].

我无法让它发挥作用:任何帮助都会受到赞赏。

编辑:发布的所有代码

3 个答案:

答案 0 :(得分:1)

我怀疑问题出在代码的其他地方。这对SWI来说很有用:

:- dynamic s/1.

foo --> "hello",   { retractall(s(_)), asserta(s(hi))  }.
foo --> "goodbye", { retractall(s(_)), asserta(s(bye)) }.

例如:

?- s(X).
false.

?- phrase(foo, "hello").
true .

?- phrase(foo, "hello").
true ;
false.

?- s(X).
X = hi.

?- phrase(foo, "goodbye").
true.

?- s(X).
X = bye.

我很好奇为什么你这样做。在所有条件相同的情况下,我倾向于使用您声称的信息来增加您生成的AST。然后,我对动态商店有偏见。

答案 1 :(得分:1)

我不完全理解您的代码,但我有一种感觉:

v(0.65, v(put)) --> {retract(s(V, NP))}, [put].

v(0.35, v(take)) --> {retract(s(V, NP, PP))}, [take].

应该是:

v(0.65, v(put)) --> [put], {retract(s(V, NP))}.

v(0.35, v(take)) --> [take], {retract(s(V, NP, PP))}.

但是,为什么VNPPP未实例化?如果要删除所有出现的内容,请使用retractall/1;如果只有一次出现我建议使用swipl's global variables。在任何情况下,通过在DCG中使用副作用就像对付魔鬼一样;我是在我的编译器中完成的,这是一个让人痛苦的地狱XD

答案 2 :(得分:1)

这是我最终做的事情,我使用约束来选择某个规则而不是另一个规则: 这是代码:

s(P0, s(V, NP)) --> v(P1, V), np(P2, NP), {P0 is P1*P2*0.35, V == v(take)}.
s(P0, s(V, NP, PP)) --> v(P1, V), np(P2, NP), pp(P3, PP), {P0 is P1*P2*P3*0.65, V == v(put)}.

np(P0, np(D, N)) --> det(P1, D), n(P2, N), {P0 is P1*P2*0.36}.
np(P0, np(D, A, N)) --> det(P1, D), a(P2, A), n(P3, N), {P0 is P1*P2*P3*0.46}.
np(P0, np(D, N, PP)) --> det(P1, D), n(P2, N), pp(P3, PP), {P0 is P1*P2*P3*0.13}.
np(P0, np(D, A, N, PP)) --> det(P1, D), a(P2, A), n(P3, N), pp(P4, PP), {P0 is P1*P2*P3*P4*0.05}.

pp(P0, pp(P, NP)) --> p(P1, P), np(P2, NP), {P0 is P1*P2*1.0, NP \= np(_, _ , _, _)}.

v(0.65, v(put)) --> [put].
v(0.35, v(take)) --> [take].

n(0.23, n(block)) --> [block].
n(0.25, n(circle)) --> [circle].
n(0.15, n(cone)) --> [cone].
n(0.12, n(cube)) --> [cube].
n(0.25, n(square)) --> [square].

a(0.56, a(blue)) --> [blue].
a(0.27, a(green)) --> [green].
a(0.17, a(red)) --> [red].

det(1.0, det(the)) --> [the].

p(1.0, p(on)) --> [on].