无法推断(显示 a10)由使用“def”引起
Could not deduce (Show a10) arising from a use of ‘def’
我已经被这个错误困扰了很长一段时间。我不确定我是不是很愚蠢,但我看不出它来自哪里...
数据类型Settings
定义如下:
data Settings a = Settings {
_nodeDrawFunction :: a -> Diagram B
, _dynamicHead :: Measure Double
, _dynamicThick :: Measure Double
, _directed :: Directed
, _horizontalOrientation :: Maybe Bool
, _layerSpacing :: Maybe Double
, _nodeSpacing :: Maybe Double
, _graphPadding :: Maybe Double
, _colF :: Maybe (Int -> Colour Double)
, _bgOp :: Maybe Double
, _initPos :: Maybe Int
}
和makeLenses ''Settings
用于为每条记录创建镜头。 _drawNodeFunction
的默认值为 drawDefaultNode
.
当我创建一个使用镜头设置器覆盖其中一些值的函数时,它工作正常,例如:
test1 :: (Show a) => Graph a -> Settings a
test1 g = def & bgOp .~ Just 1
按预期工作。 Graph
的类型来自库 algebraic-graphs.
如果我尝试设置 nodeDrawFunction
,例如:
test2 :: (Show a) => Graph a -> Settings a
test2 g = def & nodeDrawFunction .~ drawDefaultNode
& bgOp .~ Just 1
产生以下错误:
Could not deduce (Show a10) arising from a use of ‘def’
from the context: Show a
bound by the type signature for:
test2 :: forall a. Show a => Graph a -> Settings a
at Visualise/Hierarchical.hs:78:1-42
The type variable ‘a10’ is ambiguous
These potential instances exist:
instance Show n => Show (Duration n) -- Defined in ‘Data.Active’
instance Show n => Show (Era n) -- Defined in ‘Data.Active’
instance Show n => Show (Time n) -- Defined in ‘Data.Active’
...plus 74 others
...plus 264 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘(&)’, namely ‘def’
In the first argument of ‘(&)’, namely
‘def & nodeDrawFunction .~ drawDefaultNode’
In the expression:
def & nodeDrawFunction .~ drawDefaultNode & bgOp .~ Just 1
即使我尝试将 nodeDrawFunction
设置为默认值(以及任何其他有效函数)。
非常感谢任何帮助,谢谢!
您可能希望 def
和 def & nodeDrawFunction .~ drawDefaultNode
具有相同的类型。 nodeDrawFunction
足够灵活,允许它们有不同的类型,Settings a
和 Settings a10
。 GHC 从这个更一般的假设开始,然后无法为 a10
选择特定类型,因为它不受函数中任何内容的约束。
一种选择是用不太通用的类型定义 nodeDrawFunction
。
另一种选择是在 def
上添加类型签名以指导 GHC。我认为如果您添加 -XScopedTypeVariables
:
这会起作用
(def :: Settings a) & nodeDrawFunction .~ drawDefaultNode
没有 ScopedTypeVariables 两个类型变量 a
被解释为不同的,并且在类型检查之前被赋予不同的唯一名称。如果 Settings
中有多个涉及 a
的字段,makeLenses
将导出无法更改 a
的镜头,因为没有单字段镜头可以这样做。
我已经被这个错误困扰了很长一段时间。我不确定我是不是很愚蠢,但我看不出它来自哪里...
数据类型Settings
定义如下:
data Settings a = Settings {
_nodeDrawFunction :: a -> Diagram B
, _dynamicHead :: Measure Double
, _dynamicThick :: Measure Double
, _directed :: Directed
, _horizontalOrientation :: Maybe Bool
, _layerSpacing :: Maybe Double
, _nodeSpacing :: Maybe Double
, _graphPadding :: Maybe Double
, _colF :: Maybe (Int -> Colour Double)
, _bgOp :: Maybe Double
, _initPos :: Maybe Int
}
和makeLenses ''Settings
用于为每条记录创建镜头。 _drawNodeFunction
的默认值为 drawDefaultNode
.
当我创建一个使用镜头设置器覆盖其中一些值的函数时,它工作正常,例如:
test1 :: (Show a) => Graph a -> Settings a
test1 g = def & bgOp .~ Just 1
按预期工作。 Graph
的类型来自库 algebraic-graphs.
如果我尝试设置 nodeDrawFunction
,例如:
test2 :: (Show a) => Graph a -> Settings a
test2 g = def & nodeDrawFunction .~ drawDefaultNode
& bgOp .~ Just 1
产生以下错误:
Could not deduce (Show a10) arising from a use of ‘def’
from the context: Show a
bound by the type signature for:
test2 :: forall a. Show a => Graph a -> Settings a
at Visualise/Hierarchical.hs:78:1-42
The type variable ‘a10’ is ambiguous
These potential instances exist:
instance Show n => Show (Duration n) -- Defined in ‘Data.Active’
instance Show n => Show (Era n) -- Defined in ‘Data.Active’
instance Show n => Show (Time n) -- Defined in ‘Data.Active’
...plus 74 others
...plus 264 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘(&)’, namely ‘def’
In the first argument of ‘(&)’, namely
‘def & nodeDrawFunction .~ drawDefaultNode’
In the expression:
def & nodeDrawFunction .~ drawDefaultNode & bgOp .~ Just 1
即使我尝试将 nodeDrawFunction
设置为默认值(以及任何其他有效函数)。
非常感谢任何帮助,谢谢!
您可能希望 def
和 def & nodeDrawFunction .~ drawDefaultNode
具有相同的类型。 nodeDrawFunction
足够灵活,允许它们有不同的类型,Settings a
和 Settings a10
。 GHC 从这个更一般的假设开始,然后无法为 a10
选择特定类型,因为它不受函数中任何内容的约束。
一种选择是用不太通用的类型定义 nodeDrawFunction
。
另一种选择是在 def
上添加类型签名以指导 GHC。我认为如果您添加 -XScopedTypeVariables
:
(def :: Settings a) & nodeDrawFunction .~ drawDefaultNode
没有 ScopedTypeVariables 两个类型变量 a
被解释为不同的,并且在类型检查之前被赋予不同的唯一名称。如果 Settings
中有多个涉及 a
的字段,makeLenses
将导出无法更改 a
的镜头,因为没有单字段镜头可以这样做。