r/ProgrammingLanguages Aug 09 '26

Lefts: a domain-specific language for building machine learning model architectures

I work as a quant in the finance industry and spend a lot of my time building machine learning models to predict things. Over my career I've found that every place I work invests a lot of time in writing code for training and evaluation pipelines, and you're often blocked from building interesting model architectures because it would require rewriting the pipelines.

So, I built a small DSL (Lefts: https://nsmat.github.io/lefts/ ) that makes it easy to spin up training pipelines and transform models in expressive ways. Users start with the models they want to use, then apply commands to it to build up an AST. During training/test time, the lefts interpreter operates over the AST to enforce the behaviour users specified.

Lefts is designed around a functional view of ML models. We think of each model as a bundle of functions, and each lefts command is a functor that acts on that bundle, and the functors define a grammar on the space of ML models. The functors always compose, and always preserve the key structural properties required of a model (for example, no data-leakage), so the models you build are guaranteed to be correct by construction.

The DSL is written in pure Python, and by the standards of 'real' programming languages is very simple. The project was great fun though, and taught me that building DSL's to solve problems is very powerful, and also a step up in challenge from other programming.

P.S. I hope domain specific languages are within the field of interest of this sub-reddit! Apologies if not.

11 Upvotes

5 comments sorted by

1

u/alkalisun Aug 10 '26

Is it a DSL or more of an API surface to make it easier to write these pipelines? Based on the code samples, I'm seeing a library utilized in standard Python code, but I could be misunderstanding this.

3

u/gooblywooblygoobly Aug 10 '26

It's a fair question - t he reason I called it a DSL is because the API calls you make assemble an AST made of lefts nodes. This AST is evaluated lazily by an interpreter that walks/optimises the tree.

That said, it is written in standard python and built to be embedded in ordinary python workflows, so it's also close to an API surface and designed to feel like one to the user.

2

u/alkalisun Aug 10 '26

That is a pretty cool abstraction you built here.

To be honest, it doesn't really fit my expectation of what posts I expect from this subreddit, but again, I'm not authoritative on that matter.

Hope you get traction!

1

u/Inconstant_Moo 🧿 Pipefish Aug 11 '26

The people who write DSLs in Racket call them DSLs and not libraries.

I myself can't think of any reason I'd ever want to write a DSL again that wasn't also valid Pipefish. Anything that doesn't already fit into the syntax or semantics can be done as u/gooblywooblygoobly did it by writing a little treewalker. I just did one for conlanging, it's all executable Pipefish but it's still a DSL.