For Programmers: Free Programming Magazines  


Home > Archive > Prolog > February 2006 > Command "listing" in SWI prolog









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Command "listing" in SWI prolog
Erik Braun

2006-01-10, 4:10 am

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?

thank you,
Erik
Jan Wielemaker

2006-01-10, 4:10 am

On 2006-01-09, Erik Braun <qs206710n5sqn8r56s6pr3s980940ss3@minet.uni-jena.de> 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
Erik Braun

2006-01-11, 7:56 am

Jan Wielemaker <jan@nospam.ct.xs4all.nl> wrote:


[...][color=darkred]
> 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:


thank you, this fits my needs.
I am always astonished, how nifty your prolog code is.

This syntax is new for me:
> Var = '$VAR'(Name),


Can you point me to the right chapter in the documantation for this?

thank you,
Erik
Jan Wielemaker

2006-01-11, 7:56 am

On 2006-01-11, Erik Braun <qs206710n5sqn8r56s6pr3s980940ss3@minet.uni-jena.de> wrote:
> Jan Wielemaker <jan@nospam.ct.xs4all.nl> wrote:
>
>
> [...]
>
> thank you, this fits my needs.
> I am always astonished, how nifty your prolog code is.
>
> This syntax is new for me:
>
> Can you point me to the right chapter in the documantation for this?


It is an extension to the normal Prolog convention of printing
'$VAR'(1) as `A', '$VAR'(2) as `B', etc. It is described with
write_term/2, option numbervars(Bool). I can't blame you for not
finding it :-)

Cheers --- Jan
Marco

2006-02-01, 7:03 pm

Hi people,
I don't know if my problem is related to this one:

I have a file that I consult at every start, with a lot of objects like
these:
object(stone, 100, [s(2, n), s(0, n), s(1, n)]).
object(apple, 100, [s(2, n), s(0, n), s(1, s)]).

sometime I have to _modify_ one of this object, I don't know if Prolog
permit to modify files at runtime, so I have read about listing, but
don't know how to do.

Many Thanx
Marco
Walter

2006-02-02, 7:04 pm

there is something like "ensure_loaded" in SICStus -- if you use that to
load your file and make sure a call to it is in your execution path, you
will get the latest.
Walter

"Marco" <cimmo@libero.it> wrote in message
news:43e0fc0e$0$351$5fc30a8@news.tiscali.it...
> Hi people,
> I don't know if my problem is related to this one:
>
> I have a file that I consult at every start, with a lot of objects like
> these:
> object(stone, 100, [s(2, n), s(0, n), s(1, n)]).
> object(apple, 100, [s(2, n), s(0, n), s(1, s)]).
>
> sometime I have to _modify_ one of this object, I don't know if Prolog
> permit to modify files at runtime, so I have read about listing, but
> don't know how to do.
>
> Many Thanx
> Marco



Marco

2006-02-08, 9:29 am

Walter ha scritto:
> there is something like "ensure_loaded" in SICStus -- if you use that to
> load your file and make sure a call to it is in your execution path, you
> will get the latest.
> Walter


ok thanx,
and for the problem to modify a term in a file how can I do? I have to
re-write the entire file?

Thanx
Marco
Jan Wielemaker

2006-02-08, 9:29 am

On 2006-02-03, Marco <cimmo@libero.it> wrote:
> Walter ha scritto:
>
> ok thanx,
> and for the problem to modify a term in a file how can I do? I have to
> re-write the entire file?


One way to do it is to have a `journal' file. Each operation (assert/retract)
you do on a predicate you _append_ to a file. To restore the state you simply
read the operations from the file and replay them. Safe and easy. If the
journal gets too long you can write a clean one with only asserts representing
the current state to a new file and at the end atomically move it over the
journal file. Works for any Prolog system.

--- Jan
Marco

2006-02-08, 9:29 am

Jan Wielemaker ha scritto:
>
> One way to do it is to have a `journal' file. Each operation (assert/retract)
> you do on a predicate you _append_ to a file. To restore the state you simply
> read the operations from the file and replay them. Safe and easy. If the
> journal gets too long you can write a clean one with only asserts representing
> the current state to a new file and at the end atomically move it over the
> journal file. Works for any Prolog system.


Exactly the thing I'm doing now, but some preficate may _change_ (they
aren't new), and I cannot _just_ append.

thanx
Marco
Jan Wielemaker

2006-02-08, 9:29 am

On 2006-02-06, Marco <cimmo@libero.it> wrote:
> Jan Wielemaker ha scritto:
>
> Exactly the thing I'm doing now, but some preficate may _change_ (they
> aren't new), and I cannot _just_ append.


What do you mean by _change_? The only things you can do to a predicate
are assert and retract. So you get something along the lines below. ok,
the file may contain assert(foo(bar)) followed by retract(foo(bar)). You
need a bit more code to re-create a clean journal if there is too much
garbage. Note that if you want to be crash-proof you must add
flush_output after adding something to the journal. Unfortunately that
may significantly slow down.

Even if there are other changes, simply add some more journalled_xyz
predicates and some more cases to the reloading code.

Cheers --- Jan

journalled_assert(Term) :-
assert(Term),
format(journal, 'assert(~q).~n', [Term]).

journalled_retract(Term) :-
retract(Term),
format(journal, 'retract(~q).~n', [Term]).

reload(File) :-
open(File, read, In),
read(In, T0),
call_cleanup(reload(T0, In), close(In)).

reload(end_of_file, _) :- !.
reload(Term, In) :-
reload_term(Term),
read(In, T2),
reload(T2, In).

reload_term(assert(X)) :-
assert(X).
reload_term(retract(X)) :-
once(retract(X)).

Marco

2006-02-08, 9:29 am

Jan Wielemaker ha scritto:
> What do you mean by _change_?


I've a file with lots of objects that I consult at the start of my
program like this:

object(stone, 100, [s(2, n), s(0, n), s(1, n)]).
object(mountain, 150, [s(2, n), s(0, s), s(1, s)]).


my problem is sometime I need to add some new object and this is easy
and just done, the other problem is when I have to change one object,
for example hte mountain from 150 to 200, and consult the file again.

How can I do?

thanx
Marco
Jan Wielemaker

2006-02-08, 9:29 am

On 2006-02-06, Marco <cimmo@libero.it> wrote:
> Jan Wielemaker ha scritto:
>
> I've a file with lots of objects that I consult at the start of my
> program like this:
>
> object(stone, 100, [s(2, n), s(0, n), s(1, n)]).
> object(mountain, 150, [s(2, n), s(0, s), s(1, s)]).
>
>
> my problem is sometime I need to add some new object and this is easy
> and just done, the other problem is when I have to change one object,
> for example hte mountain from 150 to 200, and consult the file again.


Maybe time to read replies carefully. Most filesystems do not have
a way to delete something in the middle of a file. Overwriting won't
really help as well. In theory you can change 150 to 200, but what about
150 to 1500? You'de have to insert in the middle. That simply won't
work.

You can however, like I tried to explain you twice, add to the end of
the file a term that indicates the change and write your own reader to
reload the file. In this case you could append a term like

setarg(2, object(mountain, 150, [s(2, n), s(0, s), s(1, s)]), 200).

This approach can be adjusted to suit your needs. If you do not want to
go that way, the only reasonable option is to use an external database.
SWI-Prolog has an ODBC interface as well as a BerkeleyDB interface (latter
only as source due to license issues with BerkeleyDB).

--- Jan

Marco

2006-02-08, 9:29 am

In-Reply-To: <slrnduf3tq.9oc.jan@ct.lan>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 0606-1, 06/02/2006), Outbound message
X-Antivirus-Status: Clean
Lines: 9
Message-ID: <43e7a358$0$330$5fc30a8@news.tiscali.it>
Organization: Tiscali SpA
NNTP-Posting-Host: 84.220.99.239
X-Trace: 1139254105 news.tiscali.it 330 84.220.99.239:1049
X-Complaints-To: abuse@tiscali.it
Xref: number1.nntp.dca.giganews.com comp.lang.prolog:29794

Jan Wielemaker ha scritto:

> Maybe time to read replies carefully.


I have read, but you are talking with a person that doens't know
anything about prolog, so I haven't understood all the things.

Now I will try, thanx for now
Marco
Jan Wielemaker

2006-02-08, 9:29 am

On 2006-02-06, Marco <cimmo@libero.it> wrote:
> Jan Wielemaker ha scritto:
>
>
> I have read, but you are talking with a person that doens't know
> anything about prolog, so I haven't understood all the things.


Sorry. Instead of simply ignoring half the reply I may suggest to
indicate what you do not understand. It helps people who spend time
trying to explain things.

Cheers --- Jan
Marco

2006-02-09, 7:03 pm

Jan Wielemaker ha scritto:
>
> Sorry. Instead of simply ignoring half the reply I may suggest to
> indicate what you do not understand. It helps people who spend time
> trying to explain things.
>
> Cheers --- Jan


sorry, good for next time ;)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com