在 SWI-prolog 中调试 - 未绑定变量

发布于 2024-11-11 18:46:49 字数 1208 浏览 5 评论 0原文

考虑以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2024-11-18 18:46:49

在 Prolog 中,以下情况是不一样的:

..., ( Cond -> Then ; Else ), ...

并且

..., ( \+ Cond -> Else ; Then ), ...

一般情况下,目标 \+ Cond 永远不会实例化其变量。所以你
必须坚持原来的配方。

如果您有兴趣处理整个
带有 DCG 的文件,请考虑 SWI 的library(pio)

In Prolog the following is not the same:

..., ( Cond -> Then ; Else ), ...

and

..., ( \+ Cond -> Else ; Then ), ...

In general, a goal \+ Cond will never instantiate its variables. So you
have to stick to the original formulation.

In case you are interested to process entire
files with DCGs, consider SWI's library(pio).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文