【问题标题】:Arrow (->) as haskell data constructor箭头 (->) 作为 haskell 数据构造函数
【发布时间】:2019-08-03 02:18:08
【问题描述】:

我对箭头及其在数据构造函数中的实际含义感到困惑。我很惊讶它甚至编译但我不知道如何使用它。

如果我尝试将它用作数据构造函数的名称,它不会解析,而且我不确定如何解释它。如果我将其解释为函数类型,则数据构造函数没有名称,这对我来说也没有意义。

data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (->) Type Type

test :: Type 
test = Arrow TBool TInt -- Ok

test' :: Type
test' = (->) TBool TInt -- Parse Error on input '->'

【问题讨论】:

  • 是否有任何语言扩展在使用?
  • 真的很奇怪。我可以确认 data 声明已解析,但尝试使用 (->) 失败,没有启用任何扩展。我怀疑解析算法在data 声明中的规则与在表达式中的规则不同,但不能明确地说出什么。
  • 这在我看来像是一个 GHC 错误。
  • 相当肯定data Type = (->) Type Type 不应该解析。中缀构造函数通常必须以: 开头,即data Type = (:->) Type Type 是最接近的合法近似值。
  • 我已将其提交为issue #16999

标签: haskell


【解决方案1】:

在您提供的用例中,它确实看起来像 GHC bug(感谢 Kevin Buhr 的报告)。

注意

这确实无法使用 GADTs 解析:

data Type where
  TBool :: Type
  TInt :: Type
  Arrow :: Type -> Type -> Type
  (->) :: Type -> Type -> Type

【讨论】:

  • 我怀疑您关于错误原因的假设是否正确。请参阅票证上的讨论。
  • @dfeuer,是的,我猜错了。事实上,我仍然不明白这个错误的原因是什么。尽管如此,答案仍然是正确的,它是一个 ghc 错误。 ;)
  • 我也不知道。解析 Haskell 比较难,解析 GHC Haskell 真的很难。我个人认为一些关于语法的决定是不明智的。回过头来用成熟的语言改变事情是相当棘手的,但看起来其中一些决定正在重新考虑到DependentHaskell 的漫长道路上。
【解决方案2】:

正如@leftaroundabout 所评论的,并且根据this,您必须添加: 才能创建中缀构造函数: 并根据this question

与数据构造函数不同,不允许使用中缀类型构造函数(除了 (->))。

所以举例来说,这是行不通的:

data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (-**) Type Type

test :: Type 
test = Arrow TBool TInt -- Ok

test' :: Type
test' = (-**) TBool TInt

但这会:

data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (:-**) Type Type

test :: Type 
test = Arrow TBool TInt -- Ok

test' :: Type
test' = (:-**) TBool TInt

还有这个:

data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (:-*) Type Type

test :: Type 
test = Arrow TBool TInt -- Ok

test' :: Type
test' = (:-*) TBool TInt

在你的情况下,你会想要这样的东西:

data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (:->) Type Type

test :: Type 
test = Arrow TBool TInt -- Ok

test' :: Type
test' = (:->) TBool TInt

【讨论】:

  • 中缀类型的构造函数可以使用TypeOperators 扩展。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2013-08-14
  • 2015-11-15
  • 2013-02-22
相关资源
最近更新 更多