Prolog 翻译成DCG文本不起作用
因为它使用列表,所以我想使用DCG解决它。在这个过程中,我意识到可以使用文本。() 最初的问题是返回列表中项目的计数,但如果两个相同的项目相邻,则不增加计数 虽然我的代码对某些测试用例有效,但对其他测试用例无效。这只是一个失败的条款。在使用调试器查看代码时,第二个状态变量returned list似乎是在调用子句时绑定的,而我认为它应该被解除绑定编辑解决了下面这部分问题 我正在使用SWI Prolog 8.0 引起问题的条款:Prolog 翻译成DCG文本不起作用,prolog,dcg,dcg-semicontext,Prolog,Dcg,Dcg Semicontext,因为它使用列表,所以我想使用DCG解决它。在这个过程中,我意识到可以使用文本。() 最初的问题是返回列表中项目的计数,但如果两个相同的项目相邻,则不增加计数 虽然我的代码对某些测试用例有效,但对其他测试用例无效。这只是一个失败的条款。在使用调试器查看代码时,第二个状态变量returned list似乎是在调用子句时绑定的,而我认为它应该被解除绑定编辑解决了下面这部分问题 我正在使用SWI Prolog 8.0 引起问题的条款: count_dcg(N0,N),[C2] --> [C
count_dcg(N0,N),[C2] -->
[C1,C2],
{ N is N0 + 1 }.
注意:C1被标记为单一变量:[C1]
通常,我会将C1
更改为。
,但在这种情况下,我需要指出当前正在处理的第一个和第二个项目是不同的。换言之,它是以统一失败为挡箭牌
使用清单/1查看DCG会发现使用\uuu
,这可能是问题所在,但不确定
count_dcg(C, B, A, E) :-
A=[_, F|D],
B is C+1,
G=D,
E=[F|G].
使用DCG解决问题的正确方法是什么
见下文
当前源代码
% empty list
% No semicontext (push back) needed because last item in list.
count_dcg(N,N) --> [].
% single item in list
% No semicontext (push back) needed because only one item removed from list.
count_dcg(N0,N) -->
[_],
\+ [_],
{ N is N0 + 1 }.
% Semicontext (push back) needed because two items removed from list.
% Need second item to stay on list.
count_dcg(N,N),[C] -->
[C,C].
% Semicontext (push back) needed because two items removed from list.
% Need second item to stay on list.
count_dcg(N0,N),[C2] -->
[C1,C2],
{ N is N0 + 1 }.
count(L,N) :-
DCG = count_dcg(0,N),
phrase(DCG,L).
测试用例
:- begin_tests(count).
test(1,[nondet]) :-
count([],N),
assertion( N == 0 ).
test(2,[nondet]) :-
count([a],N),
assertion( N == 1 ).
test(3,[nondet]) :-
count([a,a],N),
assertion( N == 1 ).
test(4,[nondet]) :-
count([a,b],N),
assertion( N == 2 ).
test(5,[nondet]) :-
count([b,a],N),
assertion( N == 2 ).
test(6,[nondet]) :-
count([a,a,b],N),
assertion( N == 2 ).
test(7,[nondet]) :-
count([a,b,a],N),
assertion( N == 3 ).
test(8,[nondet]) :-
count([b,a,a],N),
assertion( N == 2 ).
:- end_tests(count).
测试运行
?- run_tests.
% PL-Unit: count ..
ERROR: c:/question_110.pl:80:
test 3: failed
ERROR: c:/question_110.pl:84:
test 4: failed
ERROR: c:/question_110.pl:88:
test 5: failed
ERROR: c:/question_110.pl:92:
test 6: failed
ERROR: c:/question_110.pl:96:
test 7: failed
ERROR: c:/question_110.pl:100:
test 8: failed
done
% 6 tests failed
% 2 tests passed
false.
?- run_tests.
% PL-Unit: count ........ done
% All 8 tests passed
true.
编辑1 意识到需要对其中两个谓词进行尾部调用
% Semicontext (push back) needed because two items removed from list.
% Need second item to stay on list.
count_dcg(N0,N),[C] -->
[C,C],
count_dcg(N0,N).
% Semicontext (push back) needed because two items removed from list.
% Need second item to stay on list.
count_dcg(N0,N),[C2] -->
[C1,C2],
{
C1 \== C2,
N1 is N0 + 1
},
count_dcg(N1,N).
代码仍然不起作用,但这解释了为什么状态变量被绑定,而我期望它被解除绑定
编辑2 虽然没有像我所希望的那样使用DCG Semicondext,而是使用Semicondext的一个变体作为前瞻,但代码仍然有效。不将此作为答案发布,因为我希望答案要么显示DCG代码与子句标题上的文本一起工作,要么解释为什么这是错误的
lookahead(C),[C] -->
[C].
% empty list
% No lookahead needed because last item in list.
count_3_dcg(N,N) --> [].
% single item in list
% No lookahead needed because only one in list.
count_3_dcg(N0,N) -->
[_],
\+ [_],
{ N is N0 + 1 }.
% Lookahead needed because two items in list and
% only want to remove first item.
count_3_dcg(N0,N) -->
[C1],
lookahead(C2),
{ C1 == C2 },
count_3_dcg(N0,N).
% Lookahead needed because two items in list and
% only want to remove first item.
count_3_dcg(N0,N) -->
[C1],
lookahead(C2),
{
C1 \== C2,
N1 is N0 + 1
},
count_3_dcg(N1,N).
count(L,N) :-
DCG = count_3_dcg(0,N),
phrase(DCG,L).
测试运行
?- run_tests.
% PL-Unit: count ..
ERROR: c:/question_110.pl:80:
test 3: failed
ERROR: c:/question_110.pl:84:
test 4: failed
ERROR: c:/question_110.pl:88:
test 5: failed
ERROR: c:/question_110.pl:92:
test 6: failed
ERROR: c:/question_110.pl:96:
test 7: failed
ERROR: c:/question_110.pl:100:
test 8: failed
done
% 6 tests failed
% 2 tests passed
false.
?- run_tests.
% PL-Unit: count ........ done
% All 8 tests passed
true.
不需要推送列表或前瞻的替代解决方案:
count_dcg(N0,N) -->
[C], {N1 is N0 + 1}, count_dcg(N1,N,C).
count_dcg(N,N) -->
[].
count_dcg(N0,N,C) -->
[C],
count_dcg(N0,N,C).
count_dcg(N0,N,C) -->
[C1],
{C \== C1, N1 is N0 + 1},
count_dcg(N1,N,C1).
count_dcg(N,N,_) -->
[].
count(L,N) :-
phrase(count_dcg(0,N),L).
self注意:有
count\u dcg/2
和count\u dcg/3
@GuyCoder:通常,在同一谓词或非谓词的子句之间避免空行-terminal@false谢谢,我一直在修改我的代码来实现这一点。这个评论是因为起初我没有看到参数数量上的差异。有一次我看到对代码的理解是显而易见的。