Reputation: 61
I've written the code below in Prolog:
go :- write(">>"), read(X), process(X).
And do various things with process/1. To catch any unknown commands I've added:
process(Y) :- write("unknown command.\n"), go.
The problem is when exiting SWI-Prolog by closing the window before the normal end of the program, it gets into an infinite loop. I tried to search for what is SWI-Prolog calling when exiting like that but couldn't find it in order to include it in the code. Any help on that part or an alternative workaround would be really appreciated. Thanks in advance!
Upvotes: 3
Views: 359
Reputation: 10102
You need to handle the term end_of_file
, too.
After the last Prolog term, that is when only layout or comments are read up to the end of the file or stream, read/1
produces the term end_of_file
.
Since (in many current systems) the action to perform when reading past end of file (on standard input) is reset
, also subsequent reads will produce this term. And thus your program loops while complaining that that it does not know the command end_of_file
.
Upvotes: 4