/usr/lib/swi-prolog/boot/expand.pl is in swi-prolog-nox 5.10.4-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2009, University of Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module('$expand',
[ expand_term/2,
expand_goal/2
]).
/** <module> Prolog source-code transformation
This module specifies, together with dcg.pl, the transformation of terms
as they are read from a file before they are processed by the compiler.
The toplevel is expand_term/2. This uses three other translators:
* Conditional compilation
* term_expansion/2 rules provided by the user
* DCG expansion
Note that this ordering implies that conditional compilation directives
cannot be generated by term_expansion/2 rules: they must literally
appear in the source-code.
Term-expansion may choose to overrule DCG expansion. If the result of
term-expansion is a DCG rule, the rule is subject to translation into a
predicate.
Next, the result is passed to expand_bodies/2, which performs goal
expansion.
*/
:- dynamic
system:term_expansion/2,
system:goal_expansion/2,
user:term_expansion/2,
user:goal_expansion/2.
:- multifile
system:term_expansion/2,
system:goal_expansion/2,
user:term_expansion/2,
user:goal_expansion/2.
%% expand_term(+Input, -Output) is det.
%
% This predicate is used to translate terms as they are read from
% a source-file before they are added to the Prolog database.
expand_term(Var, Expanded) :-
var(Var), !,
Expanded = Var.
expand_term(Term, []) :-
cond_compilation(Term, X),
X == [], !.
expand_term(Term, Expanded) :- % local term-expansion
'$def_modules'(term_expansion/2, MList),
call_term_expansion(MList, Term, Term2),
expand_term_2(Term2, Expanded).
call_term_expansion([], Term, Term).
call_term_expansion([M|T], Term0, Term) :-
( M:term_expansion(Term0, Term1)
-> expand_terms(call_term_expansion(T), Term1, Term)
; call_term_expansion(T, Term0, Term)
).
expand_term_2((Head --> Body), Expanded) :-
dcg_translate_rule((Head --> Body), Expanded0), !,
expand_bodies(Expanded0, Expanded).
expand_term_2(Term0, Term) :-
expand_bodies(Term0, Term).
%% expand_bodies(+Term, -Out) is det.
%
% Find the body terms in Term and give them to expand_goal/2 for
% further processing.
expand_bodies(Terms, Out) :-
'$def_modules'(goal_expansion/2, MList),
MList \== [], !,
expand_terms(expand_body(MList), Terms, Out).
expand_bodies(Terms, Terms).
expand_body(MList, (Head :- Body), (Head :- ExpandedBody)) :-
nonvar(Body), !,
expand_goal(Body, ExpandedBody, MList).
expand_body(MList, (:- Body), (:- ExpandedBody)) :-
nonvar(Body), !,
expand_goal(Body, ExpandedBody, MList).
expand_body(_, Head, Head).
%% expand_terms(:Closure, +In, -Out)
%
% Loop over two constructs that can be added by term-expansion
% rules in order to run the next phase. Term_expansion/2 can
% return a list and terms may be preceeded with a source-location.
:- meta_predicate
expand_terms(2, +, -).
expand_terms(_, X, X) :-
var(X), !.
expand_terms(C, [H0|T0], [H|T]) :- !,
expand_terms(C, H0, H),
expand_terms(C, T0, T).
expand_terms(C, '$source_location'(File, Line):Clause0,
'$source_location'(File, Line):Clause) :- !,
expand_terms(C, Clause0, Clause).
expand_terms(C, Term0, Term) :-
call(C, Term0, Term).
/*******************************
* GOAL_EXPANSION/2 SUPPORT *
*******************************/
%% expand_goal(+BodyTerm, -Out) is det.
%
% Perform macro-expansion on body terms by calling
% goal_expansion/2.
expand_goal(A, B) :-
'$def_modules'(goal_expansion/2, MList),
( expand_goal(A, B, MList)
-> A \== B
), !.
expand_goal(A, A).
expand_goal(G0, G, MList) :-
'$set_source_module'(M, M),
expand_goal(G0, G, M, MList).
expand_goal(G, G, _, _) :-
var(G), !.
expand_goal(G0, G, M, MList) :-
call_goal_expansion(MList, G0, G1), !,
expand_goal(G1, G, M, MList).
expand_goal((A,B), Conj, M, MList) :- !,
expand_goal(A, EA, M, MList),
expand_goal(B, EB, M, MList),
simplify((EA, EB), Conj).
expand_goal((A;B), Or, M, MList) :- !,
expand_goal(A, EA, M, MList),
expand_goal(B, EB, M, MList),
simplify((EA;EB), Or).
expand_goal((A->B;C), ITE, M, MList) :- !,
expand_goal(A, EA, M, MList),
expand_goal(B, EB, M, MList),
expand_goal(C, EC, M, MList),
simplify((EA->EB;EC), ITE).
expand_goal((A->B), (EA->EB), M, MList) :- !,
expand_goal(A, EA, M, MList),
expand_goal(B, EB, M, MList).
expand_goal((A*->B), (EA*->EB), M, MList) :- !,
expand_goal(A, EA, M, MList),
expand_goal(B, EB, M, MList).
expand_goal((\+A), (\+EA), M, MList) :- !,
expand_goal(A, EA, M, MList).
expand_goal(setof(T,G,L), setof(T,EG,L), M, MList) :- !,
expand_setof_goal(G, EG, M, MList).
expand_goal(bagof(T,G,L), bagof(T,EG,L), M, MList) :- !,
expand_setof_goal(G, EG, M, MList).
expand_goal(M:G, M:EG, _M, _MList) :-
atom(M), !,
'$def_modules'(M:goal_expansion/2, MList),
setup_call_cleanup('$set_source_module'(Old, M),
'$expand':expand_goal(G, EG, M, MList),
'$set_source_module'(_, Old)).
expand_goal(G0, G, M, MList) :-
callable(G0),
functor(G0, N, A),
( default_module(M, M2),
current_predicate(M2:N/A)
-> true
),
'$get_predicate_attribute'(M2:G0, meta_predicate, Head),
has_meta_arg(Head),
expand_meta(Head, G0, G, M, MList),
G0 \== G, !.
expand_goal(A, A, _, _).
expand_meta(Spec, G0, G, M, MList) :-
functor(Spec, _, Arity),
functor(G0, Name, Arity),
functor(G, Name, Arity),
expand_meta(1, Arity, Spec, G0, G, M, MList).
expand_meta(I, Arity, Spec, G0, G, M, MList) :-
I =< Arity, !,
arg(I, Spec, Meta),
arg(I, G0, A0),
arg(I, G, A),
expand_meta_arg(Meta, A0, A, M, MList),
I2 is I + 1,
expand_meta(I2, Arity, Spec, G0, G, M, MList).
expand_meta(_, _, _, _, _, _, _).
expand_meta_arg(0, A0, A, M, MList) :- !,
expand_goal(A0, A, M, MList).
expand_meta_arg(_, A, A, _, _).
has_meta_arg(Head) :-
arg(_, Head, Arg),
Arg == 0, !.
expand_setof_goal(Var, Var, _, _) :-
var(Var), !.
expand_setof_goal(V^G, V^EG, M, MList) :- !,
expand_setof_goal(G, EG, M, MList).
expand_setof_goal(G, EG, M, MList) :- !,
expand_goal(G, EG, M, MList).
%% call_goal_expansion(+ExpandModules, +Goal0, -Goal) is semidet.
%
% Succeeds if the context has a module that defines
% goal_expansion/2 this rule succeeds and Goal is not equal to
% Goal0. Note that the translator is called recursively until a
% fixed-point is reached.
call_goal_expansion(MList, G0, G) :-
'$member'(M, MList),
M:goal_expansion(G0, G),
G0 \== G, !.
/*******************************
* SIMPLIFICATION ROUTINES *
*******************************/
%% simplify(+ControlIn, -ControlOut) is det.
%
% Try to simplify control structure.
%
% @tbd Much more analysis
% @tbd Turn this into a separate module
simplify(Control, Control) :-
current_prolog_flag(optimise, false), !.
simplify(Control, Simple) :-
simple(Control, Simple), !.
simplify(Control, Control).
simple((X, Y), Conj) :-
( true(X)
-> Conj = Y
; false(X)
-> Conj = fail
; true(Y)
-> Conj = X
).
simple((I->T;E), ITE) :-
( true(I)
-> ITE = T
; false(I)
-> ITE = E
).
simple((X;Y), Or) :-
false(X),
Or = Y.
true(X) :-
nonvar(X),
eval_true(X).
false(X) :-
nonvar(X),
eval_false(X).
%% eval_true(+Goal) is semidet.
%% eval_false(+Goal) is semidet.
eval_true(true).
eval_true(otherwise).
eval_false(fail).
eval_false(false).
/*******************************
* :- IF ... :- ENDIF *
*******************************/
:- thread_local
'$include_code'/1.
'$including' :-
'$include_code'(X), !,
X == true.
'$including'.
cond_compilation((:- if(G)), []) :-
( '$including'
-> ( catch('$eval_if'(G), E, (print_message(error, E), fail))
-> asserta('$include_code'(true))
; asserta('$include_code'(false))
)
; asserta('$include_code'(else_false))
).
cond_compilation((:- elif(G)), []) :-
( retract('$include_code'(Old))
-> ( Old == true
-> asserta('$include_code'(else_false))
; Old == false,
catch('$eval_if'(G), E, (print_message(error, E), fail))
-> asserta('$include_code'(true))
; asserta('$include_code'(Old))
)
; throw(error(context_error(no_if), _))
).
cond_compilation((:- else), []) :-
( retract('$include_code'(X))
-> ( X == true
-> X2 = false
; X == false
-> X2 = true
; X2 = X
),
asserta('$include_code'(X2))
; throw(error(context_error(no_if), _))
).
cond_compilation(end_of_file, end_of_file) :- !. % TBD: Check completeness
cond_compilation((:- endif), []) :-
retract('$include_code'(_)), !.
cond_compilation(_, []) :-
\+ '$including'.
'$eval_if'(G) :-
expand_goal(G, G2),
'$set_source_module'(Module, Module),
Module:G2.
|