Zulip Chat Archive

Stream: mathlib4

Topic: Finset card question


Sorrachai Yingchareonthawornchai (Sep 23 2024 at 06:09):

I define an n-by-n matrix as M (i j : Fin n) : Prop := i.val = 0 ∨ j.val = 0.
I want to count the number of 1's (True) in M which is 2n-1. Is there an easy way to count?

For my purpose, I would need only to prove only the lower bound of it.
Let t := (filter (fun (i,j) ↦ M i j) univ)
have : 2*n - 1 ≤ t.card

Following Yaël's advice, I plan to use an injective map

let s : Finset  := Icc (-n + 1) (n - 1)
let f : Finset   Fin n × Fin n  := fun (i)  ( posPart i , negPart i) --- this line said it failed to synthesize
  AddGroup (Fin n)

Any advice on how to proceed would be appreciated. Thanks.

Kevin Buzzard (Sep 23 2024 at 06:13):

What's the full error?

Sorrachai Yingchareonthawornchai (Sep 23 2024 at 06:28):

application type mismatch
i⁺
argument
i
has type
Finset ℤ : Type
but is expected to have type
Fin n : Type

Kevin Buzzard (Sep 23 2024 at 06:28):

I think that's a pretty clear description of why your code doesn't work

Yaël Dillies (Sep 23 2024 at 06:29):

The codomain should be \Z, not Finset \Z

Kevin Buzzard (Sep 23 2024 at 06:30):

Domain not codomain?

Sorrachai Yingchareonthawornchai (Sep 23 2024 at 06:30):

Thanks.
application type mismatch
i⁺
argument
i
has type
ℤ : Type
but is expected to have type
Fin n : Type

Sorrachai Yingchareonthawornchai (Sep 23 2024 at 06:31):

So it does not automatically cast an integer to Fin n?

Kevin Buzzard (Sep 23 2024 at 06:31):

There's no coercion if n=0 which isn't ruled out

Sorrachai Yingchareonthawornchai (Sep 23 2024 at 06:32):

I see thanks a lot

Kevin Buzzard (Sep 23 2024 at 06:32):

Once you figure out how to get around this (ie by making the assumption which the coercion is expecting to see, in a way which it can see it) then the code will work

Kevin Buzzard (Sep 23 2024 at 06:33):

Unfortunately I'm not at a computer so can only guess. [NeZero n]?

Sorrachai Yingchareonthawornchai (Sep 23 2024 at 06:35):

It works now thanks! Somehow the error message does not say much.

Sorrachai Yingchareonthawornchai (Sep 23 2024 at 13:32):

Another question:

  • let f : ℤ → Fin n × Fin n := fun (i) ↦ ( posPart i , negPart i)
  • in my tactic state, it shows f : ℤ → Fin n × Fin n := fun i ↦ ((↑i)⁺, (↑i)⁻)
  • but it should be f : ℤ → Fin n × Fin n := fun i ↦ (↑(i⁺), ↑(i⁻))? And how do I enforce this?

Yaël Dillies (Sep 23 2024 at 13:40):

Write let f (i : ℤ) : Fin n × Fin n := (↑(i⁺), ↑(i⁻))


Last updated: May 02 2025 at 03:31 UTC