[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"CqWxEX2nEe":3},"# CSP: A Formalization in Lean of Constraint Satisfaction Problems\n\nThis Lean 4 project formalizes Constraint Satisfaction Problems (CSPs), with proofs of equivalence, equisatisfiability, and symmetry-breaking. The **L2S (LeanToSolver)** framework enables using Lean as a constraint programming language with automatic export to MiniZinc and SMT-LIB.\n\n## Project Structure\n\n```\nCSP/\n├── Core.lean                 # General CSP definitions\n├── Symmetry.lean             # General symmetry theory\n├── Equivalence.lean          # General equivalence theory\n├── GlobalConstraints.lean    # Predefined constraints\n├── Transport.lean            # Type casting utilities\n│\n└── L2S/                      # LeanToSolver framework\n    ├── Core.lean             # HomogeneousCSP foundations\n    ├── Constraints.lean      # 50+ constraint types\n    ├── Translate.lean        # Unified translation API\n    ├── Backends/\n    │   ├── MiniZinc.lean     # MiniZinc code generation\n    │   └── SMTLIB.lean       # SMT-LIB code generation\n    ├── Embedding.lean        # Embedding to heterogeneous CSP\n    ├── Equivalence.lean      # Equivalence theory\n    ├── Symmetry.lean         # Symmetry breaking theory\n    ├── Proofs/               # Verified theorems (see below)\n    └── Tests/                # 32 example problems\n```\n\n## Verified Proofs\n\n### Symmetry Breaking\n\n| File | Problem | Symmetry | Constraint |\n|------|---------|----------|------------|\n| `NQueensSB.lean` | N-Queens | Horizontal reflection | First queen in upper half |\n| `GraphColoringSB.lean` | Graph Coloring | Color swap (0 ↔ c) | Node 0 has color 0 |\n| `LatinSquareSB.lean` | Latin Square | Column permutation | First row sorted |\n\n### Equivalence Proofs\n\n| File | Formulations | Method |\n|------|--------------|--------|\n| `NQueensEquivalence.lean` | 1D (column vars) ↔ 2D (cell vars) | π-equivalence via projection |\n| `GraphColoringEquivalence.lean` | Vertex model ↔ Binary matrix | π-equivalence via one-hot encoding |\n\n### Circuit Optimization Theorems\n\n| File | Result |\n|------|--------|\n| `UnreachableInputElimination.lean` | Inputs with no path to outputs can be fixed to any value |\n| `ParityPathTheorem.lean` | Inputs with uniform parity to all outputs can be optimally fixed |\n\n## Requirements\n\n| Tool | Purpose | Link |\n|------|---------|------|\n| **Lean 4** | Theorem prover (required) | [lean-lang.org/install](https://lean-lang.org/install/) |\n| **MiniZinc** | Constraint solver (optional) | [minizinc.org](https://www.minizinc.org/downloads/) |\n| **Z3** | SMT solver (optional) | [github.com/Z3Prover/z3](https://github.com/Z3Prover/z3/releases) |\n| **CVC5** | SMT solver (optional) | [github.com/cvc5/cvc5](https://github.com/cvc5/cvc5/releases) |\n\nMake sure to have [elan](https://lean-lang.org/install/manual/) installed to avoid building the Mathlib dependencies. Elan auto-downloads the correct Lean version. You can install it this way:\n\n```bash\n# Install git and curl\nsudo apt install git curl\n\n# Install elan (choose 1 to accept the default install option)\ncurl https://elan.lean-lang.org/elan-init.sh -sSf | sh\n\n# Add the elan executables to the PATH\nsource $HOME/.elan/env\n```\n\nOnce `elan` is installed, it will manage all Lean versions for you automatically.\n\n## Building the Project\n\nEven if you have installed Lean and `elan` with using the VS Code extension, you will need to build the project using the following commands to avoid building Mathlib from source:\n\n```bash\n# 1. Clone the repository\ngit clone https://github.com/leansolving/leancsp.git\ncd leancsp\n\n# 2. Download pre-built Mathlib cache (IMPORTANT: saves 30+ min build time)\nlake exe cache get\n\n# 3. Build the project\nlake build\n```\n\nOnce the project is built, you should see the message `Build completed successfully (7277 jobs)`, which confirms that everything worked fine. Then, you will be able to open it in your editor (VS Code recommended) and do not wait for Mathlib to build.\n\n## Usage\n\n### Modelling CSPs\n\nThe L2S framework uses `HomogeneousCSP` with a number of variables and a list of constraints:\n\n```lean\nimport CSP.L2S.Core\nimport CSP.L2S.Constraints\n\nopen CSP.L2S\n\ndef graph_coloring (nodes : ℕ) (edges : List (Fin nodes × Fin nodes)) (colors : ℕ) : HomogeneousCSP :=\n  let bounds := (List.finRange nodes).map fun v => bound v 1 colors\n  let edge_constrs := edges.map fun (u, v) => not_equal u v\n  ⟨nodes, bounds ++ edge_constrs⟩\n```\n\nAvailable constraints: `alldifferent`, `count`, `element`, `sum_eq`, `linear_eq`, `less_than`, `not_equal`, `bound`, and 40+ more (see `L2S/Constraints.lean`).\n\n### Solver Translation\n\nExport CSPs to MiniZinc or SMT-LIB:\n\n```lean\nimport CSP.L2S.Translate\n\nopen CSP.L2S\n\ndef my_csp : HomogeneousCSP := ...\n\n-- Save to file\ndef main : IO Unit := do\n  saveTo my_csp \"model.mzn\" BackendType.MiniZinc\n  saveTo my_csp \"model.smt2\" BackendType.SMTLIB\n```\n\nConvenience functions:\n```lean\ntranslateTo my_csp BackendType.MiniZinc     -- Returns MiniZinc string\ntranslateTo my_csp BackendType.SMTLIB       -- Returns SMT-LIB string\n```\n\n### Solving\n\nAfter generating solver files:\n\n```bash\n# Run Lean to generate files\nlake env lean --run CSP/L2S/Tests/lean/08_queens.lean\n\n# Solve with MiniZinc\nminizinc model.mzn\n\n# Or solve with Z3\nz3 model.smt2\n\n# Or solve with CVC5\ncvc5 model.smt2\n```\n\n## Example: Petersen Graph Coloring\n\n```lean\nimport CSP.L2S.Core\nimport CSP.L2S.Constraints\nimport CSP.L2S.Translate\n\nopen CSP.L2S\n\ndef petersen : HomogeneousCSP :=\n  let nodes := 10\n  let edges := [\n    (0, 1), (1, 2), (2, 3), (3, 4), (4, 0),  -- outer pentagon\n    (5, 7), (7, 9), (9, 6), (6, 8), (8, 5),  -- inner pentagram\n    (0, 5), (1, 6), (2, 7), (3, 8), (4, 9)   -- connections\n  ]\n  let bounds := (List.finRange nodes).map fun v => bound v 1 3\n  let edge_constrs := edges.map fun (u, v) => not_equal ⟨u, by omega⟩ ⟨v, by omega⟩\n  ⟨nodes, bounds ++ edge_constrs⟩\n\ndef main : IO Unit := do\n  saveTo petersen \"petersen.mzn\" BackendType.MiniZinc\n```\n\nRun:\n```bash\nlake env lean --run MyFile.lean\nminizinc petersen.mzn\n```\n\n## Main Definitions\n\n### CSP Equivalence\n\nTwo CSPs are equivalent if there exists a bijection between their solution sets:\n\n```lean\ndef equivalent (csp₁ : CSP VarIndex₁ DomainType₁) (csp₂ : CSP VarIndex₂ DomainType₂) : Prop :=\n  ∃ f : {x // x ∈ sol_set csp₁} → {x // x ∈ sol_set csp₂}, Function.Bijective f\n```\n\n### CSP Equisatisfiability\n\nTwo CSPs are equisatisfiable if satisfiability is preserved:\n\n```lean\ndef equisatisfiable (csp₁ : CSP VarIndex₁ DomainType₁) (csp₂ : CSP VarIndex₂ DomainType₂) : Prop :=\n  is_satisfiable csp₁ ↔ is_satisfiable csp₂\n```\n",1783784690539]