[comp.lang.icon] Reversible Assignment Problem

schreck@hpldola.HP.COM (Jim Schreckengast) (05/09/90)

/ hpldola:comp.lang.icon / leff@dept.csci.unt.edu (Dr. Laurence L. Leff) /  9:12 am  May  1, 1990 /

> Reversible assignments to global variables inside procedures are
> not being reversed. 

> The program below prints out three.  It should print out one.  Obviously,
> the reversible assignment to z is not being reversed when test does not 
> lead to an eventual success.

> Why does the reversing of the assignment not take place, and what would
> make it do so?

> The Icon Programming Language, Chapter 11, section 11.8.2 did not shed
> any light on these issues.

> global z
> procedure test(i)
> z<-z+1
> if i~=3 then fail
> if i=3 then return 1
> end
> 
> procedure main()
> z:=0
> every i:=(1 to 10) do if test(i) then write("test succeeded ",i," ",z)
> end

There is no reason for reversing the assignment, because the choice point
created by "<-" is never resumed.  The expression z <- z+1 succeeds.  To get
the effect you're looking for, you could substitute the following for the body
of test:

    return (z <- z+1, i = 3, 1)

When the i = 3 expression fails, the assignment statement will be resumed
and z will be restored to its original value.  Backtracking is initiated,
in this case, by a failure.