HugoPeters1024 / blog

0 stars 0 forks source link

Higher Kinded Types in Rust #21

Open HugoPeters1024 opened 2 years ago

HugoPeters1024 commented 2 years ago

https://hugopeters.me/posts/14/

exFalso commented 2 years ago

Nice! Although this isn't quite proper higher kinded types, it's not truly parametric in B. Good news is that people are working on it, if you look at the TODO list, it seems it's coming soon!

Completely unrelated sidenote: if you like this sort of hackery, I've always wanted to try something but never had the time: I'm conjecturing that with async Rust where async Futures are Clone, we could implement monadic do notation using the mother of all monads idea. Could be a fun project

HugoPeters1024 commented 2 years ago

@exFalso Nice! Although this isn't quite proper higher kinded types, it's not truly parametric in B. Good news is that people are working on it, if you look at the TODO list, it seems it's coming soon!

Completely unrelated sidenote: if you like this sort of hackery, I've always wanted to try something but never had the time: I'm conjecturing that with async Rust where async Futures are Clone, we could implement monadic do notation using the mother of all monads idea. Could be a fun project

Thanks for you comment! What exactly do you mean with it being not truly parametric in B? Regarding the mother of all monads, that's a cool concept. Too bad that the IO-pandora box has already been opened for rust :)

pchampin commented 2 years ago

Interesting read, thanks. Small typo: the associated type in the declaration of Generic1 is named R, should be I.

HugoPeters1024 commented 2 years ago

@pchampin Interesting read, thanks. Small typo: the associated type in the declaration of Generic1 is named R, should be I.

Thanks for the kind words, typo is fixed :)

exFalso commented 2 years ago

@HugoPeters1024

@exFalso Nice! Although this isn't quite proper higher kinded types, it's not truly parametric in B. Good news is that people are working on it, if you look at the TODO list, it seems it's coming soon!

Completely unrelated sidenote: if you like this sort of hackery, I've always wanted to try something but never had the time: I'm conjecturing that with async Rust where async Futures are Clone, we could implement monadic do notation using the mother of all monads idea. Could be a fun project

Thanks for you comment! What exactly do you mean with it being not truly parametric in B? Regarding the mother of all monads, that's a cool concept. Too bad that the IO-pandora box has already been opened for rust :)

Ah my bad, didn't see the Plug action, thought B is bound by a Functor associated type. Yeah this works, sweet!

Cypher1 commented 2 years ago

Copied all the code into this playground for easy viewing and play.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f12fcdd7b9962f2c2a1e443b6ff6ed01

Made two changes: 1) switch from println! to dbg! which I feel is cleaner, 2) use .to_string()

HugoPeters1024 commented 2 years ago

@Cypher1 Copied all the code into this playground for easy viewing and play.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f12fcdd7b9962f2c2a1e443b6ff6ed01

Made two changes: 1) switch from println! to dbg! which I feel is cleaner, 2) use .to_string()

Thanks for your suggestions! Especially the .to_string() method is something I will use from now on

Pegasust commented 1 year ago

For Functor, why does this not work?

trait Functor {
    fn fmap<B, F: Fn(&<Self as Generic1>::I) -> B>(&self, f: &F) -> <Self as Plug<B>>::R
        where Self: Plug<B> + Generic1;
}

or similarly:

trait Functor {
    fn fmap<B>(&self, f: &fn(&<Self as Generic1>::I) -> B>) -> <Self as Plug<B>>::R
        where Self: Plug<B> + Generic1;
}

Why is dyn required in this case?

I also messed around and looks like the following works (though it is less powerful type-wise)

trait Functor<B>: Plug<B> + Generic1 {
    fn fmap(&self, f: &fn(&<Self as Generic1>::I) -> B>) -> <Self as Plug<B>>::R;
}