Prolog:递归函数重定义
有没有办法“递归地重新定义”(不知道技术术语)序言谓词?
考虑这些谓词:
f(X,Y,A):-A is Y xor X.
arity(f,2).
现在我想使用以下定义自动创建 2 个新谓词 f1/2 和 f2/1:
f1(Y,A):-f(1,Y,A).
f2(A):-f1(1,A).
因此谓词应该获取一个(二进制)函数作为输入,并通过填充函数的参数来创建新谓词(# 通过arity)从左到右为 1。
这可能吗?我尝试了 univ 运算符和 call() 的各种组合,但没有成功。
有谁知道该怎么做?任何帮助将不胜感激。
编辑:更高数量的示例:
f(W,X,Y,Z,A):-A is Y xor X xor W xor Z.
arity(f,4).
-->
f1(X,Y,Z,A):-f(1,X,Y,Z,A).
f2(Y,Z,A):-f1(1,Y,Z,A).
f3(Z,A):-f2(1,Z,A).
f4(A):-f3(1,A).
由于我只对所有参数设置为 1 的 f (A) 的返回值感兴趣,因此可能有一种更简单的方法来做到这一点...... 不管怎样,谢谢你的帮助!
Is there a way to "recursively redefine" (don't know the technical term) prolog predicates?
Consider these predicates:
f(X,Y,A):-A is Y xor X.
arity(f,2).
now i want to automatically create 2 new predicates f1/2 and f2/1 with the following definition:
f1(Y,A):-f(1,Y,A).
f2(A):-f1(1,A).
So the predicate should get a (binary) function as input and creates new predicates by filling the function's parameters (# defined through arity) from left to right with 1.
Is this possible? I tried various combinations of the univ operator and call() but nothing succeded.
Does anyone know how to do this? Any help would really be appreciated.
Edit: An example for a higher arity:
f(W,X,Y,Z,A):-A is Y xor X xor W xor Z.
arity(f,4).
-->
f1(X,Y,Z,A):-f(1,X,Y,Z,A).
f2(Y,Z,A):-f1(1,Y,Z,A).
f3(Z,A):-f2(1,Z,A).
f4(A):-f3(1,A).
Since I'm only interrested in the return value of f (A) with all parameters set to 1 there might be an easier way to do this...
Anyway, thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下term_expansion/2,它可以修改当编译器读取程序时,可以任意编写程序。
尽管要小心,但这是一个强大的功能,您很容易造成混乱。
Take a look at term_expansion/2, it can modify the program arbitrarily when it is read by the compiler.
Though be careful, this is a powerful feature and you can easily make a big confusing mess.
我不太明白你的问题,但这也许会有所帮助:
测试:
I haven't quite get your question, but maybe this could be helpful:
test: