From jan@swi.psy.uva.nl  Thu Apr  6 21:42:50 2000
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id VAA21364;
	Thu, 6 Apr 2000 21:42:49 +0200 (MET DST)
Received: (from jan@localhost)
	by gollem.swi.psy.uva.nl (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id VAA11672;
	Thu, 6 Apr 2000 21:43:09 +0200
Date: Thu, 6 Apr 2000 21:43:09 +0200
Message-Id: <200004061943.VAA11672@gollem.swi.psy.uva.nl>
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Subject: Re: Did this message ever arrive (inserting/replacing clauses)?
To: Robert van Engelen <engelen@nu.cs.fsu.edu>,
        ok@atlas.otago.ac.nz (Richard A. O'Keefe)
In-Reply-To: Robert van Engelen's message of Thu, 6 Apr 2000 12:02:41 -0400 (EDT)
Phone: +31 - 20 - 525 6121
Cc: engelen@nu.cs.fsu.edu, jan@swi.psy.uva.nl, ok@atlas.otago.ac.nz,
        prolog@gollem.swi.psy.uva.nl

> > 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

