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 *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/* listing : Listing clauses in the database
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
listing :-
|
2002-10-01 21:28:47 +01:00
|
|
|
current_output(Stream),
|
2001-11-15 00:01:43 +00:00
|
|
|
'$current_module'(Mod),
|
|
|
|
'$current_predicate_no_modules'(Mod,_,Pred),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$list_clauses'(Stream,Mod,Pred).
|
2001-04-09 20:54:03 +01:00
|
|
|
listing.
|
|
|
|
|
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
listing(V) :-
|
2002-10-01 21:28:47 +01:00
|
|
|
current_output(Stream),
|
2001-11-15 00:01:43 +00:00
|
|
|
'$current_module'(M),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$listing'(V,M,Stream).
|
2001-10-03 14:39:16 +01:00
|
|
|
|
2002-10-01 21:28:47 +01:00
|
|
|
'$listing'(V,Mod,Stream) :- var(V), !,
|
|
|
|
'$current_predicate_no_modules'(Mod,_,Pred),
|
|
|
|
'$list_clauses'(Stream,Mod,Pred).
|
2002-10-08 06:35:09 +01:00
|
|
|
'$listing'(M:V,_,Stream) :- !,
|
|
|
|
'$listing'(V,M,Stream).
|
2002-10-01 21:28:47 +01:00
|
|
|
'$listing'([],_,_) :- !.
|
|
|
|
'$listing'([X|Rest], M, Stream) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$listing'(X, M, Stream),
|
|
|
|
'$listing'(Rest, M, Stream).
|
|
|
|
'$listing'(X, M, Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$funcspec'(X,Name,Arity),
|
2001-11-15 00:01:43 +00:00
|
|
|
'$current_predicate_no_modules'(M,Name,Pred),
|
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).
|
|
|
|
'$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
|
|
|
|
2002-10-01 21:28:47 +01:00
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
|
|
|
( '$recordedp'(M:Pred,_,_) -> nl(Stream) ),
|
2001-04-09 20:54:03 +01:00
|
|
|
fail.
|
2002-10-01 21:28:47 +01:00
|
|
|
'$list_clauses'(Stream, M, Pred) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
'$recordedp'(M:Pred,(Pred:-Body),_),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$portray_clause'(Stream,(Pred:-Body)),
|
2001-04-09 20:54:03 +01:00
|
|
|
fail.
|
|
|
|
|
2001-05-28 20:54:53 +01:00
|
|
|
portray_clause(Stream, Clause) :-
|
2002-10-01 21:28:47 +01:00
|
|
|
'$portray_clause'(Stream, Clause),
|
|
|
|
fail.
|
|
|
|
portray_clause(_, _).
|
2001-05-28 20:54:53 +01:00
|
|
|
|
2002-10-01 21:28:47 +01:00
|
|
|
portray_clause(Clause) :-
|
|
|
|
current_output(Stream),
|
|
|
|
'$portray_clause'(Stream, Clause),
|
|
|
|
fail.
|
|
|
|
portray_clause(_).
|
|
|
|
|
|
|
|
'$portray_clause'(Stream, (Pred :- true)) :- !,
|
|
|
|
'$beautify_vars'(Pred),
|
2002-10-27 18:11:01 +00:00
|
|
|
writeq(Stream, Pred),
|
|
|
|
'$format'(Stream, ".~n", []).
|
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)),
|
2002-10-27 18:11:01 +00:00
|
|
|
writeq(Stream, Pred),
|
|
|
|
'$format'(Stream, " :-", []),
|
2002-10-01 21:28:47 +01:00
|
|
|
'$write_body'(Body, 3, ',', Stream),
|
|
|
|
'$format'(Stream, ".~n", []).
|
2002-10-31 17:35:24 +00:00
|
|
|
'$portray_clause'(Stream, Pred) :- !,
|
|
|
|
'$beautify_vars'(Pred),
|
|
|
|
writeq(Stream, Pred),
|
|
|
|
'$format'(Stream, ".~n", []).
|
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),
|
|
|
|
put(Stream, ","),
|
|
|
|
'$write_body'(Q,I,',',Stream).
|
|
|
|
'$write_body'((P->Q;S),I,_, Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$format'(Stream, "~n~*c(",[I,0' ]),
|
|
|
|
I1 is I+2,
|
|
|
|
'$write_body'(P,I1,'(',Stream),
|
|
|
|
'$format'(Stream, " ->",[]),
|
|
|
|
'$write_disj'((Q;S),I,I1,'->',Stream),
|
|
|
|
'$format'(Stream, "~n~*c)",[I,0' ]).
|
|
|
|
'$write_body'((P->Q|S),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$format'(Stream, "~n~*c(",[I,0' ]),
|
|
|
|
I1 is I+2,
|
|
|
|
'$write_body'(P,I,'(',Stream),
|
|
|
|
'$format'(Stream, " ->",[]),
|
|
|
|
'$write_disj'((Q|S),I,I1,'->',Stream),
|
|
|
|
'$format'(Stream, "~n~*c)",[I,0' ]).
|
|
|
|
'$write_body'((P->Q),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$format'(Stream, "~n~*c(",[I,0' ]),
|
|
|
|
I1 is I+2,
|
|
|
|
'$write_body'(P,I1,'(',Stream),
|
|
|
|
'$format'(Stream, " ->",[]),
|
|
|
|
'$write_body'(Q,I1,'->',Stream),
|
|
|
|
'$format'(Stream, "~n~*c)",[I,0' ]).
|
|
|
|
'$write_body'((P;Q),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$format'(Stream, "~n~*c(",[I,0' ]),
|
|
|
|
I1 is I+2,
|
|
|
|
'$write_disj'((P;Q),I,I1,'->',Stream),
|
|
|
|
'$format'(Stream, "~n~*c)",[I,0' ]).
|
|
|
|
'$write_body'((P|Q),I,_,Stream) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
!,
|
2002-10-01 21:28:47 +01:00
|
|
|
'$format'(Stream, "~n~*c(",[I,0' ]),
|
|
|
|
I1 is I+2,
|
|
|
|
'$write_disj'((P|Q),I,I1,'->',Stream),
|
|
|
|
'$format'(Stream, "~n~*c)",[I,0' ]).
|
|
|
|
'$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
|
|
|
|
|
|
|
|
|
|
|
'$write_disj'((Q;S),I0,I,C,Stream) :- !,
|
|
|
|
'$write_body'(Q,I,C,Stream),
|
|
|
|
'$format'(Stream, "~n~*c;",[I0,0' ]),
|
|
|
|
'$write_disj'(S,I0,I,';',Stream).
|
|
|
|
'$write_disj'((Q|S),I0,I,C,Stream) :- !,
|
|
|
|
'$write_body'(Q,I,C,Stream),
|
|
|
|
'$format'(Stream, "~n~*c|",[I0,0' ]),
|
|
|
|
'$write_disj'(S,I0,I,'|',Stream).
|
|
|
|
'$write_disj'(S,I0,I,C,Stream) :-
|
|
|
|
'$write_body'(S,I,C,Stream).
|
|
|
|
|
|
|
|
|
|
|
|
'$beforelit'('(',_,Stream) :- !, '$format'(Stream," ",[]).
|
|
|
|
'$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'([],_) :- !.
|
|
|
|
'$list_transform'([X,Y|L],M) :- X == Y, X = '$VAR'(M), !, N is M+1,
|
|
|
|
'$list_transform'(L,N).
|
|
|
|
'$list_transform'('$VAR'(-1).L,M) :- !, '$list_transform'(L,M).
|
2001-10-30 16:42:05 +00:00
|
|
|
'$list_transform'(_.L,M) :- '$list_transform'(L,M).
|
2002-11-11 17:38:10 +00:00
|
|
|
|