Skip to content

Add a Sudoku example over an int[][] environment - #124

Open
HowardvanRooijen wants to merge 4 commits into
feature/nested-collection-symbolsfrom
feature/sudoku-grid-example
Open

HowardvanRooijen wants to merge 4 commits into
feature/nested-collection-symbolsfrom
feature/sudoku-grid-example

Conversation

@HowardvanRooijen

@HowardvanRooijen HowardvanRooijen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Part of stack #125: #122 → #123 → #124. Stacked on #123; this PR shows only the Sudoku example.

Summary

Adds a 9x9 Sudoku example whose environment is an int[][]. This is the Sudoku from #43, rewritten in the style of the existing SudokuTable/SudokuTheorem example.

  • solutions/Z3.Linq.Examples/Sudoku/SudokuGrid.cs: int[][] Cells, set up as 9 rows of 9, with a grid ToString().
  • solutions/Z3.Linq.Examples/Sudoku/SudokuGridTheorem.cs:
    • Create adds a 1-9 range constraint for each cell, and 27 Distinct constraints for the rows, columns and boxes.
    • Given(row, column, value) builds the constraint for one given cell.
    • The expression trees are built by hand with literal indices. Nothing is captured from a closure, so ToString() and the logs stay readable.
  • solutions/Z3.Linq.Tests/SudokuGridTheoremTests.cs: uses the same published puzzle and expected solution as SudokuTheoremTests, so the two examples can be compared.
    • The solution matches the published one.
    • The grid is valid and all 36 givens are unchanged.
    • Contradictory givens give null.

No demo, README or project file changes. demos/sudoku.cs already shows Sudoku.

Timing

Release build, same puzzle. Each run covers building the theorem, solving it and reading the answer back.

First run Next five runs
SudokuGridTheorem (int[][]) 150 ms 35-57 ms
SudokuTheorem (existing) 40 ms 34-44 ms

The first int[][] run is slower because it is the first solve in the process.

Verification

  • dotnet build solutions/Z3.Linq.slnx: 0 warnings, 0 errors.
  • dotnet test --solution solutions/Z3.Linq.slnx: 387 of 387 passed.

🤖 Generated with Claude Code

SudokuGrid holds the puzzle as one int[][] symbol, nine rows of nine,
instead of the 81 scalar properties of SudokuTable. SudokuGridTheorem
builds the same rules as SudokuTheorem - every cell between 1 and 9,
and 27 Distinct constraints over the rows, columns and boxes - as
expression trees whose cells are g.Cells[row][column] with the indices
as literal constants, so nothing is captured from a closure. Given
builds a given the same way. It shows a realistic-size use of nested
collection symbols: 81 nested selects over one array, read back row by
row. See #43.

SudokuGridTheoremTests mirrors SudokuTheoremTests with the same
published puzzle and expected solution, so the two can be compared:
the known solution cell by cell, the grid's invariants with the givens
preserved, and contradictory givens reporting unsatisfiable.

Measured with a Stopwatch in Release, six runs each on that puzzle,
building the theorem, solving and reading back: the int[][] theorem
took 35-57 ms (150 ms on the first, cold run), the existing
SudokuTheorem 34-44 ms. The nested array costs next to nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Invalid row or column indices can constrain off-grid elements, allowing solving to succeed while the returned grid silently omits a given, so coordinate validation is needed.

1 open issue
What changed in this PR

Adds an int[][] Sudoku example alongside the existing scalar-property example, demonstrating the nested collection support introduced in #123.

Changes:

  • Introduces a pre-sized 9×9 grid with formatted text output.
  • Builds cell-range, row, column, box, and given-cell constraints using expression trees with literal indices.
  • Adds tests for the published solution, grid validity, preserved givens, and contradictory givens.
File Description
solutions/​Z3.Linq.Tests/​SudokuGridTheoremTests.cs Tests solution correctness, Sudoku rules, and contradictory givens.
solutions/​Z3.Linq.Examples/​Sudoku/​SudokuGridTheorem.cs Builds Sudoku constraints over nested-array cells.
solutions/​Z3.Linq.Examples/​Sudoku/​SudokuGrid.cs Defines and formats the 9×9 nested-array environment.

Review effort: Balanced

HowardvanRooijen and others added 3 commits November 5, 2026 13:39
The grid is a Z3 array indexed by any integer, so Given(9, 0, 5)
constrained an element the 9x9 grid never reads back, and the puzzle
solved with the given silently missing. Given now throws
ArgumentOutOfRangeException for a row or column outside 0 to 8. Found
by review on #124.

Checked by mutation: without the guard, all four out-of-grid cases of
the new test fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Code Coverage Summary Report - Linux (No TFM)

Summary
Generated on: 11/05/2026 - 13:55:16
Parser: Cobertura
Assemblies: 2
Classes: 21
Files: 18
Line coverage: 90.3% (967 of 1070)
Covered lines: 967
Uncovered lines: 103
Coverable lines: 1070
Total lines: 3260
Branch coverage: 80.2% (653 of 814)
Covered branches: 653
Total branches: 814
Method coverage: Feature is only available for sponsors

Coverage

Z3.Linq - 93.9%
Name Line Branch
Z3.Linq 93.9% 81.8%
Z3.Linq.Environment 100%
Z3.Linq.ExpressionVisitor 95.1% 81.5%
Z3.Linq.SolveableExtensions 100% 100%
Z3.Linq.Symbols 100% 66.6%
Z3.Linq.Theorem 92.7% 81.1%
Z3.Linq.Theorem.DeferredSolvable 100%
Z3.Linq.Theorem.TypeMappingEntry 100%
Z3.Linq.Theorem<T, TResult> 92.7% 81.1%
Z3.Linq.Theorem 100%
Z3.Linq.TheoremGlobalRewriterAttribute 100%
Z3.Linq.TheoremPredicateRewriterAttribute 100%
Z3.Linq.TheoremUndecidedException 100%
Z3.Linq.TheoremVariableTypeMappingAttribute 100%
Z3.Linq.Z3Context 100% 100%
Z3.Linq.Z3Methods 100%
Z3.Linq.Examples - 70%
Name Line Branch
Z3.Linq.Examples 70% 56%
Z3.Linq.Examples.RiverCrossing.MissionariesAndCannibals 87.5% 66.6%
Z3.Linq.Examples.Sudoku.SudokuGrid 5% 0%
Z3.Linq.Examples.Sudoku.SudokuGridTheorem 100% 100%
Z3.Linq.Examples.Sudoku.SudokuTable 0% 0%
Z3.Linq.Examples.Sudoku.SudokuTheorem 100% 100%
Z3.Linq.Examples.Sudoku.SudokuTheoremRewriter 100%

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Test Results

  1 files    1 suites   11s ⏱️
374 tests 374 ✅ 0 💤 0 ❌
395 runs  395 ✅ 0 💤 0 ❌

Results for commit 348bb93.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants