Did you know ... Search Documentation:
arithmetic.pl -- Extensible arithmetic
PublicShow source

This module provides a portable partial replacement of SWI-Prolog's user-defined arithmetic (evaluable) functions. It defines the compatibility directive arithmetic_function/1 and support for both runtime and compile-time evaluation of expressions that are a mixture between Prolog predicates used as functions and built-in evaluable terms.

Source arithmetic_function(:NameArity) is det
Declare a predicate as an arithmetic function. The function is visible in the module in which it is defined as well as modules that import the implementation predicate or inherit from this module. For example:
:- use_module(library(arithmetic)).
:- arithmetic_function(mid/2).
mid(A,B,C) :- C is (A+B)/2.

After which we may call ?- A is mid(3,5)., resulting in A = 4.

The implementation uses goal_expansion/2 to rewrite an arithmetic expression using user functions into a conjunction of arithmetic evaluation and predicate calls. This implies that the expression must be known at compile time. Runtime evaluation is supported using arithmetic_expression_value/2.

deprecated
- This function provides a partial work around for pure Prolog user-defined arithmetic functions that has been dropped in SWI-Prolog 5.11.23. Notably, it only deals with expression know at compile time.
Source eval_clause(+Term, -Clause) is det[private]
Clause is a clause for evaluating the arithmetic expression Term.
Source arithmetic_expression_value(:Expression, -Result) is det
True when Result unifies with the arithmetic result of evaluating Expression.
Source evaluable(F) is semidet[private]
True if F and all its subterms are evaluable terms or variables.
Source tidy(+GoalIn, -GoalOut)[private]
Cleanup the output from expand_function/3.