Monday, August 9, 2021

Pattern introduction

Most programming languages have a concept of handling some action more than once. Loops are probably the most familiar mechanism, and most programmers are also aware of recursion. But there are other options for dealing with this need. For example, functional languages also frequently supply functions like: map, filter, flatten, fold, and others. Used creatively, many operations that seem to require a loop or recursion can be instead solved with these functions.

The benefit of map etc is that they provide well typed and composable intermediates. Loops do a bunch of stuff and then we hope that the end result makes sense. Recursion is kind of hard to glue together with other recursion. Map etc compose nicely with each other.

The problem is that there are some missing use cases where you need to fall back on recursion or loops to get the functionality that you need. If you have a tree structure and want to know about leaf nodes that have grandparent nodes in common, then you'll need some recursion. Similarly, if you have items in a collection that dictate how you iterate through the rest of the collection, then you'll need a loop.

My thought is that it should be possible to specify these things with a series of patterns. The underlying language infrastructure will need loops and recursion, but that's always the case. The programmer can focus only on the specification of the data structure that's interesting.

It seems that there's already a programming language that's trying this sort of thing. The Egison programming language. I haven't looked deeply enough into it to see if their goal is the same as my own. But a precursory glance shows some impressive results.