Clause retrival and information

These predicates enable the contents of the database to be inspected during execution.

The examples provided for these predicates assume the database has been created from the following Prolog theory.

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

:- dynamic(cat/0).
cat.

:- dynamic(dog/0).
dog :- true.

elk(X) :- moose(X).

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

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

1. clause/2

clause(Head, Body) is true iff:

clause(Head, Body) is re-executable.

Note that the process of converting a clause to a term produces a renamed copy of the term H :- B corresponding to the clause.

Templates and modes for the predicate are as follows:

clause(+head, ?callable_term)

1.1 Example tests

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

alice.tuprolog.PrologActionFixture
enter query clause(cat, true).
check hasSolution true
enter query clause(dog, true).
check hasSolution true
enter query clause(legs(I, 6), Body).
check hasSolution true
enter variable Body
check binding insect(I)
enter query clause(legs(C, 7), Body).
check hasSolution true
enter variable Body
check binding (call(C), call(C))
enter query clause(insect(I), T).
check hasSolution true
enter variable I
check binding ant
enter variable T
check binding true
check hasAnotherSolution true
enter variable I
check binding bee
enter variable T
check binding true
enter query clause(x, Body).
check hasSolution false
enter query clause(legs(A, 6), insect(f(A))).
check hasSolution

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 clause(_, B).
check hasSolution false
check exception instantiation_error
enter query clause(4, X).
check hasSolution false
check exception type_error(callable, 4)
enter query clause(elk(N), Body).
check hasSolution true
enter query clause(atom(_), Body).
check hasSolution false
check exception permission_error(access, private_procedure, atom/1)

2. current_predicate/1

current_predicate(PI) is true iff PI is a predicate indicator for one of the user-defined procedures in the database.

current_predicate(PI) is re-executable.

Note that all user-defined procedures are found, whether static or dynamic. A user-defined procedure is also found even when it has no clauses. A user-defined procedure is not found if it has been abolished.

Templates and modes for the predicate are as follows:

current_predicate(?predicate_indicator)

2.1 Example tests

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

alice.tuprolog.PrologActionFixture
enter query current_predicate(dog/0).
check hasSolution true
enter query current_predicate(current_predicate/1).
check hasSolution false
enter query current_predicate(elk/Arity).
check hasSolution true
enter variable Arity
check binding 1
enter query current_predicate(foo/A).
check hasSolution false
enter query current_predicate(Name/1).
check hasSolution true
enter variable Name
check binding elk
check hasAnotherSolution true
check binding insect

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 current_predicate(4).
check hasSolution false
check exception type_error(predicate_indicator, 4)

Run the tests!


The results of the tests for Clause retrival and information are as follows:

fit.Summary