简体   繁体   English

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

[英]Arrow (->) as haskell data constructor

I'm confused about the arrow and what it actually means in the data constructor. 我对箭头及其在数据构造函数中的实际含义感到困惑。 I'm surprised it even compiled but I have no idea how to use it. 我很惊讶它甚至编译但我不知道如何使用它。

If I try to use it as the name of a data constructor it doesn't parse, and I'm not sure how else to interpret it. 如果我尝试使用它作为数据构造函数的名称,它不解析,我不知道如何解释它。 If I interpret it as a function type then the data constructor has no name which also does not make sense to me. 如果我将它解释为函数类型,那么数据构造函数没有名称,这对我来说也没有意义。

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 '->'

It does look like a GHC bug (thanks to Kevin Buhr for reporting it) in the use case you provided. 在您提供的用例中,它确实看起来像GHC错误 (感谢Kevin Buhr报告)。

Note 注意

This does fail to parse with GADTs : 这无法解析GADTs

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

As @leftaroundabout commented, and According to this you must add a : in order to create infix constructors: And according to this question : 正如@leftaroundabout评论的那样,根据这个你必须添加一个:以创建中缀构造函数:并根据这个问题

Unlike data constructors, infix type constructors are not allowed (other than (->)). 与数据构造函数不同,不允许使用中缀类型构造函数(( - >)除外)。

So by example this won't work: 所以通过示例这将不起作用:

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

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

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

but this will: 但这会:

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

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

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

and this: 还有这个:

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

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

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

and in your case you will want something like: 在你的情况下,你会想要这样的东西:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM