如何从命令行运行 SWI-Prolog? [英] How to run SWI-Prolog from the command line?

查看:34
本文介绍了如何从命令行运行 SWI-Prolog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法像这样创建一个名为 hello.pl 的 prolog 脚本:

Is there a way to just create a prolog script called hello.pl like this:

#!/usr/local/bin/swipl -q -s -t main

main:-
  write('Hello World
').

并且能够像这样从终端运行它?

And be able to run it from the terminal like this?

$ hello.pl
Hello World
$

当我这样做时,它给了我这个:

When I do that it gives me this:

hello.pl: line 3: main:-: command not found
hello.pl: line 4: syntax error near unexpected token `'Hello World
''
hello.pl: line 4: `  write('Hello World
').'

我可以通过在命令行上写这个来让它工作:

I am able to get it working by writing this on the command line:

$ swipl -q -f hello.pl -t main
Hello World
$

但是有没有办法将直接脚本作为可执行文件运行?

But is there a way to just run the straight script as an executable instead?

编辑

还没有能够让它发挥作用.这是@Boris 在他的回答中的评论中询问的命令的输出:

Haven't yet been able to get this to work. Here is the output from the commands @Boris asked in the comments in his answer:

$ ls -l
total 8
-rwxr-xr-x  1 viatropos  staff  235 Aug 26 20:28 example.pl
$ cat example.pl
#!/usr/local/bin/swipl

:- set_prolog_flag(verbose, silent).

:- initialization main.

main :-
    format('Example script~n'),
    current_prolog_flag(argv, Argv),
    format('Called with ~q~n', [Argv]),
    halt.
main :-
    halt(1).
$ which swipl
/usr/local/bin/swipl
$ swipl --version
SWI-Prolog version 6.6.6 for x86_64-darwin13.1.0
$ ./example.pl
./example.pl: line 3: syntax error near unexpected token `('
./example.pl: line 3: `:- set_prolog_flag(verbose, silent).'
$

我使用的是 Mac OSX 10.9.2,并通过 brew install swi-prolog --with-libarchive

I am on Mac OSX 10.9.2, and installed swipl with homebrew via brew install swi-prolog --with-libarchive

推荐答案

ISO 指令:初始化.这应该有效.

ISO directive: initialization. This should work.

:- initialization main.

main :-
  write('Hello World
').

edit 抱歉,我跳过了最有趣的细节.这是一个示例脚本,假设保存在 ~/test/main.pl

edit sorry, I skipped over most interesting details. Here is a sample script, let's say saved in ~/test/main.pl

#!/home/carlo/bin/swipl -f -q

:- initialization main.

main :-
  current_prolog_flag(argv, Argv),
  format('Hello World, argv:~w
', [Argv]),
  halt(0).

并使用

chmod +x ~/test/main.pl

然后我得到

~$ ~/test/main.pl
Hello World, argv:[]

~$ ~/test/main.pl as,dnj asdl
Hello World, argv:[as,dnj,asdl]

在脚本 main.pl 中,我使用了从没有管理员权限的源代码构建的 swipl 路径.SWI-Prolog 构建过程将 bin 和 lib 放在 ~/bin 和 ~/lib 下

In script main.pl, I used the swipl path that results from building from source without admin privileges. The SWI-Prolog build process put bin and lib under ~/bin and ~/lib

注意:-f 标志禁止加载初始化 ~/.plrc,这可能是对执行进行更严格控制"所必需的...

Note: the -f flag disables loading the initialization ~/.plrc, and this could be necessary to get more 'strict control' over execution...

我目前不确定文档页面是否与当前的 SW 状态保持同步.来自一些邮件列表消息,以及我自己对重用thea,似乎命令行标志最近发生了变化...

I'm currently unsure if the documentation page is up-to-date with current SW status. From some mailing list message, and my own efforts to reuse thea, seems that command line flags changed recently...

这篇关于如何从命令行运行 SWI-Prolog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆