Sophie

Sophie

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

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               - Fast/concurrent update of facts</TITLE>
</HEAD>
<BODY> 
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_27.html">previous</A>, <A HREF="ciao_29.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="SEC167" HREF="ciao_toc.html#TOC167">Fast/concurrent update of facts</A></H1>
<P>
<A NAME="IDX1905"></A>


<P>
<STRONG>Author(s):</STRONG> Daniel Cabeza, 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#95 (2001/5/2, 12:18:6 CEST)


<P>
<A NAME="IDX1906"></A>
<A NAME="IDX1907"></A>
Prolog implementations traditionally implement the concept of 
<A NAME="IDX1908"></A>
dynamic predicates: predicates which can be inspected or modified at run-time, adding or deleting individual clauses. The power of this feature comes at a cost: as new clause bodies can be arbitrarily added to the program, new predicate calls can arise which are not 'visible' at compile-time, thus complicating global analysis and optimization of the code. But it is the case that most of the time what the programmer wants is simply to store data, with the purpose of sharing it between search branches, predicates, or even execution threads. In Ciao the concept of data predicate serves this purpose: a data predicate is a predicate composed exclusively by facts, which can be inspected, and dynamically added or deleted, at run-time. Using data predicates instead of normal dynamic predicates brings benefits in terms of speed, but above all makes the code much easier to analyze automatically and thus allows better optimization.
<A NAME="IDX1909"></A>
<A NAME="IDX1910"></A>
<A NAME="IDX1911"></A>
<A NAME="IDX1912"></A>


<P>
Also, a special kind of data predicates exists, 
<A NAME="IDX1913"></A>
<A NAME="IDX1914"></A>
<EM>concurrent predicates</EM>, which can be used to communicate/synchronize among different execution threads (see section <A HREF="ciao_87.html#SEC368">Low-level concurrency/multithreading primitives</A>). 


<P>
Data predicates must be declared through a 
<A NAME="IDX1915"></A>
<CODE>data/1</CODE> declaration. Concurrent data predicates must be declared through a 
<A NAME="IDX1916"></A>
<CODE>concurrent/1</CODE> declaration. 



<UL>
<LI><A HREF="ciao_28.html#SEC168">Usage and interface (data_facts)</A>
<LI><A HREF="ciao_28.html#SEC169">Documentation on exports (data_facts)</A>
<LI><A HREF="ciao_28.html#SEC170">Documentation on internals (data_facts)</A>
</UL>



<H2><A NAME="SEC168" HREF="ciao_toc.html#TOC168">Usage and interface (<CODE>data_facts</CODE>)</A></H2>

<div class="cartouche">

<UL>

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

These predicates are builtin in Ciao, so nothing special has to be done to use them.

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


<UL>

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

<A NAME="IDX1917"></A>
<CODE>asserta_fact/1</CODE>, 
<A NAME="IDX1918"></A>
<CODE>asserta_fact/2</CODE>, 
<A NAME="IDX1919"></A>
<CODE>assertz_fact/1</CODE>, 
<A NAME="IDX1920"></A>
<CODE>assertz_fact/2</CODE>, 
<A NAME="IDX1921"></A>
<CODE>current_fact/1</CODE>, 
<A NAME="IDX1922"></A>
<CODE>current_fact/2</CODE>, 
<A NAME="IDX1923"></A>
<CODE>retract_fact/1</CODE>, 
<A NAME="IDX1924"></A>
<CODE>retractall_fact/1</CODE>, 
<A NAME="IDX1925"></A>
<CODE>current_fact_nb/1</CODE>, 
<A NAME="IDX1926"></A>
<CODE>retract_fact_nb/1</CODE>, 
<A NAME="IDX1927"></A>
<CODE>close_predicate/1</CODE>, 
<A NAME="IDX1928"></A>
<CODE>open_predicate/1</CODE>, 
<A NAME="IDX1929"></A>
<CODE>set_fact/1</CODE>, 
<A NAME="IDX1930"></A>
<CODE>erase/1</CODE>.

</UL>

</UL>

</div class="cartouche">



<H2><A NAME="SEC169" HREF="ciao_toc.html#TOC169">Documentation on exports (<CODE>data_facts</CODE>)</A></H2>
<P>
<A NAME="IDX1931"></A>
<A NAME="IDX1932"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>asserta_fact/1:</B>
<DD><A NAME="IDX1933"></A>


<P>
<CODE>asserta_fact(Fact)</CODE>


<P>
<CODE>Fact</CODE> is added to the corresponding 
<A NAME="IDX1934"></A>
data predicate. The fact becomes the first clause of the predicate concerned.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>asserta_fact(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1935"></A>
<A NAME="IDX1936"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>asserta_fact/2:</B>
<DD><A NAME="IDX1937"></A>


<P>
<CODE>asserta_fact(Fact, Ref)</CODE>


<P>
Same as 
<A NAME="IDX1938"></A>
<CODE>asserta_fact/1</CODE>, instantiating <CODE>Ref</CODE> to a unique identifier of the asserted fact.


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


</DL>

<P>
<A NAME="IDX1939"></A>
<A NAME="IDX1940"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>assertz_fact/1:</B>
<DD><A NAME="IDX1941"></A>


<P>
<CODE>assertz_fact(Fact)</CODE>


<P>
<CODE>Fact</CODE> is added to the corresponding 
<A NAME="IDX1942"></A>
data predicate. The fact becomes the last clause of the predicate concerned.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>assertz_fact(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1943"></A>
<A NAME="IDX1944"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>assertz_fact/2:</B>
<DD><A NAME="IDX1945"></A>


<P>
<CODE>assertz_fact(Fact, Ref)</CODE>


<P>
Same as 
<A NAME="IDX1946"></A>
<CODE>assertz_fact/1</CODE>, instantiating <CODE>Ref</CODE> to a unique identifier of the asserted fact.


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


</DL>

<P>
<A NAME="IDX1947"></A>
<A NAME="IDX1948"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>current_fact/1:</B>
<DD><A NAME="IDX1949"></A>


<P>
<CODE>current_fact(Fact)</CODE>


<P>
Gives on backtracking all the facts defined as data or concurrent which unify with <CODE>Fact</CODE>. It is faster than calling the predicate explicitly, which do invoke the meta-interpreter. If the <CODE>Fact</CODE> has been defined as concurrent and has not been 
<A NAME="IDX1950"></A>
closed, 
<A NAME="IDX1951"></A>
<CODE>current_fact/1</CODE> will wait (instead of failing) for more clauses to appear after the last clause of <CODE>Fact</CODE> is returned.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>current_fact(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1952"></A>
<A NAME="IDX1953"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>current_fact/2:</B>
<DD><A NAME="IDX1954"></A>


<P>
<CODE>current_fact(Fact, Ref)</CODE>


<P>
<CODE>Fact</CODE> is a fact of a 
<A NAME="IDX1955"></A>
data predicate and <CODE>Ref</CODE> is its reference identifying it uniquely.


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


<P>
<STRONG>Usage 1:</STRONG> <CODE>current_fact(+callable, -reference)</CODE>

<UL>
<LI><EM>Description:</EM> Gives on backtracking all the facts defined as data which unify with <CODE>Fact</CODE>, instantiating <CODE>Ref</CODE> to a unique identifier for each fact.

</UL>

<P>
<STRONG>Usage 2:</STRONG> <CODE>current_fact(?callable, +reference)</CODE>

<UL>
<LI><EM>Description:</EM> Given <CODE>Ref</CODE>, unifies <CODE>Fact</CODE> with the fact identified by it.

</UL>

</DL>

<P>
<A NAME="IDX1956"></A>
<A NAME="IDX1957"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>retract_fact/1:</B>
<DD><A NAME="IDX1958"></A>


<P>
<CODE>retract_fact(Fact)</CODE>


<P>
Unifies <CODE>Fact</CODE> with the first matching fact of a 
<A NAME="IDX1959"></A>
data predicate, and then erases it. On backtracking successively unifies with and erases new matching facts. If <CODE>Fact</CODE> is declared as 
<A NAME="IDX1960"></A>
concurrent and is non-
<A NAME="IDX1961"></A>
closed, 
<A NAME="IDX1962"></A>
<CODE>retract_fact/1</CODE> will wait for more clauses or for the closing of the predicate after the last matching clause has been removed.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>retract_fact(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1963"></A>
<A NAME="IDX1964"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>retractall_fact/1:</B>
<DD><A NAME="IDX1965"></A>


<P>
<CODE>retractall_fact(Fact)</CODE>


<P>
Erase all the facts of a 
<A NAME="IDX1966"></A>
data predicate unifying with <CODE>Fact</CODE>. Even if all facts are removed, the predicate continues to exist.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>retractall_fact(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1967"></A>
<A NAME="IDX1968"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>current_fact_nb/1:</B>
<DD><A NAME="IDX1969"></A>


<P>
<CODE>current_fact_nb(Fact)</CODE>


<P>
Behaves as 
<A NAME="IDX1970"></A>
<CODE>current_fact/1</CODE> but a fact is never waited on even if it is 
<A NAME="IDX1971"></A>
concurrent and non-closed.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>current_fact_nb(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1972"></A>
<A NAME="IDX1973"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>retract_fact_nb/1:</B>
<DD><A NAME="IDX1974"></A>


<P>
<CODE>retract_fact_nb(Fact)</CODE>


<P>
Behaves as 
<A NAME="IDX1975"></A>
<CODE>retract_fact/1</CODE>, but never waits on a fact, even if it has been declared as 
<A NAME="IDX1976"></A>
concurrent and is non-
<A NAME="IDX1977"></A>
closed.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>retract_fact_nb(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1978"></A>
<A NAME="IDX1979"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>close_predicate/1:</B>
<DD><A NAME="IDX1980"></A>


<P>
<CODE>close_predicate(Pred)</CODE>


<P>
<A NAME="IDX1981"></A>
<A NAME="IDX1982"></A>
Changes the behavior of the predicate <CODE>Pred</CODE> if it has been declared as a 
<A NAME="IDX1983"></A>
concurrent predicate: calls to this predicate will fail (instead of wait) if no more clauses of <CODE>Pred</CODE> are available.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>close_predicate(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1984"></A>
<A NAME="IDX1985"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>open_predicate/1:</B>
<DD><A NAME="IDX1986"></A>


<P>
<CODE>open_predicate(Pred)</CODE>


<P>
Reverts the behavior of 
<A NAME="IDX1987"></A>
concurrent predicate <CODE>Pred</CODE> to waiting instead of failing if no more clauses of <CODE>Pred</CODE> are available.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>open_predicate(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1988"></A>
<A NAME="IDX1989"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>set_fact/1:</B>
<DD><A NAME="IDX1990"></A>


<P>
<CODE>set_fact(Fact)</CODE>


<P>
Sets <CODE>Fact</CODE> as the unique fact of the corresponding 
<A NAME="IDX1991"></A>
data predicate.


<P>
<EM>Meta-predicate</EM> with arguments: <CODE>set_fact(fact)</CODE>.


</DL>

<P>
<A NAME="IDX1992"></A>
<A NAME="IDX1993"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>erase/1:</B>
<DD><A NAME="IDX1994"></A>


<P>
<CODE>erase(Ref)</CODE>


<P>
Deletes the clause referenced by <CODE>Ref</CODE>.


</DL>



<H2><A NAME="SEC170" HREF="ciao_toc.html#TOC170">Documentation on internals (<CODE>data_facts</CODE>)</A></H2>
<P>
<A NAME="IDX1995"></A>
<A NAME="IDX1996"></A>
<DL>
<DT><span class="define">DECLARATION:</span> <B>data/1:</B>
<DD><A NAME="IDX1997"></A>


<P>
<STRONG>Usage:</STRONG> :- <CODE>data Predicates</CODE>.

<UL>
<LI><EM>Description:</EM> Defines each predicate in <CODE>Predicates</CODE> as a

<A NAME="IDX1998"></A>
data predicate. If a predicate is defined data in a file, it must be defined data in every file containing clauses for that predicate. The directive should precede all clauses of the affected predicates. This directive is defined as a prefix operator in the compiler. 
<LI><EM>The following properties hold at call time:</EM>

<CODE>Predicates</CODE> is a sequence or list of <CODE>predname</CODE>s.
 (<CODE>basic_props:sequence_or_list/2</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1999"></A>
<A NAME="IDX2000"></A>
<DL>
<DT><span class="define">DECLARATION:</span> <B>concurrent/1:</B>
<DD><A NAME="IDX2001"></A>


<P>
<STRONG>Usage:</STRONG> :- <CODE>concurrent Predicates</CODE>.

<UL>
<LI><EM>Description:</EM> Defines each predicate in <CODE>Predicates</CODE> as a

<A NAME="IDX2002"></A>
concurrent predicate. If a predicate is defined concurrent in a file, it must be defined concurrent in every file containing clauses for that predicate. The directive should precede all clauses of the affected predicates. This directive is defined as a prefix operator in the compiler. 
<LI><EM>The following properties hold at call time:</EM>

<CODE>Predicates</CODE> is a sequence or list of <CODE>predname</CODE>s.
 (<CODE>basic_props:sequence_or_list/2</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX2003"></A>
<A NAME="IDX2004"></A>
<DL>
<DT><span class="define">REGTYPE:</span> <B>reference/1:</B>
<DD><A NAME="IDX2005"></A>


<P>
<STRONG>Usage:</STRONG> <CODE>reference(R)</CODE>

<UL>
<LI><EM>Description:</EM> <CODE>R</CODE> is a reference of a dynamic or data clause.

</UL>

</DL>

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