SWI-Prolog 中的约束编程库或语法问题

发布于 2024-10-31 08:05:25 字数 831 浏览 0 评论 0原文

我只是想弄清楚 SWI-Prolog 中的约束编程,请查看本教程: http:// /en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming

然而,我似乎在第一个障碍处跌倒了。

?- use_module(library(clpfd)).
true.

?- X #> Y, X in 1..3, Y=2.
ERROR: Syntax error: Operator expected
ERROR: X 
ERROR: ** here **
ERROR: #> Y, X in 1..3, Y=2 . 
?- 

这里出了什么问题?我似乎已经包含了该库,但教程中的第一个示例行引发了语法错误。

我能找到的所有教程似乎都使用像#=、#<这样的运算符。但我的 SWI-Prolog 对它们犹豫不决。它们是约束库附带的额外语法吗? (我是否无法加载它?)

或者我误读了教程示例?

更新:试图从下面霍什的回复中理解一些事情。如果我使用该库并在交互式终端中运行该行,我就可以让它工作。但是,如果我尝试导入库并在源文件中使用这些运算符,则会再次引发错误。我不明白什么?

更新2:

好的。如果在我的源文件中,我调用该库,然后编写一条包含 #> 的规则。然后我尝试从命令行查阅它。它会抛出一个错误并且 #>;语法无法识别。如果尝试查阅程序之前将库导入到命令行,则它可以工作。这可以吗?

I'm just trying to figure out constraint programming in SWI-Prolog, looking at this tutorial : http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming

However I seem to be falling at the first hurdle.

?- use_module(library(clpfd)).
true.

?- X #> Y, X in 1..3, Y=2.
ERROR: Syntax error: Operator expected
ERROR: X 
ERROR: ** here **
ERROR: #> Y, X in 1..3, Y=2 . 
?- 

What's going wrong here? I seem to have included the library, but the first example line from the tutorial throws a syntax error.

All the tutorials I can find seem to use operators like #=, #< etc. But my SWI-Prolog baulks at them. Are they an extra syntax which comes with that constraint library? (And am I failing to load it?)

Or am I misreading the tutorial examples?

Update : Trying to understand things from Horsh's reply below. I can get this to work if I use the library and run the line in the interactive terminal. But if I try to import the library and use these operators in a source file, then it throws the error again. What am I not understanding?

Update 2 :

OK. If, in my source file, I invoke the library and then write a rule which contains a #>. Then I try to consult it from the command-line. It will throw an error and the #> syntax is un-recognised. If import the library to the command line before trying to consult the program, it works. Can this be right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

沫离伤花 2024-11-07 08:05:25

基于 Horsh 的答案,您应该在源代码中导入库,记住将 ?- 放在行的开头,如下所示:

?- use_module(library(clpfd)).

?- 告诉 SWI- Prolog 执行该行,就好像直接将其输入到解释器中一样,而不是尝试将其声明为程序中的谓词。

不必担心 SWI-Prolog 多次导入库,它知道检查库是否被修改,并且仅在库自上次加载以来发生更改时才重新加载它。

Building on Horsh's answer, you should be importing the library in your source code, remembering to put ?- at the beginning of the line like so:

?- use_module(library(clpfd)).

The ?- tells SWI-Prolog to execute the line as if it were typed into the interpreter directly, instead of trying to declare it as a predicate in your program.

Don't be concerned about SWI-Prolog importing the library more than once, it knows to check if the library was modified and only reloads it if the library was changed since the last time it was loaded.

我不咬妳我踢妳 2024-11-07 08:05:25

对于将来发现此问题的其他人,如果您想在 SWI-Prolog 源文件中导入库,以下操作也将起作用:

:- use_module(library(clpfd)).

注意 :- 而不是 ?-< /代码>。

For anyone else that finds this in the future, if you want to import a library in an SWI-Prolog source file, the following will also work:

:- use_module(library(clpfd)).

Note the :- and not ?-.

神魇的王 2024-11-07 08:05:25

全部内容都在此处那里

?- [library(clpfd)].
%   library(error) compiled into error 0.00 sec, 10,128 bytes
%  library(apply) compiled into apply 0.00 sec, 16,840 bytes
%  library(assoc) compiled into assoc 0.00 sec, 13,132 bytes
%  library(lists) compiled into lists 0.00 sec, 14,332 bytes
%  library(pairs) compiled into pairs 0.00 sec, 5,372 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 392,604 bytes
true.

?- X #> Y, X in 1..3, Y=2.
X = 3,
Y = 2.

The is all in the manual here and there.

?- [library(clpfd)].
%   library(error) compiled into error 0.00 sec, 10,128 bytes
%  library(apply) compiled into apply 0.00 sec, 16,840 bytes
%  library(assoc) compiled into assoc 0.00 sec, 13,132 bytes
%  library(lists) compiled into lists 0.00 sec, 14,332 bytes
%  library(pairs) compiled into pairs 0.00 sec, 5,372 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 392,604 bytes
true.

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