UNPKG

@ciao-lang/ts-ciao-interface

Version:

Simple Ciao interface for node.

59 lines (52 loc) 14.8 kB
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><meta name="theme-color" content="#273f79"/><link rel="stylesheet" href="lpdoc.css" type="text/css"/><script type="text/javascript" src="lpdoc.js"></script><title>Tabling execution &mdash; The Ciao System v1.22</title></head><body><div class="lpdoc-page fixleftbar"><a href="#" id="sidebar-toggle-button" class="lpdoc-navbutton"><span id="sidebar-button-arrow">&#9776;</span></a><div id="sidebar" class="lpdoc-sidebar"><div style="height: 40px; margin-left: auto; margin-right: auto"><img src="ciao-logo_autofig.png" width=auto height=100%></div><div class="lpdoc-nav"><span class="lpdoc-on-right"><a class="lpdoc-navbutton" href="ExtendLang.html">&#x2191;</a><a class="lpdoc-navbutton" href="clpfd_rt.html">&#x2190;</a><a class="lpdoc-navbutton" href="attr_doc.html">&#x2192;</a><a class="lpdoc-navbutton" href="ciaosearch.html">&#x1F50D;</a></span><span><a href="ciaofulltoc.html">TOC</a></span></div><hr></hr><ul class="lpdoc-itemize-sectpath"><li><a href="ciao.html">The Ciao System</a> &raquo;<br/> </li><li><a href="ExtendLang.html">PART IV - Language extensions</a> &raquo;<br/> </li><li><a href=""><strong>Tabling execution</strong></a></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#Tabling">Tabling</a></li><li><a href="#Tabled Constraint Logic Programming">Tabled Constraint Logic Programming</a></li><li><a href="#Some TCLP applications">Some TCLP applications</a></li><li><a href="#Usage and interface">Usage and interface</a></li><li><a href="#Documentation on new declarations">Documentation on new declarations</a></li><li><a href="#Other information">Other information</a></li><ul><li><a href="#Some examples using Tabling">Some examples using Tabling</a></li><li><a href="#Some examples using Tabled Constraint Logic Programming">Some examples using Tabled Constraint Logic Programming</a></li><li><a href="#Some examples of TCLP interface">Some examples of TCLP interface</a></li></ul></ul></div><div class="lpdoc-main"><div id=""><h1>Tabling execution</h1><a class="lpdoc-idx-anchor" href="ciaosearch.html#tabling"></a> <strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="Pablo Chico de Guzman Huerta" href="ciaosearch.html#Pablo Chico de Guzman Huerta">Pablo Chico de Guzm&aacute;n Huerta</a>, <a class="lpdoc-idx-anchor" id="Joaquin Arias" href="ciaosearch.html#Joaquin Arias">Joaqu&iacute;n Arias</a>, <a class="lpdoc-idx-anchor" id="The Ciao Development Team" href="ciaosearch.html#The Ciao Development Team">The Ciao Development Team</a>.<p> <strong>Stability: [</strong><strong>beta</strong><strong>] </strong>Most of the functionality is there but it is still missing some testing and/or verification.<p><br/> This library package allows the evaluation of predicates using tabled resolution (<em>tabling</em>). Tabling is an alternative execution strategy for logic programs that records calls and their answers in order to reuse them in future calls. It improves Prolog declarativity and can improve efficiency by avoiding repeated computations [<a class="lpdoc-idx-anchor" id="0" href="ciaorefs.html#tamaki.iclp86-short">TS86</a>,<a class="lpdoc-idx-anchor" id="1" href="ciaorefs.html#Warren92-short">War92</a>]. Tabling is guaranteed to terminate when Prolog programs have the <em>bounded term-depth property</em>. Some examples of the use of tabling can be found at the end of this section.<p><div id="Tabling"><h2>Tabling</h2> <p>Adding a <a class="lpdoc-idx-anchor" id="2" href="#table/1"><tt>table/1</tt></a> declaration to a predicate makes the compiler and run-time system distinguish the first occurrence of a tabled goal (the <em>generator</em>) and subsequent calls which are identical up to variable renaming (the <em>consumers</em>). The generator applies resolution using the program clauses to derive answers for the goal. Consumers <em>suspend</em> the current execution path (using implementation-dependent means) and move to a different branch. When such an alternative branch finally succeeds, the answer generated for the initial query is inserted in a table associated with the original goal. This makes it possible to reactivate suspended calls and to continue execution at the point where it was stopped. Thus, consumers do not recompute those calls, but obtain instead the answers from the table where they have been previously inserted by the producer.<p>Predicates not marked as tabled are executed following standard SLD resolution, hopefully with (minimal or no) overhead due to the availability of tabling in the system.<p>Our current version of tabling only supports <em>local</em> evaluation and <em>variant tabling</em> [<a class="lpdoc-idx-anchor" id="3" href="ciaorefs.html#Rao96athread-short">RRR96</a>]. Local evaluation computes all the answers of a tabled predicate before returning any of them. Note that a call to a tabled predicate will never return if this call has infinite answers. Variant tabling considers two calls/answers to be the same only when they are identical under variable renaming.<p>By declaring a predicate as tabled, a program translation is performed to abstract the use of internal tabling primitives for the user. Our tabling implementation technique follows the <script type="math/tex">Theta</script>-CHAT approach [<a class="lpdoc-idx-anchor" id="4" href="ciaorefs.html#demoen99:chat_complexity">DS99</a>] which does not require major changes in the compiler or run-time system.<p></div><div id="Tabled Constraint Logic Programming"><h2>Tabled Constraint Logic Programming</h2> <p>The TCLP implementation allows the combination of tabling with constraints. The initial implementation described in [<a class="lpdoc-idx-anchor" id="5" href="ciaorefs.html#chico-tclp-flops2012-large">dGCHS12</a>] has been modified by a modular implementation, called Mod TCLP, which is described in [<a class="lpdoc-idx-anchor" id="6" href="ciaorefs.html#TCLP-tplp2019">AC19a</a>]. By using the Mod TCLP interface of a constraint solver, e.g., <a class="lpdoc-idx-anchor" id="7" href="ciaosearch.html#:- use_package(t_clpq)"><tt>:- use_package(t_clpq)</tt></a>, the tabling engine uses the entailment check provided by the constraint solver to detect more particular calls / answers.<p>The TCLP interface of a constraint solver must implement the following interface:<p><pre class="lpdoc-codeblock">call_domain_projection/2 answer_domain_projection/2 call_store_projection/3 answer_store_projection/3 call_entail/2 answer_check_entail/3 apply_answer/2 </pre> <p>as an API for the tablingq engine. Some examples of TCLP interfaces are <tt>different_constraints</tt>, <tt>t_clpq</tt>, and <tt>t_clpr</tt> libraries.<p>Some examples of the use of TCLP can be found at the end of this section (with some examples of the TCLP interface).<p></div><div id="Some TCLP applications"><h2>Some TCLP applications</h2> <p>The current implementation of Mod TCLP has been used to develop more complex applications:<p><ul> <p><li>The Implementation of an Abstract Interpretation Algorithm described in [<a class="lpdoc-idx-anchor" id="8" href="ciaorefs.html#arias19:ciaopp-tclp">AC19b</a>]:<p>Abstract interpretation requires a fixpoint computation. PLAI is a fixpoint algorithm implemented by the abstract interpreter of CiaoPP, an analyzer and optimizer suite for logic programs, part of the Ciao development environment. In this paper, we adapt the existing PLAI implementation in CiaoPP using tabled constraint logic programming. The tabling engine is used to compute the fixpoint and the constraint engine computes the LUB of the abstract substitutions of different clauses. That provides, on one hand, much simpler code since the fixpoint computation is taken care of by the underlying tabling machinery, and, in most cases, performance gains, since some crucial operations (such as branch switching and resumption) are executed by the tabling engine. Determining that the fixpoint has been reached uses semantic equivalence, e.g., whether syntactically different representations of an abstract substitution actually refer to the same element in the abstract domain. This is delegated to the abstract domain operations, transparently to the analyzer. As a result, the tabling analyzer can reuse answers in more cases than if syntactical equality were used to detect repeated calls, and better performance, even taking into account the additional cost associated to these checks, is achieved. The implementation presented is based on the TCLP framework available in Ciao Prolog. It is one-third the size of the initial fixpoint implementation we started with, and its performance has been evaluated by analysing several programs with different abstract domains.<p><li>Incremental Evaluation of Lattice-Based Aggregates in Logic Programming described in [<a class="lpdoc-idx-anchor" id="9" href="ciaorefs.html#atclp-padl2019">AC19c</a>] and available as a bundle, <tt>:- use_package(tclp_aggregates)</tt>, in the current distribution of Ciao:<p>Aggregates are used to compute single pieces of information from separate data items, such as records in a database or answers to a query to a logic program. The maximum and minimum are well-known examples of aggregates. The computation of aggregates in Prolog or variant-based tabling can loop even if the aggregate at hand can be finitely determined. When answer subsumption or mode-directed tabling is used, termination improves, but the behavior observed in existing proposals is not consistent. We present a framework to incrementally compute aggregates for elements in a lattice. We use the entailment and join relations of the lattice to define (and compute) aggregates and decide whether some atom is compatible with (entails) the aggregate. The semantics of the aggregates defined in this way is consistent with the LFP semantics of tabling with constraints. Our implementation is based on the TCLP framework available in Ciao Prolog, and improves its termination properties w.r.t. similar approaches. Defining aggregates that do not fit into the lattice structure is possible, but some properties guaranteed by the lattice may not hold. However, the flexibility provided by this possibility justifies its inclusion. We validate our design with several examples and we evaluate their performance.<p></ul> <p></div><br/><div id="Usage and interface"><h2>Usage and interface</h2><div class="lpdoc-cartouche"><ul><li><strong>Library usage:</strong><br/>The <tt>TABLED_EXECUTION</tt> flag must be set to <tt>yes</tt> during system configuration in order to compile the engine with support for the execution of goals with tabling. It is set to <tt>yes</tt> by default.<li><strong>New declarations defined:</strong><br/><a class="lpdoc-idx-anchor" id="10" href="#table/1"><tt>table/1</tt></a>. <li><strong>Implicit imports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>Packages:</em><br/><a class="lpdoc-idx-anchor" id="11" href="ciaosearch.html#prelude"><tt>prelude</tt></a>, <a class="lpdoc-idx-anchor" id="12" href="ciaosearch.html#initial"><tt>initial</tt></a>, <a class="lpdoc-idx-anchor" id="13" href="condcomp_doc.html"><tt>condcomp</tt></a>, <a class="lpdoc-idx-anchor" id="14" href="assertions_doc.html"><tt>assertions</tt></a>, <a class="lpdoc-idx-anchor" id="15" href="ciaosearch.html#assertions/assertions_basic"><tt>assertions/assertions_basic</tt></a>. </ul></ul></div></div><div id="Documentation on new declarations"><h2>Documentation on new declarations</h2><div><div class="lpdoc-defname"><span class="lpdoc-predtag">DECLARATION</span><a class="lpdoc-idx-anchor" id="table/1" href="ciaosearch.html#table/1">table/1</a></div><div class="lpdoc-deftext">It declares a tabled predicate.</div></div><p> </div><div id="Other information"><h2>Other information</h2> <p><div id="Some examples using Tabling"><h3>Some examples using Tabling</h3> <p>We now illustrate some of the uses of the package of tabling through examples. The following example defines a simple predicte <tt>path(X,Y)</tt> which returns the transitive closuer of <tt>edge/2</tt> without entering loops:<p><pre class="lpdoc-codeblock">:- use_package(tabling). :- table path/2. path(X,Y) :- path(X,Z), edge(Z,Y). path(X,Y) :- edge(X,Y). edge(a,b). edge(b,a). </pre> <p>Other examples can be found in the source and library directories and in [<a class="lpdoc-idx-anchor" id="16" href="ciaorefs.html#pablo-phd">dG12</a>].<p></div><div id="Some examples using Tabled Constraint Logic Programming"><h3>Some examples using Tabled Constraint Logic Programming</h3> <p>We now illustrate some of the uses of the package of t_clpq through examples. The following example defines a simple predicte <tt>fibonacci(N,F)</tt> which returns the fibonacci number <tt>F</tt> given the index <tt>N</tt> and returns the index <tt>N</tt> given the fibonacci number <tt>F</tt>:<p><pre class="lpdoc-codeblock">:- use_package(tabling). :- use_package(t_clpq). :- table fibonacci/2. fibonacci(0, 0). fibonacci(1, 1). fibonacci(N, F) :- N .&gt;=. 2, N1 .=. N - 1, N2 .=. N - 2, F1 .&gt;=. 0, F2 .&gt;=. 0, F .=. F1 + F2, fibonacci(N1, F1), fibonacci(N2, F2). </pre> <p>Other examples can be found in the source and library directories and in [<a class="lpdoc-idx-anchor" id="17" href="ciaorefs.html#TCLP-tplp2019">AC19a</a>].<p></div><div id="Some examples of TCLP interface"><h3>Some examples of TCLP interface</h3> <p>We now illustrate the implementation of a TCLP interface to link a constraint solver wiht the tabling engine through examples. The following example defines the interface of <tt>clpq</tt> wiht the tabling engine:<p><pre class="lpdoc-codeblock">:- use_package(clpq). :- use_module(library(clpq/clpq_dump), [clpqr_dump_constraints/3]). :- active tclp. call_domain_projection(Vars, st(Vars,_)). call_entail(st(Vars,_), st(FGen-ProjGen)) :- Vars = FGen, clpq_entailed(ProjGen). call_store_projection(_, st(Vars,_), st(F,Proj)) :- clpqr_dump_constraints(Vars, F , Proj). answer_domain_projection(Vars, st(F,Proj)) :- clpqr_dump_constraints(Vars, F, Proj). answer_check_entail(st(F,_), st(FAns,ProjAns), 1) :- F = FAns, clpq_entailed(ProjAns), !. answer_check_entail(st(F,Proj), st(FAns,ProjAns), -1) :- F = FAns, clpq_meta(ProjAns), clpq_entailed(Proj). answer_store_projection(_, St, St). apply_answer(Vars, st(FAns,ProjAns)) :- Vars = FAns, clpq_meta(ProjAns). </pre> <p>Other examples can be found in the source and library directories and in [<a class="lpdoc-idx-anchor" id="18" href="ciaorefs.html#TCLP-tplp2019">AC19a</a>].<p> </div><br/></div></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>