The SWI meta-predicate declarations and modules are similar to those
in Quintus, SICStus, and YAP. The fundamental assumption in those
systems is that all information is passed through the declared
meta-argument using (:)/2
. There is no hidden state
or context. For the common cases (simple instantiated arguments), the
meta-predicate declarations are sufficient to relieve the burden of
explicit qualification from the programmer.
However, in more complex situations as the present one, you have to
ensure that explicit qualification will be added. Further, you need
to ensure to "dereference" the (:)/2
prefixes accordingly. In SWI,
there is strip_module/3
:
?- strip_module(a:b:c:X,M,G).
X = G,
M = c.
Assume the definition:
rec(_, -1, local).
rec(_, 0, =).
rec(F, 1, F).
local(S0,S) :-
S is S0+1.
Which now has to be written like so:
:- meta_predicate goal_qualified(:,-).
goal_qualified(G,G).
:- meta_predicate rec(2,+,2).
rec(_, -1, G) :-
strip_module(G,_,VG),
goal_qualified(local,VG).
rec(_, 0, G) :-
strip_module(G,_,VG),
goal_qualified(=,VG).
rec(F, 1, G) :-
strip_module(G,_,F).
Many prefer to add module prefixes manually:
:- meta_predicate rec(2,+,2).
rec(_, -1, G) :-
strip_module(G,_,mymodule:local).
...
And if we restrict ourselves to SWI only, thereby sacrificing
compatibility to SICStus or YAP:
:- meta_predicate rec(2,+,2).
rec(_, -1, _:mymodule:local).
rec(_, 0, _:(=)).
rec(F, 1, _:F).
The rule in your question
rec(F,N,\A^B^(call(F,A,H),call(G,H,B))) :-
N>1, M is N-1, rec(F,M,G).
is thus translated as:
rec(F, N, MG) :-
N > 1, M is N - 1,
strip_module(MG,_,VG),
goal_qualified(\A^B^(call(F,A,H),call(G,H,B)),VG),
rec(F, M, G).
Assuming that library(lambda)
is imported everywhere this can again be simplified in SWI to:
rec(F, N, _:(\A^B^(call(F,A,H),call(G,H,B)) )) :-
N > 1, M is N -1,
rec(F, M, G).
My conclusion
1mo: Systems should produce a warning for always failing clauses, like in:
| ?- [user].
% compiling user...
| :- meta_predicate p(0).
| p(1).
% compiled user in module user, 0 msec 2080 bytes
yes
| ?- p(X).
no
2do: Maybe it would be best to use the following auxiliary predicate:
:- meta_predicate cont_to(:,:).
cont_to(MGoal, MVar) :-
strip_module(MVar, _, Var),
( nonvar(Var)
-> throw(error(uninstantiation_error(Var),_))
; true
),
( strip_module(MGoal,_,Goal),
var(Goal)
-> throw(error(instantiation_error,_))
; true
),
Var = MGoal.
Usage.
rec(_, -1, MV) :-
cont_to(local, MV).
Or rather: one version for each number of auxiliary arguments, thus
:- meta_predicate cont0_to(0,0).
:- meta_predicate cont1_to(1,1).
:- meta_predicate cont2_to(2,2).
...
The name could be better, an operator would not do, though.