Tuprolog和定义中缀运算符(Tuprolog and defining infix opera

2019-10-18 05:33发布

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

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



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

它定义了一些缀运营商。 我运行它使用SWI序言和得到以下(完美预期)结果

?- 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();
    }
}

序言文件不解析 - 失败的“+ =”令牌。

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代码中添加操作...

公共静态无效的主要(字串[] args)抛出异常{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());

}

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

谁能告诉我为什么发生这种情况? (和解决方案也将受到欢迎)。

Answer 1:

SWI-Prolog的可能是太宽容在解析指令。 尝试封闭括号之间的运营商:

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

编辑我试着用2p.jar,这让我发现了问题。 需要引用运营商原子:

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

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

互动2P控制台接受此语法。 注意:默认情况下,负载tuprolog ,2p.jar



文章来源: Tuprolog and defining infix operators