It's basically Hindley-Milner.
Elfstone
elfstone at yeah.net
Sat Mar 25 05:35:46 UTC 2023
On Friday, 24 March 2023 at 12:29:02 UTC, kdevel wrote:
> On Friday, 24 March 2023 at 09:47:59 UTC, FeepingCreature wrote:
>> On Friday, 24 March 2023 at 09:44:28 UTC, FeepingCreature
>> wrote:
>>> If we insert the missing step, what the compiler should do
>>> is: "Ah, `B` is a 'trivial alias'
>>
>> My apologies, correction: `A` is a 'trivial alias'. Sorry for
>> the minipost, but this topic demands precision.
>
> Thanks for explaining what goes on under the hood. I would like
> to branch to something entirely different, namely the original
> code Elfstone drafted in the first post:
>
> ```
> struct Matrix(S, size_t M, size_t N)
> {
> }
>
> alias Vec3(S) = Matrix!(S, 3, 1);
> ```
>
> What is conceptually wrong with this code? Can't we read
> everywhere [1] that matrices with only one column ‘are’
> (column) vectors?
>
> However, a matrix is a two-dimensional aggregate, its elements
> are refered to with two indices. By contrast vectors are
> one-dimensional. Of course it is elegant to define the dot
> product of a (row) vector and a (column) vector in terms of
> matrix multiplication [2] but the interesting part here is the
> the ‘identification’ of the result (dot product) with the
> element (1, 1) of the resulting 1×1 matrix. Sometimes this
> precision gets lost, e.g. [3].
>
> Hence: A vector should not be defined as a matrix with only
> column (or row).
>
> [1]
> https://www.google.de/search?q=vectors+are+matrices+with+one+column
> [2] https://en.wikipedia.org/wiki/Dot_product
> [3]
> https://yutsumura.com/a-relation-between-the-dot-product-and-the-trace/
> - "Recall that v^Tw is, by definition, the dot product of the
> vectors v and w."
The design can't be perfect for everything but it's good enough
for many things. `Eigen` in C++ does the same and I think it's
still one of the best libraries out there.
Actually the main benifit I see is that I don't have to
specialize heavy operations such as `Matrix * Vector` with
largely the same code as the general `Matrix * Matrix`. But I do
happily define `opIndex(size_t i)` when `M == 1 || N == 1`, which
is trivial.
It's but a convenient example to demonstrate the problem with D's
`alias` anyway.
More information about the Digitalmars-d
mailing list