Search code examples
prolog

How to unify a term's operator in Prolog?


I can unify operands in SWI-Prolog, for example:

?- +(X,Y) = 1 + 2.
X = 1,
Y = 2.

but how to unify an operator?

?- Op(X,Y) = 1 + 2.
ERROR: Syntax error: Operator expected

I hope Prolog to return

X = 1,
Y = 2,
Op = +.

Thanks.


Solution

  • You unify terms. Both +(1,2) and 1 + 2 are the same term. You may use the univ predicate (=..) to decompose a term like this:

    ?- 1+2 =.. [Op, X, Y].
    Op =  (+),
    X = 1,
    Y = 2.