gpt4 book ai didi

haskell - 这种语法和 do-notation 一样具有表现力吗?

转载 作者:行者123 更新时间:2023-12-04 22:10:02 25 4
gpt4 key购买 nike

do 表示法允许我们在没有过多嵌套的情况下表达 monadic 代码,因此

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。

最佳答案

putStrLn 已经是 String -> IO () ,所以你的脱糖 ... return (... return (putStrLn (a ++ b))) 最终具有类型 IO (IO (IO ())) ,这可能不是你想要的:运行这个程序不会打印任何东西!
更一般地说,您的符号不能表达任何不以 do 结尾的 return -block。 [见德里克埃尔金斯的评论。]
我不相信你的符号可以表达 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 会使你的符号完整,但在很多情况下仍然不方便,因为你最终需要很多 join s。
但是,如果您从翻译中删除 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,但被拒绝(到目前为止)。不幸的是,我找不到讨论它的线程。

关于haskell - 这种语法和 do-notation 一样具有表现力吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43433781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com