为什么不能在Haskell中重新定义:运算符? [英] Why can't I redefine the : operator in Haskell?

查看:118
本文介绍了为什么不能在Haskell中重新定义:运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于学习目的,我试图在Haskell中重新实现列表数据类型的某些功能.当我尝试使用以下代码重新定义:时:

I'm trying to reimplement some functionality of the list data type in Haskell for learning purposes. When I try to redefine : with this code:

{-# LANGUAGE NoImplicitPrelude #-}

data List a = Nil
            | Cons a (List a)

(:) :: a -> List a -> List a
(:) = Cons

我在使用stack runghc时遇到以下错误:

I get the following error with stack runghc:

无效的类型签名:(:) :: ...

Invalid type signature: (:) :: ...

应采用< variable>形式::< type>

Should be of form <variable> :: <type>

是否无法重新定义:?这就是为什么我会收到此错误吗?

Is it impossible to redefine :? Is that why I'm getting this error?

推荐答案

无法重新定义:,但这不是为什么会出现此错误的原因.之所以会出现此错误,是因为:被认为是大写标点符号",也就是说,任何以:开头的名称都必须是(中缀)值构造函数.但是,即使打开NoImplicitPreludeRebindableSyntax,您也会发现它,例如

It is impossible to redefine :, but that is not why you are getting that error. You are getting that error because : is considered "upper-case punctuation" -- that is, any name that starts with : must be an (infix) value constructor. Nevertheless, even with NoImplicitPrelude and RebindableSyntax turned on, you will find that, e.g.

data Foo = Foo : Foo

给您一个错误,说:

error: Illegal binding of built-in syntax: :

大概需要一些额外的工程工作,未来的GHC可以通过重新打开一些合适的扩展来支持重新定义:,但目前尚不可能.

Presumably with some extra engineering effort, a future GHC could support redefining : with some suitable extensions turned on, but for now it's not possible.

这篇关于为什么不能在Haskell中重新定义:运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆