Zulip Chat Archive

Stream: lean4

Topic: notation `x[i]` inferring the type of `i`


Tomas Skrivan (Mar 11 2024 at 15:15):

The current element access notation currently does not allow you to write fun i => x[i] as the type of i does not get inferred. This is particularly unfortunate for types like Vector α n. Here is my attempt at fixing it. I would like know if there is a cleaner solution to this problem.

I'm using James' LeanColls library which defines Indexed

class Indexed (Cont : Type u) (Idx : outParam (Type v)) (Elem : outParam (Type w)) where
   ...

which determined the canonical index type Idx for the container type Cont. This is unlike GetElem class where the index type is not marked as outParam. I want to uses this class to determine the index type.

Here is my solution:

code

And few test cases that I found out to be challenging when trying different approach

variable {Cont Idx Elem} [Indexed Cont Idx Elem] [Inhabited Elem]
variable {Cont'} [Indexed Cont' (Fin 10) Elem]

variable (c : Cont) (c' : Cont') (j : Idx) (b : Array Nat) (k : Fin b.size)

#check c[j]
#check c'[0]
#check fun i => c[i]
#check fun i j => (c[i],c[j])
#check fun i j => (c[i],c[j])
#check b[k]

example (f : Idx  Elem) :
  Function.invFun (fun (f : Idx  Elem) => Indexed.ofFn (Cont:=Cont) f)
  =
  fun x i => x[i] := sorry

Mac Malone (Mar 11 2024 at 20:16):

An outParam is not used on the index type in core because some container types like Array support multiple different index types (e.g., Array supports Nat, Fin, and USize).

Tomas Skrivan (Mar 11 2024 at 20:36):

Yes it is completely understandable that the index type is not outParam in general. But for certain types, like Vector, it would be nice to have a canonical index type.


Last updated: May 02 2025 at 03:31 UTC