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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2019-2020, VU University Amsterdam
CWI, 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(prolog_code,
[ comma_list/2, % (A,B) <-> [A,B]
semicolon_list/2, % (A;B) <-> [A,B]
mkconj/3, % +A, +B, -Conjunction
mkdisj/3, % +A, +B, -Disjunction
pi_head/2, % :PI, :Head
head_name_arity/3, % ?Goal, ?Name, ?Arity
most_general_goal/2, % :Goal, -General
extend_goal/3, % :Goal, +Extra, -GoalOut
predicate_label/2, % +PI, -Label
predicate_sort_key/2, % +PI, -Key
is_control_goal/1, % @Term
is_predicate_indicator/1, % @Term
body_term_calls/2 % :BodyTerm, -Goal
]).
:- autoload(library(error),[must_be/2, instantiation_error/1]).
:- autoload(library(lists),[append/3]).
:- meta_predicate
body_term_calls(:, -).
:- multifile
user:prolog_predicate_name/2.
/** <module> Utilities for reasoning about code
This library collects utilities to reason about terms commonly needed
for reasoning about Prolog code. Note that many related facilities can
be found in the core as well as other libraries:
- =@=/2, subsumes_term/2, etc.
- library(occurs)
- library(listing)
- library(prolog_source)
- library(prolog_xref)
- library(prolog_codewalk)
*/
%! comma_list(?CommaList, ?List).
%! semicolon_list(?SemicolonList, ?List).
%
% True if CommaList is a nested term over the ','/2 (';'/2) functor
% and List is a list expressing the elements of the conjunction. The
% predicate is deterministic if at least CommaList or List is
% sufficiently instantiated. If both are partial structures it
% enumerates ever growing conjunctions and lists. CommaList may be
% left or right associative on input. When generated, the CommaList is
% always right associative.
%
% This predicate is typically used to reason about Prolog conjunctions
% (disjunctions) as many operations are easier on lists than on binary
% trees over some operator.
comma_list(CommaList, List) :-
phrase(binlist(CommaList, ','), List).
semicolon_list(CommaList, List) :-
phrase(binlist(CommaList, ';'), List).
binlist(Term, Functor) -->
{ nonvar(Term) },
!,
( { Term =.. [Functor,A,B] }
-> binlist(A, Functor),
binlist(B, Functor)
; [Term]
).
binlist(Term, Functor) -->
[A],
( var_tail
-> ( { Term = A }
; { Term =.. [Functor,A,B] },
binlist(B,Functor)
)
; \+ [_]
-> {Term = A}
; binlist(B,Functor),
{Term =.. [Functor,A,B]}
).
var_tail(H, H) :-
var(H).
%! mkconj(A,B,Conj) is det.
%! mkdisj(A,B,Disj) is det.
%
% Create a conjunction or disjunction from two terms. Reduces on
% `true` (mkconj/2) and `false` (mkdisj/2). Note that a `false`
% encountered in a conjunction does __not__ cause the conjunction to
% be `false`, i.e. semantics under side effects are preserved.
%
% The Prolog `,` and `;` operators are of type `xfy`, i.e. _right
% associative_. These predicates preserve this grouping. For example,
%
% ?- mkconj((a,b), c, Conj)
% Conj = (a,b,c)
mkconj(A,B,Conj) :-
( is_true(A)
-> Conj = B
; is_true(B)
-> Conj = A
; mkconj_(A,B,Conj)
).
mkconj_((A,B), C, Conj) =>
Conj = (A,C2),
mkconj_(B,C,C2).
mkconj_(A, B, C) =>
C = (A,B).
mkdisj(A,B,Disj) :-
( is_false(A)
-> Disj = B
; is_false(B)
-> Disj = A
; mkdisj_(A,B,Disj)
).
mkdisj_((A;B), C, Disj) =>
Disj = (A;C2),
mkdisj_(B, C, C2).
mkdisj_(A, B, C) =>
C = (A;B).
is_true(Goal) :- Goal == true.
is_false(Goal) :- (Goal == false -> true ; Goal == fail).
%! is_predicate_indicator(@Term) is semidet.
%
% True when Term is a predicate indicator
is_predicate_indicator(Var) :-
var(Var),
!,
instantiation_error(Var).
is_predicate_indicator(PI) :-
strip_module(PI, M, PI1),
atom(M),
( PI1 = (Name/Arity)
-> true
; PI1 = (Name//Arity)
),
atom(Name),
integer(Arity),
Arity >= 0.
%! pi_head(?PredicateIndicator, ?Goal) is det.
%
% Translate between a PredicateIndicator and a Goal term. The terms
% may have a module qualification.
%
% @error type_error(predicate_indicator, PredicateIndicator)
pi_head(PI, Head) :-
'$pi_head'(PI, Head).
%! head_name_arity(?Goal, ?Name, ?Arity) is det.
%
% Similar to functor/3, but deals with SWI-Prolog's zero-argument
% callable terms and avoids creating a non-callable term if Name is
% not an atom and Arity is zero.
head_name_arity(Goal, Name, Arity) :-
'$head_name_arity'(Goal, Name, Arity).
%! most_general_goal(+Goal, -General) is det.
%
% General is the most general version of Goal. Goal can be qualified.
%
% @see is_most_general_term/1.
most_general_goal(Goal, General) :-
var(Goal),
!,
General = Goal.
most_general_goal(Goal, General) :-
atom(Goal),
!,
General = Goal.
most_general_goal(M:Goal, M:General) :-
!,
most_general_goal(Goal, General).
most_general_goal(Compound, General) :-
compound_name_arity(Compound, Name, Arity),
compound_name_arity(General, Name, Arity).
%! extend_goal(:Goal0, +Extra, -Goal) is det.
%
% Extend the possibly qualified Goal0 with additional arguments from
% Extra. If Goal0 is insufficiantly instantiated (i.e., a variable), a
% term call(Goal0, ...) is returned.
extend_goal(Goal0, Extra, Goal) :-
var(Goal0),
!,
Goal =.. [call,Goal0|Extra].
extend_goal(M:Goal0, Extra, M:Goal) :-
extend_goal(Goal0, Extra, Goal).
extend_goal(Atom, Extra, Goal) :-
atom(Atom),
!,
Goal =.. [Atom|Extra].
extend_goal(Goal0, Extra, Goal) :-
compound_name_arguments(Goal0, Name, Args0),
append(Args0, Extra, Args),
compound_name_arguments(Goal, Name, Args).
/*******************************
* LABELS *
*******************************/
%! predicate_label(++PI, -Label) is det.
%
% Create a human-readable label for the given predicate indicator.
% This notably hides the module qualification from `user` and built-in
% predicates. This predicate is intended for reporting predicate
% information to the user, for example in the profiler.
%
% First PI is converted to a _head_ and the hook
% user:prolog_predicate_name/2 is tried.
predicate_label(PI, Label) :-
must_be(ground, PI),
pi_head(PI, Head),
user:prolog_predicate_name(Head, Label),
!.
predicate_label(M:Name/Arity, Label) :-
!,
( hidden_module(M, Name/Arity)
-> atomic_list_concat([Name, /, Arity], Label)
; atomic_list_concat([M, :, Name, /, Arity], Label)
).
predicate_label(M:Name//Arity, Label) :-
!,
( hidden_module(M, Name//Arity)
-> atomic_list_concat([Name, //, Arity], Label)
; atomic_list_concat([M, :, Name, //, Arity], Label)
).
predicate_label(Name/Arity, Label) :-
!,
atomic_list_concat([Name, /, Arity], Label).
predicate_label(Name//Arity, Label) :-
!,
atomic_list_concat([Name, //, Arity], Label).
hidden_module(system, _).
hidden_module(user, _).
hidden_module(M, Name/Arity) :-
functor(H, Name, Arity),
predicate_property(system:H, imported_from(M)).
hidden_module(M, Name//DCGArity) :-
Arity is DCGArity+1,
functor(H, Name, Arity),
predicate_property(system:H, imported_from(M)).
%! predicate_sort_key(+PI, -Key) is det.
%
% Key is the (module-free) name of the predicate for sorting purposes.
predicate_sort_key(_:PI, Name) :-
!,
predicate_sort_key(PI, Name).
predicate_sort_key(Name/_Arity, Name).
predicate_sort_key(Name//_Arity, Name).
%! is_control_goal(@Goal)
%
% True if Goal is a compiled Prolog control structure. The difference
% between control structures and meta-predicates is rather unclear.
% The constructs below are recognised by the compiler and cannot be
% redefined. Note that (if->then;else) is recognised as
% ((if->then);else).
is_control_goal(Goal) :-
var(Goal),
!, fail.
is_control_goal((_,_)).
is_control_goal((_;_)).
is_control_goal((_->_)).
is_control_goal((_|_)).
is_control_goal((_*->_)).
is_control_goal(\+(_)).
%! body_term_calls(:BodyTerm, -Goal) is nondet.
%
% True when BodyTerm calls Goal. This predicate looks into control
% structures as well as meta predicates based on predicate_property/2.
%
% When a variable is called, this is normally returned in Goal.
% Currently if a variable is called with additional arguments, e.g.,
% call(Var, a1), this call is reported as call(Var, a1).
body_term_calls(M:Body, Calls) :-
body_term_calls(Body, M, M, Calls).
body_term_calls(Var, M, C, Calls) :-
var(Var),
!,
qualify(M, C, Var, Calls).
body_term_calls(M:Goal, _, C, Calls) :-
!,
body_term_calls(Goal, M, C, Calls).
body_term_calls(Goal, M, C, Calls) :-
qualify(M, C, Goal, Calls).
body_term_calls((A,B), M, C, Calls) :-
!,
( body_term_calls(A, M, C, Calls)
; body_term_calls(B, M, C, Calls)
).
body_term_calls((A;B), M, C, Calls) :-
!,
( body_term_calls(A, M, C, Calls)
; body_term_calls(B, M, C, Calls)
).
body_term_calls((A->B), M, C, Calls) :-
!,
( body_term_calls(A, M, C, Calls)
; body_term_calls(B, M, C, Calls)
).
body_term_calls((A*->B), M, C, Calls) :-
!,
( body_term_calls(A, M, C, Calls)
; body_term_calls(B, M, C, Calls)
).
body_term_calls(\+ A, M, C, Calls) :-
!,
body_term_calls(A, M, C, Calls).
body_term_calls(Goal, M, C, Calls) :-
predicate_property(M:Goal, meta_predicate(Spec)),
\+ ( functor(Goal, call, _),
arg(1, Goal, A1),
strip_module(A1, _, P1),
var(P1)
),
!,
arg(I, Spec, SArg),
arg(I, Goal, GArg),
meta_calls(SArg, GArg, Call0),
body_term_calls(Call0, M, C, Calls).
meta_calls(0, Goal, Goal) :-
!.
meta_calls(I, Goal0, Goal) :-
integer(I),
!,
length(Extra, I),
extend_goal(Goal0, Extra, Goal).
meta_calls(//, Goal0, Goal) :-
extend_goal(Goal0, [_,_], Goal).
meta_calls(^, Goal0, Goal) :-
!,
strip_existential(Goal0, Goal).
strip_existential(Var, Var) :-
var(Var),
!.
strip_existential(_^In, Out) :-
strip_existential(In, Out).
qualify(M, C, Goal, Calls) :-
M == C,
!,
Calls = Goal.
qualify(M, _, Goal, M:Goal).
|