1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2017, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(near_utils,
   36          [fact_near/1,
   37           fact_near/2,
   38           retract_near/1,
   39           retractall_near/1,
   40           real_near/2,
   41           real_compare/3,
   42           near_compare/3,
   43           unify_near/2,
   44           equiv_near/2,
   45           repsilon/1,
   46           repsilon/2]).   47
   48:- use_module(library(apply)).   49:- use_module(library(call_ref)).   50:- use_module(library(mapargs)).   51:- use_module(library(compare_eq)).   52
   53:- meta_predicate
   54        fact_near(0 ),
   55        fact_near(0, -),
   56        retract_near(0 ),
   57        retractall_near(0 ).   58
   59%   Note: For some reason we have to module qualify Call to avoid green choicepoints here
   60
   61fact_near(M:Call) :-
   62    freeze_near(Call, Mask),
   63    M:Mask,
   64    frozen_near(Mask).
   65
   66fact_near(Call, Ref) :-
   67    freeze_near(Call, Mask),
   68    call_ref(Mask, Ref),
   69    frozen_near(Mask).
   70
   71retract_near(Call) :-
   72    fact_near(Call, Ref),
   73    erase(Ref).
   74
   75retractall_near(Call) :-
   76    forall(( freeze_near(Call, Mask),
   77             call_ref(Mask, Ref)
   78           ),
   79           erase(Ref)).
   80
   81real(R) :-
   82    ( R == 1.5NaN
   83    ->fail
   84    ; float(R)
   85    ->true
   86    ; rational(R),
   87      \+ integer(R)
   88    ).
   89
   90rnum(R) :-
   91    ( R == 1.5NaN
   92    ->fail
   93    ; float(R)
   94    ->true
   95    ; rational(R)
   96    ).
   97
   98attr_unify_hook(near(Arg1), Arg) :-
   99    rnum(Arg),
  100    real_near(Arg1, Arg).
  101
  102put_near(Arg1, Arg) :-
  103    ( nonvar(Arg1)
  104    ->put_attr(Arg, near_utils, near(Arg1))
  105    ; Arg = Arg1
  106    ).
  107
  108freeze_near(Arg1, Arg) :-
  109    ( real(Arg1)
  110    ->put_near(Arg1, Arg)
  111    % ; % If at least one element of a list is real, and all the instantiated
  112    %   % elements are rational or floats, then freeze the near unification
  113    %   is_list(Arg1),
  114    %   once(( member(R, Arg1),
  115    %          real(R)
  116    %        )),
  117    %   forall(( member(A, Arg1),
  118    %            nonvar(A)
  119    %          ), rnum(A))
  120    % ->maplist(put_near, Arg1, Arg)
  121    ; var(Arg1)
  122    ->Arg = Arg1
  123    ; mapargs(freeze_near, Arg1, Arg)
  124    ).
  125
  126frozen_near(Mask) :-
  127    term_attvars(Mask, Vars),
  128    maplist(frozen_near_1, Vars).
  129
  130frozen_near_1(Var) :-
  131    ( get_attr(Var, near_utils, near(Val))
  132    ->del_attr(Var, near_utils),
  133      Var = Val
  134    ; true
  135    ).
  136
  137real_near(A, B) :- near_compare(=, A, B).
  138
  139real_compare(A, C, B) :- near_compare(C, A, B).
  140
  141repsilon(E) :- E is 1024*epsilon.
  142
  143repsilon(N, E) :-
  144    repsilon(R),
  145    E is R*N.
  146
  147near_compare(Comparator, A, B) :-
  148    ( A =:= B
  149    ->compare_eq(Comparator)
  150    ; repsilon(max(abs(A), abs(B)), E),
  151      compare(Comparator, A, B, E)
  152    ).
  153
  154compare(=,  A, B, E) :- abs(A - B) =< E.
  155compare(=<, A, B, E) :- A - B =< E.
  156compare(>=, A, B, E) :- B - A =< E.
  157compare(<,  A, B, E) :- B - A >  E.
  158compare(>,  A, B, E) :- A - B >  E.
  159compare(\=, A, B, E) :- abs(A - B) > E.
  160
  161% =/2
  162unify_near(Arg1, Arg2) :-
  163    ( ( var(Arg1)
  164      ; var(Arg2)
  165      ; integer(Arg1),
  166        integer(Arg2)
  167      )
  168    ->Arg1 = Arg2
  169    ; rnum(Arg1),
  170      rnum(Arg2)
  171    ->real_near(Arg1, Arg2)
  172    ; mapargs(unify_near, Arg1, Arg2)
  173    ).
  174
  175% ==/2
  176equiv_near(Arg1, Arg2) :-
  177    ( ( var(Arg1)
  178      ; var(Arg2)
  179      ; integer(Arg1),
  180        integer(Arg2)
  181      )
  182    ->Arg1 == Arg2
  183    ; rnum(Arg1),
  184      rnum(Arg2)
  185    ->real_near(Arg1, Arg2)
  186    ; mapargs(equiv_near, Arg1, Arg2)
  187    )