lur*_*ker 5 portability prolog swi-prolog gnu-prolog iso-prolog
我意识到这会有限制,但是有没有一种合理的方法可以在 Prolog 代码中放入条件指令,以便在 GNU 或 SWI 中合理地工作?我至少在考虑最简单的情况,即sumlist
SWI 和sum_list
GNU中的内置谓词在拼写上彼此不匹配。或者 SWI 有assert
但 GNU 没有。所以最好有这样的东西:
:- if($SWI).
SWI version of stuff
:- else.
GNU version of stuff
:- endif.
Run Code Online (Sandbox Code Playgroud)
或者干脆:
:- if(not_a_builtin(sumlist))
sumlist(L, S) :- sum_list(L, S).
:- endif.
Run Code Online (Sandbox Code Playgroud)
或者什么不是。两种语言中都存在条件指令,但似乎只是提供了做这种事情所需的条件。我可能错过了手动搜索没有出现的东西。
最新版本的 GNU Prolog 和 SWI-Prolog 都定义了一个名为的标志dialect
(顺便说一句,这是大多数 Prolog 系统实现的事实上的标准),您可以在条件编译指令中使用它:
$ gprolog
GNU Prolog 1.4.4 (64 bits)
Compiled Apr 23 2013, 17:24:33 with /opt/local/bin/gcc-apple-4.2
By Daniel Diaz
Copyright (C) 1999-2013 Daniel Diaz
| ?- current_prolog_flag(dialect, Dialect).
Dialect = gprolog
yes
$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.3.16-6-g9c0199c-DIRTY)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
?- current_prolog_flag(dialect, Dialect).
Dialect = swi.
Run Code Online (Sandbox Code Playgroud)
因此,简单地写一些类似的东西:
:- if(current_prolog_flag(dialect, swi)).
% SWI-Prolog specific code
:- elif(current_prolog_flag(dialect, gprolog)).
% GNU Prolog specific code
:- else.
% catchall code
:- endif.
Run Code Online (Sandbox Code Playgroud)