Tuprolog 和定义中缀运算符 [英] Tuprolog and defining infix operators

查看:62
本文介绍了Tuprolog 和定义中缀运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些序言...

So I have some prolog...

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



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

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

Which defines some infix operators. I run it using SWI prolog and get the following (perfectly expected) results

?- 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 的相同文件时(使用以下代码)

However, when I use Tuprolog to process the same file from Java (using the following code)

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 文件不解析 - '+=' 标记失败.

The prolog file does not parse - failing on the '+=' token.

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代码中添加运算符...

We can try a slightly different approach, adding the operator directly in the java code with...

public static void main(String[] args) 抛出异常 {序言引擎=新序言();engine.loadLibrary("alice.tuprolog.lib.DCGLibrary");

public static void main(String[] args) throws Exception { Prolog engine = 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());

}

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

but we get the same error... :(

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

Can anyone tell me why this is happening? (and solutions would also be welcome).

推荐答案

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

SWI-Prolog could be too much permissive while parsing directives. Try enclosing operators between parenthesis:

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

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

edit I tried using 2p.jar, that allowed me to spot the problem. Need to quote operator' atom:

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

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

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

interactive 2p console accepts this syntax. Note that 2p.jar by default load tuprolog libraries

这篇关于Tuprolog 和定义中缀运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆