Filling The Matrix (Language Design)

Filling the matrix is a language design principle. I’ve coined the term because I am not aware of a proper term describing the principle below. Let me know if you are aware of a proper existing term; I assume I’m not the first thinking about this.

Background

Next Generation Shell, a shell I’m working on, has a language. That language relies on multiple dispatch heavily. There is a guiding principle I’m following when implementing methods. That’s the principle this post describes.

As a side note, relevant for understanding the text below: in NGS, the operators are just syntactic sugar for a method call. Therefore, 1 + 2 is treated the same as +(1, 2). The method + (plus) is called with two arguments.

Why Matrix?

I found it convenient to think about multimethods and their arguments as a matrix. Each column is a multimethod (ex: echo, each, filter, +, *). Each row is a tuple of arguments’ types (ex: (Int), (Int, Int), (Int, Str)). The cell at the intersection of a method and arguments’ types therefore refers to the implementation of the method for the given types.

+*
(Str, Int)no “obvious” implementationrepeat the string
(Int, Int)add numbersmultiply numbers
(Str, Str)concatenate stringsno “obvious” implementation
(Arr, Arr)concatenate arraysCartesian product
The Methods / Arguments Matrix

The Principle

“Filling the Matrix” principle says the matrix should be as full as reasonable.

If there is a method, it should work with any combination of arguments that makes sense, filling the corresponding cell. Example with multimethod + (plus) follows:

1 + 1          # a programmer knows it works
"abc" + "def"  # should work too
[1] + [2,3]    # should work too

That implementation of the method for the given types should do the “obvious” thing. If the obvious thing is not obvious (excuse me my french), than there should be no implementation for the given types. Note that the “obvious” thing might become apparent later.

What’s Behind the Principle?

Behind the principle is the idea that a programmer should know relatively few verbs (method names) in the language and they would cover most needs. It should be pretty safe for the programmer to assume that the known verb would work with any combination of nouns (arguments). My theory is that it makes programming experience better than having differently named methods for different combinations of arguments. I do feel this subjectively. Pointers to research proving or disproving the theory are welcome.


Hope this helps. Constructive feedback is welcome.


Update: reddit discussion

Leave a comment