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

feature: pure function mark #321

Open
oovm opened this issue Mar 15, 2024 · 1 comment
Open

feature: pure function mark #321

oovm opened this issue Mar 15, 2024 · 1 comment

Comments

@oovm
Copy link

oovm commented Mar 15, 2024

I need a mechanism to mark functions as having no side effects (pure functions).

Most external interfaces come with state, so I recommend having side effects by default and adding the @pure(after #58) tag if it is a pure function

Because pure functions and side-effect functions have different behaviors in dead code elimination analysis.

Pure functions such as wasi:clocks/monotonic-clock/now can be deleted directly from the control flow if they are not used.

However, functions such as wasi:random/random/get-random-u64 may change external state, so even if their return value is not used, they cannot be deleted.

@lukewagner
Copy link
Member

This is an interesting idea. Since we wouldn't be able to actually enforce purity, the "pure" mark would just be advisory, which is I think what you're suggesting. But this means that someone might (for valid reasons) virtualize wasi:clocks/monotonic-clock.now to log every time now was called, and a compiler that optimized away the call to now would then be affecting the observable behavior of the program. However, if pure was considered a part of the interface contract of now, then we could say that's actually fine. But this makes me think we shouldn't be putting purity in a generic (open-ended, ignorable, extensible) syntax, but rather something that is a built-in/official part of the WIT syntax and C-M AST, so that it's just as official a part of the contract as the types (even if not semantically enforced).

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

2 participants