[英]making top-level patterns monomorphic
import Control.Lens
-- is there a way I can write top-level definitions
-- in an arbitrary order like
px = proto & _1 %~ asTypeOf '2'
py = proto & _2 %~ asTypeOf "2"
proto = (undefined, undefined)
-- but have types inferred like the following:
(qx,qy,qroto) = case (undefined, undefined) of
qxy -> (qxy & _1 %~ asTypeOf '2',
qxy & _2 %~ asTypeOf "2",
qxy)
I get the desired qroto :: (Char, [Char])
, but proto :: (t, t1)
is too general. 我得到了所需的qroto :: (Char, [Char])
,但是proto :: (t, t1)
太笼统了。 More importantly, this leads to px :: (Char, t)
instead of qx :: (Char, [Char])
. 更重要的是,这导致px :: (Char, t)
而不是qx :: (Char, [Char])
。
The larger problem is that I am trying to make it possible to reduce the type annotations needed with the third argument to Data.HList.Variant.mkVariant . 更大的问题是,我试图通过使用Data.HList.Variant.mkVariant的第三个参数来减少所需的类型注释。
Try this: 尝试这个:
(dx,rx) = ((), rroto & _1 %~ asTypeOf '2')
(dy,ry) = ((), rroto & _2 %~ asTypeOf "2")
rroto = const (undefined, undefined) (dx,dy)
This forces rx,ry,rroto
to be monomorphic: 这迫使rx,ry,rroto
是单态的:
> :t px
px :: (Char, t)
> :t qx
qx :: (Char, [Char])
> :t rx
rx :: (Char, [Char])
> :t rroto
rroto :: (Char, [Char])
To "trigger" the monomorphism restriction you have to use a set of definitions which is mutually dependant. 要“触发”单态性限制,您必须使用一组相互依赖的定义。 That is, every equation must depend on the others. 也就是说,每个方程式都必须依赖于其他方程式。 Above, we obtain this by adding dx,dy
to force the dependency. 上面,我们通过添加dx,dy
来强制依赖。
A simpler way to achieve the same effect: 实现相同效果的更简单方法:
rx = rroto & _1 %~ asTypeOf '2'
ry = rroto & _2 %~ asTypeOf "2"
rroto = const (undefined, undefined) (rx,ry)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.