gnu-prolog - 初始化谓词失败

时间:2017-10-31 16:32:16

标签: command-line gnu-prolog

在编译和运行下面的代码(pl_check_input.pl)时,我得到了" user directive failed"关于": - 初始化......"线

:- dynamic(doit/0).
:- initialization(doit).
:- include(head).

doit :-
    readFB(user_input),
    writeFB,
    halt.

:- include(tail).

$ gplc --no-del-temp --no-top-level pl_check_input.pl
$ ./pl_check_input <fb1 >fb2
warning: /home/tarvydas/Dropbox/Projects/vsh/pl-vsh/pl_check_input.pl:2: user directive failed

如果我删除违规行

:- dynamic(doit/0).
:- include(head).

doit :-
    readFB(user_input),
    writeFB,
halt.

:- include(tail).

$ gplc --no-del-temp --no-top-level pl_check_input.pl
$ ./pl_check_input <fb1 >fb2
Warning: no initial goal executed
   use a directive :- initialization(Goal)
   or remove the link option --no-top-level (or --min-bips or --min-size)

任何见解都会非常受欢迎。

最终,我从REPL开始运行此代码,但我想将它放在Linux管道脚本中,并删除顶级/ 0附带的各种标题行。

1 个答案:

答案 0 :(得分:0)

“没关系”,结果证明这是一个缺少规则,产生了一个非常误导性的错误信息。要复制错误,请创建junk.pl:

favourite_candy_boys_girls = [['Chips' , 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4]]
max(favourite_candy_boys_girls, key=lambda x: x[2])[0]
# 'Chocolate'

并创建文件head.pl:

:- initialization(main).
:- include(head).

main :-
    readFB(user_input),
    writeFB,
    halt.

:- include(tail).

创建文件tail.pl(第一个注释掉的行是缺少的规则)

:- dynamic(component/1) .
:- dynamic(edge/1) .

创建文件fb1a:

% writeterm(Term) :- current_output(Out), write_term(Out, Term, []), write(Out, '.'), nl.


writeFB :-
    forall(component(X), writeterm(component(X))),
    forall(edge(X), writeterm(edge(X))).

readFB(Str) :-
    read_term(Str,T0,[]),
    element(T0,Str).

element(end_of_file, _) :- !.
element(component(X), Str) :- !,
               asserta(component(X)),
               readFB(Str).
element(edge(X), Str) :- !,
               asserta(edge(X)),
               readFB(Str).

然后运行编译并执行命令:

component('pl_vsh') .
edge(e0) .

导致错误消息:

$ gplc junk.pl --no-top-level 
$ ./junk <fb1a >fb2