scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

The irrational number e is not always understood

Open josd opened this issue 1 year ago • 6 comments

The following case https://github.com/eyereasoner/eye/blob/1ea6435149beb4d4a483ddb761eae85def65db16/see/temp/complex.pl gives

$ scryer-prolog -g test complex.pl
'https://josd.github.io/cigol#exp'([-1,0],[0.5,0],[6.123233995736766e-17,1.0]).
run,halt causes: error(type_error(evaluable,e),(is)/2)
?-

whereas we expect

$ tpl -g test complex.pl
'https://josd.github.io/cigol#exp'([-1,0],[0.5,0],[6.123233995736766e-17,1.0]).
'https://josd.github.io/cigol#exp'([e,0],[0,pi],[-1.0,1.2246467991473532e-16]).
'https://josd.github.io/cigol#log'([e,0],[-1,0],[0.0,3.141592653589793]).
'https://josd.github.io/cigol#log'([0,1],[0,1],[1.0,0.0]).
'https://josd.github.io/cigol#sin'([1.5707963267949,1.316957896924817],[2.0,-6.6312755068093511e-16]).
'https://josd.github.io/cigol#cos'([0,-1.316957896924817],[2.0,0.0]).
'https://josd.github.io/cigol#tan'([1.338972522294493,0.402359478108525],[1.0,2.0]).
'https://josd.github.io/cigol#asin'([2,0],[1.5707963267949,1.31695789692482]).
'https://josd.github.io/cigol#acos'([2,0],[0.0,-1.31695789692482]).
'https://josd.github.io/cigol#atan'([1,2],[1.33897252229449,0.402359478108525]).

The irrational number pi seems to be understood correctly. This is also fine

$ scryer-prolog
?- X is e^pi.
   X = 23.140692632779263.
?- X is pi^e.
   X = 22.45915771836104.
?-

josd avatar Jul 21 '24 10:07 josd

Excellent catch!

I have reduced this to the following test case:

:- use_module(library(debug)).

p([X], Y) :-
        $Y is X.

Yielding:

?- p("e", R).
call:(A is e).
exception:error(type_error(evaluable,e),(is)/2):(A is e).
   error(type_error(evaluable,e),(is)/2), unexpected.

On the other hand, querying the traced goal in isolation works as expected:

?- A is e.
   A = 2.718281828459045. % expected

triska avatar Jul 21 '24 19:07 triska

I think this is even shorter:

?- [X] = "e", _ is X.

Surprisingly it works for pi:

?- [X] = [pi], _ is X.

hurufu avatar Jul 21 '24 20:07 hurufu

It looks suspiciously like yet another issue due to the currently only partially finished partial string representation (#24).

triska avatar Jul 21 '24 20:07 triska

Makes sense, also this

?- [X,Y] = [e,foo], Z is X.
   X = e, Y = foo, Z = 2.718281828459045.

works fine whereas this

?- [X,Y] = [e,f], Z is X.
   error(type_error(evaluable,e),(is)/2).

is not working fine.

josd avatar Jul 21 '24 21:07 josd

The case is now moved to https://github.com/eyereasoner/euler/blob/main/cases/complex.pl

josd avatar Jul 29 '24 18:07 josd

The problem got worse with rebis-dev:

?- _ is e.
   error(type_error(evaluable,e),(is)/2), unexpected.
   true. % expected; works in master

Update: e is in fact not a standard arithmetic function, so this works as expected.

triska avatar Sep 28 '24 09:09 triska

I found a workaround so that you can at least continue for the time being if you want to use it:

?- X is 0 + e.
   X = 2.718281828459045.

triska avatar Oct 27 '24 07:10 triska

Thanks @triska and this now works fine in https://github.com/eyereasoner/euler/blob/574fcec024bfd86e0c035de1210a891ed390dc66/cases/complex.pl

josd avatar Oct 27 '24 08:10 josd

I stumbled on a similar problem recently when dealing with module qualifications. If it's the same thing, the problem here isn't partial strings exactly, it's the Char heap cell variant. It should be interpreted the same as an Atom, but for some reason it isn't in some places in the code.

bakaq avatar Feb 20 '25 00:02 bakaq

e is not an arithmetic functor in the standard. It can be considered as an extension (5.5.10) only. exp(1) is working in any conforming system.

?- X is exp(1).
   X = 2.718281828459045.
?- X is log(exp(1)).
   X = 1.0.
?- X is exp(1)-e.
   X = 0.0, unexpected. % in strictly conforming mode (5.1e) 
   type_error(evaluable,e/0).

UWN avatar Feb 20 '25 07:02 UWN

With the latest rebis-dev, we get:

?- [X,Y] = [e,f], Z is X.
   X = e, Y = f, Z = 2.718281828459045, unexpected.
   error(type_error(evaluable,e/0),(is)/2). % expected

So, we no longer have the problem with quoting/unquoting. The remaining issue that e is not an evaluable functor yet X is e unexpectedly succeeds remains.

triska avatar Apr 10 '25 17:04 triska

Same for Trealla it seems

$ tpl
?- [X,Y] = [e,f], Z is X.
   X = e, Y = f, Z = 2.718281828459045.
?-

josd avatar Apr 10 '25 19:04 josd