Show
Ignore:
Timestamp:
04/12/08 14:14:06 (8 months ago)
Author:
pmoura
Message:

Added documentation on using compilation hooks and the term expansion mechanism for conditional compilation of debugging goals.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/manuals/userman/running.html

    r3944 r4206  
    182182    <dl> 
    183183        <dt><code>hook(Object)</code></dt> 
    184             <dd>Allows the definition of a compiler hooks that are called for each term read form a source file and for each compiled goal. This option specifies both an object (which can be the pseudo-object <code>user</code>) implementing the <code>expanding</code> built-in protocol. The object is expected to define clauses for the <a title="Consult reference manual" href="../refman/methods/term_expansion2.html"><code>term_expansion/2</code></a> and <a title="Consult reference manual" href="../refman/methods/goal_expansion2.html"><code>goal_expansion/2</code></a> predicates. In the case of the <code>term_expansion/2</code> predicate, the first argument is the term read form the source file while the second argument returns a list of terms corresponding to the expansion of the first argument. In the case of the <code>goal_expansion/2</code> predicate, the second argument should be a goal resulting from the expansion of the goal in the first argument. The predicate <code>goal_expansion/2</code> is called on the expanded goals so care must be taken to avoid compilation loops.</dd> 
     184            <dd>Allows the definition of a compiler hooks that are called for each term read form a source file and for each compiled goal. This option specifies an object (which can be the pseudo-object <code>user</code>) implementing the <code>expanding</code> built-in protocol. The object is expected to define clauses for the <a title="Consult reference manual" href="../refman/methods/term_expansion2.html"><code>term_expansion/2</code></a> and <a title="Consult reference manual" href="../refman/methods/goal_expansion2.html"><code>goal_expansion/2</code></a> predicates. In the case of the <code>term_expansion/2</code> predicate, the first argument is the term read form the source file while the second argument returns a list of terms corresponding to the expansion of the first argument. In the case of the <code>goal_expansion/2</code> predicate, the second argument should be a goal resulting from the expansion of the goal in the first argument. The predicate <code>goal_expansion/2</code> is called on the expanded goals so care must be taken to avoid compilation loops.</dd> 
    185185    </dl> 
    186186    <dl> 
     
    490490</p> 
    491491 
     492<h3 id="running_hooks">Using compilation hooks and term expansion for debugging</h3> 
     493 
     494<p> 
     495It is possible to use compilation hooks and the term expansion mechanism for conditional compilation of debugging goals. Assume that we chose the predicate <code>debug/1</code> to represent debug goals. For example: 
     496</p> 
     497<pre>append([], List, List) :- 
     498    debug((write('Base case: '), writeq(append([], List, List)), nl)). 
     499append([Head| Tail], List, [Head| Tail2]) :- 
     500    debug((write('Recursive case: '), writeq(append(Tail, List, Tail2)), nl)), 
     501    append(Tail, List, Tail2).</pre> 
     502<p> 
     503When debugging, we want to call the argument of the predicate <code>debug/1</code>. This can be easily accomplished by defining a hook object containing the following definition for <code>goal_expansion/2</code>: 
     504</p> 
     505<pre>goal_expansion(debug(Goal), Goal).</pre> 
     506<p> 
     507When not debugging, we can use a second hook object to discard the <code>debug/1</code> calls by defining the predicate <code>goal_expansion/2</code> as follows: 
     508</p> 
     509<pre>goal_expansion(debug(_), true).</pre> 
     510<p> 
     511The Logtalk compiler automatically removes any redundant calls to the built-in predicate <code>true/0</code> when compiling object predicates. 
     512</p> 
     513 
    492514<div class="footer"> 
    493515    <div class="copyright"> 
    494516        <span>Copyright &copy; <a href="mailto:pmoura@logtalk.org">Paulo Moura</a> &mdash; <a href="http://logtalk.org">Logtalk.org</a></span><br/>  
    495         <span>Last updated on: December 3, 2007</span> 
     517        <span>Last updated on: April 9, 2008</span> 
    496518    </div> 
    497519    <div class="navbottom">