[英]How to fully evaluate a recursive data type using Control.DeepSeq in Haskell?
I am trying to benchmark (with Criterion ) a function, which uses a recursive data type. 我正在尝试使用一种递归数据类型的函数(使用Criterion )进行基准测试。 I found a similar question with an answer that I haven't been able to apply for my case.
我找到了一个类似的问题 ,答案是我无法申请我的案子。 For non-recursive data types, the following works:
对于非递归数据类型,以下工作原理:
data ExampleDataType1 a =
ValueConst1 String String String String
| ValueConst2 String String
| ValueConst3 a
| ValueConst4 String
deriving (Show, Eq, Ord)
instance DeepSeq.NFData a => DeepSeq.NFData (ExampleDataType1 a) where
rnf (ValueConst1 c1 c2 c3 c4) = DeepSeq.rnf c1 `seq` DeepSeq.rnf c2 `seq` DeepSeq.rnf c3 `seq` DeepSeq.rnf c4
rnf (ValueConst2 c1 c2) = DeepSeq.rnf c1 `seq` DeepSeq.rnf c2
rnf (ValueConst3 c1) = DeepSeq.rnf c1
rnf (ValueConst4 c2) = DeepSeq.rnf c2
However, doing the following: 但是,执行以下操作:
infixl 6 :+: -- Addition
infixl 7 :*: -- Multiplication
data ExampleDataType2 a =
ValueConst5 (ExampleDataType2 a)
| a :*: String
| (ExampleDataType2 a) :+: (ExampleDataType2 a)
| ValueConst6 String a
| ValueConst7 String a
deriving (Show, Eq, Ord)
type MapExample a b = Map.Map String (Either (ExampleDataType1 a) (ExampleDataType2 b))
data ExampleDataType3 a b = ExampleDataType3 {
start :: String,
mapList :: [MapExample a b]
} deriving Show
instance DeepSeq.NFData a => DeepSeq.NFData (ExampleDataType1 a) where
rnf (ValueConst1 c1 c2 c3 c4) = DeepSeq.rnf c1 `seq` DeepSeq.rnf c2 `seq` DeepSeq.rnf c3 `seq` DeepSeq.rnf c4
rnf (ValueConst2 c1 c2) = DeepSeq.rnf c1 `seq` DeepSeq.rnf c2
rnf (ValueConst3 c1) = DeepSeq.rnf c1
rnf (ValueConst4 c2) = DeepSeq.rnf c2
instance DeepSeq.NFData b => DeepSeq.NFData (ExampleDataType2 b) where
rnf (ValueConst5 c1) = DeepSeq.rnf c1
rnf (val1 :+: val2) = DeepSeq.rnf val1 `seq` DeepSeq.rnf val2
rnf (val :*: str) = DeepSeq.rnf val `seq` DeepSeq.rnf str
rnf (ValueConst6 str val) = DeepSeq.rnf str `seq` DeepSeq.rnf val
rnf (ValueConst7 str val) = DeepSeq.rnf str `seq` DeepSeq.rnf val
instance (DeepSeq.NFData a, DeepSeq.NFData b) => DeepSeq.NFData (ExampleDataType3 a b) where
rnf (ExampleDataType3 s lst) = DeepSeq.rnf s `seq` DeepSeq.rnf lst
results in an error, when calling the nf
function of Criterion.Main
on the function I wish to benchmark, which has the signature testFunction :: (Show a1, Integral a1, Num a2, Eq a2) => [[a1]] -> ExampleDataType3 a2 a1
: 当我想要基准的函数调用
Criterion.Main
的nf
函数时,会产生错误,该函数具有签名testFunction :: (Show a1, Integral a1, Num a2, Eq a2) => [[a1]] -> ExampleDataType3 a2 a1
:
• Ambiguous type variable ‘a20’ arising from a use of ‘nf’
prevents the constraint ‘(Control.DeepSeq.NFData
a20)’ from being solved.
Probable fix: use a type annotation to specify what ‘a20’ should be.
These potential instances exist:
instance [safe] (Control.DeepSeq.NFData a,
Control.DeepSeq.NFData b) =>
Control.DeepSeq.NFData (Either a b)
-- Defined in ‘Control.DeepSeq’
instance (Control.DeepSeq.NFData k, Control.DeepSeq.NFData a) =>
Control.DeepSeq.NFData (Map.Map k a)
-- Defined in ‘Data.Map.Internal’
instance Control.DeepSeq.NFData a =>
Control.DeepSeq.NFData (Set.Set a)
-- Defined in ‘Data.Set.Internal’
...plus 20 others
...plus 150 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
I would appreciate every answer on how one should fully evaluate a recursive data type. 我很感激每个人应该如何全面评估递归数据类型。
Edit 1: 编辑1:
The benchmark call that causes the error: 导致错误的基准调用:
main = defaultMain [
bgroup "TestCases" [ bench "Case 1" $ nf testFunction [[1,0,1,1],[0,0,0,1],[1,1,1,0],[0,1,0,1]]
]]
The function testFunction
does its job as desired except I fail to fully evaluate the recursive data type so that nf
function of Criterion can accept my function as an input. 函数
testFunction
按需要完成它的工作,除了我无法完全评估递归数据类型,因此Criterion的nf
函数可以接受我的函数作为输入。 Therefore, I would like to avoid changing the data type. 因此,我想避免更改数据类型。
You can add a type signature to testFunction
where you pass it to nf
. 您可以将类型签名添加到
testFunction
,并将其传递给nf
。 Something like: 就像是:
nf (testFunction :: [[Int]] -> ExampleDataType3 Double Int) [[1,0,1,1],[0,0,0,1],[1,1,1,0],[0,1,0,1]]
I picked the type Double; 我选择了Double类型; you may pick some other type.
你可以选择其他类型。 Since there are several options, GHC gives the
Ambiguous type variable
error rather than arbitrarily picking one. 由于有几个选项,GHC给出
Ambiguous type variable
错误而不是任意选择一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.