:- module(counter,[	initialize_counter/1,			%create & init a counter from 1
			initialize_counter/2,			%init counter from a certain no.
			clear_counter/1,			%remove a counter
			generate_counter/2			%get counter value & increment it
		]).

:- dynamic(counter/2).


:- [prolog_detect].
:- prolog_detect:prolog(Prolog),
	(	(Prolog=eclipse,
		[retractall]
		);
		true
	).


%
% general purpose counter management
%

initialize_counter(Counter) :-
	retractall(counter(Counter,_)),			%retract existing value
	!,
	asserta(counter(Counter,1)).			%and set it to 1

initialize_counter(Counter) :-
	asserta(counter(Counter,1)).			%if no current value exists, just set it

initialize_counter(Counter,Count) :-
	retractall(counter(Counter,_)),			%retract existing value
	!,
	asserta(counter(Counter,Count)).		%and set it to the value specified

initalize_count(Counter,Count) :-
	asserta(counter(Counter,Count)).		%if no current value exists

%
% remove a counter
%
clear_counter(Counter) :-
	retractall(counter(Counter,_)).



%
% get the value of a counter and advances it
%
generate_counter(Counter,Count) :-
	retract(counter(Counter,Count)),		%retract old one
	!,
	Next is Count+1,				%increment it
	asserta(counter(Counter,Next)).			%and save for next use

generate_counter(Counter,Count) :-
	initialize_counter(Counter),			%no value exist, just create a new one
	generate_counter(Counter,Count).		%and get value

