首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >箭头(->)作为haskell数据构造函数

箭头(->)作为haskell数据构造函数
EN

Stack Overflow用户
提问于 2019-07-26 12:09:48
回答 2查看 308关注 0票数 10

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

如果我试图将其用作数据构造函数的名称,它将无法解析,并且我不确定如何解释它。如果我将其解释为函数类型,那么数据构造函数没有名称,这对我来说也没有意义。

代码语言:javascript
复制
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 '->'
EN

回答 2

Stack Overflow用户

发布于 2019-07-26 18:05:01

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

便笺

这不能用GADTs解析

代码语言:javascript
复制
data Type where
  TBool :: Type
  TInt :: Type
  Arrow :: Type -> Type -> Type
  (->) :: Type -> Type -> Type
票数 2
EN

Stack Overflow用户

发布于 2019-07-26 22:28:13

正如@leftaroundabout评论的那样,根据this的说法,你必须添加一个:来创建中缀构造函数:并且根据this question

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

所以举个例子,这是行不通的:

代码语言:javascript
复制
data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (-**) Type Type

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

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

但这将会:

代码语言:javascript
复制
data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (:-**) Type Type

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

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

还有这个:

代码语言:javascript
复制
data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (:-*) Type Type

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

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

在您的情况下,您将需要类似以下内容:

代码语言:javascript
复制
data Type
  = TBool
  | TInt
  | Arrow Type Type
  | (:->) Type Type

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

test' :: Type
test' = (:->) TBool TInt
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57213096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档