【问题标题】:Prolog read compound term and treating it like an expressionProlog 读取复合术语并将其视为表达式
【发布时间】:2011-12-16 16:51:40
【问题描述】:

以下代码不起作用

:- arithmetic_function(i/2).

i(X,Y,Z) :-
         Z is X+Y.


calcola :-
        write('Give me an expression'),nl,
        read(ESP),
        Z is ESP,nl,nl,
        write(Z).

但以下是

:- arithmetic_function(i/2).

i(X,Y,Z) :-
         Z is X+Y.


calcola :-
        write('Give me an expression'),nl,
        Z is 4 i 2,nl,nl,
        write(Z).

这是为什么呢?似乎“读取”功能无法正常工作

【问题讨论】:

  • “不工作”是什么意思?

标签: prolog logic swi-prolog gnu-prolog


【解决方案1】:

来自 SWI-Prolog 邮件列表([SWIPL] Ann:SWI-Prolog 5.11.23,6 月 23 日):

  • 修改:用户定义的算术函数已从 内核。有一个新的库(算术)模拟 部分旧行为。值得注意的是:

    • 此库必须在算术函数/1 之前加载 用过。
    • 它仅涵盖作为参数可见的算术函数 在编译时为 is/2、>/2 等。
    • 新的谓词算术表达式值/2 可用于 使用嵌入式用户算术计算表达式成为 在运行时实例化。

【讨论】:

    【解决方案2】:

    作为一个线索,当我用is/2 测试它失败但当我使用arithmetic_expression_value/2 它成功:

    :- arithmetic_function(i/2).
    
    :- op(20, xfx, i).
    
    i(X, Y, Z) :-
        Z is X + Y.
    
    calcola :-
        writeln('Give me an expression'),
        read(ESP),
        arithmetic_expression_value(ESP, Z), nl,
        write(Z).
    

    对于@gusbro,它开箱即用。我在这里使用 windows swi-pl,记录一下!

    其他人可能对我们为什么失败有线索!

    【讨论】:

    • 我使用 is/2 成功运行了 OP 代码。 ?- 可乐。给我一个表达式 |: 4 i 2. 6
    • 这很奇怪。我正在使用 SWI 5.10.4。我猜你正在使用 GNU Prolog ? (事实上​​,我在 SWI 中没有看到算术表达式值/2)
    • 不,正如我在编辑中所说的那样,我也在 swi 上(5.10.5,windows)
    • swipl 5.10.1,这里是debian,原版不行,修改版可以。我不应该被定义为运营商吗?如果我写 'i(4,2)' 操作代码工作正常。 @gusbro swi-prolog.org/pldoc/…(可能是在 5.10.5 中添加的)
    • @thanosQR:是的,您必须将其定义为运算符。我以为 OP 忘了放它,因为否则他的第二个程序将无效。在我的 SWI 5.10.4 中,我得到 Undefined procedure:arithmetic_expression_value/2 和 Mog 的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2014-05-23
    • 2015-12-20
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多