Subject: Re: Did this message ever arrive (inserting/replacing clauses)?

From: Jan Wielemaker <[email-masked]>
Date: Thu, 6 Apr 2000 21:43:09 +0200

> > How deeply does SWI Prolog index clauses, anyway?

Principal functor on name+arity and atomic data (int, float, atom, string).

> This is easy. I can use term unification to find a clause.
> 
> Let's say I want to insert Head :- Body.
> 
> (	copy_term(Head, Copy),
> 	clause(Copy, _, Ref),
> 	clause(Prev, _, Ref),
> 	(	Head =@= Copy,
> 		\+ Prev =@= Copy
> 	->	insert Head :- Body before Ref
> 	;	\+ Head =@= Copy,
> 		Prev =@= Copy
> 	->	fail
> 	;	\+order(Head, Prev, >)
> 	->	insert Head :- Body before Ref
> 	)
> ;	assertz(Head :- Body)
> )

As you are already doing clause/3 on anyway, the only extra you'll
have to add is one asserta.  You collect the clauses and their
references upto the place you have to insert.  Then you use erase/1
and asserta to stick them all back.

I'm not sure, but asserta/1 isn't much slower then clause/3.  So
at most it is about twice as slow.  I think that isn't worth the
trouble adding an assert_before.  Most likely it isn't very hard
to do, but it does require complicated code to deal with the
various different clause-indexing schemas used.

	Regards --- Jan