syntax - Haskell: Would "do" notation be useful for contexts other than monads? -
we love do
, , curious if perhaps sort of alternate syntax theoretically useful outside of monad world. if so, other sorts of computations simplify? make sense have equivalent applicative, example?
it might consider, regarding do
notation itself, it's actually for. travis brown points out, i've advocated use of "function application" style monad
s , related types, there's flip side well: some expressions can't written cleanly in direct function application style. instance, following can make applicative style clumsy:
- intermediate results used in multiple subexpressions, @ different depths of nesting
- arguments outermost function used deeply-nested in subexpressions
- awkward or inconsistent argument order, i.e. needing partially apply function other first argument
- deeply embedded flow control based on intermediate results, shared subexpressions between branches
- pattern matching on intermediate results, particularly in case of extracting part of result, using further computation, reconstructing modified version next result
writing such function single expression requires either multiple nested lambdas, or kind of absurd obfuscating nonsense gives point-free style bad name. do
block, on other hand, provides syntactic sugar easy nested scoping of intermediate results embedded control flow.
normally you'd extract such subexpressions , put them in where
clause or something, since ordinary values form monad function application (>>=)
--namely identity
monad--you conceivably write such function in do
block instead, though people might @ funny.
besides scoping/binding stuff, other thing do
block elide operator chains subexpressions together. it's not hard imagine other cases nice have notation "combine these expressions using function within block", , let compiler fill in blanks.
in easy case, expressions have same type, putting them in list , folding works well--building strings in manner using unwords
, unlines
, instance. benefit of do
combines expressions common structure , compatible--but not identical--types.
in fact, same general principle true of "idiom bracket" notation applicative
paper: do
blocks use newlines elide monadic construction, idiom brackets use juxtaposition elide lifted function application. proc
notation arrow
similar, , other concepts cleanly expressed in such fashion well, such as:
- composing data structures, e.g. merging result sets of sort, eliding merge function
- other function application idioms, such argument-first "forward pipe" style, eliding application operator
- parallel computations, eliding result aggregation function
although it's not hard make many of these either single type or full monad
instance, might nice have unified, extensible bit of syntactic sugar general concept. there's common thread tying these , more, that's larger topic not related syntax...
Comments
Post a Comment