[英]does this GADT actually have type role representational
This data type can have type role HCons' representational representational
, which allows using coerce
to add or remove newtypes applied to the elements, without needing to traverse the list. 此数据类型可以具有
type role HCons' representational representational
,它允许使用coerce
来添加或删除应用于元素的新类型,而无需遍历列表。
data HNil' = HNil'
data HCons' a b = HCons' a b
However the syntax for those lists is not as nice as those with the following GADT 但是,这些列表的语法不如具有以下GADT的语法好
data HList (l::[*]) where
HNil :: HList '[]
HCons :: e -> HList l -> HList (e ': l)
I have a class to convert between these two representations , such that Prime (HList [a,b]) ~ HCons' a (HCons' b HNil')
. 我有一个类在这两个表示之间进行转换 ,例如
Prime (HList [a,b]) ~ HCons' a (HCons' b HNil')
。 Does that class make 该课程是否成功?
coerceHList :: Coercible (Prime a) (Prime b) => HList a -> HList b
coerceHList = unsafeCoerce
safe? 安全?
I don't think the existence of a conversion on its own is enough. 我认为转换本身并不足够。 For example, the following also lets me convert between a GADT and a coercible pair of types, but it certainly wouldn't be safe to directly coerce the GADT:
例如,以下内容还允许我在GADT和一对强制类型之间进行转换,但直接强制GADT肯定不安全:
newtype Age = Age Int
data Foo a where
I :: Bool -> Int -> Foo Int
A :: Age -> Bool -> Foo Age
class ConvFoo a where
toFoo :: (Bool, a) -> Foo a
fromFoo :: Foo a -> (Bool, a)
instance ConvFoo Int where
toFoo (b, i) = I b i
fromFoo (I b i) = (b, i)
instance ConvFoo Age where
toFoo (b, a) = A a b
fromFoo (A a b) = (b, a)
I could also trivially define an UnFoo
type function similar to Prime
. 我也可以简单地定义一个类似于
Prime
的UnFoo
类型函数。
I think the key difference between the two examples is that in mine, Age
and Int
do have the same representation, whereas in yours '[]
and e':l
don't have the same representation. 我认为两个例子之间的关键区别在于,在我的中,
Age
和Int
确实具有相同的表示,而在你的'[]
和e':l
没有相同的表示。
So there's still a case for saying, as you suggest in the title, that l
has type role representational, because it's kind of obvious that HList l1
and HList l2
have the same representations if l1
and l2
have the same representations. 所以仍然有一种情况可以说,正如你在标题中所建议的那样,
l
具有类型角色代表性,因为如果l1
和l2
具有相同的表示,则HList l1
和HList l2
具有相同的表示是显而易见的。
However since in theory representations are implementation-dependent, I don't think you can ever consider this absolutely safe until GHC accepts it directly. 然而,由于在理论上表示是依赖于实现的,所以我认为在GHC直接接受它之前你不能认为这绝对安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.