command-line - 在命令行中使用 swipl 运行 prolog 代码

标签 command-line prolog server swi-prolog

我正在寻找 swiplperl -e 相似的功能
特别是,我想以这种方式运行 prolog 代码:

swipl --wanted-flag "fact(a). message:-writeln('hello')." -g "message" -t halt

这可能与
 swipl -f file -g "message" -t halt

其中 prolog 子句写在 file

我在服务器端运行 swipl,它将用户输入作为 prolog 子句,因此在服务器上编写文件不是一个好主意。

最佳答案

您可以做的一件事是使用 load_files/2 with the option stream ,并从标准输入加载,而不是从参数加载(我猜你仍然可以将入口点作为参数传递):

在文件中说 fromstdin.pl你有:

main :-
    load_files(stdin, [stream(user_input)]),
    current_prolog_flag(argv, [Goal|_]),
    call(Goal),
    halt.
main :- halt(1).

有了这个,你可以做到:
$ echo 'message :- format("hello~n").' | swipl -q -t main fromstdin.pl -- message
|: hello

@false 对 this answer 的评论这个问题会告诉你这是什么|:是,如果您想知道,但如果它让您烦恼,请执行以下操作:
$ echo 'message :- format("hello~n").' \
    | swipl -q -t main fromstdin.pl -- message \
    | cat
hello

反而。

这将让您从标准输入读取任何 Prolog 并从中调用任意谓词。这是否是一件聪明的事情,我不知道。如果有更简单的方法来实现同样的目标,我也不会感到惊讶。

关于command-line - 在命令行中使用 swipl 运行 prolog 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769440/

相关文章:

Node.js 服务器的 Socket.IO 解释?

bash - 如何对名称中包含空格的文件执行 shell "for"命令?

c++ - Windows命令行语法中的破折号是什么意思?

python - 嵌套参数解析器

linux - Bash 脚本,查找命令,使用通配符或正则表达式

golang 服务器中间件请求取消

prolog - 为什么我会出现无限循环 (Prolog)

prolog - "Squares"Prolog中的逻辑谜语解决方案

序言错误 : is/2: Arguments are not sufficiently instantiated

node.js - 如何在不重新启动 Node 服务器的情况下反射(reflect)语言环境更改 - i18n?