简体   繁体   English

临时类型的可输入实例

[英]Typeable Instances for Temporary Types

I am trying to make a reified (using Data.Reflection ) type an instance of Typeable. 我试图做一个类型化(使用Data.Reflection )的Typeable实例。

newtype Zq q = Zq Int

I can derive a Typeable1 instance for Zq , but this comes with the implicit constraint (Typeable q) . 我可以得出一个Typeable1例如Zq ,但这种带有隐含的约束(Typeable q) I want to use a value of type Zq q in a function which requires the argument to be Typeable . 我想在需要参数为Typeable的函数中使用Typeable Zq q类型的值。 So there are a few questions: 所以有几个问题:

  1. Is there any way to make a reified type Typeable ? 有什么方法可以使类型化的Typeable吗?
  2. If not, is there any way to modify the existing library to allow for this? 如果不是,是否有任何方法可以修改现有库以实现此目的?
  3. In general, which types can be made Typeable ? 通常, 可以使哪些类型成为Typeable It seems that by using DeriveDataTypeable and StandaloneDeriving , any concrete type I can think of (aside from a temporary type like the reified type) can be an instance. 似乎通过使用DeriveDataTypeableStandaloneDeriving ,我可以想到的任何具体类型(除了临时类型(如reified类型)之外)都可以是实例。

EDIT Code snippet of what I'm talking about: 我在说什么的EDIT代码片段:

f = reify (42::Int) (\ (_::Proxy q) ->
  let x = Zq 15 :: Zq q -- x = 15 mod 42
  in ... 

I would like Zq q , where q is the reified type, to be an instance of Typeable . 我想Zq q ,其中q具体化类型,为的一个实例Typeable The only way I know to do this is to make q Typeable as well. 我知道做到这一点的唯一方法是也使q Typeable Of course, if I reify 42 to q1 and then reify 43 to q2 , these should not be equal types. 当然,如果我将42验证为q1 ,然后将43验证为q2 ,则这些类型不应相等。 On the other hand, I realize I cannot expect two different type variables which reify the same value to have equal TypeReps (even though in practice they are identical). 另一方面,我意识到我不能指望两个不同的类型变量可以使相同的值具有相同的TypeReps(即使实际上它们是相同的)。

It depends. 这取决于。

*Main> :t (Zq 42, Zq 42)
(Zq 42, Zq 42) :: (Zq q, Zq q1)
*Main> :t [Zq 42, Zq 42]
[Zq 42, Zq 42] :: [Zq q]
*Main>

The elements of the pair have different types, while the elements of the list are natutally of the same type. 该对中的元素具有不同的类型,而列表中的元素在本质上却是同一类型。 No surprise here. 毫不奇怪。

If you want to keep it this way, it appears impossible to make Zq q typeable. 如果要保持这种方式,似乎不可能使Zq q键入。 The whole point of Typeable is that identical types get identical representations, while distinct types get distinct representations. Typeable的全部要点是,相同的类型获得相同的表示,而不同的类型获得不同的表示。 The whole point of phantom types is just the opposite, to generate a new unique type at every opportunity. 幻像类型的要点恰恰相反,它会在每一次机会中生成一个新的唯一类型。 The type class mechanism does not support this kind of abuse. 类型类机制不支持这种滥用。 We would need a new unique TypeRep whenever our value is bound to something with a new type variable, or something like that. 每当我们的值绑定到具有新类型变量的东西或类似的东西时,我们将需要一个新的唯一TypeRep

OTOH if you want Zq q and Zq q1 to be convertible to each other even if q and q1 are distinct, you just write an instance of Typeable (Zq a) by hand. OTOH如果您希望Zq qZq q1彼此可转换,即使qq1是不同的,则只需Typeable (Zq a)编写一个Typeable (Zq a)实例。 Make typeOf return a TypeRep that an automatically generated instance for 使typeOf返回一个TypeRep ,它是自动生成的实例

newtype Zq = Zq Int

would return. 会回来。

Disclaimer: I have no idea what I'm talking about, honest! 免责声明:老实说,我不知道我在说什么!

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

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