Re: No permission to modify static procedure

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 19/01/2014, at 15:05, Lumj <[email protected]> wrote:

> Is it possible to write a module in which there's a predicate write/1?
> When I try to do this, I got the 'No permission to modify static procedure' error. What I intend to do is to have a write/1 in my module and the system:write/1 remain where it is.
> Module file attached:
> :-module(m,[write/1]).
> write(x).
> Am I missing anything?

Not really. This behavior in not exclusive of the SWI-Prolog module system. But you can redefine backend Prolog compiler predicates using Logtalk objects easily:

:- object(o).

	:- public(write/1).
	write(Term) :-
		...

:- end_object.

If you want to call the built-in write/1 predicate from within this object, you still have access to it by writing {write(Term)}. The {}/1 control construct allows you to bypass the Logtalk compiler. You can then use your own definition of write/1 from other objects easily. For example:

:- object(other).

	:- uses(o, [write/1]).

	foo(Term) :-
		...,
		write(Term),
		...

:- end_object.

Turning on the optimize flag and compiling the object "o" before the object "other" gives you static binding and thus the call o::write/1 will have the same performance of a local call without any messages sending overhead.

A practical example where redefining built-in predicates is useful is:

https://github.com/LogtalkDotOrg/logtalk3/blob/master/library/comparingp.lgt
https://github.com/LogtalkDotOrg/logtalk3/blob/master/examples/sicstus/sorting.lgt

Cheers,

Paulo

-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.