Prolog: Recursive function redefinition
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!
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:
t :-
assert(my_add(A,B,C):-C is A+B),
my_add(1,2,R),
writeln(R).
test:
?- t.
3
true.
精彩评论