3
votes

Some of my Prolog programs could profit quite a bit if I could replace all (is)/2-based integer arithmetics by their counterpart.

So I want the power ... with ... so I can replace X is 10^3 with something clpfd-y :)

Consider the following five Prolog processors supporting :

  • GNU Prolog 1.4.4

    ?- X #= 10^3.
    uncaught exception: error(type_error(fd_evaluable,(^)/2),(#=)/2)
    
    ?- X #= 10**3.
    X = 1000.
    
  • SWI-Prolog 7.3.14

    ?- use_module(library(clpfd)).  % autoload would be even more awesome
    true.
    
    ?- X #= 10^3.
    X = 1000.
    
    ?- X #= 10**3.
    ERROR: Domain error: `clpfd_expression' expected, found `10**3'
    
  • B-Prolog 8.1

    ?- X #= 10^3.
    X #= 10^3.
    *** error(illegal_array_access,10^3)
    
    ?- X #= 10**3.
    X = 1000.
    
  • SICStus Prolog 4.3.2

    ?- use_module(library(clpfd)).
    true.
    
    ?- X #= 10^3.
    ! Existence error in (^)/2
    ! constraint user:wi(^)/2 does not exist
    ! goal:  10^3
    
    ?- X #= 10**3.
    ! Existence error in user:(**)/2
    ! constraint user:(**)/2 does not exist
    ! goal:  10**3
    

Ideas / hints / advice, please. What can I do? Use some compatibility layer(s), perhaps?

Thank you in advance!

2
Far more difficult will be to handle numeric range differences ! See for instance Markus Triska thesis - CapelliC
@CapelliC. I didn't get that... What exactly are you referring to? Please point me to some page in mat's thesis... Or are you possibly referring to one of the following limitations? How multiple solvers can interact gracefully? Or how built-ins like length/2 can't handle FD vars because the implementation hasn't caught up yet? - repeat
@CapelliC. Merry X-mas, btw! Rise and shine for the lord has come to the Earth... - repeat
start on page 21 (par. 3.3.1), then read on :) - CapelliC
not really... but I like Prolog for its own merits, and despite its problems... I appreciate the efforts to make it more useful, and hope Markus will succeed fully with his high level objectives. From an engineering viewpoint (that's the perspective from where I read your question), consider that the author of B-Prolog created a new language (Picat) to be free and unencumbered from Prolog heritage. - CapelliC

2 Answers

3
votes

Quick hack to the rescue?

Warning: massive overkill ahead, but... does it even work? And is it portable?

Let's check it out!

  • SWI-Prolog 7.3.14

    • using

      ?- use_module(library(clpq)).
      true.
      
      ?- clpq:{X = 10^3}, integer(X).
      X = 1000.                          % <== SUCCESS!
      
    • using

      ?- use_module(library(clpr)).
      true.
      
      ?- clpr:{X = 10^3}, integer(X).
      false.
      
  • SICStus Prolog 4.3.2

    • using

      ?- use_module(library(clpq)).
      true.
      
      ?- clpq:{X = 10^3}, integer(X).
      false.
      
    • using

      ?- use_module(library(clpr)).
      true.
      
      ?- clpr:{X = 10^3}, integer(X).
      false.
      

1X success, 3X failure... Works, well, kind of... Then again, I guess it ain't it.

0
votes

You can use for constant expressions:

?- X is 10^3.
X = 1000.

Which should work in a ISO compliant prolog thanks to corr2.

It will also propagate inside CLP(FD) as if it were X #= 10^3:

?- Y #= X+1, X is 10^3.
Y = 1001
X = 1000