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

Convenient syntax to destructure newtypes #854

Open
Timmmm opened this issue Jan 6, 2025 · 2 comments
Open

Convenient syntax to destructure newtypes #854

Timmmm opened this issue Jan 6, 2025 · 2 comments

Comments

@Timmmm
Copy link
Contributor

Timmmm commented Jan 6, 2025

Now that we're using newtype physaddr = physaddr : xlenbits (and virtaddr) in the Sail model, it's a bit awkward not being able to easily access the xlenbits. Currently the solution is a function physaddr_bits() and virtaddr_bits() but it's a bit verbose.

Rust has addr.0 which makes sense because in Rust newtypes are single-entry tuples, but as I understand it in Sail they are single-entry enums, so it doesn't make as much sense to use .0. .something seems reasonable though. Some ideas:

  • addr.inner
  • addr.val

Or maybe it would be better to change newtype to use a 1-tuple and add .0, .1 syntax?

@Alasdair
Copy link
Collaborator

Alasdair commented Jan 6, 2025

I think the Rust .0, .1, ..., .N syntax is maybe a bit cryptic for people not familiar with it, although I do find it convenient when writing Rust. Something like .inner would probably be fine. OCaml lets you do:

type physaddr = Physaddr of { inner : t }

where a x : physaddr can be unwrapped as x.inner

I don't know if we could make them single element tuples, as single element tuples aren't really a thing in Sail - I suspect internally tuple length is >= 2 is probably assumed in quite a few places as an invariant. The current implementation (single element enum) is a direct copy of Haskell's newtype for a single element.

@bacam
Copy link
Contributor

bacam commented Jan 6, 2025

I think the Rust .0, .1, ..., .N syntax is maybe a bit cryptic for people not familiar with it

That might be one of those bits of Rust inspired by Standard ML, and it's a pretty obscure feature there.

I don't know if we could make them single element tuples, as single element tuples aren't really a thing in Sail - I suspect internally tuple length is >= 2 is probably assumed in quite a few places as an invariant.

It would be a pain because it would need special syntax, although I seem to remember them occurring occasionally in intermediate Sail (maybe in the translation of mappings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants