Возможно, самое простое решение - расширить до do/2
, аналогично тому, что library(lists)
в SICStus использует для реализации maplist/n
.
/* -*- Mode:Prolog; coding:iso-8859-1; indent-tabs-mode:nil; prolog-indent-width:8; prolog-paren-indent:4; tab-width:8; -*- */
/*
Replacement for maplist from library lists, that inlines the calls when possible.
In your code, instead of doing:
:- use_module(library(lists),[maplist/2, maplist/3, ... other non-maplist things ...]).
Do:
:- use_module(library(lists),[... other non-maplist things ...]).
:- use_module(maplist_inliner, [maplist/2,maplist/3]).
*/
:- module(maplist_inliner, [maplist/2,maplist/3]).
% We can not import (and reexport) maplist/2 etc from the module 'lists' (because
% our goal_expansion will only be used from our own predicates, not predicates we reexport).
% Instead we use thin wrappers for those cases where we are unable to inline the calls to maplist.
% However, these will never be used, because we always fallback to expanding to a plain lists:maplist/n call.
:- use_module(library(lists), []).
:- meta_predicate maplist(1, +).
:- meta_predicate maplist(2, +, +).
% TODO: Add more arities
% A thin wrapper around lists:maplist/2. See module documentation for rationale.
maplist(G_1, L1) :-
lists:maplist(G_1, L1).
% A thin wrapper around lists:maplist/3. See module documentation for rationale.
maplist(G_2, L1, L2) :-
lists:maplist(G_2, L1, L2).
get_module(X, ModuleContext, G, M) :-
var(X),
!,
G = X,
M = ModuleContext.
get_module(M1:X, _ModuleContext, G, M) :-
!,
get_module(X, M1, G, M).
get_module(X, ModuleContext, G, M) :-
!,
G = X,
M = ModuleContext.
:- if(fail).
goal_expansion(G, Layout0, ModuleContext, Expansion, Layout1) :-
writeq(goal_expansion(G,Layout0,ModuleContext,Expansion,Layout1)),
nl,
fail.
:- endif.
goal_expansion(maplist(G, L1), _Layout0, ModuleContext, Expansion, Layout1) :-
callable(G),
get_module(G, ModuleContext, G_1, M),
callable(G_1),
atom(M),
!,
Layout1 = [], % No source info
inline_maplist_2(G_1, M, L1, Expansion).
goal_expansion(maplist(G, L1), _Layout0, ModuleContext, Expansion, Layout1) :-
!,
Layout1 = [], % No source info
Expansion = lists:maplist(ModuleContext:G,L1).
goal_expansion(maplist(G, L1, L2), _Layout0, ModuleContext, Expansion, Layout1) :-
callable(G),
get_module(G, ModuleContext, G_2, M),
callable(G_2),
atom(M),
!,
Layout1 = [], % No source info
inline_maplist_3(G_2, M, L1, L2, Expansion).
goal_expansion(maplist(G, L1, L2), _Layout0, ModuleContext, Expansion, Layout1) :-
!,
Layout1 = [], % No source info
Expansion = lists:maplist(ModuleContext:G,L1,L2).
inline_maplist_2(G_1, M, L1, Expansion) :-
G_1 =.. [F|ClosureArgs],
append([F|ClosureArgs], [X], G_ClosureArgs_X),
BodyGoal =.. G_ClosureArgs_X,
Expansion =
(foreach(X,L1),
param(G_1)
do
M:BodyGoal
).
inline_maplist_3(G_2, M, L1, L2, Expansion) :-
G_2 =.. [F|ClosureArgs],
append([F|ClosureArgs], [X1,X2], G_ClosureArgs_X1_X2),
BodyGoal =.. G_ClosureArgs_X1_X2,
Expansion =
(foreach(X1,L1),
foreach(X2,L2),
param(G_2)
do
M:BodyGoal
).
Пример
/* -*- Mode:Prolog; coding:iso-8859-1; indent-tabs-mode:nil; prolog-indent-width:8; prolog-paren-indent:4; tab-width:8; -*- */
:- use_module(maplist_inline, [maplist/2,maplist/3]).
p_1(X1) :-
writeq(call(p_1(X1))).
p_2(X1, X2) :-
writeq(call(p_2(X1,X2))).
p_3(X1, X2, X3) :-
writeq(call(p_3(X1,X2,X3))).
test(L1) :-
ClosureArg1 = 'a',
maplist(p_2(ClosureArg1), L1).
test(L1, L2) :-
maplist(p_2, L1, L2).
test_1(L1, Arg) :-
maplist(p_2(Arg), L1).
test_1(L1, L2, Arg) :-
maplist(p_3(Arg), L1, L2).
test_noinline(L1, L2, Arg) :-
G_2 = p_3(Arg), % Inliner will not see this
maplist(G_2, L1, L2).
Использованиеconsult/1
и listing/1
показывают, что происходит:
bash$ /usr/local/sicstus4.5.0/bin/sicstus
SICStus 4.5.0 (x86_64-darwin-17.7.0): Thu Jan 17 17:17:35 CET 2019
Licensed to SICS
| ?- consult(test).
% ...
| ?- listing.
maplist_inliner:maplist(A, B) :-
lists:maplist(A, B).
maplist_inliner:maplist(A, B, C) :-
lists:maplist(A, B, C).
p_1(A) :-
writeq(call(p_1(A))).
p_2(A, B) :-
writeq(call(p_2(A,B))).
p_3(A, B, C) :-
writeq(call(p_3(A,B,C))).
test(A) :-
B=a,
( foreach(C, A),
fromto(B, D, D, _)
do p_2(D, C)
).
test(A, B) :-
( foreach(C, A),
foreach(D, B)
do p_2(C, D)
).
test_1(A, B) :-
( foreach(C, A),
fromto(B, D, D, _)
do p_2(D, C)
).
test_1(A, B, C) :-
( foreach(D, A),
foreach(E, B),
fromto(C, F, F, _)
do p_3(F, D, E)
).
test_noinline(A, B, C) :-
D=p_3(C),
lists:maplist(user:user:D, A, B).
Осторожно.Я не проверял это более нескольких минут.