【问题标题】:Tuprolog and defining infix operatorsTuprolog 和定义中缀运算符
【发布时间】:2013-06-03 11:46:06
【问题描述】:

所以我有一些序言...

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) 抛出异常 { Prolog 引擎 = new 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());

}

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

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

【问题讨论】:

  • 也许 Tuprolog 不支持,注意它似乎有一个operator management class。该错误是一个词法(tokenizer)错误,所以它显然没有期待新的标记,并且没有在它的词法分析器中注册它。

标签: java prolog tuprolog


【解决方案1】:

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

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

edit 我尝试使用 2p.jar,这让我发现了问题。需要引用运算符的原子:

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

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

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

【讨论】:

  • 这是个好主意,感谢您的回答,但恐怕没有帮助:(
  • SWI 在这里并不是“过于宽松”。既不需要引号也不需要括号。 :- op(35, xfx, +=). 是有效的 ISO Prolog 语法。
猜你喜欢
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多