Skip to content

Add samples ported from SAT/SMT by Example - #120

Open
HowardvanRooijen wants to merge 10 commits into
mainfrom
feature/sat-smt-by-example-samples
Open

HowardvanRooijen wants to merge 10 commits into
mainfrom
feature/sat-smt-by-example-samples

Conversation

@HowardvanRooijen

@HowardvanRooijen HowardvanRooijen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Adds 11 new demos, with models in Z3.Linq.Examples and tests in Z3.Linq.Tests. Each one is based on a problem from Dennis Yurichev's book SAT/SMT by Example. The library itself is unchanged: every sample uses features Z3.Linq already supports.

Samples

Demo Book section What it shows
bit-twiddling.cs §3.7, §4.3–4.12 Proofs on uint/ulong bit-vectors: XOR swap, Dietz's average, branchless abs/min/max, the has-a-zero-byte hack, modular inverse. When TrySolve finds no counterexample, that is the proof.
alphametics.cs §8.10 SEND + MORE = MONEY with Z3Methods.Distinct
zebra.cs §8.2 The Zebra puzzle over a nested environment type
n-queens.cs §8.4 N queens on an array whose size comes from a template
assignment.cs §14.5 Cost minimised with ternary lookups
wood-workshop.cs §3.3 Kantorovich's integer program
knapsack.cs §10.1, §3.2 Popsicles (maximisation), and every XKCD 287 order, listed by excluding each answer in turn
package-manager.cs §22.5 Dependency resolution; a version conflict comes back as unsatisfiable
map-colouring.cs §9.1–9.2 Europe needs 4 colours, and 3 is proved impossible; committee scheduling
job-shop.cs §22.8 Minimum makespan 11, with 10 proved impossible; a Gantt chart
sudoku-variants.cs §8.1.3–8.1.4 Killer and Greater-Than Sudoku

The menu, demos/README.md and the root README list the new demos. The root README also has a short XOR-swap proof example.

Notes for review

  • Sudoku variants use a new uint[81] grid, SudokuGrid. Z3 bit-blasts bit-vectors, so these puzzles solve in about 1 s, where an int grid took minutes. The existing SudokuTable and SudokuTheorem are unchanged. Cells are read through Cell(row, col) rather than an indexer, because Z3.Linq writes every public property back through a setter and an indexer has no setter.
  • The Greater-Than clue list in the book allows more than one grid. The book's source repeats one sign, which suggests a sign was lost. The demo shows Z3's grid next to the book's, and the tests check only invariants: a valid Sudoku that obeys all 48 signs.
  • The package manager does not copy the book's objective. The book maximises a sum where 999 means "not installed", which rewards leaving a library out. Here an uninstalled library is version 0, and the objective installs only the libraries the programs need, at their newest versions.
  • The new Sudoku tests are the slowest in the suite. The Killer uniqueness proof takes about 2 s when the suite runs in parallel.
  • The book is published under CC BY-SA 4.0 (the badge on smt.st is the only licence statement). Only problem data (numbers, clues, constraints) was taken from it, and all prose and code are new. Each file credits the book and its section, and the READMEs note the licence.

Testing

  • dotnet build solutions/Z3.Linq.slnx: 0 warnings, 0 errors
  • dotnet test: 457 of 457 passed (368 existing and 89 new)
  • Ran each of the 11 new demos with dotnet run and no input. All exited 0, printed no warnings and showed the expected answers.
  • Not yet checked: demos/menu.cs, which is interactive and needs a real terminal.

🤖 Generated with Claude Code

HowardvanRooijen and others added 6 commits October 28, 2026 10:30
Proves XOR and add/subtract swap, Dietz's average, branchless abs and
min/max, the has-a-zero-byte test and the multiplicative inverse of 3
modulo 2^32 by asking Z3 for a counterexample. Shows counterexamples
for the naive average and the misprinted zero-byte test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ports sections 8.10, 8.2.1 and 8.4 of Dennis Yurichev's book as Examples models, MSTest suites and single-file Spectre.Console demos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Map colouring of Europe (4 colours; 3 unsatisfiable; fewest/most of one
colour 3/22) and committee scheduling (3 hours) share one GraphColouring
model. Job-shop scheduling finds the 11-unit optimum for the book's
three-job instance and proves 10 unsatisfiable. Each has a Spectre.Console
demo and MSTest acceptance tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rom SAT/SMT by Example

Four single-file demos (sections 14.5, 3.3, 10.1 + 3.2, 22.5.1), a reusable
PackageManager model in Z3.Linq.Examples, and tests that pin each unique optimum
and prove it unique.

The package manager uses version 0 for "not installed" and minimises an explicit
install cost, instead of the book's 999 sentinel and maximised version sum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds a uint bit-vector Sudoku grid (SudokuGrid / SudokuGridTheorem) with
WithClues, WithCages, WithGreaterThan and Excluding, the puzzle data from
sections 8.1.4 and 8.1.3, a demo, and tests. Killer is pinned and proved
unique; the Greater-Than signs admit several grids, so it is tested by
invariants only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the eleven new demos to the interactive launcher (and raise its page
size so they all show), the demos README table and run list, and a section
in the root README with a bit-twiddling proof example. Widen the Examples
package description and tags to cover the new samples.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 28, 2026 10:48
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Test Results

  1 files  ±  0    1 suites  ±0   17s ⏱️ +4s
450 tests +103  450 ✅ +103  0 💤 ±0  0 ❌ ±0 
473 runs  +105  473 ✅ +105  0 💤 ±0  0 ❌ ±0 

Results for commit 9241268. ± Comparison against base commit 964f177.

♻️ This comment has been updated with latest results.

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

Unconstrained objective variables cause empty graphs and empty job workloads to return int.MinValue instead of 0, requiring unconditional nonnegative bounds on Colours and Makespan.

2 open issues
What changed in this PR

Adds 11 demos adapted from SAT/SMT by Example, expanding the examples and tests without changing the core Z3.Linq library.

Changes:

  • Adds puzzle, optimisation, scheduling, dependency-resolution and bit-vector proof examples.
  • Adds tests for solutions, invariants, optimality, uniqueness and unsatisfiable cases.
  • Updates the interactive launcher, documentation and example-package metadata.
File Description
solutions/​Z3.Linq.Tests/​ZebraPuzzleTests.cs Checks house assignments and uniqueness.
solutions/​Z3.Linq.Tests/​WoodWorkshopTests.cs Verifies the unique optimal cutting plan.
solutions/​Z3.Linq.Tests/​SudokuGridTheoremTests.cs Tests Sudoku variants, clues and uniqueness.
solutions/​Z3.Linq.Tests/​PackageManagerTests.cs Checks optimal installation plans and conflicts.
solutions/​Z3.Linq.Tests/​NQueensTests.cs Tests valid placements and impossible sizes.
solutions/​Z3.Linq.Tests/​KnapsackTests.cs Verifies optimal purchases and enumerated orders.
solutions/​Z3.Linq.Tests/​JobShopTests.cs Checks scheduling rules and minimum makespan.
solutions/​Z3.Linq.Tests/​GraphColouringTests.cs Tests colour counts and graph constraints.
solutions/​Z3.Linq.Tests/​BitTwiddlingTheoremsTests.cs Checks proofs, counterexamples and modular inverses.
solutions/​Z3.Linq.Tests/​AssignmentProblemTests.cs Verifies assignment optimality independently.
solutions/​Z3.Linq.Tests/​AlphameticsTests.cs Tests SEND + MORE = MONEY and uniqueness.
solutions/​Z3.Linq.Examples/​Z3.Linq.Examples.csproj Updates package description and tags.
solutions/​Z3.Linq.Examples/​Sudoku/​SudokuGridTheorem.cs Adds Sudoku constraints and solution exclusion.
solutions/​Z3.Linq.Examples/​Sudoku/​SudokuGrid.cs Adds an unsigned bit-vector Sudoku grid.
solutions/​Z3.Linq.Examples/​Sudoku/​KillerSudokuPuzzles.cs Supplies Killer Sudoku cage data.
solutions/​Z3.Linq.Examples/​Sudoku/​GreaterThanSudokuPuzzles.cs Supplies inequalities and the book’s answer.
solutions/​Z3.Linq.Examples/​Scheduling/​JobShop.cs Models task precedence and machine constraints.
solutions/​Z3.Linq.Examples/​Puzzles/​ZebraPuzzle.cs Models the Zebra puzzle with nested properties.
solutions/​Z3.Linq.Examples/​Puzzles/​NQueens.cs Models variable-size queen placements.
solutions/​Z3.Linq.Examples/​Puzzles/​Alphametics.cs Models the alphametic and digit conversion.
solutions/​Z3.Linq.Examples/​Packaging/​PackageManager.cs Models dependencies and installation cost.
solutions/​Z3.Linq.Examples/​GraphColouring/​GraphColouring.cs Adds reusable graph-colouring constraints.
solutions/​Z3.Linq.Examples/​GraphColouring/​Europe.cs Supplies country and border data.
solutions/​Z3.Linq.Examples/​GraphColouring/​Committees.cs Converts shared membership into scheduling conflicts.
solutions/​Z3.Linq.Examples/​BitTwiddling/​BitTwiddlingTheorems.cs Adds counterexample searches and inverse models.
README.md Introduces the ports and XOR-swap proof.
demos/​zebra.cs Displays solved houses and puzzle answers.
demos/​wood-workshop.cs Displays the optimal cutting plan.
demos/​sudoku-variants.cs Displays cages, inequalities and solved grids.
demos/​README.md Lists new demos and run commands.
demos/​package-manager.cs Displays installation plans and conflicts.
demos/​n-queens.cs Displays placements and unsatisfiable boards.
demos/​menu.cs Registers demos and enlarges the menu page.
demos/​map-colouring.cs Demonstrates map colouring and committee scheduling.
demos/​knapsack.cs Optimises purchases and enumerates exact-cost orders.
demos/​job-shop.cs Displays a Gantt chart and optimality check.
demos/​bit-twiddling.cs Displays proof verdicts and modular inverses.
demos/​assignment.cs Displays the least-cost worker assignment.
demos/​alphametics.cs Displays the solution and uniqueness check.

Review effort: Balanced

HowardvanRooijen and others added 4 commits October 28, 2026 11:10
An empty graph, or a job shop with no tasks, left the objective symbol with
no constraint at all, so minimising it returned the library's implicit
int.MinValue instead of 0. Constrain both to be non-negative up front and
cover the empty cases with tests.

Addresses the Copilot review comments on #120.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Bit twiddling: the zero-byte variant without ~v is no longer called the
  book's misprint (the book's SMT-LIB model uses bvnot; the tilde is lost
  from its code font in text extraction), and the text now says it fails
  both ways. Modular inverses are described as exact division and
  divisibility tests, not general division by a constant.
- Package manager: say that the book's 999 encoding gives the same plans
  for this catalogue, and rename a test that exercises Resolve.
- N queens: enumerate every 4 x 4 placement, so a model that is too strict
  would fail; bring the demo header up to date.
- Soften test timing remarks to what a run actually shows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Say int cells took about a minute (Greater-Than) or several (Killer),
  not minutes for both, and drop the Z3-version-specific "27 cells".
- Pin the Greater-Than data at 48 distinct signs and the Killer data at
  25 cages over 77 cells summing to 384, so a dropped clue fails a test.
- Spot-check the Europe borders, including the four mutual neighbours
  that make three colours impossible.
- Reflow the committee panel to fit 80 columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The book is CC BY-SA 4.0. The samples reuse only the problems' data, so
say "based on a problem in" rather than "adapted from" or "ported from",
and note the book's licence and what was reused in both READMEs.

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

Copy link
Copy Markdown

Code Coverage Summary Report - Linux (No TFM)

Summary
Generated on: 10/28/2026 - 12:09:05
Parser: Cobertura
Assemblies: 2
Classes: 33
Files: 29
Line coverage: 92.8% (1281 of 1379)
Covered lines: 1281
Uncovered lines: 98
Coverable lines: 1379
Total lines: 4408
Branch coverage: 81.5% (708 of 868)
Covered branches: 708
Total branches: 868
Method coverage: Feature is only available for sponsors

Coverage

Z3.Linq - 93.8%
Name Line Branch
Z3.Linq 93.8% 82.2%
Z3.Linq.Environment 100%
Z3.Linq.ExpressionVisitor 95.6% 83.3%
Z3.Linq.SolveableExtensions 100% 100%
Z3.Linq.Symbols 100% 66.6%
Z3.Linq.Theorem 92.2% 80.7%
Z3.Linq.Theorem.DeferredSolvable 100%
Z3.Linq.Theorem.TypeMappingEntry 100%
Z3.Linq.Theorem<T, TResult> 92.2% 80.7%
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 - 91.3%
Name Line Branch
Z3.Linq.Examples 91.3% 77.4%
Z3.Linq.Examples.BitTwiddling.BitTwiddlingTheorems 100% 100%
Z3.Linq.Examples.GraphColouring.Committees 100% 100%
Z3.Linq.Examples.GraphColouring.Europe 100% 75%
Z3.Linq.Examples.GraphColouring.GraphColouring 100% 100%
Z3.Linq.Examples.Packaging.PackageManager 93.9% 0%
Z3.Linq.Examples.Puzzles.Alphametics 100% 100%
Z3.Linq.Examples.Puzzles.NQueens 100% 100%
Z3.Linq.Examples.Puzzles.QueensBoard 100%
Z3.Linq.Examples.Puzzles.ZebraPuzzle 100% 100%
Z3.Linq.Examples.RiverCrossing.MissionariesAndCannibals 87.5% 66.6%
Z3.Linq.Examples.Scheduling.JobShop 100% 100%
Z3.Linq.Examples.Sudoku.GreaterThanSudokuPuzzles 100%
Z3.Linq.Examples.Sudoku.KillerSudokuPuzzles 100%
Z3.Linq.Examples.Sudoku.SudokuGrid 14.2% 0%
Z3.Linq.Examples.Sudoku.SudokuGridTheorem 98.2% 86.1%
Z3.Linq.Examples.Sudoku.SudokuTable 0% 0%
Z3.Linq.Examples.Sudoku.SudokuTheorem 100% 100%
Z3.Linq.Examples.Sudoku.SudokuTheoremRewriter 100%

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