@ciao-lang/ts-ciao-interface
Version:
Simple Ciao interface for node.
58 lines (47 loc) • 8.13 kB
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>Attributed variables — 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">☰</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">↑</a><a class="lpdoc-navbutton" href="tabling_doc.html">←</a><a class="lpdoc-navbutton" href="attr_rt.html">→</a><a class="lpdoc-navbutton" href="ciaosearch.html">🔍</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> »<br/> </li><li><a href="ExtendLang.html">PART IV - Language extensions</a> »<br/> </li><li><a href=""><strong>Attributed variables</strong></a> ▾<ul><li><a href="attr_rt.html">Attributed variables runtime</a></li><li><a href="attributes.html">Low-level attributed variables</a></li></ul></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#Example">Example</a></li><li><a href="#Usage and interface">Usage and interface</a></li></ul></div><div class="lpdoc-main"><div id=""><h1>Attributed variables</h1><a class="lpdoc-idx-anchor" href="ciaosearch.html#attr"></a>
<strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="Remy Haemmerle" href="ciaosearch.html#Remy Haemmerle">Rémy Haemmerlé</a>, <a class="lpdoc-idx-anchor" id="Christian Holzbaur" href="ciaosearch.html#Christian Holzbaur">Christian Holzbaur</a>, <a class="lpdoc-idx-anchor" id="Daniel Cabeza" href="ciaosearch.html#Daniel Cabeza">Daniel Cabeza</a>, <a class="lpdoc-idx-anchor" id="Manuel Carro" href="ciaosearch.html#Manuel Carro">Manuel Carro</a>.<p>
This package implements <a class="lpdoc-idx-anchor" id="0" href="ciaosearch.html#attributed variables">attributed variables</a> in the style of Holzbaur [<a class="lpdoc-idx-anchor" id="1" href="ciaorefs.html#holzbaur-phd">Hol90</a>]. It provides a way to associate to variables one or several arbitrary terms called <a class="lpdoc-idx-anchor" id="2" href="ciaosearch.html#attributes">attributes</a>. By allowing the user to redefine the unification of attributed variables, this extension makes possible the design of coroutining facilities (see subsection <a href="clpfd_doc.html#Example">Example</a>) and clean interfaces between Prolog and constraints solvers.<p>Attributes are private to module and each variable can have at most one attribute in each module. Attributes are handled by predicate provided by <a class="lpdoc-idx-anchor" id="3" href="attr_rt.html"><tt>attr_rt</tt></a> module.<p> <a class="lpdoc-idx-anchor" id="4" href=""><tt>attr</tt></a> package imports automatically attributes variables manipulation predicates, <tt>put_attr_local/2</tt>, <tt>get_attr_local/2</tt>, and <tt>del_attr_local/2</tt> from module <tt>'attr/attr_rt'</tt>, and set up the following hooks:<p><ul> <p><li><tt>attr_unify_hook(AttValue, VarValue)</tt>.<p>Hook that must be defined in the module using package <tt>attr</tt>. It is called after the attributed variable of that module has been unified with a non-var term, possibly another attributed variable. <span class="lpdoc-var">AttValue</span> is the attribute that was associated to the variable in this module and <span class="lpdoc-var">VarValue</span> is the new value of the variable. Normally this predicate fails to veto binding the variable to <span class="lpdoc-var">VarValue</span>, forcing backtracking to undo the binding. If <span class="lpdoc-var">VarValue</span> is another attributed variable the hook often combines the two attribute and associates the combined attribute with <span class="lpdoc-var">VarValue</span> using <tt>attr_rt:put_attr_local/2</tt>.<p><li><tt>attribute_goal(Var, S0, S)</tt>.<p>This optional hook, if it is defined, is used by <tt>attr_rt:copy_term/3</tt> to project attributes of that module to residual goals, and by the toplevel to obtain residual goals after executing a query. The predicate is supposed to unified <span class="lpdoc-var">S0</span> with a different list containing the residual goals and which have <span class="lpdoc-var">S</span> as tail. For the sake of simplicity, it can be defined using <a class="lpdoc-idx-anchor" id="5" href="dcg_doc.html"><tt>dcg</tt></a>. (See DCG non-terminal <tt>attribute_goal(Var)</tt> in example below.)<p> <li><tt>attr_portray_hook(Attribute, Var)</tt>.<p>Called by <a class="lpdoc-idx-anchor" id="6" href="write.html#write_term/2"><tt>write_term/2</tt></a> for each attribute associate to the variable <span class="lpdoc-var">Var</span> if the option option attributes(portray) is in effect. If the hook succeeds the variable is considered to be printed otherwise <tt>Module =... </tt> is printed to indicate the existence of an attribute defined in module <tt>Module</tt>. At call time <span class="lpdoc-var">Attribute</span> is the actual value of the attribute associate to the variables <span class="lpdoc-var">Var</span>.<p></ul> <p><div id="Example"><h2>Example</h2> <p>In the following example we give an implementation of <tt>freeze/2</tt>. We name it <tt>myfreeze/2</tt> in order to avoid a name clash with the built-in predicate of the same name. The code is available in the module <tt>library(attr/example/myfreeze)</tt> <p><pre class="lpdoc-codeblock">:- module(myfreeze, [myfreeze/2], [attr, dcg, hiord]).
:- use_module(engine(io_basic)).
:- meta_predicate(myfreeze(?, goal)).
myfreeze(X, Goal) :-
( nonvar(X) ->
call(Goal)
; get_attr_local(X, Fb) ->
meta_conj(Fb, Goal, C),
put_attr_local(X, C) % rescue conjunction
; put_attr_local(X, Goal)
).
:- if(defined(optim_comp)).
attr_unify_hook(Fa, Other) :-
( nonvar(Other) ->
'$trust_metatype'(Fa, goal),
call(Fa)
; get_attr_local(Other, Fb) ->
meta_conj(Fa, Fb, C),
put_attr_local(Other, C) % rescue conjunction
; put_attr_local(Other, Fa) % rescue conjunction
).
:- else.
attr_unify_hook(Fa, Other) :-
( nonvar(Other) ->
call(Fa)
; get_attr_local(Other, Fb) ->
meta_conj(Fa, Fb, C),
put_attr_local(Other, C) % rescue conjunction
; put_attr_local(Other, Fa) % rescue conjunction
).
:- endif.
attribute_goals(X) -->
[myfreeze:myfreeze(X, G)],
{get_attr_local(X, G)}.
attr_portray_hook(G, Var):-
display(Var),
display('<-myfrozen('),
display(G),
display(')').
% A (meta) conjunction of two goals
meta_conj('$:'(Fa), '$:'(Fb), '$:'('basiccontrol:,'(Fa, Fb))).
</pre> <p> </div><br/><div id="Usage and interface"><h2>Usage and interface</h2><div class="lpdoc-cartouche"><ul><li><strong>Library usage:</strong><br/><tt>:- use_package(attr).</tt>
or
<tt>:- module(...,...,[attr]).</tt><li><strong>Implicit imports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>Packages:</em><br/><a class="lpdoc-idx-anchor" id="7" href="ciaosearch.html#prelude"><tt>prelude</tt></a>, <a class="lpdoc-idx-anchor" id="8" href="ciaosearch.html#initial"><tt>initial</tt></a>, <a class="lpdoc-idx-anchor" id="9" href="condcomp_doc.html"><tt>condcomp</tt></a>, <a class="lpdoc-idx-anchor" id="10" href="assertions_doc.html"><tt>assertions</tt></a>, <a class="lpdoc-idx-anchor" id="11" href="ciaosearch.html#assertions/assertions_basic"><tt>assertions/assertions_basic</tt></a>.
</ul></ul></div></div></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>