If one wants to add the capability to reason about shape and shape compatibility, Barry Jay's FiSh would be an interesting detour.
https://web.archive.org/web/20111015133833/http://www-staff....
This was used in his shape aware language FiSh, for dealing with multidimensional arrays. Shape compatibilities were statically type checked, if I recall correctly. Shapes were also used to optimize the loops.
[Programming in FISh] https://link.springer.com/article/10.1007/s100090050037
[Towards Dynamic Shaping] https://www.researchgate.net/publication/265975794_Towards_D...
I wish we used something else but Python as the default ML language, so that some advanced type system would work for us to ensure tensor shapes and compatibility.