-- Frap/Inductive.lean

section rewriting

variable (α : Type) (a b c d : α)  -- a, b, c, d are elements of some type

/-
## Rewriting

Recall our equality proof from last time.
-/

example : a = b → c = b → c = d → a = d := by
  intro hab hcb hcd
  apply Eq.trans
  . exact hab
  . apply Eq.trans
    . apply Eq.symm
      exact hcb
    . exact hcd

/-
Notice that in each subgoal, we are attempting to use a hypothesis by way of an equality property.
For example, we invoke the transitive property on `a = d` so that we can use the assumption `a = b` and be left with `b = d` to prove.
In effect, we are substituting `b` for `a` in the goal.

This substitution operation can be seen as rewriting some term in the goal with another, via an equality in the hypothesis.
This rewriting operation is so common that Lean provides a `rw` tactic to do so.
It replaces equals for equals, which could be an equality (`=`) or equivalence (`↔`).

Be default, it rewrites the left-hand side with the right-hand side.
For right-to-left rewriting, a left arrow needs to be indicated before the hypothesis (`←`, typed with `\<-` shortcut) .
-/

example : a = b → c = b → c = d → a = d := by
  intro hab hcb hcd
  rw [hab]
  rw [← hcd]
  rw [hcb]

end rewriting

/-
## reflexivity tactic

One of the most powerful tactics is the _reflexivity_ tactic, written `rfl` in Lean.
It will attempt to conclude that both sides of an equality goal is indeed equal, up to some trivial computation, such as expansion of definitions and function applications.
-/

example : 2 + 5 = 7 := by
  rfl

example : ¬ Even = 3 := by
  intro h
  sorry

namespace Hidden

inductive Nat where
  | zero : Nat
  | succ : Nat → Nat

namespace Nat

end Nat

end Hidden

section opendemo

open Nat

end opendemo

namespace Hidden

namespace Nat

end Nat

end Hidden

namespace Hidden

namespace Nat

-- Repository definitions used:
content: ...

-- Repository lemmas used:
content: theorem List.getElem_append_left{α : Type u_1} {l₁ l₂ : List α} {i : Nat} (hn : i < l₁.length) :\n(l₁ ++ l₂)[i] = l₁[i]

-- Local definitions used:
content: inductive Nat where
  | zero : Nat
  | succ : Nat → Nat

-- Local lemmas used:
content: theorem add_zero (m : Nat) : m + zero = m

content: theorem add_succ (m n : Nat) : m + succ n = succ (m + n)

content: theorem zero_add (n : Nat) : zero + n = n

content: theorem succ_add (m n : Nat) : succ (m + n) = succ m + n

-- Target theorem and proof:
theorem add_comm (m n : Nat) : m + n = n + m
:= by
  induction n with
  | zero =>
      rw [add_zero]
      rw [zero_add]
  | succ n' ih =>
      rw [add_succ]
      rw [ih]
      rw [succ_add]

/-
Alternatively, if the supporting fact is only useful within a single theorem, we can embed the proof within, using the
-/

example (m n : Nat) : m + n = n + m := by
  induction n with
  | zero =>
      rw [add_zero]
      rw [zero_add]
  | succ n' ih =>
      rw [add_succ]
      rw [ih]
      have h (m n : Nat) : succ (m + n) = succ m + n := by
        induction n with
        | zero =>
            rw [add_zero]
            rw [add_zero]
        | succ n' ih =>
            rw [add_succ]
            rw [add_succ]
            rw [ih]
      rw [h]