# LeanHammer LeanHammer is an automated reasoning tool for Lean that brings together multiple proof search and reconstruction techniques and combines them into one tool. The `hammer` tactic provided by LeanHammer uses a variety of techniques to search for a proof of the current goal, then constructs a suggestion for a tactic script which can replace the `hammer` invocation. There are currently versions of the hammer that are compatible with the stable versions of Lean from `v4.20.0` through `v4.36.0` (and the corresponding versions of Mathlib). Note that versions of LeanHammer prior to `v4.36.0` relied on an external [premise selection server](https://github.com/hanwenzhu/premise-selection), so effective use of older versions of LeanHammer may require setting up your own premise selection server. The code to do so is [here](https://github.com/hanwenzhu/lean-premise-server). Pull requests and issues are welcome. ## Adding LeanHammer to Your Project To add LeanHammer for `v4.36.0` to an existing project with a `lakefile.toml` file, replace the Mathlib dependency in `lakefile.toml` with the following: ```toml [[require]] name = "Hammer" git = "https://github.com/JOSHCLUNE/LeanHammer" rev = "main" # Replace this with `rev = "v4.XX.X"` to add LeanHammer for an earlier supported Lean version. [[require]] name = "mathlib" scope = "leanprover-community" rev = "v4.36.0" ``` The file `lean-toolchain` should contain the following: ``` leanprover/lean4:v4.36.0 ``` If you have a project with a `lakefile.lean` instead of `lakefile.toml`, you can use this instead: ```lean require Hammer from git "https://github.com/JOSHCLUNE/LeanHammer" @ "main" require mathlib from git "https://github.com/leanprover-community/mathlib4.git" @ "v4.36.0" ``` Then use `lake update` to fetch the hammer and the corresponding versions of Lean and Mathlib. This also retrieves the Zipperposition executable that comes with LeanHammer. (This executable will be stored in the existing project's `.lake` directory.) The following example should then compile without any warnings or errors: ```lean import Hammer example : True := by hammer ``` The first time you `import Hammer`, Lake has to build the hammer component, which takes a few minutes, but after that, you can start hammering away. If you use a version of Mathlib that differs from the most recent one, LeanHammer may have to calculate embeddings for the theorems it doesn't have. This can take a little while, but the calculated embeddings will be cached locally after that (prior iterations of LeanHammer used an external premise selection server but LeanHammer now performs premise selection locally). You can test the hammer without premise selection by replacing `hammer` by `hammer {aesopPremises := 0, duperPremises := 0, grindPremises := 0, smtPremises := 0}` in the example above. You are free to try to use a version of the hammer with a nearby version of Lean and Mathlib, but there are no guarantees it will work. As explained below, the hammer has several dependencies, and they break often as Lean changes. If you add the Hammer to an existing project and don't use `lake update`, you should use `lake update Hammer` to fetch the hammer and Zipperposition. You should also put the `Hammer` dependency before the `mathlib` dependency in `lakefile.toml` or `lakefile.lean`: Mathlib and LeanHammer share a dependency on `batteries`, and unless you favor Mathlib's version, you will end up recompiling Mathlib. ## Components Currently, LeanHammer consists of/depends on the following components: - **Premise selection** - A [local LM-based premise selector](https://github.com/chasenorman/PremiseSelection/) - **Translation procedure** - [Lean-auto](https://github.com/leanprover-community/lean-auto) which serves as an interface to translate from Lean into TPTP and SMT - **Automatic theorem provers** - [Zipperposition](https://github.com/sneeuwballen/zipperposition) via [Lean-auto](https://github.com/leanprover-community/lean-auto/blob/hammer/lakefile.lean) - [cvc5](https://github.com/cvc5/cvc5) via the [Lean cvc5 FFI](https://github.com/abdoo8080/lean-cvc5) - **Proof search and proof reconstruction tools native to Lean** - [Aesop](https://github.com/leanprover-community/aesop) - [Duper](https://github.com/leanprover-community/duper) - [Grind](https://lean-lang.org/doc/reference/latest/The--grind--tactic/) - [Lean-SMT](https://github.com/ufmg-smite/lean-smt/tree/main) ## Usage The syntax for invoking the `hammer` tactic is `by hammer [lemmas] {options}`. The `lemmas` and `options` arguments are both optional, with the former being used to tell `hammer` to use the supplied list of lemmas (in addition to any lemmas recommended by premise selection), and the latter being used to set LeanHammer's various configuration options. ### Options Each of the `options` supplied to `hammer` have the form `option := value` and are separated by commas. Options that can be used to customize a LeanHammer call include: - `parallelism`: Can be set to `true` or `false` (default `true`). This option is used to determine whether LeanHammer attempts to call all of its available solvers and proof reconstruction procedures in parallel. - `outputAllSuggestions`: Can be set to `true` or `false` (default `false`). This option determines whether LeanHammer shows the user all of the proofs it finds or if it only shows the user the first proof it finds. Note that enabling this option is likely to increase LeanHammer's average runtime, because when `outputAllSuggestions` is set to `false`, LeanHammer can terminate as soon as any proof is found, even if other proofs might be found later with more time. This option can only be set to `true` if `parallelism` is also set to `true`. - `disableAesop`: Can be set to `true` or `false` (default `false`). This option is used to remove Aesop from the LeanHammer call. - `disableDuper`: Can be set to `true` or `false` (default `false`). This option is used to remove the Lean-auto to Zipperposition to Duper pipeline from the LeanHammer call. - `disableGrind`: Can be set to `true` or `false` (default `false`). This option is used to remove Grind from the LeanHammer call. - `disableSmt`: Can be set to `true` or `false` (default `false`). This option is used to remove the Lean-SMT pipeline (which involves calling Lean-auto and the Lean cvc5 FFI). - `preprocessing`: Can be set to `aesop`, `simp_target`, `simp_all`, or `no_preprocessing` (default `aesop`). This option determines whether the initial goal is first processed by `aesop`, `simp`, `simp_all`, or none of these. This option can only be set to a value other than `aesop` if `disableAesop` is set to `true`, and must be set to `aesop` if `disableAesop` is set to `false`. - `aesopPremises`: Can be set to any Nat (default 32). This option determines the number of lemmas from premise selection that are passed to Aesop as unsafe rules. - `duperPremisesShort`: Can be set to any Nat (default 16). This option determines the number of lemmas from premise selection that are passed to Lean-auto, Zipperposition, and Duper when called as a subprocedure in Aesop. - `duperPremisesLong`: Can be set to any Nat (default 100). This option determines the number of lemmas from premise selection that are passed to Lean-auto, Zipperposition, and Duper when called as an independent procedure. - `grindPremises`: Can be set to any Nat (default 100). This option determines the number of lemmas from premise selection that are passed to Grind. - `smtPremises`: Can be set to any Nat (default 16). This option determines the number of lemmas from premise selection that are passed to Lean-SMT. - `aesopPremisePriority`: Can be set to any Nat between 0 and 100 (default 20). This option determines the Aesop success priority assigned to each of the lemmas from premise selection when passed to Aesop as unsafe rules. See [Aesop's README](https://github.com/leanprover-community/aesop) for additional details on the meaning of this success priority. - `aesopDuperPriority`: Can be set to any Nat between 0 and 100 (default 10). This option determines the Aesop success priority assigned to the unsafe rule that attempts to use Lean-auto, Zipperposition, and Duper to solve the current goal. - `aesopGrindPriority`: Can be set to any Nat between 0 and 100 (default 5). This option determines the Aesop success priority assigned to the unsafe rule that attempts to use Grind to solve the current goal. - `aesopSmtPriority`: Can be set to any Nat between 0 and 100 (default 10). This option determines the Aesop success priority assigned to the unsafe rule that attempts to use Lean-SMT to solve the current goal. Each of these options' defaults can be changed with `set_option hammer.Default `. For example, the command that changes the default number of premises passed to Duper from 16 to 32 is `set_option hammer.duperPremisesDefault 32`. ### Debugging You can use the following to get more information: - `set_option trace.hammer.premises true` to display the list of premises retrieved - `set_option trace.hammer.debug true` to display what is passed to Zipperposition and the minimized set sent to Duper if it succeeds - `set_option trace.auto.tptp.printQuery true` to display the query sent to Zipperposition - `set_option trace.auto.tptp.result true` to display the response from Zipperposition - `set_option trace.aesop true` to display the Aesop search tree