[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"W1M7Qtorvi":3},"#  Gröbner Tactics\n\nThis repository contains Lean 4 tactics for the automated formal verification of Gröbner basis properties and computations.\n\nOur main goal is to create powerful tactics that can automatically prove theorems related to Groebner Basis or the application of Groebner Basis within the Lean 4 theorem prover.\n\nThe foundational definitions and core theorems on which this work is built are developed in the companion project  [groebner_proj](https://github.com/WuProver/groebner_proj).  One of the main technical challenges in Lean is that the standard `MvPolynomial` type  is, in general, non-computable from the perspective of proof automation.  To overcome this limitation, we introduce a dedicated implementation of [computable polynomials](https://github.com/WuProver/MonomialOrderedPolynomial)  that is suitable for execution and computation within Lean's kernel.\n\nBoth the library and its accompanying documentation are  still under development.\n\n\n## Introduction\n### Workflow\n1. Lean to Externel Solvers: We extract mathematical objects (such as  MvPolynomial and generators of Ideals) from the Lean context and employ meta-programming techniques, specifically [Qq](https://github.com/leanprover-community/quote4) matching, or manual pattern matching, to deconstruct and analyze Lean expressions.\n2. External Computation: Gröbner basis–related computations on multivariate polynomials are delegated to externel solvers. The computed results are serialized into a structured JSON format and returned to Lean, where they are reinterpreted for subsequent verifications.\n3. Externel Solver to Lean: Lean parses the incoming JSON data into well-defined  intermediate data structures. These structures are then   systematically translated back into `Expr` and  ultimately elaborated  into `Term` objects in Lean, allowing the externally computed results to be integrated into the proof environment.\n4. Kernel Verification: The verification tasks  are reduced to  polynomial identity testing （PIT) problems which  are discharged entirely  within the Lean kernel using its internal   [computable polynomial representation](https://github.com/WuProver/MonomialOrderedPolynomial).\n\n### Tactics\n1. `gb_solve`:\n   - Certifies that the remainder of a polynomial $f$ upon reduction  modulo a set $B$ is $r$.\n   - Verifies that a set $B$ is a groebner basis of an ideal $I$.\n   - Decides whether a polynomial $f$ belongs to an ideal $I$.\n   - Decides whether a polynomial $f$ belongs to the radical ideal  $\\sqrt{I}$.\n2. `idealeq`: Verifies the equality of two ideals, $I = J$\n3. `add_gb_hyp`: Automatically computes a Gröbner basis $G$ for the ideal generated by $B$ using SageMath and adds the certified  $G$ as a hypothesis in Lean.\n\n### Modes\nOur tactic supports three modes: local SageMath computation (default), external SageMath computation (not recommended), and local SymPy computation.\n\n```lean\n-- local SageMath computation mode\nexample :\n    lex.IsRemainder (X 0 ^ 2 + X 1 ^ 3 + X 2 ^ 4 + X 3 ^ 5: MvPolynomial (Fin 4) ℚ)\n      {X 0, X 1, X 2, X 3} 0 := by\n  gb_solve\n```\n\n```lean\n-- externel SageMath computation (API, not recommended)\nset_option gb_tactic.backend 1 in\nexample :\n    lex.IsRemainder (X 0 ^ 2 + X 1 ^ 3 + X 2 ^ 4 + X 3 ^ 5: MvPolynomial (Fin 4) ℚ)\n      {X 0, X 1, X 2, X 3} 0 := by\n  gb_solve\n```\n\n```lean\n-- local Sympy computation\nset_option gb_tactic.backend 2 in\nexample :\n    lex.IsRemainder (X 0 ^ 2 + X 1 ^ 3 + X 2 ^ 4 + X 3 ^ 5: MvPolynomial (Fin 4) ℚ)\n      {X 0, X 1, X 2, X 3} 0 := by\n  gb_solve\n```\n## Some Example\n### `gb_solve`\nGiven a polynomial $f$ and a divisor set $G$, the `gb_solve` tactic automatically certifies the remainder of $f$ reduced modulo $G$.\nIn this example, it proves that reducing $f = x_0 x_1$ modulo the set $\\\\{2x_0 - x_1\\\\}$ yields the remainder $r = \\frac{1}{2} x_1^2$.\n```lean\n-- Certifies that the remainder of a polynomial $f$ upon reduction  modulo a set $B$ is $r$\nexample :\n    lex.IsRemainder (X 0 * X 1 : MvPolynomial (Fin 3) ℚ)\n      {2 * X 0 - X 1}\n      (C (1/2 : ℚ) * X 1 ^ 2) := by\n  gb_solve\n```\nThe tactic can formally verify if a given set of polynomials constitutes a Gröbner basis for a specific ideal.\nHere, it proves that $\\\\{1\\\\}$ is indeed the Gröbner basis for the ideal generated by $\\\\{x_0, 1 - x_0\\\\}$.\n```lean\n-- Verifies that a set $B$ is a groebner basis of an ideal $I$\nexample :\n  lex.IsGroebnerBasis\n  ({1} : Set \u003C| MvPolynomial (Fin 3) ℚ)\n  (Ideal.span ({X 0, 1 - X 0} : Set \u003C| MvPolynomial (Fin 3) ℚ)) := by\n  gb_solve\n```\nThe tactic can automatically decide and prove whether a given polynomial $f$ belongs to an ideal $I$ generated by a set of polynomials. It seamlessly handles both positive ($\\in$) and negative ($\\notin$) membership cases.\nHere, it proves that $x_0$ is an element of the ideal generated by $\\\\{x_0, x_1\\\\}$.\n```lean\n-- Decides a polynomial $f$ belongs to an ideal $I$\nexample :\n  X 0 ∈ Ideal.span ({X 0, X 1} : Set (MvPolynomial (Fin 2) ℚ)) := by\n  gb_solve\n```\nIn this example, it proves that $x_2$ does not belong to the ideal generated by $\\\\{x_0 + x_1^2, x_1^2\\\\}$.\n```lean\n-- Decides a polynomial $f$ doesn't belong to an ideal $I$\nexample :\n   X 2 ∉ Ideal.span ({X 0 + X 1^ 2, X 1 ^ 2} : Set (MvPolynomial (Fin 3) ℚ)) := by\n  gb_solve\n```\nBeyond standard ideals, the tactic can determine if a polynomial $f$ lies within the radical of an ideal, denoted as $\\sqrt{I}$. It automatically handles the underlying algebraic transformations to prove both membership and non-membership. For instance, it verifies that the polynomial $x_0 x_1$ is in the radical of the ideal generated by $\\\\{\\frac{1}{2}(x_0 + x_1), \\frac{1}{2}(x_0 - x_1)\\\\}$.\n```lean\n-- Decides a polynomial $f$ belongs to the radical ideal  $\\sqrt{I}$\nexample :\n  X 0 * X 1 ∈ (Ideal.span ({C (1/2 : ℚ) * (X 0 + X 1), C (1/2 : ℚ) * (X 0 - X 1)} :\n   Set (MvPolynomial (Fin 2) ℚ))).radical := by\n  gb_solve\n```\nConversely, it correctly determines that $x_0$ is not in the radical of the ideal generated by $\\\\{x_0 + x_1\\\\}$.\n```lean\n-- Decides a polynomial $f$ doesn't belong to the radical ideal  $\\sqrt{I}$\nexample :\n  X 0 ∉ (Ideal.span ({X 0 + X 1} : Set (MvPolynomial (Fin 3) ℚ))).radical := by\n  gb_solve\n```\n### `idealeq`\nThe `idealeq` tactic automates the proof of equality between two polynomial ideals. It internally computes and compares their Gröbner bases to determine if the ideals generated by two different sets of polynomials are mathematically identical.\n\nIn the example below, it automatically verifies that the ideal generated by $\\\\{x_0 + x_1^2, x_1\\\\}$ is equal to the ideal generated by $\\\\{x_0, x_1\\\\}$.\n```lean\nexample :\n  Ideal.span ({X 0 + X 1^2, X 1 }) =\n    Ideal.span ({X 0, X 1 } : Set (MvPolynomial (Fin 3) ℚ)) := by\n   idealeq\n```\n### `add_gb_hyp`\nInstead of directly closing a goal, the `add_gb_hyp` tactic computes the Gröbner basis for a specified set of polynomials and injects this fact as a new hypothesis into the local proof context. This is particularly useful when the Gröbner basis property is required as an intermediate step for further manual reasoning.\n\nIn this example, calling `add_gb_hyp h` calculates the Gröbner basis for the ideal generated by $\\\\{x_0, x_0 + x_1\\\\}$ and introduces a new hypothesis `h` stating that this basis is $\\\\{x_0, x_1\\\\}$. After some standard simplification, `h` exactly matches and closes the goal.\n```lean\nexample : lex.IsGroebnerBasis ({X 0, X 1} :\n Set (MvPolynomial (Fin 3) ℚ)) (Ideal.span {X 0, X 0 + X 1}) := by\n  add_gb_hyp h ({X 0, X 0 + X 1} : Set (MvPolynomial (Fin 3) ℚ))\n  simp only [ne_eq, one_ne_zero, not_false_eq_true, div_self, C_1, Fin.isValue, pow_one, one_mul,\n    zero_add] at h\n  exact h\n```\n\n## Build\nIf you don't already have Lean 4 set up, please follow the official [Lean 4 installation instructions](https://leanprover-community.github.io/get_started.html).\n\nOnce Lean is installed, you can clone this repository and build the project:\n\n```bash\ngit clone https://github.com/WuProver/GroebnerTactic.git\ncd GroebnerTactic\nlake exe cache get\nlake build\n```\n\nIf you want to use Sagemath as an external calculation tool, please follow the official [SageMath installation instructions](https://doc.sagemath.org/html/en/installation/index.html).\n\nIf you find that SageMath takes up too much storage space, or that installing SageMath on Windows is too cumbersome, you can opt to use SymPy as an external computation tool.\n\nFor Linux/MacOS systems, please run the following command:\n```bash\npython -m venv newenv\nsource newenv/bin/activate\npip install -r requirements.txt\n```\n\nFor Windows CMD, please run the following command:\n```bash\npython -m venv newenv\nnewenv\\Scripts\\activate.bat\npip install -r requirements.txt\n```\n\nFor Windows Powershell, please run the following command:\n```bash\npython -m venv newenv\n.\\newenv\\Scripts\\Activate.ps1\npip install -r requirements.txt\n```\n## Installation\nTo use our tactics in your Lean 4 project, you need to add this repository as a dependency. Adding he following block to your project's `lakefile.toml`:\n```\n[[require]]\nname = \"GroebnerTactic\"\ngit = \"https://github.com/WuProver/GroebnerTactic.git\"\nscope  = \"WuProver\"\nrev = \"main\"\n```\nTo use the full features, please download Sage as instructed above. Since installing Sage on Windows can be quite cumbersome, you can install Python instead and run the following commands.\n```bash\npython -m venv newenv\nsource newenv/bin/activate\npip install -r .lake/packages/GroebnerTactic/requirements.txt\n```\nIf you prefer not to install anything, you can use the API mode, but please use it sparingly.\n",1780846771393]