A minor observation about dcg_basics:number

20 views
Skip to first unread message

Boris Vassilev

unread,
Apr 3, 2018, 8:06:16 AM4/3/18
to SWI-Prolog
Hello,

This is not a bug. It took me a bit to figure out what is going on (it was hidden in a unit test, and I was re-factoring the unit tests.....) so I hope it is worth sharing.

Here is the executive summary:

~~~
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 7.7.11-6-gde6e44cd9)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- use_module(library(dcg/basics)).
true.

?- phrase(number(0.12), `0.12`).
true.

?- phrase(number(0.12), `12e-2`).
false.

?- phrase(number(N), `12e-2`), N == 0.12.
N = 0.12.

?- X = 12e-2.
X = 0.12.

~~~

So, dcg_basics:number/1 will correctly parse `12e-2`, and read also correctly deals with 12e-2.

But dcg_basics:number//1 can extract or generate a number, and if the argument is already a number (tested with number/1) it will try to format/3 to a code list using "~w" and then fail because `12e-2` and `0.12` are not the same code lists.

I can't tell if this is a "problem" but worth keeping in mind.

Cheers,
Boris

Save our in-boxes! http://emailcharter.org
Reply all
Reply to author
Forward
0 new messages