Hypertext Markup "chap11b"

Admin User, erstellt 23. Aug. 2023
         
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<!--
Modern Albufeira Prolog Interpreter
Literate Programming
Warranty & Liability
To the extent permitted by applicable law and unless explicitly
otherwise agreed upon, XLOG Technologies AG makes no warranties
regarding the provided information. XLOG Technologies AG assumes
no liability that any problems might be solved with the information
provided by XLOG Technologies AG.
Rights & License
All industrial property rights regarding the information - copyright
and patent rights in particular - are the sole property of XLOG
Technologies AG. If the company was not the originator of some
excerpts, XLOG Technologies AG has at least obtained the right to
reproduce, change and translate the information.
Reproduction is restricted to the whole unaltered document.
Reproduction of the information is only allowed for non-commercial
uses. Selling, giving away or letting of the execution of the
library is prohibited. The library can be distributed as part of
your applications and libraries for execution provided this comment
remains unchanged.
Restrictions
Only to be distributed with programs that add significant and primary
functionality to the library. Not to be distributed with additional
software intended to replace any components of the library.
Trademarks
Jekejeke is a registered trademark of XLOG Technologies AG.
-->
<body>
<link rel="stylesheet" href="notebook.css" type="text/css">
<title editable="nocomment">Chap11b</title>
<h1>Chap11b: retract</h1>
<p>The database manipulations we have been making have
changed the meaning of the predicate happy/1 . More generally,
database manipulation commands give us the ability to change
the meaning of predicates while we are running programs.
Predicates whose definitions change during run-time are
called dynamic predicates, as opposed to the static predicates
that we have previously dealt with. Most Prolog interpreters
insist that we explicitly declare the predicates that we wish
to be dynamic. We will soon examine an example involving dynamic
predicates, but let’s first complete our discussion of the
database manipulation commands.</p>
<p>Now that we know how to assert new information into the database,
we should also learn how to remove information when we no longer
need it. There is an inverse predicate to assertz/1 , namely retract/1 .
For example, if we carry straight on from the previous example by
giving the command:</p>
<table style="width: 100%">
<tr>
<td class="input" style="width: 100%"
contenteditable="true" id="text0" spellcheck="false"
>:- dynamic happy/1.
happy(mia).
happy(vincent).
happy(marcellus).
happy(butch).
happy(vincent).
?- retract(happy(marcellus)).
</td>
<td rowspan="2" valign="top">
<button class="run" id="launch0"
></button>
</td>
</tr>
<tr>
<td class="result" id="demo0"></td>
</tr>
</table>
<p>and then list the database, we get:</p>
<table style="width: 100%">
<tr>
<td class="input" style="width: 100%"
contenteditable="true" id="text1" spellcheck="false"
>?- listing.
</td>
<td rowspan="2" valign="top">
<button class="run" id="launch1"
></button>
</td>
</tr>
<tr>
<td class="result" id="demo1"></td>
</tr>
</table>
<p>That is, the fact happy(marcellus) has been removed.</p>
<p>Suppose we go on further, and say</p>
<table style="width: 100%">
<tr>
<td class="input" style="width: 100%"
contenteditable="true" id="text2" spellcheck="false"
>?- once(retract(happy(vincent))).
</td>
<td rowspan="2" valign="top">
<button class="run" id="launch2"
></button>
</td>
</tr>
<tr>
<td class="result" id="demo2"></td>
</tr>
</table>
<p>and then ask for a listing. We get:</p>
<table style="width: 100%">
<tr>
<td class="input" style="width: 100%"
contenteditable="true" id="text3" spellcheck="false"
>?- listing.
</td>
<td rowspan="2" valign="top">
<button class="run" id="launch3"
></button>
</td>
</tr>
<tr>
<td class="result" id="demo3"></td>
</tr>
</table>
<p>Note that the first occurrence of happy(vincent) , and only the
first occurrence, was removed.</p>
<p>To remove all of our assertions contributing to the definition of
the predicate happy/1 we can use a variable:</p>
<table style="width: 100%">
<tr>
<td class="input" style="width: 100%"
contenteditable="true" id="text4" spellcheck="false"
>?- retract(happy(X)).
</td>
<td rowspan="2" valign="top">
<button class="run" id="launch4"
></button>
</td>
</tr>
<tr>
<td class="result" id="demo4"></td>
</tr>
</table>
<p>A listing reveals that the database is now empty.</p>
<table style="width: 100%">
<tr>
<td class="input" style="width: 100%"
contenteditable="true" id="text5" spellcheck="false"
>?- listing.</td>
<td rowspan="2" valign="top">
<button class="run" id="launch5"
></button>
</td>
</tr>
<tr>
<td class="result" id="demo5"></td>
</tr>
</table>
<script type="module">
import {setup_engine, link_cells_async
} from "./engine.mjs";
setup_engine();
link_cells_async();
</script>
</body>
</html>