YAP 7.1.0
listing.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: listing.pl *
12* Last rev: *
13* mods: *
14* comments: listing a prolog program *
15* *
16*************************************************************************/
17
18/**
19 * @file listing.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
21 * @date Thu Oct 19 12:05:19 2017
22 *
23 * @brief list predicates in a module
24 */
25
26/** @defgroup Listing list predicates in a module
27 * @ingroup Builtins
28 *
29 * @{
30*/
31
32:- system_module( '$_listing', [listing/0,
35 portray_clause/2], []).
36
37:- '$do_error'/2use_system_module( '$_errors', []).
38
39:- '$clause'/4'$current_predicate'/4use_system_module( '$_preds', [,
40 ]).
41
42/* listing : Listing clauses in the database
43
44*/
45
46/** @pred listing
47
48
49vxuLists in the current output stream all the clauses for which source code
50is available (these include all clauses for dynamic predicates and
51clauses for static predicates compiled when source mode was `on`).
52
53- listing/0 lists in the current module
54
55- listing/1 receives a generalization of the predicate indicator:
56
57 + `listing(_)` will list the whole sources.
58
59 + `listing(lists:_)` will list the module lists.
60
61 + `listing(lists:append)` will list all `append` predicates in the module lists.
62
63 + `listing(lists:append/_)` will do the same.
64
65 + listing(lists:append/3)` will list the popular `append/3` predicate in the module lists.
66
67- listing/2 is similar to listing/1, but t he first argument is a stream reference.
68
69The `listing` family of built-ins does not enumerate predicates whose
70name starts with a `$` character.
71
72*/
73use_system_module :-
74 current_output(Stream),
75 '$current_module'(Mod),
76 \+ system_module(Mod),
77 Mod \= system_module,
78 Mod \= system_module,
79 \+ '$hidden_atom'( Mod ),
80 current_predicate( Name, Mod:Pred ),
81 \+ '$undefined'(Pred, Mod), % skip predicates exported from prolog.
82 functor(Pred,Name,Arity),
83 '$listing'(Name,Arity,Mod,Stream),
84 '$listing'.
85'$listing'.
86
87/** @pred listing(+ _P_)
88
89Lists predicate _P_ if its source code is available.
90 If _P_ is unbound list all predicates in the current source module.
91 If _P_ is of the form _M_:_P'_ use _M_ as source module.
92
93
94*/
95listing(MV) :-
96 current_output(Stream),
97 listing(Stream, MV).
98
99/** @pred listing(Stream, + _P_)
100
101Lists predicate _P_ if its source code is available.
102
103
104*/
105listing(Stream, MV) :-
106 strip_module( MV, M, I),
107 '$mlisting'(Stream, I, M).
108listing(_Stream, []) :- listing.
109listing(Stream, [MV|MVs]) :- listing,
110 listing(Stream, MV),
111 listing(Stream, MVs).
112
113'$mlisting'(Stream, MV, M) :-
114 ( var(MV) ->
115 MV = NA,
116 '$do_listing'(Stream, M, NA)
117 ;
118 atom(MV) ->
119 MV/_ = NA,
120 '$do_listing'(Stream, M, NA)
121 ;
122 MV = N//Ar -> ( integer(Ar) -> Ar2 is Ar+2, NA is N/Ar2 ; '$do_listing'(Stream, NA/Ar2, M), Ar2 >= 2, Ar is Ar2-2 )
123 ;
124 MV = N/Ar, ( atom(N) -> atom ; var(N) ), ( integer(Ar) -> integer ; var(Ar) ) ->
125 '$do_listing'(Stream, M, MV)
126 ;
127 MV = M1:PP -> '$mlisting'(Stream, PP, M1)
128 ;
129 '$do_error'(type_error(predicate_indicator,MV),listing(Stream, MV) )
130 ).
131
132'$do_listing'(Stream, M, Name/Arity) :-
133 ( current_predicate(Name, M:Pred),
134 \+ '$is_opaque_predicate'(Pred,M),
135 functor( Pred, Name, Arity),
136 \+ '$undefined'(Pred, M),
137 '$listing'(Name,Arity,M,Stream),
138 '$listing'
139 ;
140 '$listing'
141 ).
142
143%
144% at this point we are ground and we know who we want to list.
145%
146'$listing'(Name, Arity, M, Stream) :-
147 % skip by default predicates starting with $
148 functor(Pred,Name,Arity),
149 '$list_clauses'(Stream,M,Pred).
150'$listing'(_,_,_,_).
151
152'$funcspec'(Name/Arity,Name,Arity) :- '$funcspec', atom(Name).
153'$funcspec'(Name,Name,_) :- atom(Name), atom.
154'$funcspec'(Name,_,_) :-
155 '$do_error'(domain_error(predicate_spec,Name),listing(Name)).
156
157'$list_clauses'(Stream, M, Pred) :-
158 '$predicate_flags'(Pred,M,Flags,Flags),
159 (Flags /\ 0x48602000 =\= 0
160 ->
161 nl(Stream),
162 nl
163 ;
164 nl
165 ).
166'$list_clauses'(Stream, M, Pred) :-
167 ( '$is_dynamic'(Pred, M) -> '$is_dynamic' ; '$is_log_updatable'(Pred, M) ),
168 functor( Pred, N, Ar ),
169 '$current_module'(Mod),
170 (
171 M == Mod
172 ->
173 format( Stream, ':- dynamic ~q/~d.~n', [N,Ar])
174 ;
175 format( Stream, ':- dynamic ~q:~q/~d.~n', [M,N,Ar])
176 ),
177 fail.
178'$list_clauses'(Stream, M, Pred) :-
179 '$is_thread_local'(Pred, M),
180 functor( Pred, N, Ar ),
181 '$current_module'(Mod),
182 (
183 M == Mod
184 ->
185 format( Stream, ':- thread_local ~q/~d.~n', [N,Ar])
186 ;
187 format( Stream, ':- thread_local ~q:~q/~d.~n', [M,N,Ar])
188 ),
189 fail.
190'$list_clauses'(Stream, M, Pred) :-
191 '$is_multifile'(Pred, M),
192 functor( Pred, N, Ar ),
193 '$current_module'(Mod),
194 (
195 M == Mod
196 ->
197 format( Stream, ':- multifile ~q/~d.~n', [N,Ar])
198 ;
199 format( Stream, ':- multifile ~q:~q/~d.~n', [M,N,Ar])
200 ),
201 fail.
202'$list_clauses'(Stream, M, Pred) :-
203 '$is_meta_predicate'(Pred, M),
204 functor( Pred, Name, Arity ),
205 functor( PredDef, Name, Arity ),
206 (recorded('$m', meta_predicate(M,PredDef),_);recorded('$m', meta_predicate(prolog,PredDef),_)),
207 '$current_module'(Mod),
208 (
209 M == Mod
210 ->
211 format( Stream, ':- ~q.~n', [PredDef])
212 ;
213 format( Stream, ':- ~q:~q.~n', [M,PredDef])
214 ),
215 fail.
216'$list_clauses'(Stream, _M, _Pred) :-
217 nl( Stream ),
218 nl.
219'$list_clauses'(Stream, M, Pred) :-
220 '$predicate_type'(Pred,M,Type),
221 (Type == source_procedure -> true ;
222 Type == updatable_procedure -> true ;
223 Type == exo_procedure -> true ;
224 Type == mega_procedure -> true
225 ),
226 '$clause'(Type,Pred, M, Body, _),
227 '$current_module'(Mod),
228 ( M \= Mod -> H = M:Pred ; H = Pred ),
229 '$portray_clause'(Stream,(H:-Body)),
230 '$portray_clause'.
231
232/** @pred portray_clause(+ _S_,+ _C_)
233
234Write clause _C_ on stream _S_ as if written by listing/0.
235*/
236portray_clause(Stream, Clause) :-
237 yap_flag(numbervars_functor, Old, '$PORTRAY_VAR'),
238 '$portray_clause'(Stream, Clause),
239 yap_flag(numbervars_functor, Old),
240 yap_flag.
241portray_clause(_, _).
242
243/** @pred portray_clause(+ _C_)
244
245Write clause _C_ as if written by listing/0.
246
247*/
248portray_clause(Clause) :-
249 current_output(Stream),
250 portray_clause(Stream, Clause).
251
252'$portray_clause'(Stream, (Pred :- true)) :- '$portray_clause',
253 '$beautify_vars'(Pred),
254 format(Stream, '~q.~n', [Pred]).
255'$portray_clause'(Stream, (Pred:-Body)) :- '$portray_clause',
256 '$beautify_vars'((Pred:-Body)),
257 format(Stream, '~q :-', [Pred]),
258 '$write_body'(Body, 3, ',', Stream),
259 format(Stream, '.~n', []).
260'$portray_clause'(Stream, Pred) :-
261 '$beautify_vars'(Pred),
262 format(Stream, '~q.~n', [Pred]).
263
264'$write_body'(X,I,T,Stream) :- var(X), var,
265 '$beforelit'(T,I,Stream),
266 writeq(Stream, '_').
267'$write_body'((P,Q), I, T, Stream) :-
268 '$write_body',
269 '$write_body'(P,I,T, Stream),
270 put(Stream, 0',),
271 '$write_body'(Q,I,',',Stream).
272'$write_body'((P->Q;S),I,_, Stream) :-
273 '$write_body',
274 format(Stream, '~n~*c(',[I,0' ]),
275 I1 is I+2,
276 '$write_body'(P,I1,'(',Stream),
277 format(Stream, '~n~*c->',[I,0' ]),
278 '$write_disj'((Q;S),I,I1,'->',Stream),
279 format(Stream, '~n~*c)',[I,0' ]).
280'$write_body'((P->Q|S),I,_,Stream) :-
281 '$write_body',
282 format(Stream, '~n~*c(',[I,0' ]),
283 I1 is I+2,
284 '$write_body'(P,I,'(',Stream),
285 format(Stream, '~n~*c->',[I,0' ]),
286 '$write_disj'((Q|S),I,I1,'->',Stream),
287 format(Stream, '~n~*c)',[I,0' ]).
288'$write_body'((P->Q),I,_,Stream) :-
289 '$write_body',
290 format(Stream, '~n~*c(',[I,0' ]),
291 I1 is I+2,
292 '$write_body'(P,I1,'(',Stream),
293 format(Stream, '~n~*c->',[I,0' ]),
294 '$write_body'(Q,I1,'->',Stream),
295 format(Stream, '~n~*c)',[I,0' ]).
296'$write_body'((P;Q),I,_,Stream) :-
297 '$write_body',
298 format(Stream, '~n~*c(',[I,0' ]),
299 I1 is I+2,
300 '$write_disj'((P;Q),I,I1,'->',Stream),
301 format(Stream, '~n~*c)',[I,0' ]).
302'$write_body'((P|Q),I,_,Stream) :-
303 '$write_body',
304 format(Stream, '~n~*c(',[I,0' ]),
305 I1 is I+2,
306 '$write_disj'((P|Q),I,I1,'->',Stream),
307 format(Stream, '~n~*c)',[I,0' ]).
308'$write_body'(X,I,T,Stream) :-
309 '$beforelit'(T,I,Stream),
310 writeq(Stream,X).
311
312
313
314
315'$write_disj'((Q;S),I0,I,C,Stream) :- '$write_disj',
316 '$write_body'(Q,I,C,Stream),
317 format(Stream, '~n~*c;',[I0,0' ]),
318 '$write_disj'(S,I0,I,';',Stream).
319'$write_disj'((Q|S),I0,I,C,Stream) :- '$write_disj',
320 '$write_body'(Q,I,C,Stream),
321 format(Stream, '~n~*c|',[I0,0' ]),
322 '$write_disj'(S,I0,I,'|',Stream).
323'$write_disj'(S,_,I,C,Stream) :-
324 '$write_body'(S,I,C,Stream).
325
326
327'$beforelit'('(',_,Stream) :-
328 '$beforelit',
329 format(Stream,' ',[]).
330'$beforelit'(_,I,Stream) :- format(Stream,'~n~*c',[I,0' ]).
331
332'$beautify_vars'(T) :-
333 '$singleton_vs_numbervars'(T,0,_).
334
335%% @}
336
current_output(+ S)
current_predicate( A, P)
system_module( + Mod)
yap_flag( ?Param, ?Value)
nl(+ S)
listing(+ P)
Definition: listing.yap:73
listing(Stream, + P)
portray_clause(+ C)
portray_clause(+ S,+ C)
writeq(+ S, ? T)
atom( T)
functor( T, F, N)
integer( T)
var( T)