这个 haskell 语法是什么?
我刚刚在一段 Haskell 代码中遇到了以下语法 -
data A = A Int Int | B
m :: A -> Int
m a = case a of
A{} -> 1
_ -> 2
A{}
在这里做什么? {}
是否会自动匹配任意数量的参数?
我有一种感觉,这是利用 Haskell 将语法脱糖记录为一堆函数和常规代数数据类型的事实。是这样吗?
I just ran across the following syntax in a piece of Haskell code -
data A = A Int Int | B
m :: A -> Int
m a = case a of
A{} -> 1
_ -> 2
What is the A{}
doing here? Does the {}
automatically match for any number of arguments?
I have a feeling that this is exploiting the fact that Haskell record syntax desugars to a bunch of functions and a regular Algebraic Datatype. Is that the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,
A{}
匹配使用A
构造函数构造的任何值,无论该类型是否已使用记录语法声明。语言报告指定
括号中提到的“第四个项目符号”指出,使用省略严格字段的记录语法构造值是一个静态错误。
在模式匹配部分中,语法规则之一对于模式是
,语义在模式匹配的形式语义(3.17.3)小节中给出:
Yes,
A{}
matches any value constructed with theA
constructor, regardless of whether the type has been declared with record syntax or not.The language report specifies
The 'fourth bullet' mentioned in the parenthesis states that it is a static error to construct a value with record syntax which omits a strict field.
And in the section on pattern matching, one of the grammar rules for patterns is
and the semantics are given in the subsection on formal semantics of pattern-matching (3.17.3) as