swi-prolog:在编译程序中用户输入之前删除“|:”

swi-prolog: remove '|:' before user input in compiled program

每当我用 swi-prolog 编译任何东西时,它都会在用户输入之前添加 |: 以表明你应该写一些东西,这很好,但我需要将这个程序的输出通过管道传递给另一个程序,最好没有 |:.

我的编译选项是:

swipl -nodebug -g true -O -q --toplevel=quiet --stand_alone=true -o main -c main.pl

你要说

prompt(_, '')

在你开始读写标准流之前在你的程序中的某个地方。来自 entry for prompt/2:

A prompt is printed if one of the read predicates is called and the cursor is at the left margin. It is also printed whenever a newline is given and the term has not been terminated. Prompts are only printed when the current input stream is user.

上面的调用只是将提示设置为空原子(忽略之前的提示)。例如,使用以下 prompt.pl:

:- current_prolog_flag(verbose, silent).
:- use_module(library(readutil)).

main :-
    read_line_to_codes(user_input, Line),
    format("~s~n", [Line]),
    halt.
main :- halt(1).

然后:

$ swipl -q --goal=main -o prompt -c prompt.pl
$ ./prompt
|:foobar
foobar
$
:- current_prolog_flag(verbose, silent).
:- use_module(library(readutil)).

main :-
    prompt(_, ''),
    read_line_to_codes(user_input, Line),
    format("~s~n", [Line]),
    halt.
main :- halt(1).

它消失了:

$ swipl -q --goal=main -o prompt -c prompt.pl
$ ./prompt
foobar
foobar
$

如果你包含了一个类似于我的第一个版本 prompt.pl 的程序和结果输出,那么更多人可能更容易理解你到底在问什么。