Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Prolog: assert and retract


1. Prolog: assert and retract

2. Prolog: assert and retract
Prolog (and Datalog) can model relational databases.

The set of facts is called a database, the intensional database. The rules are the extensional database.

Here is an example. Here is the Prolog code.

Here is the output of the Prolog code.

How can we add a running count to the list of solutions generated using backtracking?

3. Running count
To support practical database representation, the assert and retract predicates are provided.

These predicates can be misused to simulate parts of an assignment-based language.

A common use of assert and retract is to count solutions in the presence of backtracking using fail.

Here is an example that uses assert and retract to display a running count of solutions. Here is the Prolog code.

Here is the output of the Prolog code.

Exercise: Change the code (in a minimal way) to output from 0 to 7 rather than 1 to 8 without changing the first assert which asserts count(0).

4. End of page