序言中带有语言树的 If 语句

If statement with language tree in prolog

我目前正在生成一个序言树,输入如下:

the flight flew.

他的树是这样的:

s(the, np(flight), vp(flew))

生成所述树后,我试图检查在这种情况下 "the" 是否实际上是 "the"。还有另一种情况,它可能是 "did".

据我所知,prolog if 语句的格式如下:

( IF -> THEN; ELSE ),

我正在尝试做:

( s(the, A, B)) -> assert(Tree); do_something_else ),

但是当我这样做时,我无法 运行 程序。我将如何在 if 语句中询问树中的特定值?我首先正确地做了一个 if 语句吗?

当您尝试 运行 您的实际程序时,您需要能够提供有关实际发生情况的有用信息。换句话说,学习如何提供 minimal, complete, and verifiable example.

如果你的 "tree" 像你展示的那样在术语中,你所需要的只是一个在头部使用模式匹配(统一)的规则来检查 s/3 的第一个参数是否是 thedid:

foo(s(the, A, B)) :- /* do some stuff */.
foo(s(did, A, B)) :- /* do other stuff */.

这可能比使用 if-then-else 结构更好。如果你坚持,你将不得不说这样的话:

T = s(Foo, A, B),
(   Foo == the
->  /* do something */
;   Foo == did
->  /* do something else */
;   /* anything else */
)

这里的重点是 if-then-else 中的条件必须是被评估的东西,要么成功要么失败:你不能像在谓词的头部那样使用模式匹配。另一种写条件的迂回方式是使用类似 arg/3:

(   arg(1, T, the)
->  /* ... */
;   arg(1, T, did)
->  /* ... */
;   /* ... */
)

但令我困扰的是,在解析之后,您最终得到的不是冠词 the,就是动词 dids/3 的第一个参数应该代表句子的哪一部分?