Repository navigation
Conversation
|
Very interesting - revisiting your suggestion in #29 has been on my backlog for a while - especially as we could leverage some agentic AI to help with any of the integration. |
|
Thanks Howard — read your note on #29 and the stack (congrats on the sweep, #110 looks like the right capstone). Three quick things on this PR: Scope reframed beyond .NET Interactive. Now that Polyglot Notebooks are dropped upstream, I want to be explicit that this fix is not Interactive-specific: any host that compiles expression trees with closures across assembly boundaries — dynamic assemblies ( Test, as offered. A real test for this rides with the test-project PR I promised on #29 rather than ballooning this one: the honest xUnit reproduction needs the closure field to live on a foreign type, which single-assembly tests cannot produce by construction — so the design is a small compiled-on-the-fly assembly (Roslyn scripting) that captures a local and hands the constraint across, making the pre-fix Targeting. The two scoping questions from #29 still stand (main vs staging branch, anything to leave alone) — happy to aim this and the upcoming PRs at the tip of your stack if that saves you rebase work; on our side the drift adaptation is agent-driven, matching the workflow you described. |
|
The reproduction I offered earlier is in — and it landed one branch over rather than inside this PR, to keep the fix diff surgical (this branch still touches exactly the two files). Where it lives: Red on 1 failed / 0 passed — the exact exception from the wild, down to the submission numbering. Green on this PR's branch: 1 passed / 0 failed, same test, same machine, only the partial-eval change in between. The single added dependency is |
|
Rebased onto the tip of the pile, as requested — published as The branch of this PR is deliberately untouched: its base is still What the rebase had to adaptTwo things, both measured on the rebased tree rather than guessed:
The Test results on the rebased headBuild is clean ( One honest caveat: the suite passes identically without this fix — it does not discriminate it. That is expected: the defect does not reproduce in ordinary compilation, where a captured constant's declaring type and the target object's type agree. It reproduces where they do not, i.e. across .NET Interactive submissions, each of which is its own dynamic assembly: The pile still resolves captured members reflectively ( |
|
Do you want to try and rebase this onto the latest in main now that the stacked PR is merged? |
|
Yes — and thank you for the pointer. I rebased onto current The two API breaks
After those two adaptations the rebased branch builds clean on the current The real conflict, with numbers Your #118 added Deterministic comparison, same machine, same SDK, full suite run twice:
Why I still think the fix is worth having, and where I could be wrong. Our case is a capture that crosses .NET Interactive submissions: the constraint body holds a member access whose target object is an instance of a different dynamic assembly than the field declaring type, which is what produced What is clearly wrong is the breadth of the fold: partial-evaluating every closed subtree is a sledgehammer that also swallows the null diagnostic you just wrote. Before pushing anything I would narrow it — fold only when the subtree actually crosses a submission boundary (or leave the tree untouched and fall back when the fold is not a clean constant), so Two questions, and I am happy to be told either answer:
Nothing has been pushed — the branch is still at |
feat: add nested int[][] array support to Z3.Linq theorem environment Port the nested collection handling from Z3.LinqBinding@EPFdevelopment (commit a94ecce) into the modern split architecture (Theorem + ExpressionVisitor + Environment). Changes: - Environment.cs: add CollectionHandling enum + MultipleEnvironment class - Theorem.cs: unify GetEnvironment(Type, prefix, isArray) with scalar leaf handling, nested array sort construction (Array Int (Array Int Int)), and ExtractCollection recursive extraction for int[][] solution rebuild - ExpressionVisitor: nested array access (Cells[i][j]) works natively via existing ArrayIndex/MkSelect chaining - SudokuGrid.cs + SudokuGridTheorem.cs: smoke test using int[][] Cells with standard Sudoku constraints (range 1-9, distinct rows/columns/boxes) Verified: Theorem<SudokuGrid> with int[][] Cells compiles and solves a 9x9 Sudoku grid in ~595ms. All existing examples (Missionaries & Cannibals, SudokuTable, ValueTuples, Symbols) pass unchanged — no regression. dotnet build Z3.Linq.sln: 0 warnings, 0 errors. See #1206 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> @
The CollectionHandling enum { Constants, Array } and the MultipleEnvironment
class were defined but never referenced — dead code. The library hardcoded the
Array mode (Z3 array theory via MkArrayConst + Select/Store, incl. nested
int[][]). This commit resurrects the Constants mode (one Z3 constant per
element, lazily created on index access) as a selectable alternative, while
preserving Array as the default so existing behavior is unchanged.
Library (solutions/Z3.Linq/):
- Theorem.DefaultCollectionHandling property (default = Array, anti-regression).
- GetEnvironment switches on the mode: Constants -> MultipleEnvironment,
Array -> existing MkArrayConst branch (nested-array Object branch preserved).
- ExtractCollection reads Constants-mode collections from SubEnvironments
(incl. nested int[][] via per-row MultipleEnvironment recursion). Scalar
conversion factored into ConvertScalarExpr, shared by both modes.
- ExpressionVisitor: new VisitArrayIndex resolves Cells[i] (and nested
Cells[i][j] recursively) against a MultipleEnvironment, materializing one
Z3 constant per element (Cells_i, Cells_i_j). ResolveMemberEnvironment /
GetMemberHierarchy factored out of VisitMember to support this. Array mode
unchanged (falls back to MkSelect).
- Z3Context.DefaultCollectionHandling propagated to NewTheorem<T>().
- Theorem<T>.Where propagates the mode across constraint chains.
Tests (new solutions/Z3.Linq.Tests/, xUnit):
- Sudoku 4x4 (int[][]) solvable in BOTH modes (Array = array theory,
Constants = per-element constants) — keystone proving both paths work
end-to-end for nested arrays.
- Flat int[] in Constants mode (Triple: three cells == 7).
- Hint honored in both modes.
- Default = Array guard (anti-regression).
dotnet test: 6/6 PASS. Array mode unchanged (nested int[][] Sudoku green).
Co-authored-by: jsboigeEpita <jean-sylvain.boige@epita.fr>
Co-authored-by: Claude <noreply@anthropic.com>
…mission capture) (#3) Theorem.AssertConstraints now calls PartialEvaluator.PartialEval on each constraint body before ExpressionVisitor.Visit. This folds host-captured constants (locals declared outside the theorem parameter scope, e.g. in a different .NET Interactive submission or closure) into literal ConstantExpressions, sidestepping the reflective MemberExpression resolution in ExpressionVisitor.VisitMember that crashed across dynamic-assembly boundaries. Bug: in .NET Interactive / Polyglot Notebooks, each cell compiles as a separate dynamic assembly (Submission#N). When a constraint lambda captures a local declared in another submission, VisitMember resolved the FieldInfo on the lambda submission type while the closure target carried the declaring submission type: System.ArgumentException: Field 'b0' defined on type 'Submission#N' is not a field on the target object of type 'Submission#M' This was invisible in xUnit (single-assembly) because the closure and the field live in the same assembly there. The fix is a no-op for same-assembly captures (PartialEval folds them to the same literal VisitMember would have produced via reflection), so all existing tests pass unchanged. Subtrees referencing the theorem parameter (e.g. t.Values[i]) are preserved (not evaluable without a parameter binding). Validation: - dotnet test: 6/6 passing (Z3.Linq.Tests, both CollectionHandling modes) - polyglot-repro/CrossSubmissionCaptureRepro.ipynb: cross-submission capture now solves (7,8,9); before the fix it raised ArgumentException. See #2. Developed with AI assistance (GLM-5.2) as part of the CoursIA pedagogical Z3.Linq series (https://github.com/jsboige/CoursIA).
Build green (0 warnings under TreatWarningsAsErrors), 371/372 tests passing. Aligns the rebased partial-eval branch with the newer upstream base: GetEnvironment/GetSolution call sites after the expression-visitor rewrite (endjin#100); ExtractCollection now uses upstream template-length sizing, the complete ReadZ3Value scalar conversion, EvaluateWithCompletion for free elements, and the no-length NotSupportedException parity. Keystone CollectionHandling tests moved to MSTest.Sdk/Shouldly. One expected failure remains: Solve_MemberChainThroughANull_ThrowsNamingTheExpression (endjin#118) - the global PartialEval fold preempts the null member-chain path. Co-Authored-By: Claude-Code <noreply@anthropic.com>
c62253e to
7a4f0d0
Compare
|
Rebased onto current
The rebase also required porting the nested |
|
Thank you for this PR, the repro notebook and the detailed follow-ups. They made it much easier to find the cause. PR #43 contains three different changes, so I've split it into a stack of smaller PRs and rewritten each one in the style of the rest of the codebase:
The cross-submission capture is already fixed by #118When a later submission uses a local from an earlier one, the constraint reaches the visitor as a member chain that starts at a constant: Before #118, We checked this in two ways:
So #122 adds no library fix, only the test, the demo and one comment line. We left out the general Nested collections (#123)
A row index chosen by a symbol, such as Why the
|
Superseded by #118 — closing with the measurementHoward's note of 5 Nov is right, and I verified it firsthand rather than taking it on trust: checked out 371 / 371 passed, 0 failed — the cross-assembly capture test included, with none of this PR's code. Two further reasons not to push this branch:
Thank you for the careful read and the rewrite, Howard — the split made the fix and the tests better than this PR would have. Closing as superseded. |
Summary
Fixes a crash in
Theorem.AssertConstraintswhen the constraint lambda captures a local declared in a different .NET Interactive / Polyglot Notebooks submission.Theorem.AssertConstraintsnow runsPartialEvaluator.PartialEvalon each constraint body beforeExpressionVisitor.Visit. This folds host-captured constants (locals declared outside the theorem parameter scope, e.g. in a different.NET Interactivesubmission or closure) into literalConstantExpressions, sidestepping the reflectiveMemberExpressionresolution inExpressionVisitor.VisitMemberthat crashed across dynamic-assembly boundaries.Bug
In .NET Interactive / Polyglot Notebooks, each cell compiles as a separate dynamic assembly (
Submission#N). When a constraint lambda captures a local declared in another submission,VisitMemberresolved theFieldInfoon the lambda submission type while the closure target carried the declaring submission type:This was invisible in xUnit (single-assembly) because the closure and the field live in the same assembly there.
Repro (manual, .NET Interactive)
Two consecutive cells in a .NET Interactive notebook (Polyglot Notebooks):
Before the fix,
Solve()raisedArgumentException: Field 'b0' defined on type 'Submission#N' is not a field on the target object of type 'Submission#M'.After the fix,
Solve()returns the expected modelx = 0.Fix
Theorem.AssertConstraintsnow callsPartialEvaluator.PartialEvalon each constraint body before handing it toExpressionVisitor.Visit. The partial evaluator folds host-captured constants (locals declared outside the theorem parameter scope) into literalConstantExpressions, soVisitMemberis never asked to resolve them reflectively.The fix is a no-op for same-assembly captures (PartialEval folds them to the same literal
VisitMemberwould have produced via reflection), so all existing xUnit tests pass unchanged.Subtrees referencing the theorem parameter (e.g.
t.Values[i]) are preserved — they are not evaluable without a parameter binding and are left for the visitor to handle..gitignore
Adds
.deploy/to the ignore list — used bydotnet publish -o .deployfor the polyglot repro infrastructure.Tests
This repository has no xUnit test infrastructure covering the cross-submission capture scenario (the bug is invisible there by construction — single-assembly), so no new test is included. The repro above is the canonical failing case and is exercised in the companion pedagogical repository (
MyIA.AI.Notebooks/ML/DataScienceWithAgents/02-ML-Cours/2.8b-Theorie-PAC-Lean.ipynbfamily + the standalonepolyglot-repro/CrossSubmissionCaptureRepro.ipynbreproduction).Notes
Cherry-picked from a downstream fork (
MyIntelligenceAgency/Z3.Linq, branchfix/c805-fc44dfa-partial-eval-cross-submission). The companion reproduction notebook and downstream.csprojglue are intentionally not included in this PR — they belong in the downstream pedagogical repo, not here.