File: apply_macros.pl

package info (click to toggle)
swi-prolog 8.0.2+dfsg-3+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (391 lines) | stat: -rw-r--r-- 12,170 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2007-2016, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(apply_macros,
          [ expand_phrase/2,            % :PhraseGoal, -Goal
            expand_phrase/4             % :PhraseGoal, +Pos0, -Goal, -Pos
          ]).
:- use_module(library(lists)).

/** <module> Goal expansion rules to avoid meta-calling

This module defines goal_expansion/2 rules to   deal with commonly used,
but fundamentally slow meta-predicates. Notable   maplist/2... defines a
useful set of predicates, but its  execution is considerable slower than
a traditional Prolog loop. Using this  library calls to maplist/2... are
translated into an call  to  a   generated  auxilary  predicate  that is
compiled using compile_aux_clauses/1. Currently this module supports:

        * maplist/2..
        * forall/2
        * once/1
        * ignore/1
        * phrase/2
        * phrase/3
        * call_dcg/2
        * call_dcg/3

The idea for this library originates from ECLiPSe and came to SWI-Prolog
through YAP.

@tbd    Support more predicates
@author Jan Wielemaker
*/

:- dynamic
    user:goal_expansion/2.
:- multifile
    user:goal_expansion/2.


%!  expand_maplist(+Callable, +Lists, -Goal) is det.
%
%   Macro expansion for maplist/2 and higher arity.

expand_maplist(Callable0, Lists, Goal) :-
    length(Lists, N),
    expand_closure_no_fail(Callable0, N, Callable1),
    (   Callable1 = _:_
    ->  strip_module(Callable1, M, Callable),
        NextGoal = M:NextCall,
        QPred = M:Pred
    ;   Callable = Callable1,
        NextGoal = NextCall,
        QPred = Pred
    ),
    Callable =.. [Pred|Args],
    length(Args, Argc),
    length(Argv, Argc),
    length(Vars, N),
    MapArity is N + 1,
    format(atom(AuxName), '__aux_maplist/~d_~w+~d', [MapArity, QPred, Argc]),
    append(Lists, Args, AuxArgs),
    Goal =.. [AuxName|AuxArgs],

    AuxArity is N+Argc,
    prolog_load_context(module, Module),
    functor(NextCall, Pred, AuxArity),
    \+ predicate_property(Module:NextGoal, transparent),
    (   predicate_property(Module:Goal, defined)
    ->  true
    ;   empty_lists(N, BaseLists),
        length(Anon, Argc),
        append(BaseLists, Anon, BaseArgs),
        BaseClause =.. [AuxName|BaseArgs],

        heads_and_tails(N, NextArgs, Vars, Tails),
        append(NextArgs, Argv, AllNextArgs),
        NextHead =.. [AuxName|AllNextArgs],
        append(Argv, Vars, PredArgs),
        NextCall =.. [Pred|PredArgs],
        append(Tails, Argv, IttArgs),
        NextIterate =.. [AuxName|IttArgs],
        NextClause = (NextHead :- NextGoal, NextIterate),
        compile_aux_clauses([BaseClause, NextClause])
    ).

expand_closure_no_fail(Callable0, N, Callable1) :-
    '$expand_closure'(Callable0, N, Callable1),
    !.
expand_closure_no_fail(Callable, _, Callable).

empty_lists(0, []) :- !.
empty_lists(N, [[]|T]) :-
    N2 is N - 1,
    empty_lists(N2, T).

heads_and_tails(0, [], [], []).
heads_and_tails(N, [[H|T]|L1], [H|L2], [T|L3]) :-
    N2 is N - 1,
    heads_and_tails(N2, L1, L2, L3).


%!  expand_apply(+GoalIn:callable, -GoalOut) is semidet.
%
%   Macro expansion for `apply' predicates.

expand_apply(Maplist, Goal) :-
    compound(Maplist),
    compound_name_arity(Maplist, maplist, N),
    N >= 2,
    Maplist =.. [maplist, Callable|Lists],
    qcall_instantiated(Callable),
    !,
    expand_maplist(Callable, Lists, Goal).

%!  expand_apply(+GoalIn:callable, -GoalOut, +PosIn, -PosOut) is semidet.
%
%   Translation  of  simple  meta  calls    to   inline  code  while
%   maintaining position information. Note that once(Goal) cannot be
%   translated  to  `(Goal->true)`  because  this   will  break  the
%   compilation of `(once(X) ; Y)`.  A   correct  translation  is to
%   `(Goal->true;fail)`.       Abramo       Bagnara        suggested
%   `((Goal->true),true)`, which is both faster   and avoids warning
%   if style_check(+var_branches) is used.

expand_apply(forall(Cond, Action), Pos0, Goal, Pos) :-
    Goal = \+((Cond, \+(Action))),
    (   nonvar(Pos0),
        Pos0 = term_position(_,_,_,_,[PosCond,PosAct])
    ->  Pos = term_position(0,0,0,0, % \+
                            [ term_position(0,0,0,0, % ,/2
                                            [ PosCond,
                                              term_position(0,0,0,0, % \+
                                                            [PosAct])
                                            ])
                            ])
    ;   true
    ).
expand_apply(once(Once), Pos0, Goal, Pos) :-
    Goal = (Once->true),
    (   nonvar(Pos0),
        Pos0 = term_position(_,_,_,_,[OncePos]),
        compound(OncePos)
    ->  Pos = term_position(0,0,0,0,        % ->/2
                            [ OncePos,
                              F-T           % true
                            ]),
        arg(2, OncePos, F),         % highlight true/false on ")"
        T is F+1
    ;   true
    ).
expand_apply(ignore(Ignore), Pos0, Goal, Pos) :-
    Goal = (Ignore->true;true),
    (   nonvar(Pos0),
        Pos0 = term_position(_,_,_,_,[IgnorePos]),
        compound(IgnorePos)
    ->  Pos = term_position(0,0,0,0,                        % ;/2
                            [ term_position(0,0,0,0,        % ->/2
                                            [ IgnorePos,
                                              F-T           % true
                                            ]),
                              F-T                           % true
                            ]),
        arg(2, IgnorePos, F),       % highlight true/false on ")"
        T is F+1
    ;   true
    ).
expand_apply(Phrase, Pos0, Expanded, Pos) :-
    expand_phrase(Phrase, Pos0, Expanded, Pos),
    !.


%!  expand_phrase(+PhraseGoal, -Goal) is semidet.
%!  expand_phrase(+PhraseGoal, +Pos0, -Goal, -Pos) is semidet.
%
%   Provide goal-expansion for  PhraseGoal.   PhraseGoal  is  either
%   phrase/2,3  or  call_dcg/2,3.  The  current   version  does  not
%   translate control structures, but  only   simple  terminals  and
%   non-terminals.
%
%   For example:
%
%     ==
%     ?- expand_phrase(phrase(("ab", rule)), List), Goal).
%     Goal = (List=[97, 98|_G121], rule(_G121, [])).
%     ==
%
%   @throws Re-throws errors from dcg_translate_rule/2

expand_phrase(Phrase, Goal) :-
    expand_phrase(Phrase, _, Goal, _).

expand_phrase(phrase(NT,Xs), Pos0, NTXsNil, Pos) :-
    !,
    extend_pos(Pos0, 1, Pos1),
    expand_phrase(phrase(NT,Xs,[]), Pos1, NTXsNil, Pos).
expand_phrase(Goal, Pos0, NewGoal, Pos) :-
    dcg_goal(Goal, NT, Xs0, Xs),
    nonvar(NT),
    nt_pos(Pos0, NTPos),
    dcg_extend(NT, NTPos, NewGoal, Pos, Xs0, Xs).

dcg_goal(phrase(NT,Xs0,Xs), NT, Xs0, Xs).
dcg_goal(call_dcg(NT,Xs0,Xs), NT, Xs0, Xs).

%!  dcg_extend(+Callable, +Pos0, -Goal, -Pos, +Xs0, ?Xs) is semidet.

dcg_extend(Compound0, Pos0, Compound, Pos, Xs0, Xs) :-
    compound(Compound0),
    \+ dcg_control(Compound0),
    !,
    extend_pos(Pos0, 2, Pos),
    compound_name_arguments(Compound0, Name, Args0),
    append(Args0, [Xs0,Xs], Args),
    compound_name_arguments(Compound, Name, Args).
dcg_extend(Name, Pos0, Compound, Pos, Xs0, Xs) :-
    atom(Name),
    \+ dcg_control(Name),
    !,
    extend_pos(Pos0, 2, Pos),
    compound_name_arguments(Compound, Name, [Xs0,Xs]).
dcg_extend(Q0, Pos0, M:Q, Pos, Xs0, Xs) :-
    compound(Q0), Q0 = M:Q1,
    '$expand':f2_pos(Pos0, MPos, APos0, Pos, MPos, APos),
    dcg_extend(Q1, APos0, Q, APos, Xs0, Xs).
dcg_extend(Terminal, Pos0, Xs0 = DList, Pos, Xs0, Xs) :-
    terminal(Terminal, DList, Xs),
    !,
    t_pos(Pos0, Pos).

dcg_control(!).
dcg_control([]).
dcg_control([_|_]).
dcg_control({_}).
dcg_control((_,_)).
dcg_control((_;_)).
dcg_control((_->_)).
dcg_control((_*->_)).
dcg_control(_:_).

terminal(List, DList, Tail) :-
    compound(List),
    List = [_|_],
    !,
    '$skip_list'(_, List, T0),
    (   var(T0)
    ->  DList = List,
        Tail = T0
    ;   T0 == []
    ->  append(List, Tail, DList)
    ;   type_error(list, List)
    ).
terminal(List, DList, Tail) :-
    List == [],
    !,
    DList = Tail.
terminal(String, DList, Tail) :-
    string(String),
    string_codes(String, List),
    append(List, Tail, DList).

extend_pos(Var, _, Var) :-
    var(Var),
    !.
extend_pos(term_position(F,T,FF,FT,ArgPos0), Extra,
           term_position(F,T,FF,FT,ArgPos)) :-
    !,
    extra_pos(Extra, T, ExtraPos),
    append(ArgPos0, ExtraPos, ArgPos).
extend_pos(FF-FT, Extra,
           term_position(FF,FT,FF,FT,ArgPos)) :-
    !,
    extra_pos(Extra, FT, ArgPos).

extra_pos(1, T, [T-T]).
extra_pos(2, T, [T-T,T-T]).

nt_pos(PhrasePos, _NTPos) :-
    var(PhrasePos),
    !.
nt_pos(term_position(_,_,_,_,[NTPos|_]), NTPos).

t_pos(Pos0, term_position(F,T,F,T,[F-T,F-T])) :-
    compound(Pos0),
    !,
    arg(1, Pos0, F),
    arg(2, Pos0, T).
t_pos(_, _).


%!  qcall_instantiated(@Term) is semidet.
%
%   True if Term is instantiated sufficiently to call it.
%
%   @tbd    Shouldn't this be callable straight away?

qcall_instantiated(Var) :-
    var(Var),
    !,
    fail.
qcall_instantiated(M:C) :-
    !,
    atom(M),
    callable(C).
qcall_instantiated(C) :-
    callable(C).


                 /*******************************
                 *            DEBUGGER          *
                 *******************************/

:- multifile
    prolog_clause:unify_goal/5.

prolog_clause:unify_goal(Maplist, Expanded, _Module, Pos0, Pos) :-
    is_maplist(Maplist),
    maplist_expansion(Expanded),
    Pos0 = term_position(F,T,FF,FT,[_MapPos|ArgsPos]),
    Pos  = term_position(F,T,FF,FT,ArgsPos).

is_maplist(Goal) :-
    compound(Goal),
    compound_name_arity(Goal, maplist, A),
    A >= 2.

maplist_expansion(Expanded) :-
    compound(Expanded),
    compound_name_arity(Expanded, Name, _),
    sub_atom(Name, 0, _, _, '__aux_maplist/').


                 /*******************************
                 *          XREF/COLOUR         *
                 *******************************/

:- multifile
    prolog_colour:vararg_goal_classification/3.

prolog_colour:vararg_goal_classification(maplist, Arity, expanded) :-
    Arity >= 2.


                 /*******************************
                 *           ACTIVATE           *
                 *******************************/

:- multifile
    system:goal_expansion/2,
    system:goal_expansion/4.

%       @tbd    Should we only apply if optimization is enabled (-O)?

system:goal_expansion(GoalIn, GoalOut) :-
    \+ current_prolog_flag(xref, true),
    expand_apply(GoalIn, GoalOut).
system:goal_expansion(GoalIn, PosIn, GoalOut, PosOut) :-
    expand_apply(GoalIn, PosIn, GoalOut, PosOut).