簡體   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