``可固定的''用于Uni-Constructor类型和`()''
可以将这种类型强制为()
:
newtype Foo = Foo ()
f :: Foo -> ()
f = coerce
但是以下几点不能:
data Foo = Foo
f :: Foo -> ()
f = coerce -- Fails: Couldn't match representation of type ‘Foo’ with that of ‘()’
GHC说,
• Couldn't match representation of type ‘Foo’ with that of ‘()’
arising from a use of ‘coerce’
• In the expression: coerce
In an equation for ‘f’: f = coerce
|
69 | f = coerce
有没有办法使第二个 foo
可牢固地()
?毕竟它们都是同构。失败,自动创建这些同构的方法是什么?对于更大的上下文,请参见这个问题(其中构造函数的产品类型可固定在 ns
的类型级列表元素类型)。
@iceland_jack的建议
尝试杰克的建议在评论中通过通用 rep
:
class (GHC.Generic a, GHC.Generic b, Coercible (GHC.Rep a ()) (GHC.Rep b ())) => HasSameRep a b where
coerceViaRep :: I a -> I b
instance
( Generic a
, Generic b
, Coercible (GHC.Rep a ()) (GHC.Rep b ())
) =>
HasSameRep a b
where
coerceViaRep = I . GHC.to @_ @() . coerce . GHC.from @_ @() . unI
data T = T
proof :: T -> ()
proof = unI . coerceViaRep . I
这失败了,但是:
src/Ema/Route/Generic/Sub.hs:122:15-26: error:
• Couldn't match representation of type: GHC.U1 @Type ()
with that of: GHC.Rep T ()
arising from a use of ‘coerceViaRep’
• In the first argument of ‘(.)’, namely ‘coerceViaRep’
In the second argument of ‘(.)’, namely ‘coerceViaRep . I’
In the expression: unI . coerceViaRep . I
|
122 | proof = unI . coerceViaRep . I
看起来不像基本的通用表示实际上是相同的吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应用 @iceland_jack的建议(另请参见这个答案
)将
giso
定义为顶级功能,但是在我的情况下,我需要一个约束,以与gensixs-sop
'strans_ns
一起使用。Applying @Iceland_Jack's suggestion (also see this answer) in the comments here's what I came up with:
You could eliminate the typeclass and define
giso
as a top-level function, but in my case I need a constraint for use withgenerics-sop
'strans_NS
. https://github.com/EmaApps/ema/pull/81/commits/760fd793104ae4235055ad49c94ac014f014496e