[net.lang.prolog] Tests for assert and retract

cdsm@icdoc.UUCP (11/01/85)

		Tests for Assert and Retract

Below is a set of tests to look at the effects of assert and
retract along the lines of my earlier tests for cut. I would be
very grateful if anyone with access to a Prolog not listed in
the set of results which I already have could run this and send
me the results (directly) and I will post the list at some
future date in the network. So far I haven't found two
implementations which give the same results! For some
implementations, you may need to add declarations or make other
slight changes to get the desired effects.

------------------------------------------------------------
main :-
    test1, test2, test3, test4, test5, test6, test7, test8, test9.
    /* Tests to distinguish various implementations of assert */

test1 :- do('1. Test assert on existing predicate',t1).
test2 :- do('2. Test assert on new predicate',t2).
test3 :- do('3. Does it find added 3rd clause on backtracking', t3).
test4 :- do('4. Does it find added next (last) clause on backtracking', t4).
test5 :- do('5. Test retract on single element clause',t5).
test6 :- do('6. Does retract backtrack? ', t6).
test7 :- do('7. Does retract work on invoked clause',t7).
test8 :- do('8. Does retract work on next (last) invoked clause',t8).
test9 :- do('9. Test retracts and asserts on invoked clause', t9).

do(Message,Test) :- w(Message), Test, !.
do(Message,Test) :- w('No Clause found').
w(X) :- write(X), nl.  
t :- w('Added clause').
tr :- w('Failed to retract clause').

t1  :- assert((t1a :- t)), t1a.
t1a :- w('Clause 1'),fail.

t2  :- assert((t2a :- t)), t2a.

t3  :- assert((t3:-t)),fail.
t3  :- fail.

t4  :- assert((t4:-t)),fail.

t5  :- retract((t5a :- tr)), t5a.
t5a :- tr.

t6  :- retract((t6a:-X)),fail.
t6  :- t6a.
t6a :- tr.
t6a :- w('Does not backtrack').

t7  :- t7a, fail.
t7  :- fail.
t7  :- tr.
t7a :- retract((t7:-tr)),!.

t8  :- t8a, fail.
t8  :- tr.
t8a :- retract((t8:-tr)),!.

t9  :- retract((t9:-w('Second'))),assert((t9:-t)),fail.
t9  :- w('Second').
-----------------------------------------------------

Results I already have are as follows:

			1   2 3 4 5 6 7 8 9
cprolog			CA  A A N N N N N A
Dec-10, interpreted	CA  A A A N F N N A
Dec-10  compiled	CN  N N N N F F F S
micro prolog (CP/M-80)	CA  A A N E D F N A
muprolog		CA  A A A N N N N A
poplog			CA  A N N N N F F S
sigmaprolog (unix)	CA  A A N E D N F A
sigmaprolog (Dec front)	CA  A A N N F N F A

where letters stand for the first letter of the message printed
out by the test, and E stands for a "no such predicate" error
message. Note that in the latter case you may have to split up
the "main" procedure into several parts.

This set of tests is certainly not complete. In particular it
doesn't explore the interactions of assert and retract (except
for one test, 9), nor does it look at versions of assert and
retract which have numbered arguments. It's main purpose is
probably to ring the alarm bells about the differences which
have crept in and point to the need for reform. One might note
that retract together with garbage collection and reuse of an
"orphaned" pointer is the easiest way to crash many prolog
systems. I haven't included such a test as the details appear to
differ for every implementation which is liable.

			Chris Moss, Imperial College, Sep 85