简体   繁体   中英

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. More importantly, this leads to px :: (Char, t) instead of 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 .

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:

> :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.


A simpler way to achieve the same effect:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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