6.3. Adding New Arithmetic Operators to Prolog

Projog provides a mechanism for "plugging in" or "injecting" implementations of ArithmeticOperator at runtime. This mechanism provides an easy way to configure and extend the arithmetic operations supported by Projog.

Example usage

If your project is configured using Maven then you can add a dependency on Projog by adding the following to your project's pom.xml:


Contents of com/example/

package org.projog.example;

import static org.projog.core.term.TermUtils.castToNumeric;

import org.projog.core.math.ArithmeticOperator;
import org.projog.core.math.Numeric;
import org.projog.core.term.IntegerNumber;
import org.projog.core.term.Term;

public class CalculatableExample implements ArithmeticOperator {
   public Numeric calculate(Term[] args) {
      Numeric input = castToNumeric(args[0]);
      long rounded = Math.round(input.getDouble());
      return new IntegerNumber(rounded);

Example of integrating com.example.ArithmeticOperatorExample into Projog:

?- pj_add_arithmetic_operator(triple/1, 'com.example.ArithmeticOperatorExample').


?- X is triple(3).
X = 9


?- X is round(147).
X = 441


?- X is round(-42.5).
X = 127.5


See Also