1

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!

4

2 に答える 2

3

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.

于 2009-12-21T08:20:49.833 に答える
0

私はあなたの質問を完全には理解していませんが、多分これは役に立つかもしれません:

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

テスト:

?- t.
3
true.
于 2009-12-21T02:29:13.200 に答える