为什么需要进行诸如“runSomeMonad $ do ...”这样的$ in通话? [英] Why there needs to be a $ in calls like "runSomeMonad $ do ..."?

查看:108
本文介绍了为什么需要进行诸如“runSomeMonad $ do ...”这样的$ in通话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然, runSomeMonad do ... 的唯一可能解释是 runSomeMonad(do ...)。为什么不是Haskell语法所允许的第一个变体?是否有某些情况下 foo do bar 可能实际上不明确?

解决方案

请注意,您可以观察到这种效应,不仅仅是 do ,还有 let if \ case ,扩展 mdo proc ...以及恐惧一元 - 。我想不出一个 以下是在 Haskell 2010语言报告§3:表达式

  exp 
→infixexp :: [context =>]
| infixexp

infixexp
→lexp qop infixexp
| - infixexp
| lexp

lexp
→\ apat1 ... apatn - > exp
|让exp加
|如果exp [;]然后exp [;] else exp
|案例exp {alts}
|做{stmts}
| fexp

fexp
→[fexp] aexp

aexp
→(exp)
| ...

fexp (函数应用程序)或 aexp (文字表达式),它允许一个未被解释的 lexp (lambda, let 等)。我会认为这是一个语法错误。



解决这个问题也将消除对 $ 输入hack


Apparently the only possible interpretation of runSomeMonad do ... is runSomeMonad (do ...). Why isn't the first variant allowed by the Haskell syntax? Is there some case where foo do bar could be actually ambiguous?

解决方案

Note that you can observe this effect with not just do, but also let, if, \, case, the extensions mdo and proc…and the dread unary -. I cannot think of a case in which this is ambiguous except for unary -. Here’s how the grammar is defined in the Haskell 2010 Language Report, §3: Expressions.

exp
    → infixexp :: [context =>] type
    | infixexp

infixexp
    → lexp qop infixexp
    | - infixexp
    | lexp

lexp
    → \ apat1 … apatn -> exp
    | let decls in exp
    | if exp [;] then exp [;] else exp
    | case exp of { alts }
    | do { stmts }
    | fexp

fexp
    → [fexp] aexp

aexp
    → ( exp )
    | …

There just happens to be no case defined in fexp (function application) or aexp (literal expression) that allows an unparenthesised lexp (lambda, let, etc.). I would consider this a bug in the grammar.

Fixing this would also remove the need for the $ typing hack.

这篇关于为什么需要进行诸如“runSomeMonad $ do ...”这样的$ in通话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆