0

The following code isn't working

:- 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).

but the following is

:- 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).

Why is that? Seems like the "read" function isn't working properly

Marco A.
  • 43,032
  • 26
  • 132
  • 246

2 Answers2

3

from SWI-Prolog mailing list ([SWIPL] Ann: SWI-Prolog 5.11.23, 23 Jun):

  • MODIFIED: User-defined arithmetic functions have been removed from the kernel. There is a new library(arithmetic) that emulates the old behaviour PARTIALLY. Notably:

    • This library must be loaded before arithmetic_function/1 is used.
    • It only covers arithmetic functions that are visible as an argument to is/2, >/2, etc. at compile-time.
    • A new predicate arithmetic_expression_value/2 can be used to evaluate expressions with embedded user arithmetic that become instantiated at runtime.
CapelliC
  • 59,646
  • 5
  • 47
  • 90
1

Well as a lead, when I test it with is/2 it fails but when I use arithmetic_expression_value/2 it succeeds :

:- 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).

For @gusbro, it works out of the box. I'm using windows swi-pl here, for the record !

Others may have clues about why it fails for us !

m09
  • 7,490
  • 3
  • 31
  • 58
  • I successfully run OP code using is/2. **?- calcola. Give me an expression |: 4 i 2. 6** – gusbro Dec 16 '11 at 17:36
  • That's strange. I am using SWI 5.10.4. I guess you are using GNU Prolog ? (In fact, i don't see arithmetic_expression_value/2 in SWI) – gusbro Dec 16 '11 at 17:44
  • nah as I precised in my edit i'm on swi too (5.10.5, windows) – m09 Dec 16 '11 at 17:46
  • swipl 5.10.1, debian here, the original doesn't work, modified version works. Shouldn't i be defined as an operator anyway? if I write 'i(4,2)' OP code works fine. @gusbro http://www.swi-prolog.org/pldoc/doc_for?object=arithmetic:arithmetic_expression_value/2 (maybe it was added in 5.10.5) – Thanos Tintinidis Dec 16 '11 at 17:55
  • @thanosQR: Yes, you have to define it as an operator. I assumed OP forgot to put it because otherwise his second program would be invalid. In my SWI 5.10.4 i get **Undefined procedure: arithmetic_expression_value/2** with Mog's code – gusbro Dec 16 '11 at 18:12
  • I remember a notice of SWI-Prolog mailing list about the change of arithmentic_function recently. Maybe some flag reenable it. Going to search... – CapelliC Dec 16 '11 at 19:19