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

Mutable cells #1660

Open
byorgey opened this issue Nov 25, 2023 · 0 comments
Open

Mutable cells #1660

byorgey opened this issue Nov 25, 2023 · 0 comments
Labels
C-Moderate Effort Should take a moderate amount of time to address. L-Language design Issues relating to the overall design of the Swarm language. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Feature A new feature to be added to the game. Z-Research This issue requires inventing something new and/or studying research papers for clues.

Comments

@byorgey
Copy link
Member

byorgey commented Nov 25, 2023

It's a little hard to believe we don't already have an issue for this, but I couldn't find one. It would be fun to add mutable reference cells to the language, so you could do some imperative programming when convenient. Our CESK machines already track a mutable store, so this really just requires adding some surface language features that allow accessing it directly.

Proposal

In particular, we could add the following primitives:

  • Ref a is the type of references to a mutable cell holding a value of type a
  • ref : a -> Cmd (Ref a) creates a new mutable cell holding the given value
  • read : Ref a -> Cmd a reads the current value of a mutable cell
  • write : Ref a -> a -> Cmd Unit writes a new value to a mutable cell

We could optionally use special syntax for read and write, e.g. !r for read r and r := x for write r x. It may be OK to get rid of the Cmd on some or all of the above primitives (particularly read); that would require some careful thought/research to make sure it doesn't break type safety somehow.

Additional considerations

  • I am not sure whether references + polymorphism could introduce type safety issues in our language; see https://en.wikipedia.org/wiki/Value_restriction . We should either convince ourselves that this problem does not apply, or come up with appropriate restrictions/safeguards to prevent unsoundness.
  • We should put a bit of thought into what device(s) will provide the capability to create and use mutable cells.
  • Within the world we've created, I'm not sure it makes sense to have shared mutable state. Or at least, it would only make sense in certain restricted situations which we can work out in the future. So passing a Ref a value to another robot (e.g. via Inter-robot communication #94 ) should result in the cell being copied, not in a shared reference to the same mutable cell. Or a value of type Ref a could contain not just a memory address but also a robot ID, so we can tell when we are trying to access memory stored on another robot (with a suitable exception being thrown in that case).
  • Once we implement immutable arrays (Array type #98), we could get mutable arrays simply by making an immutable array full of references; if desired, we could optimize this to use an actual mutable array under the hood.
@byorgey byorgey added Z-Feature A new feature to be added to the game. C-Moderate Effort Should take a moderate amount of time to address. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. L-Language design Issues relating to the overall design of the Swarm language. labels Nov 25, 2023
mergify bot pushed a commit that referenced this issue Jun 19, 2024
Closes #1948; see that issue for a much more in-depth discussion.

Depends on merging #1928 first.

A lot of this PR consists in deleting code that is either (1) ugly or (2) overly clever! 🥳 

The short version is that the `Store` used to incorporate some laziness + memoization: when a cell was first allocated, it was an unevaluated thunk; it then got evaluated the first time it was referenced.  However, this wasn't really needed to handle recursive definitions (which is the only thing we were using it for).  Getting rid of it means we can get rid of a lot of weird ugly code needed to wrap free variables in extra calls to `force` and so on.

The new and improved `Store` just stores `Value`s, period.  A special `VBlackhole` value was added, to be used while evaluating a recursive `let`.

Note that `VRef` is no longer really used, but I left it there for use in implementing #1660 .  Once we have mutable references we can use them + delay/force to implement lazy cells.
@byorgey byorgey added the Z-Research This issue requires inventing something new and/or studying research papers for clues. label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Moderate Effort Should take a moderate amount of time to address. L-Language design Issues relating to the overall design of the Swarm language. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Feature A new feature to be added to the game. Z-Research This issue requires inventing something new and/or studying research papers for clues.
Projects
None yet
Development

No branches or pull requests

1 participant