Show
Ignore:
Timestamp:
07/01/08 09:53:49 (5 months ago)
Author:
pmoura
Message:

Improved the Prolog migration guide on converting code that makes use of multifile predicates.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/manuals/migration/index.html

    r3847 r4336  
    5656By adding the <em>gateway</em> clause after or before the local clauses, you can choose if the local clauses should be used before or after the overridden clauses. 
    5757</p> 
     58<p> 
     59A more complicated scenario is when you have several files containing clauses for a multifile predicate. For example, assume that a multifile predicate named <code>mp/2</code> is defined in two files. In this case, start by defining a protocol containing the declaration of the multifile predicate: 
     60</p> 
     61<pre> 
     62:- protocol(ptc).  
     63 
     64    :- public(mp/1).  
     65 
     66:- end_protocol.  
     67</pre> 
     68<p> 
     69Second, for each file, define a category containing the corresponding clauses for the <code>mp/2</code> predicate:  
     70</p> 
     71<pre> 
     72:- category(ctg1,  
     73    implements(ptc)).  
     74 
     75    mp(1).    % clauses from the first file 
     76    mp(2).  
     77 
     78:- end_category.  
     79 
     80 
     81:- category(ctg2,  
     82    implements(ptc)). 
     83 
     84    mp(3).    % clauses from the first file 
     85    mp(4).  
     86 
     87:- end_category.  
     88</pre> 
     89<p> 
     90Next, define an object importing all the categories and defining aliases to the imported predicates: 
     91</p> 
     92<pre> 
     93:- object(obj,  
     94    implements(ptc),  
     95    imports(ctg1, ctg2)). 
     96 
     97    :- alias(ctg1, mp/1, ctg1_mp/1).  
     98    :- alias(ctg2, mp/1, ctg2_mp/1).  
     99 
     100    mp(X) :-  
     101        ::ctg1_mp(X).  
     102    mp(X) :-  
     103        ::ctg2_mp(X).  
     104 
     105:- end_ object.  
     106</pre> 
     107<p> 
     108The predicate aliases above are needed to allow access to the overridded clauses of the <code>mp/1</code> predicate (by default, the clauses from  <code>ctg1</code> would override the clauses from the <code>ctg2</code> category as a consequence of the predicate lookup mechanism).  
     109</p> 
     110<p> 
     111Compiling and loading the above Logtalk entities allows you to access all clauses of the former multifile predicate. For example:  
     112</p> 
     113<pre> 
     114| ?- obj::mp(X). 
     115 
     116X = 1 ;  
     117X = 2 ;  
     118X = 3 ;  
     119X = 4  
     120yes  
     121</pre> 
    58122 
    59123<h2>Converting Prolog modules into objects<a id="converting"></a></h2> 
     
    143207    <div class="copyright"> 
    144208        <span>Copyright &copy; <a href="mailto:pmoura@logtalk.org">Paulo Moura</a> &mdash; <a href="http://logtalk.org">Logtalk.org</a></span><br/>  
    145         <span>Last updated on: June 6, 2007</span> 
     209        <span>Last updated on: July 1, 2008</span> 
    146210    </div> 
    147211    <div class="navbottom">