为什么某些DCG测试用例使用assert(Rest == [])?

时间:2019-03-25 14:42:21

标签: prolog dcg

answer使用assertion( Rest == [] )?

pharse/3

phrase(:DCGBody, ?List, ?Rest)

由于phrase/2等效于phrase(DCGBody, InputList, []).,而Rest = []只是Rest == [],在测试用例assertion( Rest == [] )中,有什么意义呢?添加--previous有什么好处?

1 个答案:

答案 0 :(得分:2)

phrase(GB__2, Xs, [])等效于phrase(GB__2, Xs),但不一定phrase(GB__2, Xs0,Xs)的第一个解决方案是Xs == []

这是一个最小的示例:

:- set_prolog_flag(double_quotes, chars).

ao1 --> "a" | [].

ao2 --> [] | "a".

虽然phrase(aoX, "a")都成功了,但phrase(aoX, "a", Xs)显示出找到的解决方案顺序有所不同。

?- phrase(ao1, "a", Xs).
      Xs = "a"
   ;  Xs = [].
?- phrase(ao2, "a", Xs).
      Xs = []
   ;  Xs = "a".