View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        jan@swi-prolog.org
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2023, SWI-Prolog Solutions b.v.
    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(prolog_evaluable,
   36          [ evaluable_property/2
   37          ]).   38:- autoload(library(prolog_code), [most_general_goal/2]).   39:- autoload(library(error), [must_be/2]).   40
   41/** <module> Inspect properties of evaluable functions
   42*/
   43
   44%!  evaluable_property(?Function, ?Property) is nondet.
   45%
   46%   True when Property is a property of the evaluable function Function.
   47%   _Evaluable functions_ are expressions processed   by  the arithmetic
   48%   predicates is/2, </2, =</2, =:=/2, >/2  and >=/2. Defined properties
   49%   are:
   50%
   51%     - built_in
   52%       Function is built-in. The proposal defines additional properties
   53%       for systems that allow defining new functions at runtime.
   54%       SWI-Prolog provides library(arithmetic) for this, which rewrites
   55%       the source as interleaved predicate calls and built-in function
   56%       evaluation.
   57%     - iso
   58%       Function is part of the ISO standard. This does, for SWI-Prolog,
   59%       __not__ imply that the implementation satisfies the ISO
   60%       standard.  It implies that it implements the same mathematical
   61%       operation as specified by the standard.  The returned type may
   62%       be more precise and may, depending on Prolog flags, return
   63%       non-normal floats where the ISO standard demands an exception.
   64%     - template(Function, Return)
   65%       Type support.  Types used are `integer`, `rational` and `float`.
   66%       Functions that copy the type to the output use a variable to
   67%       indicate this, e.g., template(-Type, Type).  A function may
   68%       have multiple type templates.
   69%
   70%   Future versions may provide this predicate as a built-in.
   71%
   72%   @compat discussed by several implementors, initiated by Paulo Moura.
   73%   @arg Function is a variable or _callable_ term.  If it is a callable
   74%   term, only the name and arity are considered to denote the
   75%   evaluable.
   76
   77evaluable_property(Templ, built_in), nonvar(Templ), \+ callable(Templ) =>
   78    must_be(callable, Templ).
   79evaluable_property(Templ, built_in) =>
   80    current_arithmetic_function(Templ).
   81evaluable_property(Templ, iso) =>
   82    iso_function(Templ).
   83evaluable_property(Templ, template(Term, Ret)) =>
   84    most_general_goal(Templ, Term),
   85    eval_type(Term, Ret).
   86evaluable_property(Templ, Prop), var(Prop) =>
   87    eval_prop(Prop),
   88    evaluable_property(Templ, Prop).
   89evaluable_property(_, _) =>             % fail on unknown properties.
   90    fail.                               % Proposal demands a domain_error.
   91
   92eval_prop(built_in).
   93eval_prop(template(_,_)).
   94eval_prop(iso).
   95
   96eval_type(float*_,float).
   97eval_type(_*float,float).
   98eval_type(rational*rational,rational).
   99eval_type(float**_,float).
  100eval_type(_**float,float).
  101eval_type(rational**rational,rational).
  102eval_type(rational**rational,float).
  103eval_type(+Type,Type).
  104eval_type(float+_,float).
  105eval_type(_+float,float).
  106eval_type(rational+rational,rational).
  107eval_type(-Type,Type).
  108eval_type(float-_,float).
  109eval_type(_-float,float).
  110eval_type(rational-rational,rational).
  111eval_type(float/_,float).
  112eval_type(_/float,float).
  113eval_type(rational/rational,rational).
  114eval_type(rational/rational,float).
  115eval_type(_//_,integer).
  116eval_type(integer/\integer,integer).
  117eval_type(integer<<integer,integer).
  118eval_type(integer>>integer,integer).
  119eval_type(\integer,integer).
  120eval_type(integer\/integer,integer).
  121eval_type(float^_,float).
  122eval_type(_^float,float).
  123eval_type(rational^rational,rational).
  124eval_type(rational^rational,float).
  125eval_type(abs(Type),Type).
  126eval_type(acos(_),float).
  127eval_type(acosh(_),float).
  128eval_type(asin(_),float).
  129eval_type(asinh(_),float_).
  130eval_type(atan(_),float).
  131eval_type(atan(_,_),float).
  132eval_type(atan2(_,_),float).
  133eval_type(atanh(_),float).
  134eval_type(ceil(_),integer).
  135eval_type(ceiling(_),integer).
  136eval_type(cmpr(_,_),integer).
  137eval_type(copysign(Type,_),Type).
  138eval_type(cos(_),float).
  139eval_type(cosh(_),float).
  140eval_type(cputime,float).
  141eval_type(denominator(rational),integer).
  142eval_type(_ div _,integer).
  143eval_type(e,float).
  144eval_type(epsilon,float).
  145eval_type(erf(_),float).
  146eval_type(erfc(_),float).
  147eval_type(eval(Type),Type).
  148eval_type(exp(_),float).
  149eval_type(float(_),float).
  150eval_type(float_fractional_part(Type),Type).
  151eval_type(float_integer_part(Type),Type).
  152eval_type(floor(_),integer).
  153eval_type(gcd(integer,integer),integer).
  154eval_type(getbit(integer,integer),integer).
  155eval_type(inf,float).
  156eval_type(integer(_),integer).
  157eval_type(lcm(integer,integer),integer).
  158eval_type(lgamma(_),float).
  159eval_type(log(_),float).
  160eval_type(log10(_),float).
  161eval_type(lsb(integer),integer).
  162eval_type(max(Type,_),Type).
  163eval_type(max(_,Type),Type).
  164eval_type(maxr(Type,_),Type).
  165eval_type(maxr(_,Type),Type).
  166eval_type(min(Type,_),Type).
  167eval_type(min(_,Type),Type).
  168eval_type(minr(Type,_),Type).
  169eval_type(minr(_,Type),Type).
  170eval_type(integer mod integer,integer).
  171eval_type(msb(integer),integer).
  172eval_type(nan,float).
  173eval_type(nexttoward(_,_),float).
  174eval_type(numerator(rational),integer).
  175eval_type(pi,float).
  176eval_type(popcount(integer),integer).
  177eval_type(powm(integer,integer,integer),integer).
  178eval_type(random(integer),integer).
  179eval_type(random_float,float).
  180eval_type(rational(_),rational).
  181eval_type(rationalize(_),rational).
  182eval_type(rational rdiv rational,rational).
  183eval_type(integer rem integer,integer).
  184eval_type(round(_),integer).
  185eval_type(roundtoward(_,_),float).
  186eval_type(sign(_),integer).
  187eval_type(sin(_),float).
  188eval_type(sinh(_),float).
  189eval_type(sqrt(_),float).
  190eval_type(tan(_),float).
  191eval_type(tanh(_),float).
  192eval_type(truncate(_),integer).
  193eval_type(integer xor integer,integer_).
  194
  195% ISO core
  196iso_function(_+_).
  197iso_function(_-_).
  198iso_function(_*_).
  199iso_function(_//_).
  200iso_function(_/_).
  201iso_function(rem(_,_)).
  202iso_function(mod(_,_)).
  203iso_function(-_).
  204iso_function(abs(_)).
  205iso_function(sign(_)).
  206iso_function(float_integer_part(_)).
  207iso_function(float_fractional_part(_)).
  208iso_function(float(_)).
  209iso_function(floor(_)).
  210iso_function(truncate(_)).
  211iso_function(round(_)).
  212iso_function(ceiling(_)).
  213iso_function(_**_).
  214iso_function(sin(_)).
  215iso_function(cos(_)).
  216iso_function(atan(_)).
  217iso_function(exp(_)).
  218iso_function(log(_)).
  219iso_function(sqrt(_)).
  220iso_function(_>>_).
  221iso_function(_<<_).
  222iso_function(_/\_).
  223iso_function(_\/_).
  224iso_function(\_).
  225% Correndum 2
  226iso_function(max(_,_)).
  227iso_function(min(_,_)).
  228iso_function(_^_).
  229iso_function(asin(_)).
  230iso_function(acos(_)).
  231iso_function(atan(_,_)).
  232iso_function(tan(_)).
  233iso_function(pi).
  234iso_function(xor(_,_))