prologmeta-predicate

Can I prepend the argument list in call/2?


call(Goal,Arg) allows to append the argument Arg to the arguments of Goal and call the resulting goal, e.g.

call(succ(1), R).

is the same as

succ(1, R).

However, I don't want to append to the argument list, but instead prepend, e.g.

callpre(succ(1), R).

should result in

succ(R, 1).

How can I prepend arguments to the list of Goal's arguments and call the resulting goal?


Solution

  • For an arbitrary number of arguments, you could define it as

    callpre(MGoal, Arg) :-
        strip_module(MGoal, M, Goal), 
        Goal =.. [F | Args],
        NewGoal =.. [F, Arg|Args], 
        M:NewGoal.
    

    You'll also need a meta_predicate/1 declaration for this:

    :- meta_predicate callpre(1, *).