Out of global stack expanding a DCG rule

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
Hibeus
Posts: 7
Joined: Wed Jan 26, 2011 2:09 am

Out of global stack expanding a DCG rule

Post by Hibeus » Mon Jan 31, 2011 7:46 am

Hello,

While debugging some code today I found this:

Code: Select all

?- expand_term((rule_1-->{if_name(X), X}), T).
T = (rule_1(_G355, _G356):- (if_name(X), X), _G356=_G355).

?- logtalk << expand_term((rule_1-->{if_name(X), X}), T).
ERROR: Unknown error term: error(resource_error(stack),global)
I am using SWI Prolog 5.11.13-28-geb341fa and Logtalk revision 6056 from svn. The rule_1 above results from a bug in my code and is not going to work as I intented, but I think Logtalk should not run out of global stack while expanding it. Note that I am running with set_logtalk_flag(debug, on) in my settings file. Could you please take a look at this?

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Out of global stack expanding a DCG rule

Post by Paulo Moura » Mon Jan 31, 2011 1:37 pm

Thanks for the bug report. It will be fixed on r6057. The problem was on the code that simplifies the predicate clause generated by the compilation of a grammar rule. The grammar rule in your example contains a meta-variable, which the Logtalk compiler should (but currently don't) report as illegal (the call/1 control construct should be used instead on this case).
Paulo Moura
Logtalk developer

Hibeus
Posts: 7
Joined: Wed Jan 26, 2011 2:09 am

Re: Out of global stack expanding a DCG rule

Post by Hibeus » Tue Feb 01, 2011 12:04 am

Using call/1 in my code as you suggest produces the intended results. Thank you. I was a little surprised SWI Prolog did not report an error, but then, neither does GNU Prolog (1.3.0) for example, so it is unlikely to be a problem in either Prolog compiler. Reporting an error is, from a user's perspective, the most helpful thing to do; I am looking forward to the next revision :)

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Out of global stack expanding a DCG rule

Post by Paulo Moura » Tue Feb 01, 2011 12:37 am

The usual behavior of Prolog compilers is to rewrite ..., X, ... as ..., call(X), ... For example:

Code: Select all

$ gprolog
GNU Prolog 1.4.0
By Daniel Diaz
Copyright (C) 1999-2010 Daniel Diaz
| ?- assertz((a(X) :- X)).

yes
| ?- listing.

a(A) :-
	call(A).

yes
| ?- 

$ swipl
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,928 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.11.13)
Copyright (c) 1990-2011 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((a(X) :- X)).
true.

?- listing.

:- dynamic a/1.

a(A) :-
	call(A).
true.
The Logtalk compiler does the same (note that the predicate clause returned by expand_term/2 in your example is only the expansion of the grammar rule, not the final, compiled, predicate clause generated by the compiler). One reason to avoid using meta-variables (not wrapped in a call/1 control construct) is to avoid some semantic and portability pitfalls when cuts are present. Consider:
a(X) :- X.
a(2).

b(X) :- call(X):
b(2).
Calling both predicates with a cut as argument results in one solution for a/1 but two solutions for b/1 (as the call/1 control construct is opaque to cuts). Regarding portability issues, not all Prolog compilers behave as illustrated for GNU Prolog or SWI-Prolog above. For example:

Code: Select all

$ eclipse
ECLiPSe Constraint Logic Programming System [kernel]
Kernel and basic libraries copyright Cisco Systems, Inc.
and subject to the Cisco-style Mozilla Public Licence 1.1
(see legal/cmpl.txt or www.eclipse-clp.org/licence)
Source available at www.sourceforge.org/projects/eclipse-clp
GMP library copyright Free Software Foundation, see legal/lgpl.txt
For other libraries see their individual copyright notices
Version 6.1unstable #46 (i386_macosx), Tue Oct 12 02:32 2010
[eclipse 42]: assertz((a(X) :- X)).

X = X
Yes (0.00s cpu)
[eclipse 43]: listing.
a(_188) :-
	_188.


Yes (0.01s cpu)
Thus, detecting and warning the user for variables used as meta-calls is a desirable feature but not as trivial (to implement) as it may seem. It's, however, in my TODO list.

P.S. The fix for the bug you reported is already committed.
Paulo Moura
Logtalk developer

Hibeus
Posts: 7
Joined: Wed Jan 26, 2011 2:09 am

Re: Out of global stack expanding a DCG rule

Post by Hibeus » Tue Feb 01, 2011 5:51 am

Paulo Moura wrote:Calling both predicates with a cut as argument results in one solution for a/1 but two solutions for b/1 (as the call/1 control construct is opaque to cuts). Regarding portability issues, not all Prolog compilers behave as illustrated for GNU Prolog or SWI-Prolog above.
This example is enlightening. Maybe I should start using Eclipse, too...
Paulo Moura wrote:Thus, detecting and warning the user for variables used as meta-calls is a desirable feature but not as trivial (to implement) as it may seem. It's, however, in my TODO list.
Yes, the problem is different from what I thought it was.
Paulo Moura wrote:P.S. The fix for the bug you reported is already committed.
Thank you! I updated my tree and checked: it works.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest