Live data from Hacker News

Type-Level Programming in Rust

willcrichton.net

21–29 of 29 posts

Re: Type-Level Programming in Rust

#21

Earlier quoted context omitted.

Further to this: transmute is really pretty dangerous, and you’ve got to be very careful if you use it, or you’ll invoke undefined behaviour. And I believe the transmutes in the original article do invoke undefined behaviour, since the layout of the struct is undefined (you’d need to use something like `#[repr(C)]` on the struct to make it be defined). In practice, these particular examples are at least 99.9999% sure…

Has there been any work in Rust on something like Haskell’s ‘Coercible’? Namely, it defines the precise criteria for safe zero-cost coercions between types that provably have the same representation, and derives the coercions automatically. It guarantees that ‘coerce’ is identical to ‘fmap coerce’, which saves the cost of that exact traverse+reconstruct pattern to change out a phantom type parameter. There are some d…

We have a project group working on "safe transmute", here's their first RFC https://github.com/rust-lang/rfcs/pull/2981

It cites the Haskell work: https://github.com/jswrenn/rfcs/blob/safer-transmute/text/00...

Re: Type-Level Programming in Rust

#22
post #2

A few months later, Will released Tyrade, an impressive macro-based DSL for type-level programming in Rust: https://github.com/willcrichton/tyrade The README uses some of the same motivating examples as the blog post. Type-level programming absolutely has its place. Rust's `typenum` and `generic_array` crates enabled projects like `nalgebra` long before const generics. I recently used type-level programming to protot…

That is really cool. Would "Use types as data primitives" be a good way to describe Tyrade?

Re: Type-Level Programming in Rust

#24
post #3

I'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here. However, there's some really neat stuff later, so it's wise to grit your teeth and get past it. In case anyone else…

Hi, author of the code here. Why do I use mem::transmute? To highlight the fact that when using typestate, it's useful to understand operations that change the the value of an object as distinct from those that change the type of an object. It's also a matter of efficiency -- you don't have to generate a new struct every time you change state. You can avoid the overhead without transmute, but it's still unsafe and no…

In response to a challenge about the use of unsafe in the very first example of a blog post that you acknowledge doesn't need it, you respond with a defense of the code in some library. The differences between those contexts is massive; this seems central to the misunderstanding here.

By applying a pattern that should rightly raise alarm bells for any seasoned Roast coder, you've distracted from your goal. By linking to a Reddit discussion rather than editing your post to clarify it, you've done your readers a disservice by drawing them into this muck.

Re: Type-Level Programming in Rust

#25
post #8
post #3

I'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here. However, there's some really neat stuff later, so it's wise to grit your teeth and get past it. In case anyone else…

Im not disagreeing with you but fyi will crichton definitely knows what he is talking about. He teaches Programming Language courses at Stanford and his brother is the all-time top contributor to Rust. So, pedagogy + rust is sort of his thing. In general, this series of posts has been more about types than about rust so I don't blame him for ignoring unsafe. It isnt relevant (its also not a best practice!)

Teachers are just like everyone else: they're fallible and half of them are below average.

Re: Type-Level Programming in Rust

#27

Earlier quoted context omitted.

Has there been any work in Rust on something like Haskell’s ‘Coercible’? Namely, it defines the precise criteria for safe zero-cost coercions between types that provably have the same representation, and derives the coercions automatically. It guarantees that ‘coerce’ is identical to ‘fmap coerce’, which saves the cost of that exact traverse+reconstruct pattern to change out a phantom type parameter. There are some d…

We have a project group working on "safe transmute", here's their first RFC https://github.com/rust-lang/rfcs/pull/2981 It cites the Haskell work: https://github.com/jswrenn/rfcs/blob/safer-transmute/text/00...

Cool, thanks!

I’m thinking about a similar sort of coercion system for a language project, and it’s good to have a slightly broader view of the design space. This area hasn’t been very well explored yet, not just in terms of semantics, but also developer ergonomics: most of the time you want to hide representations for convenience, but when considering safe coercions, you really want to make it possible to talk about them explicitly & precisely.

This feature is also very subtle, and right at the very edge of the type system, so it’s extremely easy to break soundness here, usually by failing to consider things like variance. The reason “roles” were added to GHC in the first place isn’t just because of the issue with type families / associated types mentioned in that RFC; it’s also because anything that provides a safe API atop unsafe internals could also be used to violate soundness, especially in conjunction with mutation. The classic examples are: 1. coercing “container of A” to “container of B” using the coercion from A to B, then inserting a B that is not a valid A, either in its value or just because A and B have incompatible trait instances (like ordering or hashing). You need to be able to disallow conversions between things even if they have identical representations.

Re: Type-Level Programming in Rust

#28
post #8

Earlier quoted context omitted.

Im not disagreeing with you but fyi will crichton definitely knows what he is talking about. He teaches Programming Language courses at Stanford and his brother is the all-time top contributor to Rust. So, pedagogy + rust is sort of his thing. In general, this series of posts has been more about types than about rust so I don't blame him for ignoring unsafe. It isnt relevant (its also not a best practice!)

Teachers are just like everyone else: they're fallible and half of them are below average.

Well, teachers are a subset of humanity and thus in principle they could all be above the overall average for humanity. It's even intended that there be a selection bias, so hopefully most of them are at least above average at teaching if nothing else. Of course half of them are worse than the average teacher, but that's not very useful information.

Re: Type-Level Programming in Rust

#29
post #7

Just please don't overdo type-level programming, especially not if it involves trait bounds and wildcard impl. Finding the right balance between using type-level programming to ensure invariants and not using it is a important skill.

Especially so with a language like Rust, IMO. Its type system is powerful enough that you kind of can do it, but it gets awkward pretty quickly. In some ways that's a dangerous combo!
Post reply on HN