While stepping through the debugger to examine a failure in a lambda, it occurred to me that they invoke a decent amount of machinery, and also that much of this might be compiled away. Isn’t it the case that:
f():-
maplist([X,Y]>>g(X,Y), [1,2,3], [4,5,6], R).
can could be automatically transformed to:
f():-
maplist(f_lambda_0, [1,2,3], [4,5,6]).
f_lambda_0(X,Y):- g(X,Y).
Apart from being faster, I think this is considerably easier to debug – avoiding the need of stepping through the lambda library onion, making parameter count mismatches clear, etc…
For closures:
f(C):-
maplist({C}\[X,Y]>>g(X,Y), [1,2,3], [4,5,6]).
would need to be transformed to something like:
f(C):-
f_lambda_c_0(C, F), % Obtain a FC(X,Y) predicate internally bound to C. Something, something, <insert magic here>
maplist(F, [1,2,3], [4,5,6]).
Is it possible to precompile lambdas in this way, automatically adding the implementation predicate? It would benefit both efficiency and debug-clarity?