r/rust • u/ggim76 • Sep 27 '24
Functional Patterns in Rust: Identity Monad
I've been exploring how functional programming concepts like monads can be applied in Rust. Here's my implementation of the Identity Monad which essentially wraps a value and allows for monadic chaining using the >> operator. The code includes an example with the Ackermann function to demonstrate how computations can be structured using this monad.
https://gist.github.com/ploki/9b94a21dbf94e9b24a106fc4df32968c
I'd love to hear your thoughts and any feedback you might have!
54
Upvotes
50
u/WormRabbit Sep 28 '24
Your signatures are wrong. You call each closure only once, so your trait bound should be
FnOnce(A) -> Id<B>rather thanFn(A) -> Id<B>. With your signature, it is impossible to pass any value by move, you'd have to explicitly clone everything all over the place.Which also means that your closures must force-move their captures, i.e. you should use the
move |..|syntax.Really, just try your design with something more complex than trivial integers. Try an owning type, like
String. Try borrowed values, e.g.&stror&Box<Foo>. You'll quickly hit the bounds of your approach.If you want to make it pretty, write a proc macro with a custom syntax, which would compile to your nested closures.
But unless you're doing it just to tease your brain, a word of advice. Don't try to write Haskell in Rust. Really, Haskell patterns rarely work well anywhere outside Haskell, because they either crucially depend on its lazy evaluation and immutability, or exist as a solution to issues caused by them.
27
u/Flowchartsman Sep 28 '24 edited Sep 28 '24
Having gone down this rabbit hole before, I think you will probably find the following useful, as I did:
https://varkor.github.io/blog/2019/03/28/idiomatic-monads-in-rust.html
In particular:
Which is about where we bottomed out, too. It starts out looking like it will be elegant and simple to do haskellish FP in Rust and you can’t believe that no one ever took the time to “do it right” (as your abstractions do), but then you start to run into the limitations a lack of HKTs brings, and each new concession generates a new construct, and pretty soon you’re knee deep in magical macros or writing something unwieldy and inscrutable that just doesn’t look like Rust anymore.
2
u/Delta-9- Sep 28 '24
First time I've seen "shove" used to name the unit/return/eta function.
As usual with identity functions, I have some trouble thinking of what I'd do with this. Obviously function composition abstracted through the monad, but like... Why write functions as
Id<A> -> Id<B>instead of justA -> B? The Monad itself isn't really doing anything, so it feels like an unnecessary abstraction.That said, it would definitely make a great basis for other monad types that can reuse traits and methods written for
Idif they don't need some special behavior on those methods or traits (likeResultneeding to returnOkorError, or a Reader that has to inject some context into the function call).I haven't played with monad transformers, but maybe there's some application there, too?
3
u/link23 Sep 28 '24
Prior art, with some actual motivating code examples: https://www.reddit.com/r/rust/s/6uZr76R2QB
1
u/thatdevilyouknow Sep 28 '24
It reminds me of Underscores.jl from Julia where, with the inclusion of Transducers.jl, you can write something like this (taken from the Julia forums):
Iterators.countfrom() |> Map(x -> 2x) |> TakeWhile(x -> x < 10) |> collect. I actually like the pipe syntax of|>because fonts with ligatures can transform it into ▶️. Using>>immediately makes right shift come to mind. So with Elm you can dog o’ f = \x -> (f x) |> andThen gorg o’ f = \x -> (f x) >>= gand there you have that bind operator in the latter example. Having that distinction in there would be great.1
u/DatGirlLucy Sep 28 '24
You might find this crate interesting. We created a proc macro for deriving functors in Rust.
https://crates.io/crates/functor_derive