开发者

Problem when trying to define an operator in Prolog

I have defined a prolog file with the following code:

divisible(X, Y) :-
    X mod Y =:= 0.

divisibleBy(X, Y) :-
    divisible(X, Y).

op(35,xfx,divisibleBy).

Prolog is complaining that

'$record_clause'/2: No permission to modify static_procedure `op/3'

What am I doing wrong? I want to define an divisibleBy operator that will allow me to write code like the following:

开发者_如何学Python4 divisibleBy 2

Thanks.


Use

:- op(35,xfx,divisibleBy).

:- tells the Prolog interpreter to evaluate the next term while loading the file, i.e. make a predicate call, instead of treating it as a definition (in this case a redefinition of op/3).


The answer given by @larsmans is spot-on regarding your original problem.

However, you should reconsider if you should define a new operator.

In general, I would strongly advise against defining new operators for the following reasons:

  • The gain in readability is often overrated.
  • It may easily introduce new problems in places you wouldn't normally expect buggy.
  • It doesn't "scale" well: a small number of operators can make code on presentation slides super-concise, but what if you add more discriminate union cases over time? More operators?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜