繁体   English   中英

使顶层模式变为单态

[英]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)

我得到了所需的qroto :: (Char, [Char]) ,但是proto :: (t, t1)太笼统了。 更重要的是,这导致px :: (Char, t)而不是qx :: (Char, [Char])

更大的问题是,我试图通过使用Data.HList.Variant.mkVariant的第三个参数来减少所需的类型注释。

尝试这个:

(dx,rx) = ((), rroto & _1 %~ asTypeOf '2')
(dy,ry) = ((), rroto & _2 %~ asTypeOf "2")
rroto = const (undefined, undefined) (dx,dy)

这迫使rx,ry,rroto是单态的:

> :t px
px :: (Char, t)
> :t qx
qx :: (Char, [Char])
> :t rx
rx :: (Char, [Char])
> :t rroto
rroto :: (Char, [Char])

要“触发”单态性限制,您必须使用一组相互依赖的定义。 也就是说,每个方程式都必须依赖于其他方程式。 上面,我们通过添加dx,dy来强制依赖。


实现相同效果的更简单方法:

rx = rroto & _1 %~ asTypeOf '2'
ry = rroto & _2 %~ asTypeOf "2"
rroto = const (undefined, undefined) (rx,ry)

暂无
暂无

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

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