Skip to content

feat: resurrect CollectionHandling — wire Constants mode end-to-end - #1

Merged
jsboige merged 1 commit into
mainfrom
feature/collection-handling-constants
Jun 14, 2026
Merged

jsboige merged 1 commit into
mainfrom
feature/collection-handling-constants

Conversation

@jsboige

@jsboige jsboige commented Jun 14, 2026

Copy link
Copy Markdown

What

Resurrects the previously-dead CollectionHandling enum { Constants, Array }. Until this PR, the enum and the MultipleEnvironment class were defined but never wired — pure dead code. This PR wires the Constants mode end-to-end (environment construction → constraint visiting → model extraction), alongside the existing Array mode.

Why

North (user, via ai-01 2026-06-14): "il faudrait réparer cette enum pour que tout soit correctement pris en charge (...) basculer sur notre fork z3.Linq partout (...) on le ressuscite !". The goal is to make the fork the single source of truth for the CoursIA Z3 notebook series, with both collection modes genuinely functional.

Changes

Library (Z3.Linq/):

  • Theorem.cs: DefaultCollectionHandling property (default Array — anti-régression); GetEnvironment Constants branch (MultipleEnvironment); ExtractCollection Constants branch reading SubEnvironments.
  • ExpressionVisitor.cs: refactored VisitMember (extracted GetMemberHierarchy/ResolveMemberEnvironment); new VisitArrayIndex that resolves via MultipleEnvironment in Constants mode, falls back to MkSelect in Array mode; recursive TryResolveArrayEnvironment handling both MemberExpression (Cells[i]) and nested BinaryExpression ArrayIndex (Cells[i][j]).
  • Theorem{T}.cs: Where propagates DefaultCollectionHandling.
  • Z3Context.cs: DefaultCollectionHandling property, propagated to created theorems.

Tests (Z3.Linq.Tests/ — new xUnit project):

  • CollectionHandlingTests: 6 tests — Sudoku 4x4 in both modes (Array + Constants), flat int[] Triple in Constants, hint honored both modes, default=Array anti-regression guard.

Anti-régression (HARD)

  • Default mode = Array = behavior unchanged. The user's hand-typed nested int[][] support (commit 096a638) is preserved byte-identical.
  • The Array mode + nested-array Object branch in GetEnvironment/ExtractCollection are untouched.

Verification

  • dotnet build solutions/Z3.Linq.sln -c Release → SUCCESS (lib + tests + examples)
  • dotnet test solutions/Z3.Linq.Tests/Z3.Linq.Tests.csproj → 6/6 PASS (Array + Constants modes)
  • NB-04 (nested-array, Array mode) remains green = 0 regression.

See #1206 (CoursIA epic — co-évolution lib+notebook).

🤖 Generated with Claude Code

Co-Authored-By: Claude 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: Claude <noreply@anthropic.com>
@jsboige
jsboige merged commit 62c277f into main Jun 14, 2026
@jsboige
jsboige deleted the feature/collection-handling-constants branch June 14, 2026 14:04
jsboige added a commit that referenced this pull request Nov 4, 2026
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>
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