Q: How can we get rid of maplist
overhead—like SWI's apply_macros
—in SICStus Prolog?
A: Goal expansion.
First, we define the auxiliary predicates we'll need. In the following we use SICStus Prolog 4.5.0.
:- module(maplist_macros, [maplist/2, maplist/3, maplist/4, maplist/5, maplist/6, maplist/7, maplist/8, maplist/9]). :- meta_predicate maplist(1,?), maplist(2,?,?), maplist(3,?,?,?), maplist(4,?,?,?,?), maplist(5,?,?,?,?,?), maplist(6,?,?,?,?,?,?), maplist(7,?,?,?,?,?,?,?), maplist(8,?,?,?,?,?,?,?,?). :- use_module(library(lists), [append/2, same_length/2]). :- use_module(library(ordsets)). callable_arglists_appended(C0, New, C1) :- C0 =.. Parts0, append([Parts0|New], Parts1), C1 =.. Parts1. :- dynamic expand_goal_aux/1. my_expand_goal(Goal, Expanded) :- asserta((expand_goal_aux(Goal) :- Goal)), retract((expand_goal_aux(Goal) :- Expanded0)), strip_module(Expanded0, _, Expanded). strip_module(MGoal_0, Module, Goal_0) :- aux_strip_module(MGoal_0, Goal_0, lambda, Module). % lambda?! aux_strip_module(MG_0,G_0, M0,M) :- ( nonvar(MG_0), MG_0 = (M1:MG1_0) -> aux_strip_module(MG1_0,G_0, M1,M) ; MG_0 = G_0, M0 = M ). :- dynamic maplist_aux_count/1. maplist_aux_count(0).
Now on to the goal expansion:
% generate specialized `maplist/N' goal_expansion(Goal0, _Layout0, FromModule, FromModule:Goal, []) :- Goal0 =.. [maplist, Rel0 | Args], callable(Rel0), Args = [_|_], !, % get count # of aux preds generated so far and increment it retract(maplist_aux_count(C)), C1 is C+1, asserta(maplist_aux_count(C1)), % build predicate functor `AuxPred' number_chars(C, C_chars), atom_chars(C_atom, C_chars), atom_concat(maplist_aux_, C_atom, AuxPred), % e.g., maplist_aux_3 % enforce all relevant lists have the same length lists:maplist(same_length(Args), [Vars_E,Nils]), lists:maplist(lists:cons, Vars_E, Vars_Es, Vars_E_Es), lists:maplist(=([]), Nils), % expand the goal in the right module (`FromModule') strip_module(Rel0, _, Rel1), callable_arglists_appended(Rel1, [Vars_E], Rel2), my_expand_goal(FromModule:Rel2, Rel), % find out which variables need to be threaded through term_variables(Rel, Vars_Schema), list_to_ord_set(Vars_Schema, VSet_Schema), list_to_ord_set(Vars_E, VSet_E), ord_subtract(VSet_Schema, VSet_E, VSet_Actual), % build call of new predicate with proper arguments callable_arglists_appended(AuxPred, [Args,VSet_Actual], Goal), % callee clauses (new predicate) callable_arglists_appended(AuxPred, [Nils, VSet_Actual], Head0), % fact callable_arglists_appended(AuxPred, [Vars_E_Es,VSet_Actual], Head1), % rule callable_arglists_appended(AuxPred, [Vars_Es, VSet_Actual], Rec1), % % dump generated clauses to a file atom_concat('/tmp/x', C_atom, FileName), % TODO: get actual tmpfilnam open(FileName, write, S), portray_clause(S, (Head0 :- true)), portray_clause(S, (Head1 :- Rel, Rec1)), close(S), % compile temporary file in proper module compile(FromModule:FileName).
So far so good;) So here's the problem...
How can I ensure that the goal-expanded variant is exactly like the predicates it replaces?
(I have a hunch that it is not, but I can't quite put my finger on it...)
Simple sample use case #1
allequal(Xs) :- maplist(=(_), Xs).
becomes
allequal(A) :- maplist_aux_0(A, _).
maplist_aux_0([], _).
maplist_aux_0([A|B], C) :- C=A, maplist_aux_0(B, C).
callable_arglists_appended(C0, New, C1) :- C0 =.. Parts0, append([Parts0|New], Parts1), i'd expect
append(Parts0, New, Parts).` C1 =.. Parts1.` expectedappend(Parts0, New, Parts)
. – Anton Danilovappend/2
instead of one or moreappend/3
goals. If you look at its use sites, you can see thatcallable_arglists_appended/3
usually appends two lists of arguments (e.g.,Nils = [[]], VSet_Schema = [_]
). – repeatappend/2
is a bit strange too... – repeatexpand_goal/2
andcompile_aux_clauses/1
. There are subtle differences in the module system. Plus, this question does not include support for lambdas. – repeat