Zulip Chat Archive

Stream: new members

Topic: working with `let rec` in proofs


Yannick Seurin (Sep 22 2025 at 21:16):

import Mathlib

def my_fac :   
  | 0 => 1
  | n + 1 => (n + 1) * my_fac n

example : my_fac 0 = 1 :=
  rfl

example : 1 + 1 = 2 := by
  let rec my_fac' :   
    | 0 => 1
    | n + 1 => (n + 1) * my_fac' n
  have h : my_fac' 0 = 1 := rfl -- Type mismatch
  sorry

I'm confused about why I can't prove h with rfl here. I can't use rw [my_fac'] either. How can I prove h then?

Aaron Liu (Sep 22 2025 at 21:30):

it's not possible to refer to the insides of the let rec until you've finished the definition or theorem it was attached to

Anthony Wang (Sep 22 2025 at 21:44):

Out of curiosity, why is it not possible to do that?

Aaron Liu (Sep 22 2025 at 21:49):

it elaborates as a mutual block

Yannick Seurin (Sep 23 2025 at 08:00):

Thanks. So let's say I want to prove a theorem and in the course of the proof I need to define a recursive function f and prove intermediate results about f. Does that mean I have to define f outside the theorem? Or is there some other way?

Eric Wieser (Sep 23 2025 at 08:49):

Either outside the theorem, or without using Lean's pattern matching

Yannick Seurin (Sep 23 2025 at 09:38):

Sorry, what do you mean by "without using Lean's pattern matching"? How would it look like for my_fac' for example?

Kenny Lau (Sep 23 2025 at 09:45):

@Yannick Seurin use Nat.rec

Yannick Seurin (Sep 23 2025 at 12:56):

Like this?

import Mathlib

example : 1 + 1 = 2 := by
  let my_fac' (n : ) :  :=
    Nat.recOn n
      (1)
      (fun n ih => (n + 1) * ih)
  have h : my_fac' 0 = 1 := rfl
  have h' (n : ) : my_fac' (n + 1) = (n + 1) * my_fac' n := rfl
  sorry

Kenny Lau (Sep 23 2025 at 12:59):

yes


Last updated: Dec 20 2025 at 21:32 UTC