
:- op(450,fx,[ins_key,del_key]).
:- op(900,xfy,[':=']).

init_keyappl :- 
	declare(key(counter/1,0)),
	ins(counter(1)).

bulk_ins(FromPred,ToPred) :-
	bulkins(FromPred,ToPred).

bulk_del(Pred) :-
	bulkdel(Pred).

% assign FromP to ToPred, note that ToPred should have been declared.
%
':='(ToPred,TestPred) :-
	ToPred =.. [Name|Args],
	Temp =.. [temp|Args],
	length(Args,NArgs),
	keyed(Name,NArgs,NKeys),
	NVal is NArgs-NKeys,
	createpreds(NVal,Vals),
	separate_key_val(Args,NKeys,Keys,_),
	append(Keys,Vals,DupArgs),
	DupPred =.. [temp|DupArgs],
	deleting(tr),
	copy((TestPred,not(DupPred)),Temp),
	retractall(OutPred),
	move(Temp,OutPred),
	inserting(tr).

incr_counter(N,Delta) :-
	retract(counter(N)),
	M is N + Delta,
	assert(counter(M)).


% create the new predicate.
set_n_create(X) :- 
		X =.. [Name|Args],
		length(Args,NArgs),
		keyed(Name,NArgs,NKeys),
		(NKeys = NArgs -> ins(X)
		;
		NVal is NArgs-NKeys,
		separate_key_val(Args,NKeys,Keys,_),
	        createpreds(NVal,OldVals),
		append(Keys,OldVals,OldArgs),
		OldPred =.. [Name|OldArgs],
		(not(OldPred), assert(Pred) )).
