Math DSL Idea
By Angus Thomsen WrittenI'd like a programming environment, ideally an interactive environment that let me experiment and iterate on different model ideas. Be it a linear regression or some variation of OLS, WLS, GLS, etc. Or a strucutured economic model (like this one homelessness) or some statistical model infused with randomness of various distributions (normal or not). I'd like something that automatically transform y = 5 + 3x + 6x² into its intergral or derivative, I'd like to define a custom number type (field, group, ring, whatever) like some kind of polynomial where the interdeterminant resolves to something after a certain transform (like how i turns to -1 in complex numbers) and I'd like to use that number type interchangably with the standard float, int, etc. I'd like to be able to feed csvs, dataframes, database data or into any model, function, whatever as easily as I load a dataframe in python, query in to my model/function. I'd like to visualise the output as nicely as you would in ggplot in R. I'd like some kind of interactive environment on top like storybook where I can tweak a bunch of knobs and levels to tweak coefficents and constants. I'd like a piece of software that did all of this in one package. Is this too much to ask for?
A hypothetical language
I had an idea for a DSL to do all of the above and just compile it down to WASM stores stuff in arrays and has some GPU operations accelerated implemented in WebGPU, like ordingary least squares. So there would be some kind of runtime for the language, but it would work whereever there's a browser.
An important language feature would be polynomials, ideally you'd be able to implement complex numbers within the language. Although it probably makes sense for them to have an internal implementation, but the language itself should provide the functionality for users to define their own, and for the language to be able to generate GPU shaders to compute large operations on them.
Goal of the language
Despite my bucket list of advance lanaguage features, its needs to be accessible for those making statical, econometric, economic, whatever models. How to make that pleasant and ergonomic isn't immediately obvious to me. But I'd like to enable experimentation with stuff such as Ordinary Least Squares with swapping out real numbers for some custom defined polynomial.
High level features
- An interactive note book environment, while having a text based file type
- Data processing and visualising
Distinct Features
-
Linear Map: Being
able to transform functions like data at runtime when they're pure mathematic
expressions.
- This does sound like a macro, however its different, and it is at runtime
- Think derivatives, intergrals, limits, sovling a function from f(x) = y to f(y) = x
- Any mathematical function automatically benefits from GPU acceleration
- With the intention of compiling to WASM & JS, the runtime can easily generate shaders on the fly to run the function on large portions of data.
- Being able define fields, groups, rings, aka custom numbers
- Said numbers automatically benefit from language level features related GPU acceleration.
Uncertainty
Can it be a general purpose language
I think that would be nice, but at that point you need to get serious about many other things, like async behaviour, multi processing, etc. I just want to a better experince with writing mathematical models.
Can this just be a library?
I don't think so. Not without a highly unpleasant experince, but the runtime can probably be a library.
Basic Types
Serialisable Math Primatives
I am making a distinction here with other math primatives
- Reals
- Integers
- Polynomials
(1 + x) - Polynomials indeterminants:
- The
xin a polynomial - I'm thinking this is how we'd implement:
- Complex Numbers
- Split complex numbers
- Dual numbers
- Quaternion
- The
- Matrices
- Vectors
- Sets, different from the collections
- I wonder if I need to make distinction between a finite set and infinite set
Data Structures
- Array
- List
- Map
- Set (collection)
- Data Frames
Other types
These are types you'd come to expect in any other language
-
Functions (which can be defined anonymously).
- I am wondering if there needs to be a certain treatment of them to make closer how they are in math, so they can be transformed and treated as data, so it would be possible to implement some first class derivative transform. I don't think all functions could work like this.
- Maybe I need to make a distictions between a parameterised relations and functions. And in a function in math is what I have in mind here to a parameterised relation.
Type System Features
- Strong Typing, with optional static typing:
- I think it's important to have a frictionless workflow to allow building without type checking.
- However I'd like there to be a culture of type safety that was followed dogmatically by library authors.
- Algerbric types:
- Basically Haskel ADTs and GADTs
- Structs
- Associative Types
- Generics
- Can be generic by anything that is a math type
- Kind of like rust const generics
- mostly so matrice sizes can be typed
- as well as providing a type for matrice minor function
- Can be generic by anything that is a math type
- Higher Kind Types
- I don't get the opposition to them in rust they aren't that complicated
- This is the only thing I worry about not working in dynamic types