Zulip Chat Archive

Stream: general

Topic: induction principle is not generated


Asei Inoue (Jun 02 2024 at 05:10):

def factorial (n : Nat) : Nat :=
  match n with
  | 0 => 1
  | m + 1 => (m + 1) * factorial m

-- induction principle is generated!
#check factorial.induct

inductive Many (α : Type u) where
  | none : Many α
  | more : α  (Unit  Many α)  Many α

def many_map {α β : Type} (f : α  β) : Many α  Many β
  | .none => .none
  | .more x xs => Many.more (f x) (fun () => many_map f <| xs ())

-- why the induction principle of `many_map` is not generated?
/-
unknown identifier 'many_map.induct'
-/
#check many_map.induct

Asei Inoue (Jun 02 2024 at 07:18):

I thought that for every recursive function, an induction principle is generated. Is that not so?

Joachim Breitner (Jun 02 2024 at 08:17):

There are more errors when you click on that line, it seems the generation of the induction principle fails. That's a bug that's on me.

Joachim Breitner (Jun 02 2024 at 08:21):

If you don’t need many_map to be structurally recursive, you can work-around the issue by writing

def many_map {α β : Type} (f : α  β) : Many α  Many β
  | .none => .none
  | .more x xs => Many.more (f x) (fun () => many_map f <| xs ())
termination_by m => m```

Joachim Breitner (Jun 02 2024 at 08:22):

This uses different code paths in the induction principle generation that don’t hit the bug.

Reported at https://github.com/leanprover/lean4/issues/4320


Last updated: May 02 2025 at 03:31 UTC