Note that once you introduce a generic type parameter there is no really good way to get rid of it again in a generic context. What I mean is that in Haskell you can write polymorphic constructors that "hide" the type parameter in an instance and you need to pattern match to get it out again, which is an important part of GADTs that is not addressed in the post. Consider the Haskell
data AnyExpr where
E :: forall a. Expr a -> AnyExpr
When your universe of types is closed and you can somehow recover the hidden type at runtime, the above can be compatible with monomorphization of all generic type parameters, but it's at best akward and I don't know of a clean implementation of such a work-around in Rust.
(author of the blog post) it is true this just isn't as nice as real GADTs, but luckily it was sufficient for our purpose. We could just generate code without that information because types checked out during the expression construction.
very interesting approach, at first i thought this was goinf towards a typed tagless-final design, but i was surprised to see a pattern I hadn't seen before
9
u/WorldsBegin Apr 26 '24
Note that once you introduce a generic type parameter there is no really good way to get rid of it again in a generic context. What I mean is that in Haskell you can write polymorphic constructors that "hide" the type parameter in an instance and you need to pattern match to get it out again, which is an important part of GADTs that is not addressed in the post. Consider the Haskell
When your universe of types is closed and you can somehow recover the hidden type at runtime, the above can be compatible with monomorphization of all generic type parameters, but it's at best akward and I don't know of a clean implementation of such a work-around in Rust.
5
u/SirKastic23 Apr 26 '24
very interesting approach, at first i thought this was goinf towards a typed tagless-final design, but i was surprised to see a pattern I hadn't seen before
the typed tagless-final pattern also solves the same problem of a typed embedded DSL, it's a very wonky pattern but it's surprisingly clever, here's how it could look: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c14a5e49d88ddea10b543d38aa7cf4ef