News for hackers. Stuff that matters.
Re:Doing First Grade Math in Rust's Type System by scns on Saturday January 13, 2024 @05:01AM (#38978560)
Can someone give an example of a useful application of this technique? Honest question, if it is just for fun that is fine with me.
Re:Doing First Grade Math in Rust's Type System by ReleaseCandidat on Saturday January 13, 2024 @05:15AM (#38978609)
Before const generics, you needed this kind of "types" to use integer values in generics, for example for fixed-length vectors/matrices/... (and const generics still have limitations).

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

Re:Doing First Grade Math in Rust's Type System by hgomersall on Saturday January 13, 2024 @07:50AM (#38979506)
You still need them for many cases the current const generics implementation doesn't support.
Re:Doing First Grade Math in Rust's Type System by turboponyy on Saturday January 13, 2024 @07:31AM (#38979365)
Not really in the programming mainstream, but in research-oriented languages with dependent typing like Lean or Idris, constraining your "implementation search space" with types and leveraging the type system to prove properties about your program is commonplace.
Re:Doing First Grade Math in Rust's Type System by SkiFire13 on Saturday January 13, 2024 @06:31AM (#38979050)
As others mentioned, nowadays const generics replace this technique for most problems. This technique however is still useful if you need something like `[T; A+B]` (an array whose length is the sum of two generic constants), which is not yet possible with const generics.
Re:Doing First Grade Math in Rust's Type System by ReleaseCandidat on Saturday January 13, 2024 @06:59AM (#38979194)
How do you prove to the type-checker that e.g. `append(v: [T, N], w: [T, M]) -> [T, N + M]`? Or, to make it easier, that just `append(v: [T, N], x: T]) -> [T, N + 1]`?
Re:Doing First Grade Math in Rust's Type System by madsbuch on Saturday January 13, 2024 @07:41AM (#38979433)
you implement the function recursively and let the compiler derive a proof by induction.
Re:Doing First Grade Math in Rust's Type System by sigi64 on Saturday January 13, 2024 @06:13AM (#38978954)
This technique is good for job interviews, when they ask me to solve leetcode problems. I was inspired by the blog:

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-...

Re:Doing First Grade Math in Rust's Type System by magicalhippo on Saturday January 13, 2024 @05:23AM (#38978654)
While not nearly as involved, I've seen similar-ish techniques used for FFTs and similar algorithms, in order to precompute constants.

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...

Re:Doing First Grade Math in Rust's Type System by ReleaseCandidat on Saturday January 13, 2024 @05:35AM (#38978740)
You do that (as nowadays in C++) with compile time "constants" https://doc.rust-lang.org/reference/const_eval.html
Re:Doing First Grade Math in Rust's Type System by magicalhippo on Saturday January 13, 2024 @06:09AM (#38978933)
Yes, but then you wouldn't be abusing the type system, so where's the fun in that? :P
Re:Doing First Grade Math in Rust's Type System by Timshel on Saturday January 13, 2024 @05:51AM (#38978829)
Something like a typed heterogeneous list is often build in a similar way.

A simpler version is probably a simple non empty list where you always have the guaranty of at least one element.

Re:Doing First Grade Math in Rust's Type System by bionhoward on Saturday January 13, 2024 @05:13AM (#38978596)
Life critical mathematics: you could verify calculations in the type system which are too risky to allow to fail at runtime.
Re:Doing First Grade Math in Rust's Type System by tsnl on Saturday January 13, 2024 @05:40AM (#38978770)
Theorem provers that use dependent typing, like Coq.
Re:Doing First Grade Math in Rust's Type System by im_down_w_otp on Saturday January 13, 2024 @10:52AM (#38981038)
We used methods akin to this back in 2019 for creating low-level HALs for insanely complicated SoCs (think experimental switch fabric controllers). We wanted the compiler to reject any program that was attempting to access the hardware incorrectly because the dev-test cycle was slow, the hardware was very fiddly, and debugging was miserable. The register definitions were all generated from the supplier data sheet (a clojure program we wrote parsed a PDF then emitted Rust).

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/

Re:Doing First Grade Math in Rust's Type System by wegfawefgawefg on Saturday January 20, 2024 @11:23AM (#39069150)
Theres a train company using these to prove the microcontrollers and train network cant be in bad states. check youtube