1
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的第三个参数所需的类型注释。

4

1 回答 1

2

试试这个:

(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)
于 2014-05-27T21:54:27.677 回答