【问题标题】:Running Prolog commands from a bash script从 bash 脚本运行 Prolog 命令
【发布时间】:2019-12-08 18:58:32
【问题描述】:

要启动加载我的程序的 Prolog shell,我只需在终端中输入 swipl app.pl。然后,为了使用文本文件参数调用app.pl(Prolog 程序)中定义的谓词(函数)verify(InputFile),我编写了verify('path/to/file.txt')。是否可以编写一个执行上述所有任务的 bash 脚本?

通过在 bash 脚本中输入以下 swipl << writeln('Hello'),似乎可以从 bash 脚本运行内置的 Prolog 命令,例如“write line”命令。但是,如果我尝试对我定义的 verify 谓词执行相同操作,我最终会出现以下错误。

输入

Input: swipl app.pl <<< "verify('tests/unit/af-test1.txt')."

输出

true
ERROR: Type error: `character_code' expected, found `-1' (an integer)
ERROR: In:
ERROR:   [11] char_code(_2720,-1)
ERROR:   [10] '$in_reply'(-1,'?h') at /usr/local/Cellar/swi-prolog/8.0.3_1/libexec/lib/swipl/boot/init.pl:848

对于上下文,verify/1 谓词由以下代码定义。

verify(Input) :-
   see(Input), read(Transitions), read(Labling), 
   read(State), read(Formula), seen,
   check(Transitions, Labling, State, [], Formula).   

【问题讨论】:

  • 也许您的问题出在验证谓词中...您可以发布代码吗?

标签: bash prolog


【解决方案1】:

您可以通过以下方式使用-g 选项,而不是使用&lt;&lt;&lt;swipl -s file -g "goal,halt."。需要halt,否则SWI不会终止。

此外,您可以像这样编写一个 bash 脚本:

VARIABLE=1                                                                                                            
swipl -s test -g "test($VARIABLE),halt."   

谓词,在本例中为test/1,将使用1 调用,如test(1)

【讨论】:

  • 如果目标失败或抛出错误,最好使用swipl -s test -g "test($VARIABLE)" -t halt 获取非零退出代码。
  • 这很好用,但是只要test 谓词返回true,就可以将true 打印到控制台。目前,除非test 返回false,否则不会向控制台打印任何内容?
猜你喜欢
  • 2015-01-09
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 2016-10-20
  • 2018-08-19
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
相关资源
最近更新 更多