【发布时间】:2013-05-15 16:03:39
【问题描述】:
我正在使用 Ivan Bratko 的《人工智能编程》一书在 Prolog 中研究 DCG 语法,我发现一些问题来理解 Prolog 如何自动将 DCG 语法转换为一组 Prolog 规则。
例如我有以下DCG语法:
move --> step.
move --> step, move.
step --> [up].
step --> [down].
地点:
moove 是一个可能的moves 列表,step 是一个可以up 或down 的单一动作
所以它说一个移动列表可以是一个单一的移动(一个步骤)或一个列表由多个移动组成的列表,可以看作是一个单一的移动(一个步骤),然后是一个移动的列表(一个移动)。
所以这个语法可以生成如下列表的短语:[up, down, down, up, down] 或like: [up]
这很简单
然后他展示了Prolog如何自动将之前的DCG语法转换成一套标准的Prolog规则(我把它重命名为move2和step2只是因为我把这段代码放在了之前的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
安德烈亚
【问题讨论】: