Edward Kmett的实验性roles package提供了各种解除强制的实用程序,我已经在这个问题的末尾粘贴了一些实用程序。包中的关键类是
class Representational (t :: k1 -> k2) where
-- | An argument is representational if you can lift a coercion of the argument into one of the whole
rep :: Coercion a b -> Coercion (t a) (t b)给定类型
newtype Fix p a = In {out :: p (Fix p a) a}我希望写一些像这样的东西
instance Representational p => Representational (Fix p)我相信下面的方法应该是可行的,除了一个缺失的部分。我还有点担心bar可能足够严格,以至于将所有东西都抛入无限循环。
-- With {-# LANGUAGE PolyKinds, ScopedTypeVariables, etc.)
import Data.Type.Coercion
import Data.Coerce
import Data.Roles
instance Representational p => Representational (Fix p) where
rep :: forall a b . Coercion a b -> Coercion (Fix p a) (Fix p b)
rep x = sym blah . quux . baz . blah
where
bar :: Coercion (p (Fix p a)) (p (Fix p b))
bar = rep (rep x)
baz :: Coercion (p (Fix p a) a) (p (Fix p b) a)
baz = eta bar
quux :: Coercion (p (Fix p b) a) (p (Fix p b) b)
quux = undefined -- ?????????
blah :: forall x . Coercion (Fix p x) (p (Fix p x) x)
blah = Coercion零碎的roles
eta :: forall (f :: x -> y) (g :: x -> y) (a :: x).
Coercion f g -> Coercion (f a) (g a)
instance Representational Coercion
instance Representational f => Representational (Compose f)发布于 2015-12-05 01:50:39
如上所述,这个问题无法解决,因为p是Representational (甚至是Phantom)这一事实并不意味着p (Fix p a)是代表性的。下面是一个反例:
data NF a b where
NF :: NF a ()
instance Representational NF where
rep _ = Coercion显然,NF a永远不是表述性的;我们不可能实现
rep :: Coercion x y -> Coercion (NF a x) (NF a y)(与a的选择无关),因为NF a x只有在x ~ ()时才有人居住。
因此,我们需要一个更精致的表象双函子概念来使这个想法变得合理。在任何情况下,unsafeCoerce几乎都是实现它所必需的,因为挖掘Coercion的无限链会花费无限的时间,并且Coercion不能懒惰地匹配。
一个(可能是有效的?)概念,我只是用suggested on GitHub
class Birepresentational t where
birep :: Coercion p q -> Coercion a b -> Coercion (t p a) (t q b)
instance Birepresentational f => Representational (Fix f) where
rep (x :: Coercion a b) = (birep :: forall p q x y . Coercion p q -> Coercion x y -> Coercion (f p x) (f q y))
(unsafeCoerce x :: Coercion (Fix f a) (Fix f b))
x `seq` unsafeCoerce x强制应用birep的目的是(希望)确保它实际终止,因此它的类型是可信的。
https://stackoverflow.com/questions/34080644
复制相似问题