2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: listing.pl *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: listing a prolog program *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2014-04-09 12:39:29 +01:00
|
|
|
:- system_module( '$_listing', [listing/0,
|
|
|
|
listing/1,
|
|
|
|
portray_clause/1,
|
|
|
|
portray_clause/2], []).
|
|
|
|
|
|
|
|
:- use_system_module( '$_errors', ['$do_error'/2]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_preds', ['$clause'/4,
|
2014-11-25 12:03:48 +00:00
|
|
|
'$current_predicate'/4]).
|
2014-04-09 12:39:29 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* listing : Listing clauses in the database
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred listing
|
|
|
|
|
|
|
|
|
|
|
|
Lists in the current output stream all the clauses for which source code
|
|
|
|
is available (these include all clauses for dynamic predicates and
|
|
|
|
clauses for static predicates compiled when source mode was `on`).
|
|
|
|
|
2014-10-10 15:52:14 +01:00
|
|
|
- listing/0 lists in the current module
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2014-10-10 15:52:14 +01:00
|
|
|
- listing/1 receives a generalization of the predicate indicator:
|
|
|
|
|
|
|
|
+ `listing(_)` will list the whole sources.
|
|
|
|
|
|
|
|
+ `listing(lists:_)` will list the module lists.
|
|
|
|
|
|
|
|
+ `listing(lists:append)` will list all `append` predicates in the module lists.
|
|
|
|
|
|
|
|
+ `listing(lists:append/_)` will do the same.
|
|
|
|
|
|
|
|
+ listing(lists:append/3)` will list the popular `append/3` predicate in the module lists.
|
|
|
|
|
|
|
|
- listing/2 is similar to listing/1, but t he first argument is a stream reference.
|
|
|
|
|
|
|
|
The `listing` family of built-ins does not enumerate predicates whose
|
|
|
|
name starts with a `$` character.
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
listing :-
|
2014-10-03 08:49:14 +01:00
|
|
|
current_output(Stream),
|
|
|
|
'$current_module'(Mod),
|
|
|
|
Mod \= prolog,
|
|
|
|
Mod \= system,
|
|
|
|
\+ '$hidden'( Mod ),
|
2014-11-25 12:03:48 +00:00
|
|
|
'$current_predicate'(_,Mod,Pred, _),
|
2014-10-03 08:49:14 +01:00
|
|
|
'$undefined'(Pred, prolog), % skip predicates exported from prolog.
|
|
|
|
functor(Pred,Name,Arity),
|
2014-10-10 15:52:14 +01:00
|
|
|
\+ atom_concat('$', _, Name),
|
|
|
|
'$listing'(Name,Arity,Mod,Stream),
|
2014-10-03 08:49:14 +01:00
|
|
|
fail.
|
2001-04-09 20:54:03 +01:00
|
|
|
listing.
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred listing(+ _P_)
|
|
|
|
|
|
|
|
Lists predicate _P_ if its source code is available.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2014-07-16 17:55:16 +01:00
|
|
|
listing(MV) :-
|
|
|
|
current_output(Stream),
|
|
|
|
listing(Stream, MV).
|
|
|
|
|
|
|
|
listing(Stream, MV) :-
|
2014-10-03 08:49:14 +01:00
|
|
|
strip_module( MV, M, I),
|
|
|
|
'$mlisting'(Stream, I, M).
|
2014-10-07 01:35:41 +01:00
|
|
|
listing(_Stream, []) :- !.
|
2014-07-16 17:55:16 +01:00
|
|
|
listing(Stream, [MV|MVs]) :- !,
|
|
|
|
listing(Stream, MV),
|
|
|
|
listing(Stream, MVs).
|
|
|
|
|
|
|
|
'$mlisting'(Stream, MV, M) :-
|
2014-10-10 15:52:14 +01:00
|
|
|
( var(MV) ->
|
|
|
|
MV = NA,
|
|
|
|
'$do_listing'(Stream, M, NA)
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-10-10 15:52:14 +01:00
|
|
|
atom(MV) ->
|
|
|
|
MV/_ = NA,
|
|
|
|
'$do_listing'(Stream, M, NA)
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-10-10 15:52:14 +01:00
|
|
|
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 )
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-10-10 15:52:14 +01:00
|
|
|
MV = N/Ar, ( atom(N) -> true ; var(N) ), ( integer(Ar) -> true ; var(Ar) ) ->
|
|
|
|
'$do_listing'(Stream, M, MV)
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
|
|
|
MV = M1:PP -> '$mlisting'(Stream, PP, M1)
|
|
|
|
;
|
|
|
|
'$do_error'(type_error(predicate_indicator,MV),listing(Stream, MV) )
|
|
|
|
).
|
|
|
|
|
2014-10-10 15:52:14 +01:00
|
|
|
'$do_listing'(Stream, M, Name/Arity) :-
|
|
|
|
( current_predicate(M:Name/Arity),
|
|
|
|
\+ atom_concat('$', _, Name),
|
|
|
|
'$listing'(Name,Arity,M,Stream),
|
|
|
|
fail
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-10-10 15:52:14 +01:00
|
|
|
true
|
2014-07-16 17:55:16 +01:00
|
|
|
).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-10-10 15:52:14 +01:00
|
|
|
%
|
|
|
|
% at this point we are ground and wew know who we want to list.
|
|
|
|
%
|
|
|
|
'$listing'(Name, Arity, M, Stream) :-
|
|
|
|
% skip by default predicates starting with $
|
2001-04-09 20:54:03 +01:00
|
|
|
functor(Pred,Name,Arity),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$list_clauses'(Stream,M,Pred).
|
2014-10-10 15:52:14 +01:00
|
|
|
'$listing'(_,_,_,_).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
'$funcspec'(Name/Arity,Name,Arity) :- !, atom(Name).
|
|
|
|
'$funcspec'(Name,Name,_) :- atom(Name), !.
|
2002-10-01 21:28:47 +01:00
|
|
|
'$funcspec'(Name,_,_) :-
|
|
|
|
'$do_error'(domain_error(predicate_spec,Name),listing(Name)).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-07-16 18:59:03 +01:00
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
|
|
|
'$flags'(Pred,M,Flags,Flags),
|
2014-10-03 08:49:14 +01:00
|
|
|
(Flags /\ 0x48602000 =\= 0
|
|
|
|
->
|
|
|
|
nl(Stream),
|
|
|
|
fail
|
|
|
|
;
|
|
|
|
!
|
|
|
|
).
|
2014-07-16 17:55:16 +01:00
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
|
|
|
( '$is_dynamic'(Pred, M) -> true ; '$is_log_updatable'(Pred, M) ),
|
2014-07-16 18:59:03 +01:00
|
|
|
functor( Pred, N, Ar ),
|
2014-07-16 17:55:16 +01:00
|
|
|
'$current_module'(Mod),
|
|
|
|
(
|
|
|
|
M == Mod
|
|
|
|
->
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- dynamic ~q/~d.~n', [N,Ar])
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- dynamic ~q:~q/~d.~n', [M,N,Ar])
|
2014-07-16 17:55:16 +01:00
|
|
|
),
|
|
|
|
fail.
|
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
|
|
|
'$is_thread_local'(Pred, M),
|
2014-07-16 18:59:03 +01:00
|
|
|
functor( Pred, N, Ar ),
|
2014-07-16 17:55:16 +01:00
|
|
|
'$current_module'(Mod),
|
|
|
|
(
|
|
|
|
M == Mod
|
|
|
|
->
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- thread_local ~q/~d.~n', [N,Ar])
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- thread_local ~q:~q/~d.~n', [M,N,Ar])
|
2014-07-16 17:55:16 +01:00
|
|
|
),
|
|
|
|
fail.
|
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
|
|
|
'$is_multifile'(Pred, M),
|
2014-07-16 18:59:03 +01:00
|
|
|
functor( Pred, N, Ar ),
|
2014-07-16 17:55:16 +01:00
|
|
|
'$current_module'(Mod),
|
|
|
|
(
|
|
|
|
M == Mod
|
|
|
|
->
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- multifile ~q/~d.~n', [N,Ar])
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- multifile ~q:~q/~d.~n', [M,N,Ar])
|
2014-07-16 17:55:16 +01:00
|
|
|
),
|
|
|
|
fail.
|
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
|
|
|
'$is_metapredicate'(Pred, M),
|
|
|
|
functor( Pred, Name, Arity ),
|
|
|
|
prolog:'$meta_predicate'(Name,M,Arity,PredDef),
|
|
|
|
'$current_module'(Mod),
|
|
|
|
(
|
|
|
|
M == Mod
|
|
|
|
->
|
2014-07-16 18:59:03 +01:00
|
|
|
format( Stream, ':- ~q.~n', [PredDef])
|
2014-07-16 17:55:16 +01:00
|
|
|
;
|
2014-10-05 10:22:03 +01:00
|
|
|
format( Stream, ':- ~q:~q.~n', [M,PredDef])
|
2014-07-16 17:55:16 +01:00
|
|
|
),
|
|
|
|
fail.
|
2014-10-07 01:35:41 +01:00
|
|
|
'$list_clauses'(Stream, _M, _Pred) :-
|
2014-07-16 17:55:16 +01:00
|
|
|
nl( Stream ),
|
|
|
|
fail.
|
2002-10-01 21:28:47 +01:00
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
2014-07-16 18:59:03 +01:00
|
|
|
'$flags'(Pred,M,Flags,Flags),
|
2003-12-27 00:38:53 +00:00
|
|
|
% has to be dynamic, source, or log update.
|
|
|
|
Flags /\ 0x08402000 =\= 0,
|
2009-03-26 08:12:24 +00:00
|
|
|
'$clause'(Pred, M, Body, _),
|
2014-07-16 17:55:16 +01:00
|
|
|
'$current_module'(Mod),
|
|
|
|
( M \= Mod -> H = M:Pred ; H = Pred ),
|
|
|
|
'$portray_clause'(Stream,(H:-Body)),
|
2001-04-09 20:54:03 +01:00
|
|
|
fail.
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred portray_clause(+ _S_,+ _C_)
|
|
|
|
|
|
|
|
Write clause _C_ on stream _S_ as if written by listing/0.
|
|
|
|
*/
|
2001-05-28 20:54:53 +01:00
|
|
|
portray_clause(Stream, Clause) :-
|
2010-03-27 15:06:36 +00:00
|
|
|
copy_term_nat(Clause, CopiedClause),
|
|
|
|
'$portray_clause'(Stream, CopiedClause),
|
2002-10-01 21:28:47 +01:00
|
|
|
fail.
|
|
|
|
portray_clause(_, _).
|
2001-05-28 20:54:53 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred portray_clause(+ _C_)
|
|
|
|
|
|
|
|
Write clause _C_ as if written by listing/0.
|
|
|
|
|
|
|
|
*/
|
2002-10-01 21:28:47 +01:00
|
|
|
portray_clause(Clause) :-
|
|
|
|
current_output(Stream),
|
2014-07-16 17:55:16 +01:00
|
|
|
portray_clause(Stream, Clause).
|
2002-10-01 21:28:47 +01:00
|
|
|
|
|
|
|
'$portray_clause'(Stream, (Pred :- true)) :- !,
|
|
|
|
'$beautify_vars'(Pred),
|
2014-10-06 00:00:42 +01:00
|
|
|
format(Stream, '~q.~n', [Pred]).
|
2002-10-31 17:35:24 +00:00
|
|
|
'$portray_clause'(Stream, (Pred:-Body)) :- !,
|
2001-04-09 20:54:03 +01:00
|
|
|
'$beautify_vars'((Pred:-Body)),
|
2014-10-06 00:00:42 +01:00
|
|
|
format(Stream, '~q :-', [Pred]),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(Body, 3, ',', Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '.~n', []).
|
2014-10-06 00:00:42 +01:00
|
|
|
'$portray_clause'(Stream, Pred) :-
|
2002-10-31 17:35:24 +00:00
|
|
|
'$beautify_vars'(Pred),
|
2014-10-06 00:00:42 +01:00
|
|
|
format(Stream, '~q.~n', [Pred]).
|
2002-10-01 21:28:47 +01:00
|
|
|
|
|
|
|
'$write_body'(X,I,T,Stream) :- var(X), !,
|
|
|
|
'$beforelit'(T,I,Stream),
|
|
|
|
writeq(Stream, '_').
|
|
|
|
'$write_body'((P,Q), I, T, Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(P,I,T, Stream),
|
2006-03-24 17:15:20 +00:00
|
|
|
put(Stream, 0',),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(Q,I,',',Stream).
|
|
|
|
'$write_body'((P->Q;S),I,_, Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c(',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
I1 is I+2,
|
|
|
|
'$write_body'(P,I1,'(',Stream),
|
2014-07-16 17:55:16 +01:00
|
|
|
format(Stream, '~n~*c->',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_disj'((Q;S),I,I1,'->',Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c)',[I,0' ]).
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'((P->Q|S),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c(',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
I1 is I+2,
|
|
|
|
'$write_body'(P,I,'(',Stream),
|
2014-07-16 17:55:16 +01:00
|
|
|
format(Stream, '~n~*c->',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_disj'((Q|S),I,I1,'->',Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c)',[I,0' ]).
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'((P->Q),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c(',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
I1 is I+2,
|
|
|
|
'$write_body'(P,I1,'(',Stream),
|
2014-07-16 17:55:16 +01:00
|
|
|
format(Stream, '~n~*c->',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(Q,I1,'->',Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c)',[I,0' ]).
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'((P;Q),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c(',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
I1 is I+2,
|
|
|
|
'$write_disj'((P;Q),I,I1,'->',Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c)',[I,0' ]).
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'((P|Q),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c(',[I,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
I1 is I+2,
|
|
|
|
'$write_disj'((P|Q),I,I1,'->',Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c)',[I,0' ]).
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(X,I,T,Stream) :-
|
|
|
|
'$beforelit'(T,I,Stream),
|
2002-10-27 18:11:01 +00:00
|
|
|
writeq(Stream,X).
|
2002-10-01 21:28:47 +01:00
|
|
|
|
|
|
|
|
2014-10-06 00:00:42 +01:00
|
|
|
|
|
|
|
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_disj'((Q;S),I0,I,C,Stream) :- !,
|
|
|
|
'$write_body'(Q,I,C,Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c;',[I0,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_disj'(S,I0,I,';',Stream).
|
|
|
|
'$write_disj'((Q|S),I0,I,C,Stream) :- !,
|
|
|
|
'$write_body'(Q,I,C,Stream),
|
2006-03-24 16:26:31 +00:00
|
|
|
format(Stream, '~n~*c|',[I0,0' ]),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_disj'(S,I0,I,'|',Stream).
|
2006-03-24 16:26:31 +00:00
|
|
|
'$write_disj'(S,_,I,C,Stream) :-
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(S,I,C,Stream).
|
|
|
|
|
|
|
|
|
2014-10-06 00:00:42 +01:00
|
|
|
'$beforelit'('(',_,Stream) :-
|
|
|
|
!,
|
|
|
|
format(Stream,' ',[]).
|
2006-03-24 16:26:31 +00:00
|
|
|
'$beforelit'(_,I,Stream) :- format(Stream,'~n~*c',[I,0' ]).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
'$beautify_vars'(T) :-
|
|
|
|
'$list_get_vars'(T,[],L),
|
|
|
|
msort(L,SL),
|
|
|
|
'$list_transform'(SL,0).
|
|
|
|
|
|
|
|
|
|
|
|
'$list_get_vars'(V,L,[V|L] ) :- var(V), !.
|
|
|
|
'$list_get_vars'(Atomic, M, M) :-
|
|
|
|
primitive(Atomic), !.
|
|
|
|
'$list_get_vars'([Arg|Args], M, N) :- !,
|
|
|
|
'$list_get_vars'(Arg, M, K),
|
|
|
|
'$list_get_vars'(Args, K, N).
|
|
|
|
'$list_get_vars'(Term, M, N) :-
|
|
|
|
Term =.. [_|Args],
|
|
|
|
'$list_get_vars'(Args, M, N).
|
|
|
|
|
|
|
|
'$list_transform'([],_) :- !.
|
2010-03-27 15:06:36 +00:00
|
|
|
'$list_transform'([X,Y|L],M) :-
|
|
|
|
X == Y,
|
|
|
|
X = '$VAR'(M),
|
|
|
|
!,
|
|
|
|
N is M+1,
|
2010-03-27 11:43:03 +00:00
|
|
|
'$list_transform'(L,N).
|
2010-03-27 15:06:36 +00:00
|
|
|
'$list_transform'('$VAR'(-1).L,M) :- !,
|
|
|
|
'$list_transform'(L,M).
|
|
|
|
'$list_transform'(_.L,M) :-
|
|
|
|
'$list_transform'(L,M).
|