Prolog:递归函数重定义

发布于 2024-08-15 01:51:32 字数 682 浏览 4 评论 0原文

有没有办法“递归地重新定义”(不知道技术术语)序言谓词?

考虑这些谓词:

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 技术交流群。

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

发布评论

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

评论(2

心如狂蝶 2024-08-22 01:51:32

看一下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.

妞丶爷亲个 2024-08-22 01:51:32

我不太明白你的问题,但这也许会有所帮助:

t :-
    assert(my_add(A,B,C):-C is A+B),
    my_add(1,2,R),
    writeln(R).

测试:

?- t.
3
true.

I haven't quite get your question, but maybe this could be helpful:

t :-
    assert(my_add(A,B,C):-C is A+B),
    my_add(1,2,R),
    writeln(R).

test:

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