Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > by-pkgid > 2fc07611b08d4a735fd34d5eb60d8e16 > files > 2049

ciao-1.10p8-3mdv2010.0.i586.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k + clip patches and <A href="http://www.clip.dia.fi.upm.es/Software">lpdoc</A> from ciao.texi on 28 January 2007 -->

<LINK rel="stylesheet" href="ciao.css" type="text/css">
<TITLE>The Ciao Prolog System               - Delaying predicates (when)</TITLE>
</HEAD>
<BODY> 
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_106.html">previous</A>, <A HREF="ciao_108.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC442" HREF="ciao_toc.html#TOC442">Delaying predicates (when)</A></H1>
<P>
<A NAME="IDX5411"></A>


<P>
<STRONG>Author(s):</STRONG> Manuel Carro.


<P>
<STRONG>Version:</STRONG> 1.10#7 (2006/4/26, 19:22:13 CEST)


<P>
<STRONG>Version of last change:</STRONG> 1.7#3 (2000/7/21, 11:54:59 CEST)


<P>
 


<P>
<A NAME="IDX5412"></A>
<CODE>when/2</CODE> 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 NAME="IDX5413"></A>
<CODE>max/3</CODE> predicate (but changing its name to 
<A NAME="IDX5414"></A>
<CODE>gmax/3</CODE> to denote that the first and second arguments must be ground) as 



<PRE>
gmax(X, Y, X):- X &#62; Y, !.
gmax(X, Y, Y):- X =&#60; Y.
</PRE>

<P>
and then define a 'safe' 
<A NAME="IDX5415"></A>
<CODE>max/3</CODE> as 
 

<PRE>
max(X, Y, Z):-
        when((ground(X),ground(Y)), gmax(X, Y, Z)).
</PRE>

<P>
which can be called as follows: 



<PRE>
?- max(X, Y, Z) , Y = 0, X = 8.

X = 8,
Y = 0,
Z = 8 ? 

yes
</PRE>

<P>
Alternatively, 
<A NAME="IDX5416"></A>
<CODE>max/3</CODE> could have been defined as 



<PRE>
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 <CODE>max.pl</CODE> implementation under the <CODE>when</CODE> library directory, where a 
<A NAME="IDX5417"></A>
<CODE>max/3</CODE> predicate is implemented which waits on all the arguments until there is enough information to determine their values: 



<PRE>
?- use_module(library('when/max')).

yes
?- max(X, Y, Z), Z = 5, Y = 4.

X = 5,
Y = 4,
Z = 5 ? 

yes
</PRE>


<UL>
<LI><A HREF="ciao_107.html#SEC443">Usage and interface (when)</A>
<LI><A HREF="ciao_107.html#SEC444">Documentation on exports (when)</A>
<LI><A HREF="ciao_107.html#SEC445">Known bugs and planned improvements (when)</A>
</UL>



<H2><A NAME="SEC443" HREF="ciao_toc.html#TOC443">Usage and interface (<CODE>when</CODE>)</A></H2>

<div class="cartouche">

<UL>

<LI><STRONG>Library usage:</STRONG>

<CODE>:- use_module(library(when)).</CODE>

<LI><STRONG>Exports:</STRONG>


<UL>

<LI><EM>Predicates:</EM>

<A NAME="IDX5418"></A>
<CODE>when/2</CODE>.

<LI><EM>Regular Types:</EM>

<A NAME="IDX5419"></A>
<CODE>wakeup_exp/1</CODE>.

</UL>

<LI><STRONG>Other modules used:</STRONG>


<UL>

<LI><EM>System library modules:</EM>

<A NAME="IDX5420"></A>
<CODE>terms_vars</CODE>, 
<A NAME="IDX5421"></A>
<CODE>sort</CODE>, 
<A NAME="IDX5422"></A>
<CODE>sets</CODE>.

</UL>

</UL>

</div class="cartouche">



<H2><A NAME="SEC444" HREF="ciao_toc.html#TOC444">Documentation on exports (<CODE>when</CODE>)</A></H2>
<P>
<A NAME="IDX5423"></A>
<A NAME="IDX5424"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>when/2:</B>
<DD><A NAME="IDX5425"></A>


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>when(?,goal)</CODE>.


<P>
<STRONG>Usage:</STRONG> <CODE>when(WakeupCond, Goal)</CODE>

<UL>
<LI><EM>Description:</EM> Delays / executes <CODE>Goal</CODE> according to <CODE>WakeupCond</CODE> given. The <CODE>WakeupCond</CODE>s now acceptable are <CODE>ground(T)</CODE> (

<A NAME="IDX5426"></A>
<CODE>Goal</CODE> is delayed until <CODE>T</CODE> is ground), <CODE>nonvar(T)</CODE> (
<A NAME="IDX5427"></A>
<CODE>Goal</CODE> is delayed until <CODE>T</CODE> is not a variable), and conjunctions and disjunctions of conditions: 


<PRE>
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>

<A NAME="IDX5428"></A>
<CODE>when/2</CODE> only fails it the <CODE>WakeupCond</CODE> is not legally formed. If <CODE>WakeupCond</CODE> is met at the time of the call no delay mechanism is involved -- but there exists a time penalty in the condition checking. 

In case that an instantiation fires the execution of several predicates, the order in which these are executed is not defined. 
<LI><EM>The following properties should hold at call time:</EM>

<CODE>WakeupCond</CODE> is a legal expression for delaying goals.
 (<CODE>when:wakeup_exp/1</CODE>)

<CODE>Goal</CODE> is a term which represents a goal, i.e., an atom or a structure.
 (<CODE>basic_props:callable/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX5429"></A>
<A NAME="IDX5430"></A>
<DL>
<DT><span class="define">REGTYPE:</span> <B>wakeup_exp/1:</B>
<DD><A NAME="IDX5431"></A>


<P>
<STRONG>Usage:</STRONG> <CODE>wakeup_exp(T)</CODE>

<UL>
<LI><EM>Description:</EM> <CODE>T</CODE> is a legal expression for delaying goals.

</UL>

</DL>



<H2><A NAME="SEC445" HREF="ciao_toc.html#TOC445">Known bugs and planned improvements (<CODE>when</CODE>)</A></H2>


<UL>

<LI>

Redundant conditions are not removed.

<LI>

Floundered goals are not appropriately printed.
</UL>

<P><HR><P>
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_106.html">previous</A>, <A HREF="ciao_108.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
</BODY>
</HTML>