Zulip Chat Archive
Stream: new members
Topic: How to convert function of `\nat` to function of `fin n`
Jeffrey Li (Jan 09 2022 at 16:10):
Any function of \nat
can be resolved to a function of fin n
because fin n \subset \nat
, but when i try to pass a function \nat \to A
as an argument that is typed as fin n \to A
, it gives me a type mismatch error. Can I do the conversion implicitly or do i have to explicitly make a new function to satisfy the fin n \to A
type constraint? Thanks
Yakov Pechersky (Jan 09 2022 at 16:33):
If you have a function f : fin 3 -> A
, what do you want f 5
to be?
Jeffrey Li (Jan 09 2022 at 16:42):
Sorry I think you misunderstand, I have a function f : \nat \to A
, and I want to pass it as an argument of type fin n \to A
Jeffrey Li (Jan 09 2022 at 16:44):
Also how I produce the proof that allows me to compare (fin n)
with ℕ
? Right now if I have the variable i : fin n
, I can't do the comparison i == 1
, because I believe I need the following proof
instance eq_decidable_fin_n(i : fin n)(k : ℕ) : decidable (i == k) := sorry
But I'm not sure how to produce that proof. Thanks
Reid Barton (Jan 09 2022 at 16:55):
Jeffrey Li said:
fin n \subset \nat
This isn't how it works in Lean--fin n
and nat
are two different types, and a type can't be a subset of another one. Rather, there is a coercion from fin n
to nat
. This means that if you have x : fin n
and you write x : ℕ
then Lean will notice that it doesn't type check and replace x
by a coercion ↑x
, which elsewhere was defined to mean x.val
or whatever.
Reid Barton (Jan 09 2022 at 16:56):
Jeffrey Li said:
Can I do the conversion implicitly or do i have to explicitly make a new function to satisfy the
fin n \to A
type constraint?
You will have to write something like λ x, f x
(which really means λ x, f (↑x)
) instead of just f
.
Reid Barton (Jan 09 2022 at 16:56):
Jeffrey Li said:
Also how I produce the proof that allows me to compare
(fin n)
withℕ
? Right now if I have the variablei : fin n
, I can't do the comparisoni == 1
, because I believe I need the following proofinstance eq_decidable_fin_n(i : fin n)(k : ℕ) : decidable (i == k) := sorry
But I'm not sure how to produce that proof. Thanks
This is not possible. Basically, don't use ==
.
Reid Barton (Jan 09 2022 at 16:56):
Oh sorry, it actually is possible but i == k
doesn't mean what you want anyways.
Jeffrey Li (Jan 09 2022 at 17:08):
Reid Barton said:
Jeffrey Li said:
fin n \subset \nat
This isn't how it works in Lean--
fin n
andnat
are two different types, and a type can't be a subset of another one. Rather, there is a coercion fromfin n
tonat
. This means that if you havex : fin n
and you writex : ℕ
then Lean will notice that it doesn't type check and replacex
by a coercion↑x
, which elsewhere was defined to meanx.val
or whatever.
Oh thank you so much, using x.val
makes everything work. Still new to lean so trying to figure out how the type / subtype system works.
Last updated: Dec 20 2023 at 11:08 UTC