Skip to content

Fix handling of do-blocks in the SAWScript interpreter #2473

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

Merged
merged 5 commits into from
Jul 9, 2025

Conversation

sauclovian-g
Copy link
Contributor

This is not stable yet, since it needs to move on top of #2471, but the content should be stable and ready for an initial review.

...provided the tests pass. If they don't, it may need to be thrashed at some length and it's probably not worth reviewing until that happens.

Given how many bizarre things I've visited in the past few days, I give that about 50/50.

and extend it to all five SAWScript monads. Will be wanted in a moment
or two.

Don't export it from Interpreter.hs; it isn't used elsewhere.

This in turn revealed that the withTopLevel element isn't used, so
drop it.
(not just TopLevel)

Has no effect yet. Coming right up...
The prior behavior was to execute into do-blocks until the first bind,
then defer execution using a chain of VBind value thunks with copies
of the interpreter closed into them. This caused a lot of unnecessary
consing; however, worse than that it caused strange eval behavior.
For example,
   let f = do { let x = undefined; return 3; };
crashes immediately, but
   let f = do { _ <- return (); let x = undefined; return 3; };
crashes only when the resulting monadic value is executed.

To fix this, introduce a VDo value, containing abstract syntax rather
than values, and when we hit a do-block when executing purely, return it
immediately as a VDo value. Then, in the same places that process VBind,
process VDo by executing the statements in it.

This changes the internals of the interpreter slightly such that
expressions are always evaluated purely and statements are always
evaluated in a monad context; it is thus necessary when executing
statements to force any VDo values that appear. (The typechecker
ensures that they'll be do-blocks in the correct monad.)

Unfortunately, we can't get rid of VBind yet. There is one thing that
still needs it: the "for" builtin. It can't use VDo: as a builtin, it
is handed only values, not abstract syntax, and for the time being we
can't lift arbitrary values to abstract syntax. The best bet is
probably to rewrite it in SAWScript and stick it in the prelude we
don't yet have (see #253). Rename VBind to VBindOnce and note all
this.

One further caution: this commit removes part of the stack trace in
the reference output for test_sawscript_builtins/fail2. This is
because the current scheme for doing stack traces can't cope with
the new VDo, and I'm not inclined to garbage things up to make it.

I'm going to add some chewing gum and baling wire to bring the stack
trace entry back in the next commit, but I expect to be able to revert
the mess in the near future. :-)
@sauclovian-g sauclovian-g force-pushed the interpreter-do-blocks branch from bccc844 to be73b4b Compare July 8, 2025 22:42
This restores the stack trace compromised in the previous commit, at
the cost of making a small mess. This is its own commit in order to be
able to revert it easily later.

This works by stuffing a Maybe String into VDo, populating it from the
stack trace goop, and feeding it to a wrapper around interpretStmts.
As an instance of chewing gum, it could be worse...
@sauclovian-g sauclovian-g force-pushed the interpreter-do-blocks branch from be73b4b to 65ba586 Compare July 8, 2025 23:33
@sauclovian-g
Copy link
Contributor Author

(first force-push was to rebase on head to bring in #2471 (which also got #2472 and #2474); second was to insert the review changes and retcon the last ("chewing-gum") commit to add comments there)

@sauclovian-g sauclovian-g merged commit ac08070 into master Jul 9, 2025
37 checks passed
@sauclovian-g sauclovian-g deleted the interpreter-do-blocks branch July 9, 2025 17:43
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

Successfully merging this pull request may close these issues.

2 participants