Theorem Proving in Lean 4

11.Β The Conversion Tactic ModeπŸ”—

Inside a tactic block, one can use the keyword conv to enter conversion mode. This mode allows to travel inside assumptions and goals, even inside function abstractions and dependent arrows, to apply rewriting or simplifying steps.

11.1.Β Basic navigation and rewritingπŸ”—

As a first example, let us prove example (a b c : Nat) : a * (b * c) = a * (c * b) (examples in this file are somewhat artificial since other tactics could finish them immediately). The naive first attempt is to enter tactic mode and try rw [Nat.mul_comm]. But this transforms the goal into b * c * a = a * (c * b), after commuting the very first multiplication appearing in the term. There are several ways to fix this issue, and one way is to use a more precise tool: the conversion mode. The following code block shows the current target after each line.

#guard_msgs (drop all) in example (a b c : Nat) : a * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ a * (b * c) = a * (c * b) a:Natb:Natc:Nat⊒ b * c * a = a * (c * b) example (a b c : Nat) : a * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ a * (b * c) = a * (c * b) a:Natb:Natc:Nat| a * (b * c) = a * (c * b) a:Natb:Natc:Nat| a * (b * c) a:Natb:Natc:Nat| aa:Natb:Natc:Nat| b * c a:Natb:Natc:Nat| b * c a:Natb:Natc:Nat| c * b

The above snippet shows three navigation commands:

  • lhs navigates to the left-hand side of a relation (equality, in this case). There is also a rhs to navigate to the right-hand side.

  • congr creates as many targets as there are (nondependent and explicit) arguments to the current head function (here the head function is multiplication).

  • rfl closes target using reflexivity.

Once arrived at the relevant target, we can use rw as in normal tactic mode.

The second main reason to use conversion mode is to rewrite under binders. Suppose we want to prove example (fun x : Nat => 0 + x) = (fun x => x). The naive first attempt is to enter tactic mode and try rw [Nat.zero_add]. But this fails with a frustrating

error: tactic 'rewrite' failed, did not find instance of the pattern
       in the target expression
  0 + ?n
⊒ (fun x => 0 + x) = fun x => x

The solution is:

example : (fun x : Nat => 0 + x) = (fun x => x) := ⊒ (fun x => 0 + x) = fun x => x | (fun x => 0 + x) = fun x => x | fun x => 0 + x x:Nat| 0 + x x:Nat| x

where intro x is the navigation command entering inside the fun binder. Note that this example is somewhat artificial, one could also do:

example : (fun x : Nat => 0 + x) = (fun x => x) := ⊒ (fun x => 0 + x) = fun x => x x:Nat⊒ 0 + x = x; All goals completed! πŸ™

or just

example : (fun x : Nat => 0 + x) = (fun x => x) := ⊒ (fun x => 0 + x) = fun x => x All goals completed! πŸ™

conv can also rewrite a hypothesis h from the local context, using conv at h.

11.2.Β Pattern matchingπŸ”—

Navigation using the above commands can be tedious. One can shortcut it using pattern matching as follows:

example (a b c : Nat) : a * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ a * (b * c) = a * (c * b) a:Natb:Natc:Nat| b * c a:Natb:Natc:Nat| c * b

which is just syntax sugar for

example (a b c : Nat) : a * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ a * (b * c) = a * (c * b) a:Natb:Natc:Nat| a * (b * c) = a * (c * b) a:Natb:Natc:Nat| b * c a:Natb:Natc:Nat| c * b

Of course, wildcards are allowed:

example (a b c : Nat) : a * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ a * (b * c) = a * (c * b) a:Natb:Natc:Nat| b * c a:Natb:Natc:Nat| c * b

11.3.Β Structuring conversion tacticsπŸ”—

Curly brackets and . can also be used in conv mode to structure tactics:

example (a b c : Nat) : (0 + a) * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ (0 + a) * (b * c) = a * (c * b) a:Natb:Natc:Nat| (0 + a) * (b * c) = a * (c * b) a:Natb:Natc:Nat| (0 + a) * (b * c) a:Natb:Natc:Nat| 0 + aa:Natb:Natc:Nat| b * c . a:Natb:Natc:Nat| a . a:Natb:Natc:Nat| c * b

11.4.Β Other tactics inside conversion modeπŸ”—

  • arg i enter the i-th nondependent explicit argument of an application.

    example (a b c : Nat) : a * (b * c) = a * (c * b) := a:Natb:Natc:Nat⊒ a * (b * c) = a * (c * b) a:Natb:Natc:Nat| a * (b * c) = a * (c * b) a:Natb:Natc:Nat| a * (b * c) a:Natb:Natc:Nat| b * c a:Natb:Natc:Nat| c * b
  • args is an alternative name for congr.

  • simp applies the simplifier to the current goal. It supports the same options available in regular tactic mode.

    def f (x : Nat) := if x > 0 then x + 1 else x + 2 example (g : Nat β†’ Nat) (h₁ : g x = x + 1) (hβ‚‚ : x > 0) : g x = f x := x:Natg:Nat β†’ Nath₁:g x = x + 1hβ‚‚:x > 0⊒ g x = f x x:Natg:Nat β†’ Nath₁:g x = x + 1hβ‚‚:x > 0| g x = f x x:Natg:Nat β†’ Nath₁:g x = x + 1hβ‚‚:x > 0| f x x:Natg:Nat β†’ Nath₁:g x = x + 1hβ‚‚:x > 0| x + 1 All goals completed! πŸ™
  • enter [1, x, 2, y] iterate arg and intro with the given arguments.

  • done fail if there are unsolved goals.

  • trace_state display the current tactic state.

  • whnf put term in weak head normal form.

  • tactic => <tactic sequence> go back to regular tactic mode. This is useful for discharging goals not supported by conv mode, and applying custom congruence and extensionality lemmas.

    example (g : Nat β†’ Nat β†’ Nat) (h₁ : βˆ€ x, x β‰  0 β†’ g x x = 1) (hβ‚‚ : x β‰  0) : g x x + x = 1 + x := x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0⊒ g x x + x = 1 + x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| g x x + x = 1 + x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| g x x + x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| g x x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| 1x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0⊒ x β‰  0 . x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| 1 . x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0⊒ x β‰  0 All goals completed! πŸ™
  • apply <term> is syntax sugar for tactic => apply <term>.

    example (g : Nat β†’ Nat β†’ Nat) (h₁ : βˆ€ x, x β‰  0 β†’ g x x = 1) (hβ‚‚ : x β‰  0) : g x x + x = 1 + x := x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0⊒ g x x + x = 1 + x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| g x x + x = 1 + x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| g x x + x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| g x x x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| 1x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0⊒ x β‰  0 . x:Natg:Nat β†’ Nat β†’ Nath₁:βˆ€ (x : Nat), x β‰  0 β†’ g x x = 1hβ‚‚:x β‰  0| 1 . All goals completed! πŸ™