Zulip Chat Archive
Stream: Type theory
Topic: Functions on an inductive type
Flo (Florent Schaffhauser) (Jun 03 2023 at 13:20):
If I is an inductive type and T is a type, can functions from I to T be used to define an inductive type?
For instance, if I = nat and T is a type, is there an inductive type with terms indexed by the integers and whose terms indexed by n are exactly the n-tuples of terms of type T?
In the context of Lean, I have been thinking about this in order to define the sum of terms in a monoid recursively:
where by definition .
I know that such sums are defined in mathlibbut I am thinking about this for teaching purposes.
Johan Commelin (Jun 03 2023 at 13:28):
It might be easier to use list M... but if you are willing to consider infinite sequences for then you can do what you want.
Johan Commelin (Jun 03 2023 at 13:29):
However, it would be a lot more convenient to define
Johan Commelin (Jun 03 2023 at 13:31):
Actually, I guess your version is also fine.
Flo (Florent Schaffhauser) (Jun 03 2023 at 13:39):
Johan Commelin said:
It might be easier to use
list M... but if you are willing to consider infinite sequences for then you can do what you want.
Yes, I forgot to say it but with infinite sequences I can implement what I want in Lean. That's why I am now wondering about -tuples. I will look into list Mmore, thanks for the tip :+1:
Johan Commelin (Jun 03 2023 at 13:49):
Yeah, you'll have to decide what you mean by n-tuple before you can sum them. list M is one option. There is also vector n M, but it has very little API. Yet another option is maps fin n → M.
Kevin Buzzard (Jun 03 2023 at 13:49):
You can make the definition recursively -- surely this is what you want? Why do you want the type itself to be inductive?
import Mathlib
/-- `sum f n` is `f(0)+f(1)+...+f(n-1)` -/
def sum {M : Type} [AddMonoid M] (f : ℕ → M) : ℕ → M
| 0 => 0
| (n + 1) => sum f n + f n
Flo (Florent Schaffhauser) (Jun 03 2023 at 13:59):
Kevin Buzzard said:
You can make the definition recursively -- surely this is what you want? Why do you want the type itself to be inductive?
import Mathlib /-- `sum f n` is `f(0)+f(1)+...+f(n-1)` -/ def sum {M : Type} [AddMonoid M] (f : ℕ → M) : ℕ → M | 0 => 0 | (n + 1) => sum f n + f n
Yes, that is indeed what I want!
And I wrote something similar to what you did here but then I started thinking about a definition that would start like this
def sum {M : Type} [AddMonoid M] (n : ℕ) (f : fin n → M) : M
and with this formalism I have not been able to make it work.
So now I am wondering if there is an inductive type of -tuples of elements of , that I can use to write a recursive definition. And that's why I got interested in the type-theoretic aspects :sweat_smile:
Eric Wieser (Jun 03 2023 at 14:04):
You can write a recursive definition using docs#fin.cons_induction / docs4#Fin.consInduction
Flo (Florent Schaffhauser) (Jun 03 2023 at 14:06):
Fantastic, thanks!!!
Eric Wieser (Jun 03 2023 at 14:08):
def sum {M : Type} [AddMonoid M] (n : ℕ) (f : Fin n → M) : M := by
induction f using Fin.consInduction with
| h0 => exact 0
| h x₀ _xs ih => exact x₀ + ih
Eric Wieser (Jun 03 2023 at 14:09):
Another option is to use the poorly-named docs#vector3, though I don't know what Lean 4 calls it
Kevin Buzzard (Jun 03 2023 at 14:43):
Hopefully vector4
Alex Keizer (Jun 10 2023 at 12:27):
Maybe to answer the question as originally stated, functions are generally coinductive, rather than inductive, so, no, a function Nat -> M cannot be used to define an inductive type (what would it's recursion/induction principle be), but it could define a coinductive type.
Last updated: May 02 2025 at 03:31 UTC