这种语法是否像 do-notation 一样富有表现力?

Is this syntax as expressive as the do-notation?

do 表示法允许我们表达一元代码而无需过多的嵌套,因此

main = getLine >>= \ a -> 
       getLine >>= \ b ->
       putStrLn (a ++ b)

可以表示为

main = do
  a <- getLine
  b <- getLine
  putStrLn (a ++ b)

不过,假设语法允许 ... #expression ... 代表 do { x <- expression; return (... x ...) }。例如,foo = f a #(b 1) c 将被脱糖为:foo = do { x <- b 1; return (f a x c) }。上面的代码可以表示为:

main = let a = #getLine in
       let b = #getLine in
       putStrLn (a ++ b)

这将被脱糖为:

main = do
  x <- getLine
  let a = x in
    return (do
      x' <- getLine
      let b = x' in
        return (putStrLn (a ++ b)))

那是等价的。这种语法对我很有吸引力,因为它似乎提供了与 do-notation 相同的功能,同时还允许一些更短的表达式,例如:

main = putStrLn (#(getLine) ++ #(getLine))

所以,我想知道这个提议的语法是否有任何缺陷,或者它是否确实完整并且等同于 do-notation。

这个怎么样:

do a <- something
   b <- somethingElse a
   somethingFinal a b

putStrLn 已经是 String -> IO (),所以你的脱糖 ... return (... return (putStrLn (a ++ b))) 最终有类型 IO (IO (IO ())),这可能不是你想要的:运行 这个程序不会打印任何东西!

更一般地说,您的符号不能表达任何不以 return. 结尾的 do 块 [参见 Derek Elkins'评论。]

我不相信你的表示法可以表示join,可以用do表示,没有任何附加功能:

join :: Monad m => m (m a) -> m a
join mx = do { x <- mx; x }

但是,你可以表达fmap约束到Monad

fmap' :: Monad m => (a -> b) -> m a -> m b
fmap' f mx = f #mx

>>=(以及其他所有内容)可以使用 fmap'join 来表示。所以添加 join 会使你的符号完整,但在很多情况下仍然不方便,因为你最终需要 lot of joins.

但是,如果您从翻译中删除 return,您会得到与 Idris' bang notation 非常相似的内容:

In many cases, using do-notation can make programs unnecessarily verbose, particularly in cases such as m_add above where the value bound is used once, immediately. In these cases, we can use a shorthand version, as follows:

m_add : Maybe Int -> Maybe Int -> Maybe Int
m_add x y = pure (!x + !y)

The notation !expr means that the expression expr should be evaluated and then implicitly bound. Conceptually, we can think of ! as being a prefix function with the following type:

(!) : m a -> a

Note, however, that it is not really a function, merely syntax! In practice, a subexpression !expr will lift expr as high as possible within its current scope, bind it to a fresh name x, and replace !expr with x. Expressions are lifted depth first, left to right. In practice, !-notation allows us to program in a more direct style, while still giving a notational clue as to which expressions are monadic.

For example, the expression:

let y = 42 in f !(g !(print y) !x)

is lifted to:

let y = 42 in do y' <- print y
                 x' <- x
                 g' <- g y' x'
                 f g'

讨论过将其添加到 GHC,但被拒绝(到目前为止)。不幸的是,我找不到讨论它的线程。