简体   繁体   English

Constraint tupling的可键入实例

[英]Typeable instance for Constraint tupling

I'm trying to derive a Typeable instance for tupled constraints. 我正在尝试为Typeable约束派生一个Typeable实例。 See the following code: 请参阅以下代码:

{-# LANGUAGE ConstraintKinds, GADTs #-}
{-# LANGUAGE DataKinds, PolyKinds, AutoDeriveTypeable #-}
{-# LANGUAGE StandaloneDeriving, DeriveDataTypeable #-}

import Data.Proxy
import Data.Typeable

data Foo (p :: (*, *))

data Dict ctx where
    Dict :: ctx => Dict ctx
  deriving (Typeable)

deriving instance Typeable '(,)
deriving instance Typeable Typeable
deriving instance Typeable Show

works :: IO ()
works = print (typeRep (Proxy :: Proxy (Foo '(Bool, Char))))

alsoWorks :: IO ()
alsoWorks = print (typeRep (Dict :: Dict (Show Bool)))

fails :: IO ()
fails = print (typeRep (Dict :: Dict (Show Bool, Typeable Bool)))

main :: IO ()
main = works >> alsoWorks >> fails

If you compile this with -fprint-explicit-kinds , the following error is given: 如果使用-fprint-explicit-kinds编译, -fprint-explicit-kinds出现以下错误:

    No instance for (Typeable
                   (Constraint -> Constraint -> Constraint) (,))

Is there a way to derive such an instance? 有没有办法推导出这样的实例? Everything I try refuses to disambiguate from the ★ -> ★ -> ★ constructor. 我尝试的一切都拒绝消除★ -> ★ -> ★构造函数的歧义。

GHC can not currently make a Typeable instance, or any other instance, for (,) :: Constraint -> Constraint -> Constraint . GHC目前无法为(,) :: Constraint -> Constraint -> Constraint创建Typeable实例或任何其他实例。 The type constructor (,) only has kind * -> * -> * . 类型构造函数(,)只有类型* -> * -> * There is no type constructor for products of the kind Constraint -> Constraint -> Constraint . 对于Constraint -> Constraint -> Constraint类型的产品,没有类型构造函数 The constructor (,) is overloaded to construct both tuples and products of Constraint s, but has no corresponding type constructor when used to make a product of Constraint s. 构造函数(,)被重载以构造Constraint的元组和产品,但在用于制作Constraint的产品时没有相应的类型构造函数。

If we did have a type constructor for products of Constraint s we should be able to define an instance as follows. 如果我们确实有Constraint产品的类型构造函数,我们应该能够如下定义一个实例。 For this, we'll pretend (,) is also a type constructor with kind (,) :: Constraint -> Constraint -> Constraint . 为此,我们假装(,)也是一个带有kind (,) :: Constraint -> Constraint -> Constraint的类型构造函数。 To define an instance for it, we'd use KindSignatures and import GHC.Exts.Constraint to be able to talk about the kind of constraints explicitly 要为它定义一个实例,我们使用KindSignatures并导入GHC.Exts.Constraint以便能够明确地讨论约束的类型

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE KindSignatures #-}

import GHC.Exts (Constraint)
import Data.Typeable

deriving instance Typeable ((,) :: Constraint -> Constraint -> Constraint)

If we do this now, it results in the following error, due to the kind of the (,) type constructor. 如果我们现在这样做,由于(,)类型构造函数的种类,它会导致以下错误。

The signature specified kind `Constraint
                              -> Constraint -> Constraint',
  but `(,)' has kind `* -> * -> *'
In the stand-alone deriving instance for
  `Typeable ((,) :: Constraint -> Constraint -> Constraint)'

The constraints package also works with products of constraints, and includes the following note . 约束包也适用于约束的产品,并包括以下注释

due to the hack for the kind of (,) in the current version of GHC we can't actually make instances for (,) :: Constraint -> Constraint -> Constraint 由于当前版本的GHC中的那种(,)的黑客,我们实际上不能为(,) :: Constraint -> Constraint -> Constraint创建实例

I presume the hack Edward Kmett is referring to is the overloading of the (,) constructor for Constraint s without a corresponding type constructor. 我认为,Edward Kmett所指的黑客是没有相应类型构造函数的Constraint s的(,)构造函数的重载。

It seems that it is not currently possible. 似乎目前不可能。 There's a revealing comment in the latest version of constraint : 最新版本constraint有一个有启发性的评论:

due to the hack for the kind of (,) in the current version of GHC we can't actually make instances for (,) :: Constraint -> Constraint -> Constraint 由于当前版本的GHC中的那种(,)的黑客,我们实际上不能为(,):: Constraint - > Constraint - > Constraint创建实例

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

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