Re: freeze/2 and nb_setarg/3 interaction

Michael Hendricks <[email protected]> Mon, 14 Apr 2014 14:13:44 -0600
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CAFHuXuZk9CriRFh5L35bqK_c=grBrPMgGAJdm_t0iHx2zmQPkA@mail.gmail.com>
Thanks Michael.  That example and thread pointed me in a helpful direction.
 This seems to be because the unification "H=a" is still trailed.  On
backtracking that unification is undone which makes H unbound again.

I've modified your broken2 example by adding an extra choicepoint to make
this apparent:

broken3 :-
    X = [H|_],
    (   H = a,
        (nb_setarg(1, X, b); true),
        writeln(X),
        fail
    ;   writeln(X)
    ).

?- broken3.
[b|_G1940]
[b|_G1940]
[_G1939|_G1940]
true.



-- 
Michael


On Fri, Apr 11, 2014 at 2:19 AM, Michael Ben Yosef <[email protected]> wrote:

> Michael,
>
> > Somehow nb_setarg/3 is being undone on backtracking when freeze/2 is
> > involved.
>
> I could be misunderstanding this, but I don't think freeze/2 is the
> culprit here. I think nb_setarg/3 (and setarg/3, see below) are
> fundamentally dangerous (broken, even) when the arg being assigned to
> is a variable. Consider the following:
>
> correct2 :-
>     X = [_|_],
>     (   nb_setarg(1, X, a),
>         writeln(X),
>         fail
>     ;   writeln(X)
>     ).
>
> broken2 :-
>     X = [H|_],
>     (   H = a,
>         nb_setarg(1, X, a),
>         writeln(X),
>         fail
>     ;   writeln(X)
>     ).
>
>
> ?- correct2.
> [a|_G2435]
> [a|_G2435]
> true.
>
> ?- broken2.
> [a|_G2271]
> [_G2270|_G2271]
> true.
>
>
> Clearly, adding in the "H = a" goal has caused the binding of that
> argument to be undone on backtracking. Maybe your bug is an instance
> of this problem?
>
> This is reminiscent of a setarg/3 bug I reported a while ago:
>
> https://lists.iai.uni-bonn.de/pipermail/swi-prolog/2013/011602.html
>
> In any case, maybe Jan's answer there - that this is basically
> expected and unfixable - can shed some light on your problem.
>
> Hope that helps!
>
> Michael
>
-------------- next part --------------
HTML attachment scrubbed and removed