@ciao-lang/ts-ciao-interface
Version:
Simple Ciao interface for node.
46 lines (41 loc) • 8.03 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>Delaying predicates (when) — 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="freeze.html">←</a><a class="lpdoc-navbutton" href="andorra_doc.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>Delaying predicates (when)</strong></a></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#Usage and interface">Usage and interface</a></li><li><a href="#Documentation on exports">Documentation on exports</a></li><li><a href="#Documentation on imports">Documentation on imports</a></li></ul></div><div class="lpdoc-main"><div id=""><h1>Delaying predicates (when)</h1><a class="lpdoc-idx-anchor" href="ciaosearch.html#when"></a>
<strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="Manuel Carro" href="ciaosearch.html#Manuel Carro">Manuel Carro</a>, <a class="lpdoc-idx-anchor" id="Remy Haemmerle" href="ciaosearch.html#Remy Haemmerle">Remy Haemmerle</a>.<p>
<p><a class="lpdoc-idx-anchor" id="0" href="#when/2"><tt>when/2</tt></a> delays a predicate until some condition in its variable is met. For example, we may want to find out the maximum of two numbers, but we are not sure when they will be instantiated. We can write the standard <a class="lpdoc-idx-anchor" id="1" href="ciaosearch.html#max/3"><tt>max/3</tt></a> predicate (but changing its name to <a class="lpdoc-idx-anchor" id="2" href="ciaosearch.html#gmax/3"><tt>gmax/3</tt></a> to denote that the first and second arguments must be ground) as<p><pre class="lpdoc-codeblock">gmax(X, Y, X):- X > Y, !.
gmax(X, Y, Y):- X =< Y.
</pre> <p>and then define a 'safe' <a class="lpdoc-idx-anchor" id="3" href="ciaosearch.html#max/3"><tt>max/3</tt></a> as<p> <pre class="lpdoc-codeblock">max(X, Y, Z):-
when((ground(X),ground(Y)), gmax(X, Y, Z)).
</pre> <p>which can be called as follows:<p><pre class="lpdoc-codeblock">?- max(X, Y, Z) , Y = 0, X = 8.
X = 8,
Y = 0,
Z = 8 ?
yes
</pre> <p>Alternatively, <a class="lpdoc-idx-anchor" id="4" href="ciaosearch.html#max/3"><tt>max/3</tt></a> could have been defined as<p><pre class="lpdoc-codeblock">max(X, Y, Z):-
when(ground((X, Y)), gmax(X, Y, Z)).
</pre> <p>with the same effects as above. More complex implementations are possible. Look, for example, at the <tt>max.pl</tt> implementation under the <tt>when</tt> library directory, where a <a class="lpdoc-idx-anchor" id="5" href="ciaosearch.html#max/3"><tt>max/3</tt></a> predicate is implemented which waits on all the arguments until there is enough information to determine their values:<p><pre class="lpdoc-codeblock">?- use_module(library(when/max)).
yes
?- max(X, Y, Z), Z = 5, Y = 4.
X = 5,
Y = 4,
Z = 5 ?
yes
</pre> <p><br/><div id="Usage and interface"><h2>Usage and interface</h2><div class="lpdoc-cartouche"><ul><li><strong>Library usage:</strong><br/><tt>:- use_module(library(when)).</tt><li><strong>Exports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>Predicates:</em><br/><a class="lpdoc-idx-anchor" id="6" href="#when/2"><tt>when/2</tt></a>.
<li><em>Regular Types:</em><br/><a class="lpdoc-idx-anchor" id="7" href="#wakeup_exp/1"><tt>wakeup_exp/1</tt></a>.
</ul></ul></div></div><div id="Documentation on exports"><h2>Documentation on exports</h2><div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="when/2" href="ciaosearch.html#when/2">when/2</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>when(WakeupCond,Goal)</tt>
</span><p>Delays / executes <span class="lpdoc-var">Goal</span> according to <span class="lpdoc-var">WakeupCond</span> given. The <span class="lpdoc-var">WakeupCond</span>s now acceptable are <tt>ground(T)</tt> (<a class="lpdoc-idx-anchor" id="8" href="ciaosearch.html#Goal"><tt>Goal</tt></a> is delayed until <tt>T</tt> is ground), <tt>nonvar(T)</tt> (<a class="lpdoc-idx-anchor" id="9" href="ciaosearch.html#Goal"><tt>Goal</tt></a> is delayed until <tt>T</tt> is not a variable), and conjunctions and disjunctions of conditions:<p><pre class="lpdoc-codeblock">wakeup_exp(ground(_1)).
wakeup_exp(nonvar(_1)).
wakeup_exp((C1,C2)) :-
wakeup_exp(C1),
wakeup_exp(C2).
wakeup_exp((C1;C2)) :-
wakeup_exp(C1),
wakeup_exp(C2).
</pre> <p><a class="lpdoc-idx-anchor" id="10" href="#when/2"><tt>when/2</tt></a> only fails it the <span class="lpdoc-var">WakeupCond</span> is not legally formed. If <span class="lpdoc-var">WakeupCond</span> is met at the time of the call no delay mechanism is involved --- but there exists a time penalty in the condition checking.<p>In case that an instantiation fires the execution of several predicates, the order in which these are executed is not defined.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="11" href="#wakeup_exp/1"><tt>when:wakeup_exp/1</tt></a>)</span><span><span class="lpdoc-var">WakeupCond</span> is a legal expression for delaying goals.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="12" href="basic_props.html#cgoal/1"><tt>basic_props:cgoal/1</tt></a>)</span><span><span class="lpdoc-var">Goal</span> is a term which represents a goal, i.e., an atom or a structure.
</span>
</ul>
<em>Meta-predicate</em> with arguments: <tt>when(?,goal)</tt>.<br/></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">REGTYPE</span><a class="lpdoc-idx-anchor" id="wakeup_exp/1" href="ciaosearch.html#wakeup_exp/1">wakeup_exp/1</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>wakeup_exp(T)</tt>
</span><p><span class="lpdoc-var">T</span> is a legal expression for delaying goals.</p><ul class="lpdoc-itemize-minus"></ul></div></div><p>
</div><div id="Documentation on imports"><h2>Documentation on imports</h2>This module has the following direct dependencies:<ul class="lpdoc-itemize-minus"><li><em>System library modules:</em><br/><a class="lpdoc-idx-anchor" id="13" href="terms_vars.html"><tt>terms_vars</tt></a>, <a class="lpdoc-idx-anchor" id="14" href="sort.html"><tt>sort</tt></a>, <a class="lpdoc-idx-anchor" id="15" href="sets.html"><tt>sets</tt></a>.
<li><em>Packages:</em><br/><a class="lpdoc-idx-anchor" id="16" href="ciaosearch.html#prelude"><tt>prelude</tt></a>, <a class="lpdoc-idx-anchor" id="17" href="ciaosearch.html#initial"><tt>initial</tt></a>, <a class="lpdoc-idx-anchor" id="18" href="condcomp_doc.html"><tt>condcomp</tt></a>, <a class="lpdoc-idx-anchor" id="19" href="assertions_doc.html"><tt>assertions</tt></a>, <a class="lpdoc-idx-anchor" id="20" href="ciaosearch.html#assertions/assertions_basic"><tt>assertions/assertions_basic</tt></a>, <a class="lpdoc-idx-anchor" id="21" href="isomodes_doc.html"><tt>isomodes</tt></a>.
</ul></div></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>