Re: term_expansion/2 with DCG clauses

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 14/08/2013, at 15:09, Michael Hendricks <[email protected]> wrote:

> On Wed, Aug 14, 2013 at 3:24 AM, Jan Wielemaker <[email protected]> wrote:
> 
>> On 08/14/2013 04:28 AM, Richard A. O'Keefe wrote:
>> 
>>> If I recall correctly, DCG expansion was done *before* user expansion.
>>> With there being no goal_expansion in those days, this was important if
>>> you wanted users to be able to plug their own goal_expansion code in and
>>> have it apply to the result of DCG expansion.
>>> Further, DCG expansion was available as library code, so if you wanted it,
>>> you could very easily call it yourself.
>>> 
>> 
>> So not doing DCG expansion if user expansion is involved seems out of
>> date. The idea to chain `translation packages' is also implemented in
>> Ciao and ECLiPSe (and Logtalk?), although the way the packages are
>> specified there is much more fine-grained.
>> 
> 
> Is there a reason why macro expansions are not all iterated until a fixed
> point is reached?  expand_goal/2 behaves that way and I find it quite
> practical.  It seems to eliminate some of the problems we get from doing
> one kind of expansion always before another.


You may want or need to control which expansions are applied and the order they are applied instead of relying and being constrained by the flow hard-coded in the expand_term/2 built-in predicate and the order the clauses for the multifile term_expansion/2 hook predicate end up being used. Also, having any (third-party) library that you load being able to contribute term_expansion/2 (and goal_expansion/2) clauses to "user" and "system" and have them used by default can be practical but can also be a recipe for trouble. A simple example:

$ swipl
% /Users/pmoura/.plrc compiled 0.00 sec, 1 clauses
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.5.1-15-g72a7b72-DIRTY)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- assertz(system:term_expansion(end_of_file, end_of_file)).
true.

?- use_module(library(chr)).
% library(chr) compiled into chr 0.49 sec, 6,527 clauses
true.

?- ['~/Documents/Prolog/pl-devel/packages/chr/Examples/gcd.chr'].
% /Users/pmoura/Documents/Prolog/pl-devel/packages/chr/Examples/gcd.chr compiled into gcd 0.00 sec, 5 clauses
true.

?- gcd:gcd(2), gcd:gcd(3).
ERROR: toplevel: Undefined procedure: (:)/2
ERROR:   However, there are definitions for:
ERROR:         (:)/2
ERROR:  (DWIM could not correct goal)

And now using a different order:

pmmbp:~ pmoura$ swipl
% /Users/pmoura/.plrc compiled 0.00 sec, 1 clauses
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.5.1-15-g72a7b72-DIRTY)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- use_module(library(chr)).
% library(chr) compiled into chr 0.48 sec, 6,527 clauses
true.

?- assertz(system:term_expansion(end_of_file, end_of_file)).
true.

?- ['~/Documents/Prolog/pl-devel/packages/chr/Examples/gcd.chr'].
================================================================================
CHR compiler WARNING: deprecated syntax      :-constraints gcd/1.
    `--> Instead use :- chr_constraint gcd/1.
    Support for deprecated syntax will be discontinued in the near future!
================================================================================
% /Users/pmoura/Documents/Prolog/pl-devel/packages/chr/Examples/gcd.chr compiled into gcd 0.01 sec, 41 clauses
true.

?- gcd:gcd(2), gcd:gcd(3).
gcd(1)
true .
gcd(1)

Thus, defaults are good... as long as they can be overridden to give you the control you require to avoid having to rely on chance. I see *automatically* iterating term expansions until a fixed point is reached as a step in the wrong direction.

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.