Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values referenced after a recursive call get overwritten #1032

Closed
kostmo opened this issue Jan 23, 2023 · 5 comments · Fixed by #1928
Closed

Values referenced after a recursive call get overwritten #1032

kostmo opened this issue Jan 23, 2023 · 5 comments · Fixed by #1928
Labels
Bug The observed behaviour is incorrect or unexpected. C-Project A larger project, more suitable for experienced contributors. G-CESK machine This issue has to do with the CESK machine (the Swarm language interpreter). S-Moderate The fix or feature would substantially improve user experience. Z-User Experience This issue seeks to make the game more enjoyable to play.

Comments

@kostmo
Copy link
Member

kostmo commented Jan 23, 2023

Describe the bug

The following program produces unexpected output in the log:

      def go = \n.
          if (n > 0) {
              idx <- random 4;
              log $ "In: " ++ format idx;

              go $ n - 1;

              log $ "Out: " ++ format idx;
              move;
          } {};
          end;

      go 4;

Output in log:

In: 1
In: 3
In: 0
In: 2
Out: 0
Out: 0
Out: 0
Out: 0
@kostmo kostmo added the Bug The observed behaviour is incorrect or unexpected. label Jan 23, 2023
This was referenced Jan 23, 2023
@xsebek
Copy link
Member

xsebek commented Jan 23, 2023

@byorgey this looks to me like a case of #681, am I right? 🤔

@xsebek
Copy link
Member

xsebek commented Jan 23, 2023

@kostmo a workaround is to pass the variable that is being overwritten after the recursive call:

def go = \o.\n.
    if (n > 0) {
        idx <- random 4;
        log $ "In: " ++ format idx;

        idx <- go idx $ n - 1;

        log $ "Out: " ++ format idx;
        move;
    } {};
  return o;
end;

go 4 4;

image

@byorgey
Copy link
Member

byorgey commented Jan 24, 2023

Yep, almost certainly #681, although to be honest I don't quite understand why we don't see the final 2 being repeated, like this:

In: 1
In: 3
In: 0
In: 2
Out: 2
Out: 2
Out: 2
Out: 2

Most likely duplicate, but I'll leave this open for now so when we fix #681 we can make sure it fixes this too.

@xsebek
Copy link
Member

xsebek commented Jan 24, 2023

@kostmo better workaround: use a let binding

def go = \n.
    if (n > 0) {
        idx <- random 4;
        let indx = idx in
        log $ "In: " ++ format indx;

        go $ n - 1;

        log $ "Out: " ++ format indx;
        move;
    } {};
end;

go 4;

@byorgey when I tried the example code by @kostmo right now, it did repeat the last value.

Maybe we have a different version or @kostmo made a typo. 🙂

log "----"; go 2

image

@byorgey
Copy link
Member

byorgey commented Jan 24, 2023

After looking at #1033 , I am now 100% certain this is caused by #681. It turns out that #1021 is also causing issues here which is why I was confused.

@kostmo kostmo added the S-Moderate The fix or feature would substantially improve user experience. label May 29, 2023
@byorgey byorgey added Z-User Experience This issue seeks to make the game more enjoyable to play. C-Project A larger project, more suitable for experienced contributors. G-CESK machine This issue has to do with the CESK machine (the Swarm language interpreter). labels May 1, 2024
byorgey added a commit that referenced this issue Jun 14, 2024
byorgey added a commit that referenced this issue Jun 16, 2024
byorgey added a commit that referenced this issue Jun 16, 2024
byorgey added a commit that referenced this issue Jun 16, 2024
@mergify mergify bot closed this as completed in #1928 Jun 19, 2024
@mergify mergify bot closed this as completed in 23b5398 Jun 19, 2024
kostmo added a commit that referenced this issue Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug The observed behaviour is incorrect or unexpected. C-Project A larger project, more suitable for experienced contributors. G-CESK machine This issue has to do with the CESK machine (the Swarm language interpreter). S-Moderate The fix or feature would substantially improve user experience. Z-User Experience This issue seeks to make the game more enjoyable to play.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants