Arithmetic Function Custom Prolog

I am looking for something like built-in arithmetic operators that have a return value in Prolog (in particular in SWI-Prolog). For instance. if you run A is (1+2) + (3+2)., it will return A = 8..

How can I define an operator functo do something like an operator +?
For instance. A is (2 func 3) func (4 func (2+1))..

+5
source share
2 answers

To place your funcinline function as an operator +(along with many others), you will need to determine the priority order for funcand its arguments. You can achieve this in SWI-PROLOG with op/3.

, ( , func/2):

:- op(500,yfx,func).

func/2, - (.. PROLOG, , func ), func/2 , arithmetic_function/1 :

:- arithmetic_function(func/2).

func/2:

func(X, Y, Z) :- 
    Z is X + Y.

:

?- A is (2 func 3) func (4 func (2+1)).
A = 12.
+6

, arithmetic_function/1 -, is, .

http://www.swi-prolog.org/pldoc/doc_forobject=section(2,'4.26',swi('/doc/Manual/extendarith.html'))
+2

All Articles