prologiso-prologsicstus-prolog

Peeking past end-of-file with SICStus Prolog


Suppose I have an empty file (imaginatively called empty) in the current directory and run the following query.

Using SICStus Prolog 4.7.1 (Intel x86-64):

| ?- open(empty,read,S,[eof_action(eof_code)]), 
     stream_property(S,end_of_stream(EOS0)),
     peek_char(S,_), stream_property(S,end_of_stream(EOS1)),
     get_char(S,_),  stream_property(S,end_of_stream(EOS2)),
     peek_char(S,_), stream_property(S,end_of_stream(EOS3)),
     peek_char(S,_), stream_property(S,end_of_stream(EOS4)),
     close(S).
S = '$stream'(140253160396048),
EOS0 = not,
EOS1 = at,
EOS2 = past,
EOS3 = at,
EOS4 = at ? 

With peek_char and get_char the stream property end_of_stream goes from not to at to past—I get that.

But another peek_char makes it step back from past to at—That got me:)

What's going on? I was expecting the property to remain at past...


Solution

  • This is a bug.

    Once a stream has reached past-end-of-stream (EOS2) it remains there (7.10.2.9) unless eof_action(reset) is used (7.10.2.11), which it is not in your example so EOS3 and EOS4 are wrong. The paragraph numbering is from the ISO Prolog standard.

    The bug is present in 4.7.1 (and likely for all earlier releases), for both text and binary streams. I will try to fix this for forthcoming 4.8. This has been fixed in forthcoming 4.8.