1

我正在尝试理解定句语法中统一的概念,是否有人能够逐步解释如何从以下 DCG 中得到答案。

s --> symbols(Sem,a), symbols(Sem,b).

symbols(s(end),S) --> [S].
symbols(s(Sem),S) --> [S], symbols(Sem,S).

答案产生aaabbb了,但我已经在网上搜索了很长时间,试图找到你如何得到这个答案,如果有人可以通过几个步骤来解释这个问题,我将永远感激不尽,或者至少展示一下工作,所以我可以看到怎么了。

在 DCG 的维基百科页面上可以找到一个类似的示例,但它会生成aaabbbccc.

谢谢

4

2 回答 2

2

Start by asking about all possible sentences. You could, for example take the most general query:

?- phrase(s,L).
L = [a, b] ;
L = [a, a, b, b] ;
L = [a, a, a, b, b, b] ;      % this is your example
L = [a, a, a, a, b, b, b, b] ...

In general, however, this method might enumerate answers/solutions in an unfair manner ; thereby hiding shorter examples. To ensure that you will find all solutions ordered by length:

?- length(L,N), phrase(s,L).

In this case, there is no difference. But think of adding the rule symbol(0,_) --> []. after all the other rules. L = [] would not show in the first query, it is "hidden by infinitely many solutions", but it would show as first solution in the second one.

So before trying to understand the more complex example you gave, consider [a, b] since it is the simplest example.

Another approach is to dissect the query phrase(s, L) and consider each of its constituents:

?- phrase(symbols(Sem,a),L).
Sem = s(0),
L = [a] ;
Sem = s(s(0)),
L = [a, a] ;
Sem = s(s(s(0))),
L = [a, a, a] 

So this part plus the same with b describes the entire sentence.

于 2014-06-18T09:01:27.380 回答
1

谓词从列表中symbols获取N时间。S但是N没有被编码为“正常”数字,它们被编码为 peano 算术中的数字:end是 0(通常使用 0 而不是 end)并且s(N)表示N+1.

该条款

symbols(s(end),S) --> [S].

需要一个元素S。我们可以解释s(end)为代表第一的术语。

该条款

symbols(s(Sem),S) --> [S], symbols(Sem,S).

接受一个元素S并递归调用具有相同元素的符号。同样,我们将其解释s(Sem)为一个数字: 那么它的意思是:取N+1( s(Sem)) 个元素S,先取一个S,然后递归取N元素S

谓词s只需要一些 as,然后是相同数量的 bs。

第一种解决方案实际上是ab因为symbols(Sem,a)与第一条规则匹配并与Sem统一s(end)。然后symbols(s(end),b)被调用,唯一匹配的规则是第一个。

于 2014-06-18T08:47:49.590 回答