我在Prolog中使用Ivan Bratko书籍“人工智能编程”研究DCG语法,并发现一些问题以了解Prolog如何自动转换DCG语法进入一套Prolog规则。关于Prolog如何自动将DCG语法转换为一组规则的疑问
例如我有以下的DCG语法:
move --> step.
move --> step, move.
step --> [up].
step --> [down].
其中:
moove是不可能性mooves的列表,并一步是一个单一的举动,可能是了或down
因此,它说一个动作列表可以是一个动作(一个步骤)或一个列表,一个由更多动作组成的列表,可以看作是一个动作(一个步骤),然后是一个动作列表(动作)。
所以这个语法可以生成词组,如下面的列表:向上,向下,向下,向上,向下]或类似:[高达]
这是非常简单的
然后,他展示了如何Prolog的自动前面的DCG语法转换成一套标准的序言规则(我已经改名为MOVE2和第二步,只是因为我已经在前面的DCG文法的同一个文件把这段代码):
/* move2 it is TRUE if the difference from the lists List and Rest is an
acceptable move.
*/
/* BASE CASE: A moves list can be a single move (a step that can be "up" or "down")
It is TRUE that the difference from the lists List and Rest is an acceptable move
if it is TRUE that the head of the list is an acceptable move
*/
move2(List, Rest) :- step2(List, Rest).
/* GENERAL CASE: A moves list can be a single move followed by a list of moves.
The difference of List1 and Rest is a valid move if it is TRUE that:
the difference between List1 and List 2 is a step
the difference between List2 and Rest is a move
*/
move2(List1, Rest) :- step2(List1, List2),
move2(List2, Rest).
/* step predicate extract a single step ("up" or "down") from a list
*/
step2([up|Rest], Rest).
step2([down|Rest], Rest).
我试图解释这些规则的意义,正如我写在评论,但我不太清楚我的解释...
你能给我一些提示,以便理解它吗?
TNX
安德烈
您的'移动// 0'不等于OP张贴的内容,因为它也接受空列表,但是OP的定义没有。另外'one_or_more // 2'应该被称为'zero_or_more // 2'来反映这一点。 – mat
mmm我对这行代码有一些怀疑:Rule = .. [R,V] 什么是= ..? – AndreaNobili
这是“univ”运算符,它构造了术语。尝试一下。 –