Reputation: 194
I am writing a wrapper for a Prolog interpreter used from a different programming language. I don't go into detail, but it basically generates a conforming Prolog program and feeds the program to an interpreter from the standard input. I am relatively new to Prolog.
The problem is, I could not find the specification for [user].
term that reads the rules from the standard input. Specifically, how it detects the end of the input.
The most intuitive way to achieve this is to send an EOT character (Ctrl-D), but it does not seem to work. Below is an illustrative example, replace the ^D with an actual EOT character. Assume this file is saved in input.pl
. I intend to generate such code programatically and feed it to the background prolog process through the standard input.
[user].
factorial(0,1).
factorial(N,F) :- (>(N,0),is(N1,-(N,1)),factorial(N1,F1),is(F,*(N,F1))).
^D
factorial(3,W).
When I run cat input.pl | <prolog>
where <prolog>
is whatever Prolog interpreter (swipl, yap, etc.), it does not seem to recognize ^D. Why is this and how do I solve this? On the terminal ^D works fine.
Above example is supposed to return "W=6". SWI complains Syntax error: illegal_character
. Yap seems to ignore ^D, just parse everything and return yes
.
Of course I can write the program onto a temporary file and tell the interpreter to consult the file, but it is slow.
Upvotes: 5
Views: 217
Reputation: 10120
Say end_of_file.
in place of ^D
. This works in many Prolog implementations because their actual reading is performed with read/1
. Historically, from DECsystem 10 Prolog on, systems only had read/1
but did not have at_end_of_stream/0
and related built-in predicates. In fact, some programmers used end_of_file.
within a Prolog source file to permit appending arbitrary text to a Prolog source.
Upvotes: 5