1% Comparison Library
    2:- module(purity, [
    3    pcompare/4,
    4    pdif/3,
    5    pdif/4,
    6    pif/3,
    7    eq/3,
    8    eq/4,
    9    lt/3,
   10    lt/4,
   11    lte/3,
   12    lte/4,
   13    gt/3,
   14    gt/4,    
   15    gte/3,
   16    gte/4    
   17]).   18
   19:- reexport([pchar, plist, pstring, punary]).   20
   21
   22% pcompare(Domain, TermA, TermB, Comparator) - 
   23%
   24% Comparator is one of <, > or =
   25% Comparator is the comparison between TermA and TermB
   26% both TermA and TermB must be in Domain
   27%
   28:- multifile(pcompare/4).   29
   30% if(Goal, TrueGoal, FalseGoal).
   31%
   32% Goal is a callable with the last argument as a boolean (true or false).
   33% if Goal results in true then TrueGoal is called.
   34% if Goal results in false then FalseGoal is called.
   35%
   36pif(Goal, TrueGoal, FalseGoal) :-
   37    call(Goal, R),
   38    pif_(R, TrueGoal, FalseGoal).
   39
   40pif_(true, Goal, _) :- call(Goal).
   41pif_(false, _, Goal) :- call(Goal).
   42
   43','(A, B, R) :-
   44    call(A, T),
   45    conj_(T, B, R).
   46
   47conj_(true, B, R) :-
   48    call(B, R).
   49conj_(false, _, false).
   50
   51';'(A, B, T) :-
   52    call(A, T)
   53    ; 
   54    call(B, T).
   55
   56
   57% pdif(Domain, TermA, TermB, Result).
   58%
   59% Result is true if TermA is not TermB for domain, otherwise false
   60%
   61pdif(D,A,B,R) :-
   62    pcompare(D,A,B,C),
   63    pdif_(C,R).
   64
   65pdif_(=,false).
   66pdif_(<,true).
   67pdif_(>,true).
   68
   69% pdif(Domain, TermA, TermB).
   70% 
   71% TermA is not TermB 
   72% TermA and TermB must be in Domain
   73% 
   74pdif(D,A,B) :- pdif(D, A, B, true).
   75
   76
   77% eq(Domain, TermA, TermB).
   78%
   79% TermA and TermB are equal for Domain.
   80%
   81eq(D, A, B) :- pcompare(D, A, B, =).
   82
   83% eq(Domain, TermA, TermB, Result).
   84%
   85% Result is true if TermA and TermB are equal for Domain.
   86% Result is false if TermA and TermB are equal for Domain.
   87%
   88eq(D, A, B, T) :- pcompare(D, A, B, C), eq_(C, T).
   89
   90eq_(=, true).
   91eq_(<, false).
   92eq_(>, false).
   93
   94
   95% lt(Domain, TermA, TermB). 
   96%
   97% TermA is less than TermB for Domain.
   98%
   99lt(D, A, B) :- pcompare(D, A, B, <).
  100
  101% lt(Domain, TermA, TermB, Result).
  102%
  103% Result is true if TermA is less than TermB for Domain.
  104% Result is false if TermA is not less than TermB for Domain.
  105%
  106lt(D, A, B, T) :- pcompare(D, A, B, C), lt_(C, T).
  107
  108lt_(=, false).
  109lt_(<, true).
  110lt_(>, false).
  111
  112
  113% lte(Domain, TermA, TermB). 
  114%
  115% TermA is less than or equal to TermB for Domain.
  116%
  117lte(D, A, B) :- pcompare(D, A, B, C), lte_(C, true).
  118
  119% lte(Domain, TermA, TermB, Result).
  120%
  121% Result is true if TermA is less than or equal to TermB for Domain.
  122% Result is false if TermA is not less than or equal to TermB for Domain.
  123%
  124lte(D, A, B, T) :- pcompare(D, A, B, C), lte_(C, T).
  125
  126lte_(=, true).
  127lte_(<, true).
  128lte_(>, false).
  129
  130
  131% gt(Domain, TermA, TermB). 
  132%
  133% TermA is greater than TermB for Domain
  134%
  135gt(D, A, B) :- pcompare(D, A, B, >).
  136
  137% gt(Domain, TermA, TermB, Result).
  138%
  139% Result is true if TermA is greater than TermB for Domain.
  140% Result is false if TermA is greater than TermB for Domain.
  141%
  142gt(D, A, B, T) :- pcompare(D, A, B, C), gt_(C, T).
  143
  144gt_(=, false).
  145gt_(<, false).
  146gt_(>, true).
  147
  148
  149% gte(Domain, TermA, TermB). 
  150% 
  151% TermA is greater than or equal to TermB for domain.
  152%
  153gte(D, A, B) :- pcompare(D, A, B, C), gte_(C, true).
  154
  155% gte(Domain, TermA, TermB, Result).
  156%
  157% Result is true if TermA is greater than or equal to TermB for Domain.
  158% Result is false if TermA is greater than or equal to TermB for Domain.
  159%
  160gte(D, A, B, T) :- pcompare(D, A, B, C), gte_(C, T).
  161
  162gte_(=, true).
  163gte_(<, false).
  164gte_(>, true)