SWI-Prolog 中的约束编程库或语法问题
我只是想弄清楚 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于 Horsh 的答案,您应该在源代码中导入库,记住将
?-
放在行的开头,如下所示:?-
告诉 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: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.
对于将来发现此问题的其他人,如果您想在 SWI-Prolog 源文件中导入库,以下操作也将起作用:
注意
:-
而不是?-< /代码>。
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:
Note the
:-
and not?-
.全部内容都在此处和那里。
The is all in the manual here and there.