how to undefine predefined operators?

View: New views
6 Messages — Rating Filter:   Alert me  

how to undefine predefined operators?

by Tobias Kuhn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

I have a question concerning operators in SWI Prolog: I know that one can redefine the predefined
operators, but can one also "undefine" them?

Concretely, I would like to use the name "<" as a common predicate name "<(A,B)" that is not
pretty-printed as "A<B".

Using write_canonical/1 or using write_term/2 with the ignore_ops option does not really help
because then also lists are written verbosely which is not what I want.

Any ideas?


Tobias
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: how to undefine predefined operators?

by Paulo Moura :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009/07/03, at 12:57, Tobias Kuhn wrote:

> I have a question concerning operators in SWI Prolog: I know that  
> one can redefine the predefined operators, but can one also  
> "undefine" them?
>
> Concretely, I would like to use the name "<" as a common predicate  
> name "<(A,B)" that is not pretty-printed as "A<B".
>
> Using write_canonical/1 or using write_term/2 with the ignore_ops  
> option does not really help because then also lists are written  
> verbosely which is not what I want.
>
> Any ideas?


?- current_op(P, T, <).
P = 700,
T = xfx ;
false.

?- op(0, xfx, <).
true.

?- write(<(a, b)).
<(a, b)
true.

Cheers,

Paulo


-----------------------------------------------------------------
Paulo Jorge Lopes de Moura, PhD
Assistant Professor
Dep. of Computer Science, University of Beira Interior
6201-001 Covilhã, Portugal

Office 3.18  Ext. 3276
Phone: +351 275319891 Fax: +351 275319899
Email: <mailto:pmoura@...>

Home page: <http://www.di.ubi.pt/~pmoura>
Research:  <http://logtalk.org/> Blog: <http://blog.logtalk.org/>
-----------------------------------------------------------------






_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: how to undefine predefined operators?

by Jan Wielemaker-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 03 July 2009 02:24:57 pm Paulo Moura wrote:

> On 2009/07/03, at 12:57, Tobias Kuhn wrote:
> > I have a question concerning operators in SWI Prolog: I know that
> > one can redefine the predefined operators, but can one also
> > "undefine" them?
> >
> > Concretely, I would like to use the name "<" as a common predicate
> > name "<(A,B)" that is not pretty-printed as "A<B".
> >
> > Using write_canonical/1 or using write_term/2 with the ignore_ops
> > option does not really help because then also lists are written
> > verbosely which is not what I want.
> >
> > Any ideas?
>
> ?- current_op(P, T, <).
> P = 700,
> T = xfx ;
> false.
>
> ?- op(0, xfx, <).
> true.
>
> ?- write(<(a, b)).
> <(a, b)
> true.

Note that there is a BIG price: If you try to load Prolog code after
this declaration that happens to use X < Y, you'll get a syntax error.

Try to make these changes inside a module because operators are local to
a module. Now you can use write_term(Term, [module(mymodule)]).
Unfortunately though, this does not work at the moment because the
system first looks for the operator locally and then globally. So, if
you cancel it locally, it is still active :-(

Another option is to use print/1 (or ~p in format) and a portray rule:

portray(A<B) :-
        format('<(~w, ~w)', [A, B]).

Of course, there is a moment where one starts wondering why one would
like to use Prolog write -which writes Prolog terms- to emit something
this weird?

        Cheers --- Jan

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: how to undefine predefined operators?

by Tobias Kuhn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jan Wielemaker wrote:

> On Friday 03 July 2009 02:24:57 pm Paulo Moura wrote:
>> On 2009/07/03, at 12:57, Tobias Kuhn wrote:
>>> I have a question concerning operators in SWI Prolog: I know that
>>> one can redefine the predefined operators, but can one also
>>> "undefine" them?
>>>
>>> Concretely, I would like to use the name "<" as a common predicate
>>> name "<(A,B)" that is not pretty-printed as "A<B".
>>>
>>> Using write_canonical/1 or using write_term/2 with the ignore_ops
>>> option does not really help because then also lists are written
>>> verbosely which is not what I want.
>>>
>>> Any ideas?
>> ?- current_op(P, T, <).
>> P = 700,
>> T = xfx ;
>> false.
>>
>> ?- op(0, xfx, <).
>> true.
>>
>> ?- write(<(a, b)).
>> <(a, b)
>> true.

Thank you all for your replies!

Actually, I should have read the manual more carefully where it cleary says "Precedence 0 removes
the declaration". Sorry.

> Note that there is a BIG price: If you try to load Prolog code after
> this declaration that happens to use X < Y, you'll get a syntax error.

Yes, this I encountered when trying it out. So I guess that retracting the operator "<" on a global
level is a very bad idea.

> Try to make these changes inside a module because operators are local to
> a module. Now you can use write_term(Term, [module(mymodule)]).
> Unfortunately though, this does not work at the moment because the
> system first looks for the operator locally and then globally. So, if
> you cancel it locally, it is still active :-(
>
> Another option is to use print/1 (or ~p in format) and a portray rule:
>
> portray(A<B) :-
> format('<(~w, ~w)', [A, B]).
>
> Of course, there is a moment where one starts wondering why one would
> like to use Prolog write -which writes Prolog terms- to emit something
> this weird?

It seems that the portray clause has to be defined in the module "user" and cannot be defined
locally in a user-defined module. Right?

Is there a simple way to print out operators in normal predicate form (as write_canonical/1 does)
but print out lists with the []-notation (as write/1 does)? This would solve my problem too.


Tobias
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: how to undefine predefined operators?

by Jan Wielemaker-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 03 July 2009 03:45:31 pm Tobias Kuhn wrote:
> Jan Wielemaker wrote:
> > On Friday 03 July 2009 02:24:57 pm Paulo Moura wrote:

<snip>

> >> true.
> >>
> >> ?- write(<(a, b)).
> >> <(a, b)
> >> true.
>
> Thank you all for your replies!
>
> Actually, I should have read the manual more carefully where it cleary says
> "Precedence 0 removes the declaration". Sorry.
>
> > Note that there is a BIG price: If you try to load Prolog code after
> > this declaration that happens to use X < Y, you'll get a syntax error.
>
> Yes, this I encountered when trying it out. So I guess that retracting the
> operator "<" on a global level is a very bad idea.

Playing around globally with system operators must be deprecated (or
worse :-)

> It seems that the portray clause has to be defined in the module "user" and
> cannot be defined locally in a user-defined module. Right?

Yes. That is current practice in all Prolog systems that support
portray/1 AFAIK. It probably makes sense to first check the current
module before user.

> Is there a simple way to print out operators in normal predicate form (as
> write_canonical/1 does) but print out lists with the []-notation (as
> write/1 does)? This would solve my problem too.

AFAIK, the options that are shared with the standard or current practice
are compatible. The big question is still: what are you trying to do? If
it is about exchanging terms to another language you probably end up
with a partial solution if you try to tweak Prolog write.  Use the XML
writer, JSON generator, etc. or roll your own.

The `best' route in SWI-Prolog is probably to make cancelation of
operators work inside modules. Afterall, the fact that that doesn't work
must be considered a bug.

        Cheers --- Jan

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: how to undefine predefined operators?

by Tobias Kuhn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jan Wielemaker wrote:

> On Friday 03 July 2009 03:45:31 pm Tobias Kuhn wrote:
>> Is there a simple way to print out operators in normal predicate form (as
>> write_canonical/1 does) but print out lists with the []-notation (as
>> write/1 does)? This would solve my problem too.
>
> AFAIK, the options that are shared with the standard or current practice
> are compatible. The big question is still: what are you trying to do? If
> it is about exchanging terms to another language you probably end up
> with a partial solution if you try to tweak Prolog write.  Use the XML
> writer, JSON generator, etc. or roll your own.

My program should read a file containing Prolog terms (describing a grammar), transform those terms
into another Prolog-based format, and write the transformed terms into another file. Both, input and
output, contain terms of the form "<(A,B)" where "<" is used to describe some kind of a backwards
reference having two arguments. It makes no sense to write "<(A,B)" in infix notation "A<B", neither
in the input file nor in the output file.

Using e.g. "<<<" instead of "<" would solve the problem. But for reasons of conciseness I would
prefer "<".

> The `best' route in SWI-Prolog is probably to make cancelation of
> operators work inside modules. Afterall, the fact that that doesn't work
> must be considered a bug.

This would perfectly solve my problem.


Tobias
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog