Re: defining operators with a result
- From: Bart Demoen <bmd@xxxxxxxxxxxxxxxxx>
- Date: Tue, 14 Nov 2006 11:10:33 +0100
Richard Szopa wrote:
First of all, thank you all for your answers.
I think that the solution proposed by Glenn Lieding doesn't really
solve my problem. Having to adapt every predicate to deal with the mton
structure won't make my code more compact or more readable. Also, I
would like to be able to use my notation in built-in predicates, like
for example reverse and so on.
On the other hand, GRIPS goes much too far for me--I realize that
Prolog uses relational notation and I'm quite happy with it. Also, I
find remaining understandable for the rest of Prolog programmers quite
important. ;-)
The idea of trying to add this sugar to Prolog came to me when I found
that I use [m..n] quite often in my pseudo-Prolog code, but when I
translate it into Prolog, it gets somehow more obscure.
Anyway, an additional question: how would one define in Prolog |, the
cons operator, if it wasn't built in the language and f one wanted it
work exactly as | works now in [Head|Tail]? Or, if one wanted to be
able to use the '@' sign in one's Prolog programs instead of '|'? Is
it possible?
Of course, hacking the source of the interpreter is not the kind of
answer I expect ;-)
-- Richard
[did nobody suggest term expansion for this ? or have I overlooked something]
You can first of all define .. as an infix operator with precedence and
associativity of your liking, but so that [X..Y] parses correctly -
:- op(100, xfx, ..).
is just one possibility that works for me.
Next write a piece of term expansion that transforms e.g. the clause
a(N) :- write([3..N]).
into
a(N) :- x2y(3,N,L), write(L).
and
f([5..9]).
into
f(L) :- x2y(5,9,L).
etc.
That's not very difficult.
And you are basically done - except that you need to decide what to do
with meta predicates, and maybe you want this transformation to be local to
a module ...
Probably CIAO would give you good support for doing all this (and more :-)
But is is doable in SWI as well of course.
Cheers
Bart Demoen
.
- Follow-Ups:
- Re: defining operators with a result
- From: Richard Szopa
- Re: defining operators with a result
- References:
- defining operators with a result
- From: Richard Szopa
- Re: defining operators with a result
- From: Nameless
- Re: defining operators with a result
- From: Richard Szopa
- Re: defining operators with a result
- From: Jan Wielemaker
- Re: defining operators with a result
- From: Richard Szopa
- defining operators with a result
- Prev by Date: Re: defining operators with a result
- Next by Date: formatting float output
- Previous by thread: Re: defining operators with a result
- Next by thread: Re: defining operators with a result
- Index(es):
Relevant Pages
|
|