We all love do, and I was curious if perhaps this sort of alternate syntax would theoretically be useful outside of the monad world. If so, what other sorts of computations would it simplify? Would it make sense to have something equivalent for Applicative, for example?
We all love do , and I was curious if perhaps this sort of
Share
It might help to consider, regarding
donotation itself, what it’s actually good for. As Travis Brown points out, I’ve previously advocated the use of a “function application” style withMonads and related types, but there’s a flip side to that as well: Some expressions simply can’t be written cleanly in direct function application style. For instance, the following can quickly make applicative style clumsy:Writing such a function as a single expression generally requires either multiple nested lambdas, or the kind of absurd obfuscating nonsense that gives point-free style a bad name. A
doblock, on the other hand, provides syntactic sugar for easy nested scoping of intermediate results with embedded control flow.Normally you’d probably extract such subexpressions and put them in a
whereclause or something, but since ordinary values form a monad with function application as(>>=)–namely theIdentitymonad–you could conceivably write such a function in adoblock instead, though people might look at you funny.Besides the scoping/binding stuff, the other thing a
doblock does for you is elide the operator that chains subexpressions together. It’s not too hard to imagine other cases where it would be nice to have a notation for “combine these expressions using this function within this block”, and then let the compiler fill in the blanks.In the easy case, where the expressions all have the same type, putting them in a list and then folding it works well–building strings in this manner using
unwordsandunlines, for instance. The benefit ofdois that it combines expressions with common structure and compatible–but not identical–types.In fact, the same general principle is true of the “idiom bracket” notation from the
Applicativepaper: Wheredoblocks use newlines to elide monadic construction, idiom brackets use juxtaposition to elide lifted function application. Theprocnotation forArrowis also similar, and other concepts could be cleanly expressed in such fashion as well, such as:Although it’s not too hard to make many of these into either a single type or a full
Monadinstance, it might be nice to have a unified, extensible bit of syntactic sugar for the general concept. There’s certainly a common thread tying together all these and more, but that’s a much larger topic not really related to syntax…