swipl-devel icon indicating copy to clipboard operation
swipl-devel copied to clipboard

union/3 might go into infinite loop, even for well typed arguments

Open ghost opened this issue 4 years ago • 5 comments
trafficstars

Try this:

?- union(X,[],[_|X])
ERROR: Stack limit (1.0Gb) exceeded

My guess, it is a steadfastness problem.

ghost avatar Nov 20 '20 14:11 ghost

It is a mode error. See Discourse comments.

JanWielemaker avatar Nov 20 '20 14:11 JanWielemaker

This here is also nice:

?- intersection(_, [], [_]).
... hangs ...

You use steadfastness via (=)/2 here, this is from SWI-Prolog source:

intersection([X|T], L, Intersect) :-
    memberchk(X, L),
    !,
    Intersect = [X|R],
    intersection(T, L, R).

Note the extra (=)/2. Oki Doki. Not sure whether I can find an error, where the mode is satisfied, but there is nevertheless an error. But since you use steadfastness rewriting anyway, despite not

having a mode checker. Why not improve type/instantiation checking, despite not having a type checker? Or maybe the rewriting was done for other reasons than steadfastness?

And why is the rewriting not applied everywhere? Sometimes behaviour is “undefined” which can mean loops. And sometimes I assume steadfastness is addressed?

ghost avatar Nov 20 '20 15:11 ghost

This issue has been mentioned on SWI-Prolog. There might be relevant details there:

https://swi-prolog.discourse.group/t/why-steadfastness/3313/4

JanWielemaker avatar Nov 28 '20 09:11 JanWielemaker

The aformentioned defects are gone for the new (=>)/2 based list predicates union/3 and intersection/3. Cool!

But they are a little asymmetric:

?- intersection(X, [1,2,3], L).
ERROR: No rule matches lists:intersection(_21734,[1,2,3],_21738)

?- intersection([1,2,3], X, L).
X = [1, 2, 3|_1062],
L = [1, 2, 3]. 

Maybe because they still use memberchk/2 which isn't implemented with (=>)/2.

ghost avatar Feb 15 '21 15:02 ghost

memberchk(x, L) is supposed to succeed. I don't want to touch that. So yes, maybe an alternative to memberchk is an option. Fot now out of scope though.

JanWielemaker avatar Feb 15 '21 20:02 JanWielemaker