Why no Dependent Matrix Type?

Hello,

I don't really know how to really introduce this topic.
Background: I want to implement compile time safe matrix multiplication.
For that i want to have a struct with const generics for the number of columns and rows.
Then i want a function which takes two const generics and does the matrix multiplication.
The result would be a matrix with the different sizes.

So far so good i was able to implement this. One idea is sketched in this old post.

Now the caveat is that this works only at compile time and not at runtime.

1. Problem: currently for my thesis i am implementing a compiler and a library which transforms some DSL to a matrix. In my opinion it would be great if the library for matrixes has these const generics since this should remove many runtime dependencies. Are there any ideas on how to implement this?

2. Problem: I want to implement something like matrix concatenation. given two $n \times m$ and $n\times k$ matrix. the result of concatenation should be a $n \times m+k$ matrix. apparently this also does not work.

Any more inside into these dependent type problems in rust would be great.
I think from my point of view with safety in mind this should be inside the language.

If I'm understanding the situation properly (and I may not be), I think what you're looking for is the work being done on generic const args (GCA).

Because it’s a very difficult problem, even if we’re just talking about comparatively trivial stuff like Foo<N> * Foo<M> -> Foo<N + M>. An entire (runtime) dependent type system, the first in any non-research language? That’s just entirely beyond the horizon right now.

Thanks. I see the problems.

In my opinion this would be a great addition to the language but i see know it is quite difficult to implement currently.

However many research languages, like lean or rocq have not the wide spread adoption like rust has. Thus maybe in some future rust has a place there to bridge the gap.

A dependently typed system is interesting and has a lot of practically useful aspects, but I think it is less likely to happen. If you really want to express such a constraint which is applicable to scenarios involving runtime operations, I think you need to use some verification tools such as Verus and Creusot. These tools let you express far more complicated logical predicates that are verified statically. One caveat of this approach is that the downstream users also need to use these tools to verify their codes. Furthermore, it is worth noting that not all the langugage features are supported for verification, but I think more features will be adopted gradually.

Oh, absolutely. When Rust would be old legacy language, similar to C++, there would be a next one that would replace it. Currently it looks as if dependent type system is good candidate for that one. Expect it somewhere near year 2050 or 2060.

Sure, but there are reason for why they remain research languages: people are just not ready to accept that much strangeness in one pass.

Rust had to drop a lot of features that are well-explored in research to ensure it would have a chance of being adopted by “normal” people, not just by eggheads.

It's unlikely to happen in Rust because it's hard to introduce it without redoing, in the incompatible fashion, everything starting from std.

But this could be a new frontier for popular languages. About one generation later for obvious reaons.

This works:

struct Matrix<T, const ROWS: usize, const COLS: usize> {
    values: [[T; COLS]; ROWS],
}

fn concatenate_arrays<T: Copy + Default, const N: usize, const N1: usize, const N2: usize>(
    v1: &[T; N1],
    v2: &[T; N2],
) -> [T; N] {
    const {
        assert!(N == N1 + N2);
    }
    let mut v = [T::default(); N];
    let (left, right) = v.split_at_mut(N1);
    left.copy_from_slice(&v1[..]);
    right.copy_from_slice(&v2[..]);
    v
}

fn concatenate<
    T: Copy + Default,
    const ROWS: usize,
    const COLS: usize,
    const COLS1: usize,
    const COLS2: usize,
>(
    m1: &Matrix<T, ROWS, COLS1>,
    m2: &Matrix<T, ROWS, COLS2>,
) -> Matrix<T, ROWS, COLS> {
    const {
        assert!(COLS == COLS1 + COLS2);
    }
    let mut values = [[T::default(); COLS]; ROWS];
    for row in 0..ROWS {
        values[row] = concatenate_arrays(&m1.values[row], &m2.values[row]);
    }
    Matrix { values }
}