<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> Dynamic clause management </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual029.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual023.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual031.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66dbff"><DIV ALIGN=center><TABLE> <TR><TD><FONT SIZE=4><B><A NAME="htoc91">7.7</A></B></FONT></TD> <TD WIDTH="100%" ALIGN=center><FONT SIZE=4><B>Dynamic clause management</B></FONT></TD> </TR></TABLE></DIV></TD> </TR></TABLE><UL> <LI><A HREF="manual030.html#toc63"> Introduction</A> <LI><A HREF="manual030.html#toc64"> <TT>asserta/1</TT>, <TT>assertz/1</TT></A> <LI><A HREF="manual030.html#toc65"> <TT>retract/1</TT></A> <LI><A HREF="manual030.html#toc66"> <TT>retractall/1</TT></A> <LI><A HREF="manual030.html#toc67"> <TT>clause/2</TT></A> <LI><A HREF="manual030.html#toc68"> <TT>abolish/1</TT></A> </UL> <BR> <A NAME="toc63"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc92">7.7.1</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Introduction</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="Introduction:(Dynamic-clause-management)"></A> <B>Static and dynamic procedures</B>: a procedure is either dynamic or static. All built-in predicates are static. A user-defined procedure is static by default unless a <TT>dynamic/1</TT> directive precedes its definition (section <A HREF="manual021.html#dynamic/1">6.1.2</A>). Adding a clause to a non-existent procedure creates a dynamic procedure. The clauses of a dynamic procedure can be altered (e.g. using <TT>asserta/1</TT>), the clauses of a static procedure cannot be altered. <BR> <BR> <B>Private and public procedures</B>: each procedure is either public or private. A dynamic procedure is always public. Each built-in predicate is private, and a static user-defined procedure is private by default unless a <TT>public/1</TT> directive precedes its definition (section <A HREF="manual021.html#public/1">6.1.3</A>). If a dynamic declaration exists it is unnecessary to add a public declaration since a dynamic procedure is also public. A clause of a public procedure can be inspected (e.g. using <TT>clause/2</TT>), a clause of a private procedure cannot be inspected.<BR> <BR> <B>A logical database update view</B>: any change in the database that occurs as the result of executing a goal (e.g. when a sub-goal is a call of <TT>assertz/1</TT> or <TT>retract/1</TT>) only affects subsequent activations. The change does not affect any activation that is currently being executed. Thus the database is frozen during the execution of a goal, and the list of clauses defining a predication is fixed at the moment of its execution.<BR> <BR> <A NAME="toc64"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc93">7.7.2</A></B></TD> <TD WIDTH="100%" ALIGN=center><B><TT>asserta/1</TT>, <TT>assertz/1</TT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> asserta(+clause)<BR> assertz(+clause)</TT></DL> <B>Description</B><BR> <BR> <TT>asserta(Clause)</TT> first converts the term <TT>Clause</TT> to a clause and then adds it to the current internal database. The predicate concerned must be dynamic (section <A HREF="#Introduction:(Dynamic-clause-management)">7.7.1</A>) or undefined and the clause is inserted before the first clause of the predicate. If the predicated is undefined it is created as a dynamic procedure.<BR> <BR> <TT>assertz(Clause)</TT> acts like <TT>asserta/1</TT> except that the clause is added at the end of all existing clauses of the concerned predicate.<BR> <BR> <B>Converting a term <TT>Clause</TT> to a clause <TT>Clause1</TT>:</B> <UL><LI>extract the head and the body of <TT>Clause</TT>: either <TT>Clause</TT> = <TT>(Head :- Body)</TT> or <TT>Clause</TT> = <TT>Head</TT> and <TT>Body</TT> = <TT>true</TT>.<BR> <BR> <LI><TT>Head</TT> must be a callable term (or else the conversion fails).<BR> <BR> <LI>convert <TT>Body</TT> to a body clause (i.e. a goal) <TT>Body1</TT>.<BR> <BR> <LI>the converted clause <TT>Clause1</TT> = <TT>(Head :- Body1)</TT>.</UL> <B>Converting a term <TT>T</TT> to a goal:</B> <UL><LI>if <TT>T</TT> is a variable it is replaced by the term <TT>call(T)</TT>.<BR> <BR> <LI>if <TT>T</TT> is a control construct <TT>(',')/2</TT>, <TT>(;)/2</TT> or <TT>(->)/2</TT> each argument of the control construct is recursively converted to a goal.<BR> <BR> <LI>if <TT>T</TT> is a callable term it remains unchanged.<BR> <BR> <LI>otherwise the conversion fails (<TT>T</TT> is neither a variable nor a callable term).</UL> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is neither a variable nor a callable term</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(callable, Head)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Body</TT> cannot be converted to a goal</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(callable, Body)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> of <TT>Head</TT> is that of a static procedure</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>permission_error(modify, static_procedure, Pred)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <B>Portability</B><BR> <BR> ISO predicates.<BR> <BR> <A NAME="toc65"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc94">7.7.3</A></B></TD> <TD WIDTH="100%" ALIGN=center><TT><B>retract/1</B></TT></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> retract(+clause)</TT></DL> <B>Description</B><BR> <BR> <TT>retract(Clause)</TT> erases the first clause of the database that unifies with <TT>Clause</TT>. The concerned predicate must be a dynamic procedure (section <A HREF="#Introduction:(Dynamic-clause-management)">7.7.1</A>). Removing all clauses of a procedure does not erase the procedure definition. To achieve this use <TT>abolish/1</TT> (section <A HREF="#abolish/1">7.7.6</A>). <TT>retract/1</TT> is re-executable on backtracking.<BR> <BR> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is neither a variable nor a callable term</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(callable, Head)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> of <TT>Head</TT> is that of a static procedure</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>permission_error(modify, static_procedure, Pred)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <B>Portability</B><BR> <BR> ISO predicate. In the ISO reference, the operation associated to the <TT>permission_error</TT> is <TT>access</TT> while it is <TT>modify</TT> in GNU Prolog. This seems to be an error of the ISO reference since for <TT>asserta/1</TT> (which is similar in spirit to <TT>retract/1</TT>) the operation is also <TT>modify</TT>.<BR> <BR> <A NAME="toc66"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc95">7.7.4</A></B></TD> <TD WIDTH="100%" ALIGN=center><TT><B>retractall/1</B></TT></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> retractall(+head)</TT></DL> <B>Description</B><BR> <BR> <TT>retractall(Head)</TT> erases all clauses whose head unifies with <TT>Head</TT>. The concerned predicate must be a dynamic procedure (section <A HREF="#Introduction:(Dynamic-clause-management)">7.7.1</A>). The procedure definition is not removed so that it is found by <TT>current_predicate/1</TT> (section <A HREF="manual031.html#current-predicate/1">7.8.1</A>). <TT>abolish/1</TT> should be used to remove the procedure (section <A HREF="#abolish/1">7.7.6</A>).<BR> <BR> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is not a callable term</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(callable, Head)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> of <TT>Head</TT> is that of a static procedure</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>permission_error(modify, static_procedure, Pred)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <B>Portability</B><BR> <BR> GNU Prolog predicate.<BR> <BR> <A NAME="toc67"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc96">7.7.5</A></B></TD> <TD WIDTH="100%" ALIGN=center><TT><B>clause/2</B></TT></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> clause(+head, ?callable_term)</TT></DL> <B>Description</B><BR> <BR> <TT>clause(Head, Body)</TT> succeeds if there exists a clause in the database that unifies with <TT>Head :- Body</TT>. The predicate in question must be a public procedure (section <A HREF="#Introduction:(Dynamic-clause-management)">7.7.1</A>). Clauses are delivered from the first to the last. This predicate is re-executable on backtracking. <BR> <BR> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Head</TT> is neither a variable nor a callable term</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(callable, Head)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> of <TT>Head</TT> is that of a private procedure</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>permission_error(access, private_procedure, Pred)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Body</TT> is neither a variable nor a callable term</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(callable, Body)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <B>Portability</B><BR> <BR> ISO predicate.<BR> <BR> <A NAME="toc68"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc97">7.7.6</A></B></TD> <TD WIDTH="100%" ALIGN=center><TT><B>abolish/1</B></TT></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="abolish/1"></A> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> abolish(+predicate_indicator)</TT></DL> <B>Description</B><BR> <BR> <TT>abolish(Pred)</TT> removes from the database the procedure whose predicate indicator is <TT>Pred</TT>. The concerned predicate must be a dynamic procedure (section <A HREF="#Introduction:(Dynamic-clause-management)">7.7.1</A>).<BR> <BR> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is a term <TT>Name/Arity</TT> and either <TT>Name</TT> or <TT>Arity</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is neither a variable nor a predicate indicator</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(predicate_indicator, Pred)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is a term <TT>Name/Arity</TT> and <TT>Arity</TT> is neither a variable nor an integer</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(integer, Arity)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is a term <TT>Name/Arity</TT> and <TT>Name</TT> is neither a variable nor an atom</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(atom, Name)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is a term <TT>Name/Arity</TT> and <TT>Arity</TT> is an integer < 0</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>domain_error(not_less_than_zero, Arity)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>Pred</TT> is a term <TT>Name/Arity</TT> and <TT>Arity</TT> is an integer > <TT>max_arity</TT> flag (section <A HREF="manual045.html#set-prolog-flag/2">7.22.1</A>)</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>representation_error(max_arity)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>The predicate indicator <TT>Pred</TT> is that of a static procedure</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>permission_error(modify, static_procedure, Pred)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <B>Portability</B><BR> <BR> ISO predicate.<BR> <BR> <HR SIZE=2> Copyright (C) 1999-2002 Daniel Diaz <BR> <BR> Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. <BR> <BR> <A HREF="index.html#copyright">More about the copyright</A> <HR> <A HREF="manual029.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual023.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual031.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>