/usr/lib/swi-prolog/library/explain.pl is in swi-prolog-nox 7.6.4+dfsg-1build1.
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 | /* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 1985-2010, 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(prolog_explain,
[ explain/1,
explain/2
]).
:- use_module(library(helpidx)).
:- use_module(library(lists)).
:- use_module(library(apply)).
/** <module> Describe Prolog Terms
The library(explain) describes prolog-terms. The most useful
functionality is its cross-referencing function.
==
?- explain(subset(_,_)).
"subset(_, _)" is a compound term
Referenced from 2-th clause of lists:subset/2
Referenced from 46-th clause of prolog_xref:imported/3
Referenced from 68-th clause of prolog_xref:imported/3
lists:subset/2 is a predicate defined in
/staff/jan/lib/pl-5.6.17/library/lists.pl:307
Referenced from 2-th clause of lists:subset/2
Possibly referenced from 2-th clause of lists:subset/2
==
Note that the help-tool for XPCE provides a nice graphical
cross-referencer.
*/
%! explain(@Term) is det
%
% Write all information known about Term to the current output.
explain(Item) :-
explain(Item, Explanation),
writeln(Explanation),
fail.
explain(_).
/********************************
* BASIC TYPES *
*********************************/
%! explain(@Term, -Explanation) is nondet.
%
% Explanation describes information about Term.
explain(Var, Explanation) :-
var(Var),
!,
utter(Explanation, '"~w" is an unbound variable', [Var]).
explain(I, Explanation) :-
integer(I),
!,
utter(Explanation, '"~w" is an integer', [I]).
explain(F, Explanation) :-
float(F),
!,
utter(Explanation, '"~w" is a floating point number', [F]).
explain(S, Explanation) :-
string(S),
!,
utter(Explanation, '"~w" is a string', S).
explain([], Explanation) :-
!,
utter(Explanation, '"[]" is a special constant denoting an empty list', []).
explain(A, Explanation) :-
atom(A),
utter(Explanation, '"~w" is an atom', [A]).
explain(A, Explanation) :-
atom(A),
current_op(Pri, F, A),
op_type(F, Type),
utter(Explanation, '"~w" is a ~w (~w) operator of priority ~d',
[A, Type, F, Pri]).
explain(A, Explanation) :-
atom(A),
!,
explain_atom(A, Explanation).
explain([H|T], Explanation) :-
is_list(T),
!,
List = [H|T],
length(List, L),
( utter(Explanation, '"~p" is a proper list with ~d elements',
[List, L])
; maplist(printable, List),
utter(Explanation, '~t~8|Text is "~s"', [List])
).
explain([H|T], Explanation) :-
!,
length([H|T], L),
!,
utter(Explanation, '"~p" is a not-closed list with ~d elements',
[[H|T], L]).
explain(Name/Arity, Explanation) :-
atom(Name),
integer(Arity),
!,
functor(Head, Name, Arity),
known_predicate(Module:Head),
( Module == system
-> true
; \+ predicate_property(Module:Head, imported_from(_))
),
explain_predicate(Module:Head, Explanation).
explain(Module:Name/Arity, Explanation) :-
atom(Module), atom(Name), integer(Arity),
!,
functor(Head, Name, Arity),
explain_predicate(Module:Head, Explanation).
explain(Module:Head, Explanation) :-
callable(Head),
!,
explain_predicate(Module:Head, Explanation).
explain(Term, Explanation) :-
numbervars(Term, 0, _, [singletons(true)]),
utter(Explanation, '"~W" is a compound term',
[Term, [quoted(true), numbervars(true)]]).
explain(Term, Explanation) :-
explain_functor(Term, Explanation).
%! known_predicate(:Head)
%
% Succeeds if we know anything about this predicate. Undefined
% predicates are considered `known' for this purpose, so we can
% provide referenced messages on them.
known_predicate(Pred) :-
current_predicate(_, Pred),
!.
known_predicate(Pred) :-
predicate_property(Pred, undefined).
known_predicate(_:Head) :-
functor(Head, Name, Arity),
'$in_library'(Name, Arity, _Path).
op_type(X, prefix) :-
atom_chars(X, [f, _]).
op_type(X, infix) :-
atom_chars(X, [_, f, _]).
op_type(X, postfix) :-
atom_chars(X, [_, f]).
printable(C) :-
integer(C),
between(32, 126, C).
/********************************
* ATOMS *
*********************************/
explain_atom(A, Explanation) :-
referenced(A, Explanation).
explain_atom(A, Explanation) :-
current_predicate(A, Module:Head),
( Module == system
-> true
; \+ predicate_property(Module:Head, imported_from(_))
),
explain_predicate(Module:Head, Explanation).
explain_atom(A, Explanation) :-
predicate_property(Module:Head, undefined),
functor(Head, A, _),
explain_predicate(Module:Head, Explanation).
/********************************
* FUNCTOR *
*********************************/
explain_functor(Head, Explanation) :-
referenced(Head, Explanation).
explain_functor(Head, Explanation) :-
current_predicate(_, Module:Head),
\+ predicate_property(Module:Head, imported_from(_)),
explain_predicate(Module:Head, Explanation).
explain_functor(Head, Explanation) :-
predicate_property(M:Head, undefined),
( functor(Head, N, A),
utter(Explanation,
'~w:~w/~d is an undefined predicate', [M,N,A])
; referenced(M:Head, Explanation)
).
/********************************
* PREDICATE *
*********************************/
lproperty(built_in, ' built-in', []).
lproperty(dynamic, ' dynamic', []).
lproperty(multifile, ' multifile', []).
lproperty(transparent, ' meta', []).
tproperty(imported_from(Module), ' imported from module ~w', [Module]).
tproperty(file(File), ' defined in~n~t~8|~w', [File]).
tproperty(line_count(Number), ':~d', [Number]).
tproperty(autoload, ' that can be autoloaded', []).
combine_utterances(Pairs, Explanation) :-
maplist(first, Pairs, Fmts),
atomic_list_concat(Fmts, Format),
maplist(second, Pairs, ArgList),
flatten(ArgList, Args),
utter(Explanation, Format, Args).
first(A-_B, A).
second(_A-B, B).
%! explain_predicate(:Head, -Explanation) is det.
explain_predicate(Pred, Explanation) :-
Pred = Module:Head,
functor(Head, Name, Arity),
( predicate_property(Pred, undefined)
-> utter(Explanation,
'~w:~w/~d is an undefined predicate', [Module,Name,Arity])
; ( var(Module)
-> U0 = '~w/~d is a' - [Name, Arity]
; U0 = '~w:~w/~d is a' - [Module, Name, Arity]
),
findall(Fmt-Arg, (lproperty(Prop, Fmt, Arg),
predicate_property(Pred, Prop)),
U1),
U2 = ' predicate' - [],
findall(Fmt-Arg, (tproperty(Prop, Fmt, Arg),
predicate_property(Pred, Prop)),
U3),
flatten([U0, U1, U2, U3], Utters),
combine_utterances(Utters, Explanation)
).
explain_predicate(Pred, Explanation) :-
predicate_property(Pred, built_in),
Pred = _Module:Head,
functor(Head, Name, Arity),
predicate(Name, Arity, Summary, _, _),
utter(Explanation, '~t~8|Summary: ``~w''''', [Summary]).
explain_predicate(Pred, Explanation) :-
referenced(Pred, Explanation).
/********************************
* REFERENCES *
*********************************/
referenced(Term, Explanation) :-
current_predicate(_, Module:Head),
( predicate_property(Module:Head, built_in)
-> current_prolog_flag(access_level, system)
; true
),
\+ predicate_property(Module:Head, imported_from(_)),
Module:Head \= help_index:predicate(_,_,_,_,_),
nth_clause(Module:Head, N, Ref),
'$xr_member'(Ref, Term),
utter_referenced(Module:Head, N, Ref,
'Referenced', Explanation).
referenced(_:Head, Explanation) :-
current_predicate(_, Module:Head),
( predicate_property(Module:Head, built_in)
-> current_prolog_flag(access_level, system)
; true
),
\+ predicate_property(Module:Head, imported_from(_)),
nth_clause(Module:Head, N, Ref),
'$xr_member'(Ref, Head),
utter_referenced(Module:Head, N, Ref,
'Possibly referenced', Explanation).
utter_referenced(_Module:class(_,_,_,_,_,_), _, _, _, _) :-
current_prolog_flag(xpce, true),
!,
fail.
utter_referenced(_Module:lazy_send_method(_,_,_), _, _, _, _) :-
current_prolog_flag(xpce, true),
!,
fail.
utter_referenced(_Module:lazy_get_method(_,_,_), _, _, _, _) :-
current_prolog_flag(xpce, true),
!,
fail.
utter_referenced(pce_xref:exported(_,_), _, _, _, _) :-
!,
fail.
utter_referenced(pce_xref:defined(_,_,_), _, _, _, _) :-
!,
fail.
utter_referenced(pce_xref:called(_,_,_), _, _, _, _) :-
!,
fail.
utter_referenced(pce_principal:send_implementation(_, _, _),
_, Ref, Text, Explanation) :-
current_prolog_flag(xpce, true),
!,
xpce_method_id(Ref, Id),
utter(Explanation, '~t~8|~w from ~w', [Text, Id]).
utter_referenced(pce_principal:get_implementation(Id, _, _, _),
_, Ref, Text, Explanation) :-
current_prolog_flag(xpce, true),
!,
xpce_method_id(Ref, Id),
utter(Explanation, '~t~8|~w from ~w', [Text, Id]).
utter_referenced(Module:Head, N, _Ref, Text, Explanation) :-
functor(Head, Name, Arity),
utter(Explanation,
'~t~8|~w from ~d-th clause of ~w:~w/~d',
[Text, N, Module, Name, Arity]).
xpce_method_id(Ref, Id) :-
clause(Head, _Body, Ref),
strip_module(Head, _, H),
arg(1, H, Id).
/********************************
* UTTER *
*********************************/
utter(Explanation, Fmt, Args) :-
format(string(Explanation), Fmt, Args).
|