Tuprolog和定义中缀运算符

时间:2013-06-03 11:46:07

标签: java prolog tuprolog

所以我有一些序言......

cobrakai$more operator.pl 
be(a,c).
:-op(35,xfx,be).



+=(a,c).
:-op(35,xfx,+=).
cobrakai$

定义了一些中缀运算符。我使用SWI prolog运行它并获得以下(完全预期的)结果

?- halt.
cobrakai$swipl -s operator.pl 
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,992 bytes
% /Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/operator.pl compiled 0.00 sec, 992 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.5)
Copyright (c) 1990-2011 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).

?- be(a,c).
true.

?- a be c.
true.

?- +=(a,c).
ERROR: toplevel: Undefined procedure: (+=)/2 (DWIM could not correct goal)
?- halt.
cobrakai$swipl -s operator.pl 
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,992 bytes
% /Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/operator.pl compiled 0.00 sec, 1,280 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.5)
Copyright (c) 1990-2011 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).

?- be(a,c).
true.

?- a be c.
true.

?- +=(a,c).
true.

?- a += c.
true.

?- halt.

但是,当我使用Tuprolog从Java处理相同的文件时(使用以下代码)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo;
import alice.tuprolog.Theory;

public class Testinfixoperatorconstruction {
    public static void main(String[] args) throws Exception {
        Prolog engine = new Prolog();
        engine.loadLibrary("alice.tuprolog.lib.DCGLibrary");
        engine.addTheory(new Theory(readFile("/Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/operator.pl")));
        SolveInfo info = engine.solve("be(a,c).");
        System.out.println(info.getSolution());
        info = engine.solve("a be c.");
        System.out.println(info.getSolution());
    }

    private static String readFile(String file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }
        return stringBuilder.toString();
    }
}

prolog文件无法解析 - 对'+ ='令牌失败。

Exception in thread "main" alice.tuprolog.InvalidTheoryException: Unexpected token '+='
    at alice.tuprolog.TheoryManager.consult(TheoryManager.java:193)
    at alice.tuprolog.Prolog.addTheory(Prolog.java:242)
    at Testinfixoperatorconstruction.main(Testinfixoperatorconstruction.java:14)

我们可以尝试稍微不同的方法,直接在java代码中添加运算符......

public static void main(String [] args)throws Exception {     Prolog引擎=新Prolog();     engine.loadLibrary( “alice.tuprolog.lib.DCGLibrary”);

engine.getOperatorManager().opNew("be", "xfx", 35);
engine.getOperatorManager().opNew("+=", "xfx", 35);
engine.addTheory(new Theory(
        readFile("/Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/operator2.pl")));
SolveInfo info = engine.solve("be(a,c).");
System.out.println(info.getSolution());
info = engine.solve("a be c.");
System.out.println(info.getSolution());

}

但我们得到同样的错误...... :(

谁能告诉我为什么会这样? (也欢迎解决方案)。

1 个答案:

答案 0 :(得分:0)

解析指令时,SWI-Prolog可能过于宽松。尝试在括号之间包含运算符:

:-op(35,xfx,(+=)).

编辑我尝试使用2p.jar,这让我发现了问题。需要引用运算符'atom:

:-op(35,xfx, '+=').

X += Y.
p :- a += b.

交互式2p控制台接受此语法。请注意,2p.jar默认情况下会加载tuprolog libraries