Doing First Grade Math in Rust's Type System
fprasx.github.io
fprasx.github.io
For complicated runtime behavior, we have various tools for stepping through the evaluations as they unfold. E.g., debuggers, print statements, etc.
But I haven't come across anything similar for these type-based evaluations. Usually the best I get is a compiler warning or error, or even worse, an unexpected successful compilation.
Are there any efforts to help with this, for e.g. C++ compilation?
Lisp is dynamically typed, so it's not the same as C++ templates or the Rust type system, but to me, it seems like there isn't a reason in principle why you couldn't make a stepper for pure C++ template metaprogramming, like this Peano arithmetic implementation: https://gist.github.com/vladris/2dc4f5a47d003da002a70f0c0184...
Particularly for "using" declarations, like this (taken from the gist that I linked):
// A few numbers...
using one = succ<zero>;
using two = succ<one>;
using three = succ<two>;
using four = succ<three>;
using five = succ<four>;
Also, the clangd language server expands C preprocessor macros, but that's less useful because the C preprocessor doesn't allow recursive macros.If the target system is a space rocket, medical systems, etc. One really want to have guarantees.
> Here, we're using the fact that x * y = x + (x - 1) * y. We recursively calculate the second term, and then add it to the first factor!
It should say
x*y = y + (x-1)*y.I'm the author of the mentioned peano and typenum libraries, if anyone has any questions.
https://doc.rust-lang.org/reference/items/generics.html#cons...
They came with 1.51: https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html
Here[1] is an example in C++ but given the article it seems you could do something similar in Rust.
[1]: https://github.com/tmolteno/template-fft/blob/master/fft_rea...
A simpler version is probably a simple non empty list where you always have the guaranty of at least one element.
https://aphyr.com/posts/342-typing-the-technical-interview
and related to Rust, the solution
https://github.com/insou22/typing-the-technical-interview-ru...
Or Typescript https://www.richard-towers.com/2023/03/11/typescripting-the-...
We wrote a blog post about the Rust type-level programming part of the approach: https://blog.auxon.io/2019/10/25/type-level-registers/
C++ is by far the most Wild West, the most gotcha, the most “do as I say but not as I do”, the most “yes, except if” language ever. From 98-today, it’s just gotten more and more bloated with alternative pathways and acid pits.
I’m hopeful for Rust. Equally so for Zig. My opinions mean shit and in the end, the Rust team will evolve Rust beyond what they originally intended, capturing dev attention and pushing Rustic concepts outwards. Such as they all have the last 15 years.
Rust can be easy enough to get going if you know to use .clone(), Rc<RefCell<>> and the like. Yes it's a bit heavy on boilerplate but that's about it. Also the boilerplate is helpful since it can guide a refactoring effort to make the code a bit more efficient. With Rust, "clean" code is usually fast and safe code which is not as true in other languages.
They came with 1.51: https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html
You need it every day to, you know, hold that meat on your carcass.
Addition would only be allowed if the types are equal.
Edit: I've just realised that that's actually correct ;)