SWI-Prolog / issues

Dummy repository for issue tracking
7 stars 3 forks source link

sub_string/5 repeats the empty-string solution #99

Closed brebs-gh closed 3 years ago

brebs-gh commented 3 years ago

In swi-prolog 8.2.4:

?- sub_string("ab", _, _, _, S), string_length(S, Len).
S = ,
Len = 0 ;
S = a,
Len = 1 ;
S = ab,
Len = 2 ;
S = ,        <-- unwanted repeat
Len = 0 ;
S = b,
Len = 1 ;
S = ,        <-- unwanted repeat
Len = 0.

More digging shows:

?- sub_string("ab", Before, 0, After, S), string_length(S, Len).
Before = Len, Len = 0,
After = 2,
S =  ;
Before = After, After = 1,
S = ,
Len = 0 ;
Before = 2,
After = Len, Len = 0,
S = .
JanWielemaker commented 3 years ago

As you see, the matches are at different locations. Prolog doesn't know the caller ignores some arguments, so there is no way to handle this differently in the implementation of the predicate. If you need unique answers, distinct/1 or distinct/2 may be the way to go. As is, sub_string/5 is the string equivalent of ISO sub_atom/5 which is defined to work as it does. Please use the forum for further discussion.