Zulip Chat Archive

Stream: mathlib4

Topic: Why Vector is defined as a List subtype


Aiken Cairncross (Nov 20 2023 at 21:15):

I was looking for a statically sized Array type in the std/mathlib and came upon Vector and was surprised to see it's defined as a subtype of List. Not being all that familiar with Lean, I was expecting it to either be defined inductively (to maybe make some proofs simpler?) or as a subtype of Array (for a generally more performant CPU representation). Is there any advantage to it being defined the way it is?

Mario Carneiro (Nov 20 2023 at 21:42):

This definition is a bit simpler for proofs than the inductive type, because it means you can prove the theorem about List and lift it to Vector

Joachim Breitner (Nov 20 2023 at 21:59):

Hopefully eventually we have custom eliminators a.k.a. pattern synonyms and you can freely switch between the two worlds as is most convenient.

Wrenna Robson (Nov 21 2023 at 10:43):

@Aiken Cairncross for some stuff, people also use tuples, i.e. Fin n -> YourTypeHere, though I'm not sure they have great performance.

Henrik Böving (Nov 21 2023 at 11:06):

Fin n -> Typ is not a good runtime representation at all and should probably only be used for proof stuff.

Eric Wieser (Nov 21 2023 at 11:12):

It's pretty reasonable as a way to consume tuples, as it lets you pass Array.get which should be just as efficient as passing an array

Wrenna Robson (Nov 21 2023 at 11:13):

Henrik Böving said:

Fin n -> Typ is not a good runtime representation at all and should probably only be used for proof stuff.

Would that I had thought about this without writing my thing that currently uses tuples. Though in my defense they are the easiest way to use a permutation on something.

Wrenna Robson (Nov 21 2023 at 11:14):

Not that I guess permutations are that nice computationally.


Last updated: Dec 20 2023 at 11:08 UTC