如何在 Prolog 中双统一术语运算符?

发布于 01-19 18:13 字数 511 浏览 2 评论 0原文

我刚刚在如何统一Prolog中的术语运算符?<中提出了问题/a> 并得到了很好的答案。

但是,它并没有完全解决我的问题,即 =.. 仅适用于单面。

例如,

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

我希望查询返回

Op = (+),
X = 1,
Y = 2;
Op = (*),
X = 1,
Y = 2.

非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦2025-01-26 18:13:11
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.

由于完全历史的原因,这也可以称为Boomy Univ。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文