Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

4 divisibleBy 2

Thanks.

like image 641
devoured elysium Avatar asked Sep 29 '10 16:09

devoured elysium


2 Answers

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).

like image 134
Fred Foo Avatar answered Sep 24 '22 00:09

Fred Foo


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?
like image 24
repeat Avatar answered Sep 21 '22 00:09

repeat