Re: Command "listing" in SWI prolog
- From: Jan Wielemaker <jan@xxxxxxxxxxxxxxxxxxx>
- Date: 09 Jan 2006 15:07:05 GMT
On 2006-01-09, Erik Braun <qs206710n5sqn8r56s6pr3s980940ss3@xxxxxxxxxxxxxxxxx> wrote:
> This sounds to me like a FAQ, but I didn't find the answer:
>
> Is there a way in SWI Prolog to get the output of the listing command
> (pretty printed) without losing the variable names?
No. After compilation, whether in debug mode or not, the variable
names are lost. Depending your goal there may be alternatives though.
There is a read version that gets the variable names. To read a file
term-by-term and pretty print it you can use the following code:
================================================================
plpp(File) :-
open(File, read, In),
read_term(In, Term, [variable_names(Names)]),
call_cleanup(pp(Term, Names, In),
close(In)).
pp(end_of_file, _, _) :- !.
pp(Term, Names, In) :-
bind_names(Names),
portray_clause(Term),
read_term(In, Term2, [variable_names(Names2)]),
pp(Term2, Names2, In).
bind_names([]).
bind_names([Name = Var|T]) :-
Var = '$VAR'(Name),
bind_names(T).
================================================================
Of course you loose the comments :-( When I can find the time
I hope to write a program beautifier on the basis of this that
does maintain comments, variable names, etc.
There is also library(prolog_clause) which can get the variable
names of a clause. It uses the clause source info to re-read
the clause and find the variable names. This process isn't
entirely robust (asserted clauses have no clause info, but
also term_expansion and goal_expansion can have changed the
clause). On most code it does a reasonable job and it it
used by the source-level debugger.
Cheers --- Jan
.
- Prev by Date: Card game
- Next by Date: Re: Lists in Learn Prolog Now
- Previous by thread: Card game
- Next by thread: Re: Command "listing" in SWI prolog
- Index(es):