%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code assumes that the file /tmp/all_compressions.pl contains
% all the hypoth(Loop_no, H, Compression) facts generated during the
% current cycle of the CLML loop.
%
% If the list of hypoth(Loop_no, H, Compression) facts in
% /tmp/all_compressions.pl contains more than one such fact for each
% hypothesis then, for each hypothesis, this code outputs the
% hypoth(Loop_no, H, Compression) fact with the highest compression
% value to the file /tmp/highest_compressions.pl.
%
% In other words, if individual hypotheses were awarded with more than
% one compression value then this code removes all but the highest
% compression value.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- set(h,1000000)?
:- set(r,1000000)?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

get_max_compressions:-
	consult('/tmp/all_compressions.pl'),
	setof(H, hypoth(_, H, _), List_of_hypotheses),
	tell('/tmp/highest_compressions.pl'),
	max_compressions(List_of_hypotheses),
	told.


max_compressions([]).

max_compressions([H|T]):-
	setof(Compression, hypoth(_, H, Compression), L),
	sort(L, L2),
	reverse(L2, [Highest_compression | _]),
	hypoth(Loop_no, H, _),
	write('hypoth('),
	write(Loop_no),	
	write(', '),
	write(H),
	write(', '),
	write(Highest_compression),
	write(').'),
	nl,
	max_compressions(T).


reverse([], []).

reverse([H|T], L):-
	reverse(T,M),
	append(M, [H], L).

append([], L, L).

append([H|T], M, [H | T1]):-
	append(T, M, T1).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- get_max_compressions?

