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

Interpreter can't reduce top-level definitions #1247

Open
3 tasks done
dhess opened this issue Apr 20, 2024 · 3 comments
Open
3 tasks done

Interpreter can't reduce top-level definitions #1247

dhess opened this issue Apr 20, 2024 · 3 comments
Assignees
Labels
bug 🐞 A confirmed bug primer Specific to the primer package priority: high This issue has high priority

Comments

@dhess
Copy link
Member

dhess commented Apr 20, 2024

Code of conduct

  • I've read the project's code of conduct, and agree to follow it.

Don't report security issues here

  • I've read the project's security policy, and I do not believe this bug is a security issue.

Is there an existing issue?

  • I've searched GitHub Issues, and I believe this is a new issue

What happened?

While adding the interpreter to the various Primer APIs, I discovered that the interpreter can't reduce top-level definitions. This test demonstrates that the interpreter can't evaluate the even 3? top-level definition in our example even3Prog:

What should have happened?

The interpreter should be able to reduce a top-level definition whose root node is an App.

Which package(s) or aspect(s) of the project are the source of the issue?

primer

Developers, please provide the output of the bug report script

No response

Do you have a reproducer?

Yes, see above.

Any additional information?

We can't use the interpreter in the frontend until this issue is resolved. See hackworthltd/primer-app#1169

@dhess dhess added the triage This issue needs triage label Apr 20, 2024
@dhess dhess self-assigned this Apr 20, 2024
@dhess dhess added priority: high This issue has high priority bug 🐞 A confirmed bug primer Specific to the primer package and removed triage This issue needs triage labels Apr 20, 2024
dhess added a commit that referenced this issue Apr 22, 2024
Note that all of these tests that use the interpreter are currently
expected to fail (or are commented out because they will fail) due to
#1247

Signed-off-by: Drew Hess <[email protected]>
dhess added a commit to hackworthltd/primer-app that referenced this issue Apr 23, 2024
This is more or less a drop-in replacement for `EvalFull` and is ready
to go, save for hackworthltd/primer#1247.
Therefore, we don't yet use it in the `Edit` and `PictureInPicture`
components.

Signed-off-by: Drew Hess <[email protected]>
@georgefst
Copy link
Contributor

I can see the problem here, and a potential solution.

The definition of EnvTm/EnvTy says "Invariant: the values in the env will be in normal form.". Yet when creating these with mkGlobalEnv we don't check this. The body of even 3? is even (succ (succ (succ zero))), which is of course not a normal form ("normal form" in this context means "fix point of interp'"). So we instantly hit the Var _ (GlobalVarRef v) branch of interp' which inserts the definition itself, which is then never reduced further.

If we try to reduce these up front by inserting some extra interp' calls in the definition of mkGlobalEnv, we find that those calls need the definition map itself as an argument, and in the mutual recursion case I don't see any simple way to resolve this. Besides, we shouldn't be calling interp' without the timeout logic from interp.

Instead, I've tried starting with an empty context, and converting all top-level defs to a recursive let, using the encoding from #114 (comment). This should be sound, but appears to reveal a separate bug: the perfectly-valid expression which results does not reduce as expected, and instead grows infinitely large.

There is a comment in the source saying "this interpretation af letrec can easily cause deadlocked or infinite-size programs". I'm not sure I'm parsing this correctly (I should have probably asked for clarification in review, but that was a little rushed in the case of #1187 for obvious reasons), and it doesn't seem to be covered explicitly in #1194, but this seems to suggest that maybe this is a known limitation? I haven't yet examined the resulting infinite expression to see exactly what keeps getting expanded.

(This problem has been nagging at me since our discussion on Monday, so I had to have a look. I suppose I got nerd sniped!)

@dhess
Copy link
Member Author

dhess commented Oct 5, 2024

@georgefst Thanks!

The first thing that pops into my head is, what is the fundamental difference between the step evaluator and the interpreter such that the step evaluator isn't caught by this?

@dhess
Copy link
Member Author

dhess commented Oct 5, 2024

Despite my preference to treat top-level definitions as simply module-scoped letrecs, they keep resisting that. So rather than trying to convert them to letrecs during interpretation, should we instead treat them specially in EnvTm/mkGlobalEnv/interp ? I can vaguely imagine an implementation where, when a top-level definition is encountered during a particular interpretation sequence, we force its evaluation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 A confirmed bug primer Specific to the primer package priority: high This issue has high priority
Projects
None yet
Development

No branches or pull requests

2 participants