Live data from Hacker News

Doing First Grade Math in Rust's Type System

fprasx.github.io

31–40 of 43 posts

Re: Doing First Grade Math in Rust's Type System

#31
post #27

Nice write-up. I'm the author of the mentioned peano and typenum libraries, if anyone has any questions.

The problem with this peano arithmetic thing is that it only goes up to 88 https://upload.wikimedia.org/wikipedia/commons/b/bd/D274.jpg

If you build rustc with MIDI support enabled, it goes up to 100.

Re: Doing First Grade Math in Rust's Type System

#32

Reading this I realize that I know nothing about Rust's type system, despite using the language daily.

When I use Rust, I feel like I'm just a dude mopping the floors of the nuclear silo.

Imagine doing C++. It's like being the new guy who has to manually take Geiger readings of the warhead.

Re: Doing First Grade Math in Rust's Type System

#33
I'm uncomfortable with programs that rely on complicated compile-time evaluation within the type system.

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?

Re: Doing First Grade Math in Rust's Type System

#34

I'm uncomfortable with programs that rely on complicated compile-time evaluation within the type system. 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 succe…

Zig's comptime feature is really well designed IMO. It uses an interpreter to let you run arbitrary Zig at compile-time, and you can use it to do code generation, custom types, etc. It's pretty cool.

Re: Doing First Grade Math in Rust's Type System

#35

I'm uncomfortable with programs that rely on complicated compile-time evaluation within the type system. 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 succe…

For Lisp macros, there is a macrostep plugin for Emacs that expands a macro within the text of your source code one step at a time until there's nothing left to be expanded. This is temporary, so after you exit the macrostep mode, the source code viewer goes back to normal.

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;
  using two = succ;
  using three = succ;
  using four = succ;
  using five = succ;
Also, the clangd language server expands C preprocessor macros, but that's less useful because the C preprocessor doesn't allow recursive macros.

Re: Doing First Grade Math in Rust's Type System

#36

Earlier quoted context omitted.

When I use Rust, I feel like I'm just a dude mopping the floors of the nuclear silo.

Imagine doing C++. It's like being the new guy who has to manually take Geiger readings of the warhead.

Basic modern C++ is easier than Rust at this point. It’s not as safe (std::shared_ptr is not your friend), but it’s easier to get going vs dealing with lifetimes and Rustic syntax. I feel like at this point Rust has inherited all the syntax diarrhea of C++ and makes me adverse to using it. I don’t want to wrestle type system crap when I’m trying to wrestle app features. Some things I love. A few things I hate. Those things I hate make me use another language. A lot of rust is inferred through use so why bother with the extra cognitive load of having to unwrap or box or ‘a or worse ‘static. Zig isn’t better in this area either, requiring allocators for everything. Go is great but it’s a bit too simple and not fast enough for Rust-like workloads. This is me with a sad face because while I applaud the effort to build modern languages and tooling, we still haven’t addressed the issue of syntax elitism and shame-by-code-review because someone didn’t know about macro meta. I just want to code, have my program work, and not have to master 4th-degree black belt syntax grammar to do it. I think any language or runtime that allows one to write 4th-degree black belt syntax grammar should be shunned for simpler terms. Imagine the quality of software we would produce with a simpler type system. This was the goal of Go but it still needs work on language features for me (I really really want operator overloading on structs using + - * and / instead of methods).

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.

Re: Doing First Grade Math in Rust's Type System

#37

Earlier quoted context omitted.

Imagine doing C++. It's like being the new guy who has to manually take Geiger readings of the warhead.

Basic modern C++ is easier than Rust at this point. It’s not as safe (std::shared_ptr is not your friend), but it’s easier to get going vs dealing with lifetimes and Rustic syntax. I feel like at this point Rust has inherited all the syntax diarrhea of C++ and makes me adverse to using it. I don’t want to wrestle type system crap when I’m trying to wrestle app features. Some things I love. A few things I hate. Those…

> It’s not as safe (std::shared_ptr is not your friend), but it’s easier to get going vs dealing with lifetimes and Rustic syntax.

Rust can be easy enough to get going if you know to use .clone(), Rc> 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.

Re: Doing First Grade Math in Rust's Type System

#38

Earlier quoted context omitted.

Basic modern C++ is easier than Rust at this point. It’s not as safe (std::shared_ptr is not your friend), but it’s easier to get going vs dealing with lifetimes and Rustic syntax. I feel like at this point Rust has inherited all the syntax diarrhea of C++ and makes me adverse to using it. I don’t want to wrestle type system crap when I’m trying to wrestle app features. Some things I love. A few things I hate. Those…

> It’s not as safe (std::shared_ptr is not your friend), but it’s easier to get going vs dealing with lifetimes and Rustic syntax. Rust can be easy enough to get going if you know to use .clone(), Rc > 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 f…

Less divergent code does not make better code. Rc> is a prime example of "Do as we say, not as we do" in Rust that has made its way in from C++. I thought the borrow-checker was gospel and yet, here we are on the cusp of circumventing that with dynamic memory allocation checking (or lack thereof, just that it's the same size).

Re: Doing First Grade Math in Rust's Type System

#39

Earlier quoted context omitted.

> It’s not as safe (std::shared_ptr is not your friend), but it’s easier to get going vs dealing with lifetimes and Rustic syntax. Rust can be easy enough to get going if you know to use .clone(), Rc > 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 f…

Less divergent code does not make better code. Rc > is a prime example of "Do as we say, not as we do" in Rust that has made its way in from C++. I thought the borrow-checker was gospel and yet, here we are on the cusp of circumventing that with dynamic memory allocation checking (or lack thereof, just that it's the same size).

Rust has always supported some variety of Rc because the borrow checker cannot account for every possible way of writing safe code. It's not something that has "made its way in" from the outside, it's just as inherent to the language as anything else in it.
Post reply on HN