You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Discovered while teaching with Z3.Linq inside Polyglot Notebooks (.NET Interactive), where each notebook cell compiles as a separate "submission". The crash does not reproduce in a console app or the xUnit test suite (single assembly / single scope) — which is why it has stayed invisible.
Repro
A Theorem<T> over a nested int[][] model where the bounds are declared in one notebook cell and .Solve() is called in another cell (i.e. the constraint lambdas capture variables from a different submission) raises:
System.ArgumentException: Field 'X' defined on type 'Submission#N' is not a field
on the target object of type 'Submission#M'
Two int[][] solves in the same cell work fine — the crash only appears on cross-submission capture.
Root cause
ExpressionVisitor.VisitMember (Z3.Linq ExpressionVisitor.cs, ~L461) resolves host-captured constants by reflection: field.GetValue(target). When a constraint lambda captures a variable declared in a different .NET Interactive submission (or a different closure scope), the target object does not carry that field → ArgumentException.
Proposed fix (direction)
PartialEvaluator.PartialEval the constraint bodies inside Theorem.AssertConstraintsbefore the Z3 visit — fold the captured constants into literals in the expression tree. This neutralizes the reflective cross-scope resolution. Needs validation against the full endjin test suite + the fork's own tests.
Happy to open a PR for this.
Workaround (current, in the pedagogical notebooks)
Keep the bounds declaration, the Where(...) constraint lambdas, and.Solve() in the same notebook cell — so no constant is captured across submissions.
Where this is exercised
The pedagogical series using the fork (CoursIA SMT/Z3/ notebooks) hits this pattern in the hierarchical meal-planner notebooks (int[][] multi-day plans). The same notebooks are candidates for upstream endjin/Z3.Linq samples once cleaned up (flagged in the ongoing conversation on endjin#29).
Investigated the fork source firsthand. The root cause is narrower and more structural than the original report.
SimplifyLambda is vestigial
ExpressionVisitor.VisitMember (L439-488) has this comment at L447:
// We only ever get here if SimplifyLambda is set to false, otherwise partial evaluation does it earlier
But SimplifyLambda does not exist as a property or mechanism anywhere in the current fork — it's a stale comment carried over from an earlier endjin revision. So the reflective branch (field.GetValue(target) / property.GetValue(target), L461/L465) is always active whenever a MemberExpression resolves to a captured constant outside the theorem's parameter.
Why the test suite is green but notebooks crash
The xUnit suite (Z3.Linq.Tests, 6/6 passing — verified local baseline) captures loop indices in locals (int i1 = i; theorem = theorem.Where(g => g.Cells[i1][j1]...), see CollectionHandlingTests.cs). In a single assembly, field.GetValue(target) succeeds: the compiler-generated closure field lives on a type in the same assembly, so reflection finds it.
In Polyglot Notebooks (.NET Interactive), each cell compiles to a separate dynamic assembly (Submission#0, Submission#1, ...). When a constraint lambda in cell N captures a variable declared in cell M (M != N), the target object is a closure instance typed as Submission#M, but the FieldInfo was resolved against Submission#N → field.GetValue(target) throws ArgumentException: Field 'X' defined on type 'Submission#N' is not a field on the target object of type 'Submission#M'.
PartialEval is already used in the visitor — just not on constraint bodies
PartialEvaluator.PartialEval is invoked at ExpressionVisitor.cs:177, :229, :344 — but only on array-index sub-expressions and a substitution case, never on the whole constraint body. Theorem.AssertConstraints (Theorem.cs:149-190) calls ExpressionVisitor.Visit(..., constraint.Body, ...) directly at L176 with no prior partial evaluation.
Proposed fix (concrete, surgical)
In Theorem.AssertConstraints, fold captured constants before visiting:
// Theorem.cs, inside the foreach at L174-189varbody=PartialEvaluator.PartialEval(constraint.Body,ExpressionInterpreter.Instance);BoolExprexpression=(BoolExpr)ExpressionVisitor.Visit(context,environment,body,constraint.Parameters[0]);
This rewrites captured-constant member accesses into literal ConstantExpressions beforeVisitMember's reflective branch ever runs — neutralizing the cross-assembly reflection. PartialEvaluator/ExpressionInterpreter are already referenced in the same file's namespace, so no new dependency.
Repro challenge
The crash is specific to .NET Interactive's per-cell dynamic-assembly model — it does not reproduce in a single-assembly xUnit test (which is why it stayed invisible). Faithfully reproducing it requires either a Polyglot Notebook execution or a dynamically-compiled multi-assembly repro. The fix can be validated by: (1) the existing 6/6 suite stays green (PartialEval is a no-op on same-assembly captures — same result), and (2) a Polyglot Notebook cross-cell repro stops crashing.
Happy to open the PR with the one-line AssertConstraints change + a notebook repro if you want it.
Fix summary: Theorem.AssertConstraints now calls PartialEvaluator.PartialEval(constraint.Body, ExpressionInterpreter.Instance) before ExpressionVisitor.Visit, folding host-captured constants into literal ConstantExpressions. This sidesteps the reflective field.GetValue(target) resolution in ExpressionVisitor.VisitMember (~L461) that crashed across .NET Interactive's dynamic-assembly boundaries.
Validation:
dotnet test Z3.Linq.Tests: 6/6 passing (no regression — PartialEval is a no-op for same-assembly captures, which is why the xUnit suite never caught the bug).
Polyglot regression test solutions/polyglot-repro/CrossSubmissionCaptureRepro.ipynb: cross-submission capture now solves 7,8,9 instead of raising ArgumentException: Field 'b0' ... Submission#N ... Submission#M.
Root-cause analysis + fix direction originally posted in this issue (comment 2026-06-24); the fix was implemented and empirically validated in PR #3. CoursIA submodule pointer bumped in jsboige/CoursIA#4124.
Thanks to the root-cause note about SimplifyLambda being vestigial (only a stale comment at ExpressionVisitor.cs:447) — confirmed firsthand: the reflective path is always active, which is exactly what PartialEval neutralizes.
Activity
jsboige commentedon Jun 24, 2026
Update — root-cause investigation (2026-06-24)
Investigated the fork source firsthand. The root cause is narrower and more structural than the original report.
SimplifyLambdais vestigialExpressionVisitor.VisitMember(L439-488) has this comment at L447:But
SimplifyLambdadoes not exist as a property or mechanism anywhere in the current fork — it's a stale comment carried over from an earlier endjin revision. So the reflective branch (field.GetValue(target)/property.GetValue(target), L461/L465) is always active whenever aMemberExpressionresolves to a captured constant outside the theorem's parameter.Why the test suite is green but notebooks crash
The xUnit suite (
Z3.Linq.Tests, 6/6 passing — verified local baseline) captures loop indices in locals (int i1 = i; theorem = theorem.Where(g => g.Cells[i1][j1]...), seeCollectionHandlingTests.cs). In a single assembly,field.GetValue(target)succeeds: the compiler-generated closure field lives on a type in the same assembly, so reflection finds it.In Polyglot Notebooks (.NET Interactive), each cell compiles to a separate dynamic assembly (
Submission#0,Submission#1, ...). When a constraint lambda in cell N captures a variable declared in cell M (M != N), thetargetobject is a closure instance typed asSubmission#M, but theFieldInfowas resolved againstSubmission#N→field.GetValue(target)throwsArgumentException: Field 'X' defined on type 'Submission#N' is not a field on the target object of type 'Submission#M'.PartialEvalis already used in the visitor — just not on constraint bodiesPartialEvaluator.PartialEvalis invoked atExpressionVisitor.cs:177,:229,:344— but only on array-index sub-expressions and a substitution case, never on the whole constraint body.Theorem.AssertConstraints(Theorem.cs:149-190) callsExpressionVisitor.Visit(..., constraint.Body, ...)directly at L176 with no prior partial evaluation.Proposed fix (concrete, surgical)
In
Theorem.AssertConstraints, fold captured constants before visiting:This rewrites captured-constant member accesses into literal
ConstantExpressions beforeVisitMember's reflective branch ever runs — neutralizing the cross-assembly reflection.PartialEvaluator/ExpressionInterpreterare already referenced in the same file's namespace, so no new dependency.Repro challenge
The crash is specific to .NET Interactive's per-cell dynamic-assembly model — it does not reproduce in a single-assembly xUnit test (which is why it stayed invisible). Faithfully reproducing it requires either a Polyglot Notebook execution or a dynamically-compiled multi-assembly repro. The fix can be validated by: (1) the existing 6/6 suite stays green (PartialEval is a no-op on same-assembly captures — same result), and (2) a Polyglot Notebook cross-cell repro stops crashing.
Happy to open the PR with the one-line
AssertConstraintschange + a notebook repro if you want it.fix(theorem): partial-eval constraint body before visiting (cross-sub…
jsboige commentedon Jun 24, 2026
Closed by #3 (merged fc44dfa).
Fix summary:
Theorem.AssertConstraintsnow callsPartialEvaluator.PartialEval(constraint.Body, ExpressionInterpreter.Instance)beforeExpressionVisitor.Visit, folding host-captured constants into literalConstantExpressions. This sidesteps the reflectivefield.GetValue(target)resolution inExpressionVisitor.VisitMember(~L461) that crashed across .NET Interactive's dynamic-assembly boundaries.Validation:
dotnet test Z3.Linq.Tests: 6/6 passing (no regression — PartialEval is a no-op for same-assembly captures, which is why the xUnit suite never caught the bug).solutions/polyglot-repro/CrossSubmissionCaptureRepro.ipynb: cross-submission capture now solves7,8,9instead of raisingArgumentException: Field 'b0' ... Submission#N ... Submission#M.Root-cause analysis + fix direction originally posted in this issue (comment 2026-06-24); the fix was implemented and empirically validated in PR #3. CoursIA submodule pointer bumped in jsboige/CoursIA#4124.
Thanks to the root-cause note about
SimplifyLambdabeing vestigial (only a stale comment at ExpressionVisitor.cs:447) — confirmed firsthand: the reflective path is always active, which is exactly what PartialEval neutralizes.chore(submodule): bump Z3.Linq to fix/assertconstraints-cross-submiss…
fix(theorem): partial-eval constraint body before visiting (cross-sub…