'How to bi-unify a term's operator in Prolog?
I just asked the question in How to unify a term's operator in Prolog? and got good answers.
However, it doesn't completely solve my problem, i.e. =.. only works for single-side.
For example,
test(Expr) :-
1 + 2 = Expr.
test(Expr) :-
1 * 2 = Expr.
?- E =.. [Op, X, Y], test(E). % E can not be a variable!
ERROR: Arguments are not sufficiently instantiated
I hope the query returns
Op = (+),
X = 1,
Y = 2;
Op = (*),
X = 1,
Y = 2.
Very thanks.
Solution 1:[1]
expr_op_x_y(Expr, Op, X, Y) :-
when((nonvar(Expr) ; nonvar(Op)), Expr =.. [Op, X, Y]).
?- expr_op_x_y(Expr, Op, X, Y).
when((nonvar(Expr);nonvar(Op)),Expr=..[Op,X,Y]).
?- expr_op_x_y(Expr, Op, X, Y), Op = (+).
Expr = X+Y, Op = (+).
?- expr_op_x_y(Expr, Op, X, Y), Expr = 1+2.
Expr = 1+2, Op = (+), X = 1, Y = 2.
This could also be called a boomy univ for entirely historical reasons.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | false |
