There are more cases where an EOF
signal
is interpreted. For example during a debugger break:
?- trace, a=b.
Call: (13) a=b ? break
% Break level 1
[1] ?- a=b.
false.
[1] ?- ^D
% Exit break level 1
Call: (13) a=b ?
One would need to check the SWI-Prolog system
and Emacs integration souce code how EOF
is
exactly detected. Besides get_code/1 and read_term/2
special values, some Prolog systems have also
stream inquiry built-ins such as at_end_of_stream/1.
But I never implemented it, since it should work the
same as this bootstrapping:
/* Is this bootstrapping supposed to work always? */
at_end_of_stream(S) :- peek_code(S, -1).
Its a little bit a can of worms especially in connection with
teletype terminal streams (tty), since as the example above
shows with the break, we expect that a terminal smoothly
allows to emit multiple ^D each ^D meaning that a section
of text has been completed, not meaning halt/0 in itself.
Inside Emacs ^D probably is never needed since Emacs
has a concept of buffer, and there are other means to
designate sections of text.
Edit 06.02.2025
But since EOF
signal is mapped to the special value end_of_file
by read_term/2 , this is very handy. When neither ^D
or ^Z
work in the terminal at hand, you can still use the atom:
?- [user].
|: p(a).
|: p(b).
|: end_of_file.
% user://1 compiled 0.02 sec, 2 clauses
true.
?- p(X).
X = a ;
X = b.
The atom end_of_file
works also inside a consulted file, across
many Prolog systems actually. But consult behaviour either via
[user]
or from a file is not covered by the ISO core standard.