Dear All,
I have some doubts regarding the interaction of tabling and predicates with side-effects.
I've got unexpected results (at least for me...).
I wanted to fill up a table with the contents of a file, indexed by its position.
I've tried the following code:
:- import file_open/3 from file_io.
:- import file_close/1 from file_io.
:- import file_getbuf/4 from file_io.
:- import file_seek/4 from file_io.
:- table stream/3.
open_stream( FileName, FileDes ) :-
file_open( FileName, 0, FileDes ).
close_stream( FileDes ) :-
file_close( FileDes ).
stream( FileDes, N, Byte ) :-
% file_seek( FileDes, N, 0, _ ),
file_getbuf( FileDes, 1, Byte, Read ), Read = 1.
Here is an example:
[configuration loaded]
[sysinitrc loaded]
[packaging loaded]
XSB Version 2.0 (Gouden Carolus) of June 27, 1999
[x86-pc-windows; mode: optimal; engine: chat; scheduling: batched]
| ?- [stream0].
[Compiling .\stream0]
[stream0 compiled, cpu time used: 0.9300 seconds]
[stream0 loaded]
yes
| ?- open_stream('lixo.P',F).
F = 3;
no
| ?- stream(3,0,N).
N = c;
no
| ?- stream(3,1,N).
N = o;
no
| ?- stream(3,2,N).
N = n;
no
| ?- stream(3,0,N).
N = n;
no
The first result has been recomputed!!! (If I put the file_seek everything works fine, but XSB executes again the same code).
| ?- close_stream(3).
yes
| ?- stream(3,0,N).
N = n;
no
| ?- stream(3,1,N).
N = n;
no
| ?- stream(3,2,N).
N = n;
no
Then, instead of performing file input I tried with read predicate:
:- table stream/3.
open_stream( FileName, 0 ) :-
see( FileName ).
close_stream( 0 ) :-
seen.
stream( F, N, Byte ) :-
read( Byte ).
| ?- open_stream('lixo.P',F).
F = 0;
no
| ?- stream(0,0,N).
N = conn(inv4,i,inv1);
no
| ?- stream(0,1,N).
N = conn(and2,i1,inv1);
no
| ?- stream(0,2,N).
N = conn(and3,i2,inv1);
no
| ?- stream(0,0,N).
N = conn(inv4,i,inv1);
no
| ?- stream(0,2,N).
N = conn(and3,i2,inv1);
no
Now, it works as I expected! But if I use get0 instead of read the same problem occurs!
I also tried a version with assert and retract, and it also works.
:- table stream/3.
:- dynamic ctr/1.
open_stream( FileName, 0 ) :-
assert( ctr(0) ).
close_stream( 0 ) :-
retract( ctr(_) ).
stream( 0, N, Byte ) :-
retract(ctr(Byte)),
NewByte is Byte + 1,
assert( ctr(NewByte) ).
| ?- open_stream('lixo.P',F).
F = 0;
no
| ?- stream(0,0,N).
N = 0;
no
| ?- stream(0,1,N).
N = 1;
no
| ?- stream(0,2,N).
N = 2;
no
| ?- stream(0,0,N).
N = 0;
| ?- stream(0,2,N).
N = 2;
no
| ?- stream(0,1,N).
N = 1;
no
| ?- stream(0,3,N).
N = 3;
no
Logged In: YES
user_id=6694
I could not reproduce your error in the current system. Does
it still happen to you?