3/*********************************** AMALGAMATING PERIODS *************************************/
    4
    5/********************************************************************************************** 
    6   If the last previous interval meets the first new interval then these intervals are united
    7   Otherwise new intervals are simply appended to previous intervals.
    8   We could have used union_all to perform this operation - however, when we are interested in 
    9   amalgamating one interval with a list of intervals the predicate is below is more efficient 
   10   than the more general union_all.
   11 **********************************************************************************************/
   12
   13amalgamatePeriods([], Periods, Periods) :- !.
   14
   15amalgamatePeriods(Periods, [], Periods) :- !.
   16
   17amalgamatePeriods([(OS,NS)], [(NS,NE)|TailNewPeriods], [(OS,NE)|TailNewPeriods]) :- !.
   18
   19amalgamatePeriods([(OS,ON)], [(NS,NE)|TailNewPeriods], [(OS,ON),(NS,NE)|TailNewPeriods])