4.17.2012

Some thoughts about expanding expressions

I've been thinking about my own programming language today and worked on the type system but also spend some time on something I call "multi target" in expressions. My first ideas was that one could create sequences like a;b;c that will be expand so that no more elements are in the sequence. So that "a;b;c = 1" becomes "a=1, b=1, c=1" and so on. But let's just think about it: how would this sequence behave when using it multiple times in an expression? Would "a;b;c = d;e;f" become "a=d, b=e, c=f" or "a=d;e;f, b=d;e;f, c=d;e;f" ? It's a question of how the "unrolling" of those sequence behaves.

I've been tinkering with this for quite a while now and came up with two different models: The first is that all sequences created with "&" that share the same expression level (no brackets seperating them) will be unrolled parallely, "a&b=1" becomes "a=1, b=1" and "a&b = c&d" becomes "a=c, b=d". To expand this, there's a second sequence type build with "|" that behaves the opposite way, working as a multiplier. In combination with brackets, inner brackets will be unrolled first and then unrolled with the outer expression. So "|" is effectly the same as a bracketized "&" and doesn't need to be used if brackets one the same expression level will be solved one after another, generating a large amount of code with just the original expression.

So the second model takes a different yet similar approach by omitting the "|" operator's superfluous function and replacing with the same as "&", but just after "&" has been applied. This generates some interesting behaviour because it gives you the multiplicative behaviour of a pure multi-bracketed "&" expression but combining each singularly unrolled bracket into one that'll be unrolled parallely after "&".

The second approach is superior because it makes additional unrolling possible. I've taken a look at differently formulas and all of them share the same layout as matrix multiplication with a fixed dimension: one completely strechted range on every column and one with a repeated pattern, both alternated as often of the matrix has columns. Think about it: if you take this setup of different operators, you could create any kind of formula extremely quickly. Even more interesting is the idea of taking not only those two operators but a bracket pair that will indicate the resolution level. This way, multiple "loop levels" are possible and you can effectively create several nested loops where each iteration can map to the n-th element of every sequence sharing the same level. Quite abstract, isn't it? Well, I'm very happy about this find as it makes it possible to create an amazing amount of large code cramped into a single function and don't need to waste to any time with loop iteration. Furthermore, this can be linked to an unroller for static content and BAM you got a lot of code merged into a single formula. with no needed iteration or so. It's not exactly about the unrolling thing makes me excited: it's the possibility to remove loop constructs that usually obscure reading. I mean just look at mathematical functions and tell me where the loop iteration is. Well, there's just no except a few sum symbols or so. Stuff can be written as it is and very easy to read. Personally, I found it more easy to understand algorithms and operations by unrolling them manually and then understanding the logic behind. It's harder to understand something the pure algorithm that doesn't have to do anything with the original formula.

So what I'm trying to explain is that I've found something truly awesome that can be used in any VM or assembly based language in equal ways, unrolled or not. So, you see, it pays off to tinker with thinking about this stuff. I'm gonna try some of this stuff with C macros and some ways of iterating variadic macros with known argument count I discovered while working on OOP macros. So this could work very nicely in C as well, though with quite a pervert syntax.

No comments: