Tuesday, July 05, 2011

[vqctislo] Sparse matrices and language syntax

While programming languages elegantly describe list and tree like organization of code (code, i.e., syntax trees, not data), there are code structures that are sparse matrices which are awkward to describe as a tree.

A concrete example is Haskell's "instance" mechanism, where you keyboard-type the keyword "instance", the name of the typeclass, then the name of the type.  Each of these (the typeclass and the type) have been declared elsewhere, so there's a feeling of unnecessary keyboard-typing.  This corresponds to a sparse matrix representation of the nonzero matrix entries as a list of (row, column, value) tuples.

An alternative is, within the definition of the typeclass, to list and define all its instances.  The dual alternative is, within the type definition (data Foo = ...) to list and define all typeclasses it is an instance of.  These save a bit on keyboard-typing, but are awkward because they could require modifying code that is not yours.  As sparse matrices, they correspond to picking one row, giving a list of its nonzero columns, and moving on to the next row.  (Or dually column-major.)  A list of tuples, the second element of each tuple is a list of (column, value) tuples.

There is no simple solution that eliminates needing to keyboard-type either the type or the typeclass name again, unless we discard our traditional notions of a programming language.  Perhaps instances are declared in cells on a spreadsheet, literally a matrix representation.  It becomes (nearly) impossible to edit code in a plain text editor.

No comments :