I am learning to use the Lean 4 proof assistant. I have a simple question about what the underscore symbol _ does in the following context:
The following example (src) works fine.
example {p q : ℚ} (h1 : p - 2 * q = 1) (h2 : q = -1) : p = -1 :=
calc
p = (p - 2 * q) + (2 * q) := by ring
_ = (1) + (2 * -1) := by rw [h1,h2]
_ = -1 := by ring
However, as a beginner, I’m tempted to use actual variable name p at the start of each line as follows:
example {p q : ℚ} (h1 : p - 2 * q = 1) (h2 : q = -1) : p = -1 :=
calc
p = (p - 2 * q) + (2 * q) := by ring
p = (1) + (2 * -1) := by rw [h1,h2]
p = -1 := by ring
gives the following error:
invalid 'calc' step, left-hand-side is
p : ℚ
previous right-hand-side is
p - 2 * q + 2 * q : ℚ
Q: why is this?
I would have expected p =
to be valid and justified as the start of each proof line, and that _ =
merely to be a shorthand for “previous” much like we write pen and paper proof where we omit the LHS for all but the first line.
Clearly _
is doing something I don’t understand or expect.