Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End_of_stream property after opening empty file

I'm running the following query in a directory having an empty file empty:

?- member(EOF_action,[error,eof_code,reset]),
   open(empty,read,S,[eof_action(EOF_action)]),
   stream_property(S,end_of_stream(EOS)),
   close(S).

Here's what I get from different Prolog systems for EOS:

Prolog system eof_action(error) eof_action(eof_code) eof_action(reset)
SICStus Prolog not not not
GNU Prolog at at not
Scryer Prolog at at at
Traella Prolog at at not
SWI-Prolog not not not

It appears that above systems behave quite differently in this particular corner case.

Does the ISO-Prolog standard specify which behaviour is right for conforming processors?

like image 518
repeat Avatar asked Oct 24 '25 10:10

repeat


2 Answers

The relation between stream position and end position:
Let S be a input stream of size n. Let p be a position in the source of the stream S.

  • The stream position is not end of stream if p < n.
  • The stream position is at end of stream if p = n.
  • The stream position is past end of stream if p > n.

On an actual implementation the stream position may be a couple (q,e) where q is the position in the source and e is in {not, at, past} such that:

  • The stream position is not end of stream if and only if q < n and e = not.
  • The stream position is at end of stream if and only if q = n and e = at.
  • The stream position is past end of stream if and only if q = n and e = past.

This is given by:

7.10.2.13
end_of_stream(E) - If the stream position is end-of-stream then E is unified with at ...

And with:

7.10.1.1
read - Input. ... input shall start at the beginning of that source.

Knowing this, the stream position is at end of stream when opening the empty file.

Either stream_property/2 has an issue or the stream is in an invalid state.

A better question could be how is a conforming system supposed to handle end of stream?

And test the empty file and non-empty file for different engines.

On the comment of the question:
There is a note on the standard that the predicate peek_char/2 leaves unaltered the stream position so query like stream_property(S, end_of_stream(Eos0)), peek_char(S, _), stream_property(S, end_of_stream(Eos)), Eos0 = Eos should succeed.

like image 188
notoria Avatar answered Oct 27 '25 01:10

notoria


7.10.2.9 End position of a stream
When all a stream S has been input (for example by
get_byte/2 or read_term/3) S has a stream position
end-of-stream. ...

Does this include empty files, where nothing has been input, although nothing is all we have? Thinking of VSAM ... This remains pretty ambiguous no matter how we twist and turn it.

More important is what happens if we peek_char/1 or get_char/1 any further.

And even worse whether or not at_end_of_stream/0/1 and stream_property(S, end_of_stream(Eos)) are the same, at least for detecting the end the first time.

As a quick check enter

?- at_end_of_stream.

(There are two factions, two who fail and those who prompt. In the past there was much more prompting)

like image 24
false Avatar answered Oct 27 '25 00:10

false