使顶级模式变为单形

时间:2014-05-27 21:29:09

标签: haskell type-inference hlist

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

1 个答案:

答案 0 :(得分: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])

To"触发"单态限制你必须使用一组相互依赖的定义。也就是说,每个等式都必须依赖于其他等式。上面,我们通过添加dx,dy以强制依赖来获得此结果。


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

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