Clause creation and destruction

These predicates enable the database to be altered during execution.

1. asserta/1

asserta(Clause) is true.

Templates and modes for the predicate are as follows:

asserta(@clause)

1.1 Example tests

The examples defined in this subsection assume the database has been created from the following Prolog text.

alice.tuprolog.PrologActionFixture
start alice.tuprolog.EngineFixture
enter theory

:- dynamic(legs/2).
legs(A, 6) :- insect(A).

:- dynamic(insect/1).
insect(ant).
insect(bee).

Let's then ask the engine to solve a query against the database, and check variable bindings.

alice.tuprolog.PrologActionFixture
enter query asserta(legs(octopus, 8)).
check hasSolution true
enter query asserta((legs(A, 4) :- animal(A))).
check hasSolution true
enter query asserta((foo(X) :- X, call(X))).
check hasSolution true

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query asserta(_).
check hasSolution false
check exception instantiation_error
enter query asserta(4).
check hasSolution false
check exception type_error(callable, 4)
enter query asserta((foo :- 4)).
check hasSolution false
check exception type_error(callable, 4)
enter query asserta((atom(_) :- true)).
check hasSolution false
check exception permission_error(modify, static_procedure, atom/1)

After these examples, the database could have been created from the following Prolog text.

alice.tuprolog.PrologActionFixture
check getTheory

:- dynamic(legs/2).
legs(A, 4) :- animal(A).
legs(octopus, 8).
legs(A, 6) :- insect(A).

:- dynamic(insect/1).
insect(ant).
insect(bee).

:- dynamic(foo/1).
foo(X) :- call(X), call(X).

2. assertz/1

assertz(Clause) is true.

Templates and modes for the predicate are as follows:

assertz(@clause)

2.1 Example tests

The examples defined in this subsection assume the database has been created from the following Prolog text.

alice.tuprolog.PrologActionFixture
start alice.tuprolog.EngineFixture
enter theory

:- dynamic(legs/2).
legs(A, 4) :- animal(A).
legs(octopus, 8).
legs(A, 6) :- insect(A).

:- dynamic(insect/1).
insect(ant).
insect(bee).

:- dynamic(foo/1).
foo(X) :- call(X), call(X).

Let's then ask the engine to solve a query against the database, and check variable bindings.

alice.tuprolog.PrologActionFixture
enter query assertz(legs(spider, 8)).
check hasSolution true
enter query assertz((legs(B, 2) :- bird(B))).
check hasSolution true
enter query assertz((foo(X) :- X -> call(X))).
check hasSolution true

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query assertz(_).
check hasSolution false
check exception instantiation_error
enter query assertz(4).
check hasSolution false
check exception type_error(callable, 4)
enter query assertz((foo :- 4)).
check hasSolution false
check exception type_error(callable, 4)
enter query assertz((atom(_) :- true)).
check hasSolution false
check exception permission_error(modify, static_procedure, atom/1)

After these examples, the database could have been created from the following Prolog text.

alice.tuprolog.PrologActionFixture
check getTheory

:- dynamic(legs/2).
legs(A, 4) :- animal(A).
legs(octopus, 8).
legs(A, 6) :- insect(A).
legs(spider, 8).
legs(B, 2) :- bird(B).

:- dynamic(insect/1).
insect(ant).
insect(bee).

:- dynamic(foo/1).
foo(X) :- call(X), call(X).
foo(X) :- call(X) -> call(X).

3. retract/1

retract(Clause) is true iff the database contains at least one dynamic procedure with a clause Clause which unifies with Head :- Body.

retract(Clause) is re-executable.

Templates and modes for the predicate are as follows:

retract(+clause)

3.1 Example tests

The examples defined in this subsection assume the database has been created from the following Prolog text.

alice.tuprolog.PrologActionFixture
start alice.tuprolog.EngineFixture
enter theory

:- dynamic(legs/2).
legs(A, 4) :- animal(A).
legs(octopus, 8).
legs(A, 6) :- insect(A).
legs(spider, 8).
legs(B, 2) :- bird(B).

:- dynamic(insect/1).
insect(ant).
insect(bee).

:- dynamic(foo/1).
foo(X) :- call(X), call(X).
foo(X) :- call(X) -> call(X).

Let's then ask the engine to solve a query against the database, and check variable bindings.

alice.tuprolog.PrologActionFixture
enter query retract(legs(octopus, 8)).
check hasSolution true Retracts the clause: legs(octopus, 8).
enter query retract(legs(spider, 6)).
check hasSolution false
enter query retract((legs(X, 2) :- T)).
check hasSolution true Retracts the clause: legs(B, 2) :- bird(B).
enter variable T
check binding bird(X)
enter query retract((legs(X, Y) :- Z)).
check hasSolution true Retracts the clause: legs(A, 4) :- animal(A).
enter variable Y
check binding 4
enter variable Z
check binding animal(X)
check hasAnotherSolution true Retracts the clause: legs(A, 6) :- insect(A).
enter variable Y
check binding 6
enter variable Z
check binding insect(X)
check hasAnotherSolution true Retracts the clause: legs(spider, 8).
enter variable Y
check binding 8
enter variable X
check binding spider
enter variable Z
check binding true
check hasAnotherSolution false
enter query retract((legs(X, Y) :- Z)).
check hasSolution false Now there are no legs/2 clauses in the database.
enter query retract(insect(I)), write(I), retract(insect(bee)), fail.
check hasSolution false Outputs ant and retracts the clause: insect(ant). And the clause: insect(bee).
check hasAnotherSolution false Fails on the choice point for: retract(insect(bee)).
check hasAnotherSolution false Fails on the choice point for: write(I).
check hasAnotherSolution false Fails on the execution of: retract(insect(bee)).
check hasAnotherSolution false Fails on the choice point for: write(I).
check hasAnotherSolution false Fails on the choice point for: retract(insect(I)).
check hasAnotherSolution false No more solutions.
enter query retract((foo(A) :- A, call(A))).
check hasSolution false
enter query retract((foo(C) :- A -> B)).
check hasSolution true Retracts the clause: foo(X) :- call(X) -> call(X).
enter variable A
check binding call(C)
enter variable B
check binding call(C)

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query retract((X :- in_eec(Y))).
check hasSolution false
check exception instantiation_error
enter query retract((4 :- X)).
check hasSolution false
check exception type_error(callable, 4)
enter query retract((atom(X) :- X == '[]')).
check hasSolution false
check exception permission_error(modify, static_procedure, atom/1)

4. abolish/1

abolish(Pred) is true.

Templates and modes for the predicate are as follows:

abolish(@predicate_indicator)

4.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

alice.tuprolog.PrologActionFixture
enter query abolish(foo/2).
check hasSolution true Also undefines foo/2 if there exists a dynamic procedure with predicate indicator foo/2.

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query abolish(foo/_).
check hasSolution false
check exception instantiation_error
enter query abolish(foo).
check hasSolution false
check exception type_error(predicate_indicator, foo)
enter query abolish(foo(_)).
check hasSolution false
check exception type_error(predicate_indicator, foo(_))
enter query abolish(abolish/1).
check hasSolution false
check exception permission_error(modify, static_procedure, abolish/1)

Run the tests!


The results of the tests for Clause creation and destruction are as follows:

fit.Summary