I’m looking for something like built-in arithmetic operators that has a return value in Prolog (specifically in SWI-Prolog). E.g. if you run A is (1+2) + (3+2)., it returns A = 8..
How can I define func operator to do something like + operator?
E.g. A is (2 func 3) func (4 func (2+1))..
In order to situate your function
funcinline just as the+operator (along with many others), you’ll need to define a precedence order forfuncand it’s arguments. You can achieve this in SWI-PROLOG withop/3.For example, the directive (preceding code where
func/2is used):To implement
func/2, you can either write a meta-interpreter for your language (i.e., you write a PROLOG program which parses term expressions includingfuncand interprets them as you wish), or iffunc/2is strictly arithmetic, you can use arithmetic_function/1 also as a directive, as follows:Testing this with the following definition for
func/2:Gives, with your example: