Completion [user]. with EOT symbol in Prolog

I am writing a wrapper for the Prolog interpreter used in another programming language. I do not go into details, but basically generates the corresponding Prolog program and passes the program to the interpreter from standard input. I am relatively new to Prolog.

The problem is that I could not find the specification for the term [user]. that reads the rules from standard input. In particular, how it determines the end of input.

The most intuitive way to achieve this is to send an EOT character (Ctrl-D), but it doesn't seem to work. Below is an illustrative example, replace ^ D with the actual EOT character. Suppose this file is saved in input.pl . I intend to generate such code programmatically and pass it to the background prolog via 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> cat input.pl | <prolog> , where <prolog> is any Prolog interpreter (swipl, yap, etc.), it does not seem to recognize ^ D. Why is this and how can I solve it? On terminal ^ D is working fine.

The above example should return "W = 6". SWI complains about Syntax error: illegal_character . Yap seems to ignore ^ D, just parse everything and return yes .

Of course, I can write the program to a temporary file and tell the interpreter to consult the file, but it is slow.

+5
source share
1 answer

Say end_of_file. instead of ^D This works in many implementations of Prolog because they are actually read using read/1 . Historically, from DECsystem 10 Prolog on, systems only had read/1 but no at_end_of_stream/0 and associated built-in predicates. In fact, some programmers used end_of_file. in the Prolog source file to allow the addition of arbitrary text to the Prolog source.

+5
source

Source: https://habr.com/ru/post/1273159/


All Articles