我怎样才能删除“如果...那么...否则...” Haskell (GHC) 中的关键字?
我想删除 if ... then ... else ...
关键字,因为我在 Haskell 中嵌入了语言/DSL。 if
、then
和 else
在许多领域中传达了很多含义,如果我可以重新定义(或保留它们未定义),那就太好了它们反映了语言/领域的本质。
我在 Google 和 stackoverflow 上搜索过,但一无所获。 (我确实找到了一个旧线程,说明为什么 if ... then ... else ...
被作为 Haskell 中的关键字包含在内。)
我的 IDE 在 Leksah 中,并且,如果可以删除关键字,如果有一个设置可以将 if ... then ... else ...
关键字更改回正常的字体/颜色/非粗体,那就太好了。
我已经尝试过将 if'
命名为 if
等。它感觉不太好,特别是当我想定义if
和if'
,并且必须定义if'< /code> 和
if''
代替,或者 if1
和 if2
。 if'
和 if
的存在也可能会造成混淆。 (在我的情况下,混乱并不是那么严重的问题,因为 DSL 的用户是 Haskell 程序员,但我认为它在其他情况下可以有所帮助)。
总结迄今为止的响应:
- 使用 GHC 的
RebindableSyntax
扩展。不像删除关键字那么普遍:Haskell 的 if-then-else 语法被保留。 (Frerich Raabe) - 解决方法:通过使用
data Conditional ba = If b (Then a) (Else a)
使用非常相似的单词/名称(仅适用于某些上下文)。 (CA McCann)
如果 RebindableSyntax 是一个相对较新的功能,那么它不太可能找到更通用的方法,至少在下一个版本的 GHC 之前不会。
I would like to remove the if ... then ... else ...
keywords, because I am embedding a language/DSL in Haskell. if
, then
and else
convey a lot of meaning in many domains, and it would be great if I could redefine (or leave them undefined) them to reflect the nature of the language/domain.
I've searched on Google and stackoverflow, but found nothing. (I did find an old thread on why if ... then ... else ...
was included as keywords in Haskell.)
My IDE is in Leksah, and, if the keywords can be removed, it would also be nice to have a setting to change the if ... then ... else ...
keywords back to their normal font/color/unbold.
I've already tried a naming convention of if'
for if
and so on. It doesn't feel as good, especially when I want to define if
and if'
, and have to define if'
and if''
instead, or if1
and if2
. The presence of both if'
and if
might also be confusing. (The confusion is not that serious an issue in my situation as the users of the DSL are Haskell programmers, but I suppose it can help in other situations).
Summarizing the responses to date:
- Use the
RebindableSyntax
extension to GHC. Not as general as removing the keywords: the syntax of Haskell's if-then-else is retained. (Frerich Raabe) - Workaround: Use very similar words/names, by using
data Conditional b a = If b (Then a) (Else a)
(only applicable in some contexts). (C. A. McCann)
If RebindableSyntax
is a relatively new feature, then it's unlikely to find a more general way, at least not till the next version of GHC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RebindableSyntax 扩展to GHC 允许您使用自己的版本重载
if ... then ... else
表达式。特别是,ifThenElse
函数用于定义替代含义。if e1 then e2 else e3"
表示ifThenElse e1 e2 e3
。请参阅博客文章 可重新绑定 if..then..else 表达式 对此功能进行了很好的讨论,包括一些例子。
The RebindableSyntax extension to GHC lets you overload
if ... then ... else
expressions with your own version. In particular, theifThenElse
function is used to define alternative meanings.if e1 then e2 else e3"
meansifThenElse e1 e2 e3
.See the blog article Rebindable if..then..else expressions for a nice discussion of this feature, including some examples.
您无法删除现有关键字。正如所指出的,您可以使用 RebindableSyntax,但这可能无法达到您想要的效果。
唯一接近删除关键字的方法是打开 CPP 选项并执行类似
预处理器将 if/then/else 扩展为 if_/then_/else_ 的操作。
You can't remove existing keywords. As was pointed out you can use RebindableSyntax, but that might not do what you want.
The only thing getting close to removing keywords is to turn on the CPP option and doing something like
The preprocessor will then expand if/then/else to if_/then_/else_.
怎么样:
How about: