fifth-postulate / julian

Date and time library for Prolog
The Unlicense
27 stars 3 forks source link

improve nth(N,Form) form implementation #5

Closed mndrix closed 10 years ago

mndrix commented 11 years ago

The current implementation is based on attributed variables and iteration. Because it's not based on CLPFD constraints, we often end up with infinite loops or very slow results. Change the implementation to be based on something like this:

% nth(?DayOfMonth,?Nth)
%
% True if DayOfMonth is the Nth such day in that month sharing the same
% day of week.
nth(Day, N) :-
    Day - (N-1)*7 #= Day1,
    Day1 in 1..7.

The predicate works in both directions and correctly notices that Nth=2 implies that Day in 8..14 This constraint should propagate better and give us much better results in wider circumstances.

Unfortunately, this doesn't work for a form like nth(2, 2013-_-07) or other forms besides dow(_). nth/2 forms are almost always describing a day of the week, so this may not be a problem. Perhaps I keep the current implementation for the general case and optimize the day of week case with the relation above.