Mercury Bugs - mercury
View Issue Details
0000267mercuryBugpublic2012-12-03 13:122015-07-22 09:14
Reporteredu500ac 
Assigned Tozs 
PriorityurgentSeveritymajorReproducibilityalways
StatusclosedResolutionno change required 
PlatformHP Compaq Intel Atom 32 bitsOSLinuxOS VersionZorin
Product Version 
Target VersionFixed in Version 
Summary0000267: Equivalent programs show different behaviors; one of the behaviors produces wrong results
Description/* The program below reproduces the issue I am reporting */
:- module isbugp.

:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module list, char, int, string.

keyword(T) -->
   ( nkeyword(P) -> {T=P};
     akeyword(Q) -> {T=Q};
     {T= []}).
  
/*
keyword(T) -->
   ( isdigit -> nkeyword(Q), {T=Q};
     isalpha -> akeyword(Q), {T=Q};
     {T= []}).
*/

isdigit([C|S], [C|S]) :- is_digit(C).
isalpha([C|S], [C|S]) :- is_alpha(C).

nkeyword(R) -->
   ( isdigit -> [C], nkeyword(KW), {R= [C|KW]};
     {R=[]}).

akeyword(R) -->
   ( isalpha -> [C], akeyword(KW), {R= [C|KW]};
     {R=[]}).

main(!IO) :-
    read_line(Res, !IO),
    (if Res = ok(Line) then
     (if keyword(W, Line, _)
         then write_chars(W, !IO),
                 io.write_char('\n', !IO)
         else print("Falha!\n", !IO)),
     main(!IO)
    else main(!IO)).

write_chars([], !IO).
write_chars([X | Xs], !IO) :-
    io.write_char(X, !IO),
    write_chars(Xs, !IO).

:- end_module isbugp.
Steps To ReproduceThe program is supposed to recognize digit prefixes or alpha prefixes of a list of chars. As you can see below, it recognizes digit prefixes, but fails with alpha prefixes.

~/nlp/mercury-tutorial$ mmc --make --infer-all isbugp
Making isbugp
~/nlp/mercury-tutorial$ ./isbugp
456abc
456
abc456

7899ghj
7899
ghj789


Consider the definition of keyword:

keyword(T) -->
   ( nkeyword(Q) -> {T=Q};
     akeyword(Q) -> {T=Q};
     {T= []}).

If nkeyword(Q) and akeyword(Q) switch positions, keyword recognizes alpha prefixes, but not digit prefixes.

keyword(T) -->
   ( akeyword(Q) -> {T=Q};
     nkeyword(P) -> {T=P};
     {T= []}).

Making Mercury/os/isbugp.o
Making isbugp
~/nlp/mercury-tutorial$ ./isbugp
abcd876
abcd
876abcd



There is a second definition of keyword. If I uncomment the second definition, and comment the first one, the program works perfectly well, i.e., it recognizes both digit prefixes and alpha prefixes. Here is the definition one should uncomment to fix the program:

% Comment the first definition
/*
keyword(T) -->
   ( nkeyword(P) -> {T=P};
     akeyword(Q) -> {T=Q};
     {T= []}).
*/

% uncomment the second definition
keyword(T) -->
   ( isdigit -> nkeyword(Q), {T=Q};
     isalpha -> akeyword(Q), {T=Q};
     {T= []}).

isdigit([C|S], [C|S]) :- is_digit(C).
isalpha([C|S], [C|S]) :- is_alpha(C).

Additional InformationPrograms like the one listed above used to work in older definitions of Mercury. I tested the program in small machines, 32 bits, Zorin Linux, Intel Atom processor, mmc --version 11.07.2
TagsNo tags attached.
Attached Files? isbugp.m (941) 2012-12-03 13:12
https://bugs.mercurylang.org/file_download.php?file_id=163&type=bug

Notes
(0000480)
wangp   
2012-12-03 21:38   
The bug is in your code. Presumably you did not intend for nkeyword and akeyword to succeed with an empty prefix.

Issue History
2012-12-03 13:12edu500acNew Issue
2012-12-03 13:12edu500acFile Added: isbugp.m
2012-12-03 21:38wangpNote Added: 0000480
2015-07-22 09:14zsStatusnew => closed
2015-07-22 09:14zsAssigned To => zs
2015-07-22 09:14zsResolutionopen => no change required