我有一个用makeLenses创建镜头的MyRecord唱片。我希望在该记录中有一个字段,它本身是一个镜头,但也可以像其他字段一样使用镜头访问。
这是我的代码来实现这一点:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE RankNTypes #-}
module Check where
import Lens.Micro ( Lens' )
import Lens.Micro.TH ( makeLenses )
data MyRecord a =
MyRecord { _normalField :: Int
, _myField :: Lens' (String, String) a
}
makeLenses ''MyRecord如果我编写myField而不是_myField,代码可以很好地编译,但这样就不会为它生成镜头。
对于给定的代码,ghc输出
/home/fabian/src/code-editor/app/Check.hs:11:1: error:
• Illegal polymorphic type: Lens' (String, String) a2
GHC doesn't yet support impredicative polymorphism
• In the type signature:
myField :: forall a_a5c6 a_a8tU.
Lens.Micro.Type.Lens (MyRecord a_a5c6) (MyRecord a_a8tU) (Lens' (String,
String) a_a5c6) (Lens' (String,
String) a_a8tU)
|
11 | makeLenses ''MyRecord有人能简要解释一下这里发生了什么吗?有没有一个很好的解决方案?
发布于 2019-01-25 06:03:12
我认为通常的解决方案是像ALens这样的秩-1类型。我不知道microlens家族是否提供了类似的功能。
GHC manual解释了问题和解决方案。它的开头是:
一般来说,GHC只会实例化单态类型的多态函数(没有forall的类型)。..。foo的定义被拒绝了,因为必须用
b := (forall s. ST s a) -> a实例化id的类型,而这是不允许的。用多态类型实例化多态类型变量称为非预测性多态。
https://stackoverflow.com/questions/54355538
复制相似问题