RustSydney / talks

Talk requests and proposals for Rust Sydney
http://www.meetup.com/Rust-Sydney/
22 stars 2 forks source link

Talk proposal: Emulating higher kinded types #32

Closed clintonmead closed 5 years ago

clintonmead commented 5 years ago

Hi All

I'm relatively new to Rust. I came to the March meetup however the first time I actually sat down and started writing some code in it was this weekend, and I just saw your email about talks.

I have played with Haskell quite a bit and I have a full time role in C#. A summary of me and my projects can be found at clintonmead.github.io.

I gave a talk at FP-Syd in November called "Type Classes in C#". Unfortunately, the guts of that talk was a live coding demonstration and there's no video, but perhaps the demostration code using the library may be of some small insight.

But in any case in attempt to learn Rust in some anger I've managed to implement the beginnings of such an approach in Rust, implementing Functor in Rust. It should run also on Rust stable, it doesn't need any extensions.

My library allows me to write something like:

fn map2<TFunctor, T1, T2, T3>(f1 : &fn(T1) -> T2, f2: f2: &fn(T2) -> T3, x: TFunctor<T1>) -> TFunctor<T3> {
  x.map(f1).map(f2)
}

Note above that TFunctor<?> is not valid rust, as we don't have higher kinded types, so I encode this in a different way, but in a way so that:

let x : Option<u16> = Some(1);
let y : Option<u32> =  map2(f, g, x);

compiles and runs quite happily as long as "Option" implements Functor, and f and g are appropriate (I know map2() is silly, as you should just compose the functions, but it's just an illustrative example). We can effectively pretend we've got higher kinded types even though the actual types and traits going on under the hood are are slightly more ugly.

I have done a scan of the existing crates and there's a few that implement type classes like Functor and Monad, but from what I've seen (and I may be wrong here) none of them allow you to write functions which abstract over Functor/Monad. As in, I couldn't seem to work out how to write map2 with these libraries. I could only write map2 for a particular instance of a Functor type, i.e. separate map2 for Option<T> and Vec<T>. Although perhaps I've missed something here, and it's quite possible I'm reinventing the wheel here so if so please point it out as I may be reinventing it poorly.

But I'm happy to put one together a talk for mid June, two weekends should be plenty of time to flesh this out as long as I don't hit any snags.

Although at that point I'd only have three weeks experience with Rust, so I might be asking more questions to the audience than they are asking me. However, as I've done this in C# and so far it's actually easier in Rust I think I'll be able to piece something together.

The talk itself shouldn't take long (there's just a few type tricks to show) but I think it's better to demonstrate it with a lot of examples and have a Q&A style feedback to get the feel of it. The talk at FP-Syd was around 40 minutes including Q&A, but that was with C# which a lot of people weren't familiar with so using Rust at a Rust meetup might make things easier to understand.

So if this is interesting to people I'm happy to be pencilled in for a talk. I'll try to upload some code over the weekend so you can have a more concrete idea of what I'm talking about.

clintonmead commented 5 years ago

On some further investigation it seems like I'm not the first to think of this style of transformation. coyoneda has a similar basic idea, although my approach doesn't require a separate representation (so should have a zero or low runtime cost for the abstraction), also doesn't need explicit wrapping/unwrapping, and extends to Applicatives and Monads.

bitshifter commented 5 years ago

Sounds interesting Clinton, what kind of time are you thinking? You might need to introduce the FP concepts you want to talk about as I don't think everyone at the meetup will have FP background (it's a real mix).

bitshifter commented 5 years ago

I'll get back to you with a date soon

clintonmead commented 5 years ago

Sounds interesting Clinton, what kind of time are you thinking? You might need to introduce the FP concepts you want to talk about as I don't think everyone at the meetup will have FP background (it's a real mix).

Hrmmm, good point about the FP intro. Perhaps I'll put together a 5-10 minute example of Monad usage (perhaps with a SafeNum style class that catches overflow/divide by zero unless you can think of a better example) to start off with so at least people can see some practical use of it.

How long it takes depends on how much people want to explore/ask questions, but it's better if people do, but I'm guessing 30 minutes to an hour. Might have a better idea when I clean up and comment the code. There's about half a dozen "tricks" that probably need the rationale behind them explained, most simple but some a bit more involved. For example there's one part which is just getting the typechecker to work with me, and I've tried to clean it up but then it just won't typecheck. I don't know why it doesn't typecheck when I clean it up and why it does when I leave it how it is (as they seem pretty similar to me) but I haven't got much of a feel of the Rust typechecker yet as I've only bashed it for a day over the weekend.

Might be best to put me last so I don't hold up another speaker if there's lots of questions.

bitshifter commented 5 years ago

Hi @clintonmead, would Monday the 17th of June work for you?

clintonmead commented 5 years ago

@bitshifter if I'm the second talk that should be fine (I've got a weekly work meeting with our UK team 5:30-6pm), so should be there by 6:30 but might be more like 7 if it runs overtime

bitshifter commented 5 years ago

Actually we're going to push this back to the 24th of June so there's a bit more time, does that still work for you?

clintonmead commented 5 years ago

Perfect the extra week will give me a bit of extra time to put it together.

Same thing applies with the Monday meeting though so just put me last and it'll be fine.

bitshifter commented 5 years ago

Hi Clinton, would you like to provide a brief summary of your talk and bio. It's not at all mandatory, just something I did last time that I think was useful for attendees. E.g. from last meetup https://www.meetup.com/Rust-Sydney/events/258785543/, could be a lot briefer than that and totally fine if you'd rather not. The new meetup description is here https://www.meetup.com/Rust-Sydney/events/262194894.

clintonmead commented 5 years ago

Will do. Is it okay if I put it together over the weekend? At that point I'll have a more complete idea of what I want to cover.

bitshifter commented 5 years ago

Yep, no problem

bitshifter commented 5 years ago

Thanks for the talk Clinton, feel free to post links to your code and/or slides on the event page for anyone who is interested https://www.meetup.com/Rust-Sydney/events/262194894/