从bash脚本运行Prolog命令

时间:2019-12-08 18:58:32

标签: bash prolog

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

通过在bash脚本中输入以下swipl << writeln('Hello'),似乎可以从bash脚本运行内置的Prolog命令,例如“写行”命令。但是,如果我尝试对我定义的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).   

1 个答案:

答案 0 :(得分:2)

您可以使用<<<选项,而不用使用-gswipl -s file -g "goal,halt."halt是必需的,否则SWI将不会终止。

此外,您可以编写如下的bash脚本:

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

和谓词(在这种情况下为test/1)将被1调用,就像test(1)