We have two entities: one our hash is a number, it is represented by a Nat
, and another is the sized vector. We want that our hash number belong to one of the elements of the sized vector. Normally we would compute a desired index with mod nat % size
.
class SizedVector[A, L <: Nat: ToInt](s: Sized[Vector[A], L]) {
def shard[Hash <: Nat: ToInt, Index <: Nat](
hash: Hash32[Hash]
)(
implicit
mod: Mod.Aux[Hash, L, Index],
index: ToInt[Index]
): A = s.at[Index]
}
How can we prove that the result of the mod Index
at the type-level is within the length of the sized vector? Currently the compiler would tell you
could not find implicit value for parameter diff: shapeless.ops.nat.Diff[L,shapeless.Succ[Index]]
): A = s.at[Index]
Assuming it has no idea what is the difference between the vector length and the computed index.
Is it possible to ensure the safe access and make it total this way?
Scastie: https://scastie.scala-lang.org/kubum/AmbBX3rwQfyXjqRrglYyIg/5