如何在swi-prolog中导入运营商?

时间:2016-09-05 12:19:32

标签: module prolog swi-prolog

我有以下文件:

:- use_module(library(clpfd)).

isPowTwo(N) :- N #> 0, N #= 2^_.

一切正常:

?- [importTest].
%     library(pairs) compiled into pairs 0.00 sec, 22 clauses
%    library(lists) compiled into lists 0.01 sec, 122 clauses
%    library(occurs) compiled into occurs 0.00 sec, 14 clauses
%   library(apply_macros) compiled into apply_macros 0.01 sec, 168 clauses
%   library(assoc) compiled into assoc 0.01 sec, 103 clauses
%  library(clpfd) compiled into clpfd 0.15 sec, 2,808 clauses
% importTest compiled 0.16 sec, 2,813 clauses
true.

现在我只想导入两个用过的运算符:

:- use_module(library(clpfd), [(#>)/2, (#=)/2]).

isPowTwo(N) :- N #> 0, N #= 2^_.

它不起作用:

?- [importTest].
%     library(pairs) compiled into pairs 0.00 sec, 22 clauses
%    library(lists) compiled into lists 0.01 sec, 122 clauses
%    library(occurs) compiled into occurs 0.00 sec, 14 clauses
%   library(apply_macros) compiled into apply_macros 0.01 sec, 168 clauses
%   library(assoc) compiled into assoc 0.01 sec, 103 clauses
%  library(clpfd) compiled into clpfd 0.16 sec, 2,808 clauses
ERROR: .../importTest.pl:3:17: Syntax error: Operator expected
% importTest compiled 0.16 sec, 2,812 clauses
true.

?- isPowTwo(1).
ERROR: toplevel: Undefined procedure: isPowTwo/1 (DWIM could not correct goal)

将括号括在#>#=附近没有任何区别。

1 个答案:

答案 0 :(得分:5)

您的use_module/2指令中存在语法错误(因此Syntax error: Operator expected错误消息)。你可以写下来纠正它:

:- use_module(library(clpfd), [(#>)/2, (#=)/2]).

但您还需要导入相应的运算符以用于isPowTwo/1谓词的定义:

:- use_module(library(clpfd), [(#>)/2, (#=)/2, op(700,xfx,(#>)), op(700,xfx,(#=))]).