在 SWI-prolog 中调试 - 未绑定变量
考虑以下 Prolog 代码。它在其输入中编辑特定类型的行,并打印出剩余的行,而不进行任何更改。它使用一个名为 rule
的 DCG,该 DCG 未包含在下面,因为它对问题并不重要。
go:-
prompt(_, ''),
processInput.
processInput:-
read_line_to_codes(current_input, Codes),
processInput(Codes).
processInput(Codes):-
(Codes \= end_of_file
->
(phrase(rule(Part1, Part2), Codes)
->
format('~s - ~s\n', [ Part1, Part2 ])
;
format('~s\n', [ Codes ])),
processInput
;
true).
:- go, halt.
这很好用。但是,假设我将 processInput/1 更改为以下内容,它只是说警告:/home/asfernan/tmp/tmp.pl:28:目标(指令)失败:用户:(走,停止)。
processInput(Codes):-
(Codes \= end_of_file
->
(\+phrase(rule(Part1, Part2), Codes)
->
format('~s\n', [ Codes ]))
;
format('~s - ~s\n', [ Part1, Part2 ]),
processInput
;
true).
如果& phrase(rule(Part1, Part2), Codes)
DCG 匹配的其他部分已交换。这显然是一个新手错误,但 go,halt
失败的事实并没有多大帮助。我该怎么做才能使错误消息表明失败是因为 Part1
& Part2
未绑定在 format('~s - ~s\n', [ Part1, Part2 ])
行中?我能够找到这个错误,因为代码很小,但如果代码很大,我可能无法做到这一点。
Consider the following Prolog code. It edits lines of a particular type in its input and prints out the remaining lines w/o any change. It uses a DCG called rule
which isn't included below, since it's not important to the question.
go:-
prompt(_, ''),
processInput.
processInput:-
read_line_to_codes(current_input, Codes),
processInput(Codes).
processInput(Codes):-
(Codes \= end_of_file
->
(phrase(rule(Part1, Part2), Codes)
->
format('~s - ~s\n', [ Part1, Part2 ])
;
format('~s\n', [ Codes ])),
processInput
;
true).
:- go, halt.
This works fine. However, suppose I change processInput/1
to the following, it just says that Warning: /home/asfernan/tmp/tmp.pl:28: Goal (directive) failed: user: (go,halt)
.
processInput(Codes):-
(Codes \= end_of_file
->
(\+phrase(rule(Part1, Part2), Codes)
->
format('~s\n', [ Codes ]))
;
format('~s - ~s\n', [ Part1, Part2 ]),
processInput
;
true).
The if & else parts of the phrase(rule(Part1, Part2), Codes)
DCG match have been exchanged. This is obviously a newbie mistake, but the fact that go, halt
failed isn't very helpful. What can I do to make the error message indicate that the failure was because Part1
& Part2
were not bound in the format('~s - ~s\n', [ Part1, Part2 ])
line? I was able to track down this error because the code is small, but I may not have been able to do so had the code been big.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Prolog 中,以下情况是不一样的:
并且
一般情况下,目标
\+ Cond
永远不会实例化其变量。所以你必须坚持原来的配方。
如果您有兴趣处理整个
带有 DCG 的文件,请考虑 SWI 的
library(pio)
。In Prolog the following is not the same:
and
In general, a goal
\+ Cond
will never instantiate its variables. So youhave to stick to the original formulation.
In case you are interested to process entire
files with DCGs, consider SWI's
library(pio)
.