如何统一c和GHC.Exts.Itemt c?
How to unify c and GHC.Exts.Item t c?
我刚刚更新了我的 dynamic time warping library 以使用 ghc-7.10
编译并偶然发现了一个奇怪的问题:
在ghc-7.8
中编译好的代码:
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE TypeFamilies #-}
dtwNaive :: (Ord c, Fractional c, DataSet a, DataSet b)
=> (Item a -> Item b -> c) -> a -> b -> c
dtwNaive δ as bs = go (len as - 1) (len bs - 1)
where go 0 0 = 0
go _ 0 = 1/0
go 0 _ = 1/0
go x y = δ (ix as x) (ix bs y) + minimum [ go (x-1) y
, go x (y-1)
, go (x-1) (y-1)
]
现在产生以下错误:
src/Data/DTW.hs:112:52:
Couldn't match type ‘c’ with ‘GHC.Exts.Item (t1 c)’
‘c’ is a rigid type variable bound by
the type signature for
dtwNaive :: (Ord c, Fractional c, DataSet a, DataSet b) =>
(Item a -> Item b -> c) -> a -> b -> c
at src/Data/DTW.hs:106:13
Expected type: Int -> [c] -> t1 c
Actual type: Int -> [GHC.Exts.Item (t1 c)] -> t1 c
Relevant bindings include
go :: Int -> Int -> c (bound at src/Data/DTW.hs:109:11)
δ :: Item a -> Item b -> c (bound at src/Data/DTW.hs:108:10)
dtwNaive :: (Item a -> Item b -> c) -> a -> b -> c
(bound at src/Data/DTW.hs:108:1)
In the first argument of ‘minimum’, namely
‘[go (x - 1) y, go x (y - 1), go (x - 1) (y - 1)]’
In the second argument of ‘(+)’, namely
‘minimum [go (x - 1) y, go x (y - 1), go (x - 1) (y - 1)]’
In the expression:
δ (ix as x) (ix bs y)
+ minimum [go (x - 1) y, go x (y - 1), go (x - 1) (y - 1)]
老实说,我很困惑,因为我认为 [c]
与 t ~ []
时的 [GHC.Exts.Item (t c)]
是一回事。我错过了什么?
minimum
的类型跟以前不一样了。旧类型是 Ord a => [a] -> a
。新类型是 (Ord a, Foldable t) => t a -> a
。
作为 minimum
的参数的列表文字有一个 IsList
实例不再明显。您可以通过添加本地注释来解决此问题:
{-# LANGUAGE ScopedTypeVariables #-}
dtwNaive ::
forall a b c.
(Ord c, Fractional c, DataSet a, DataSet b)
=> (Item a -> Item b -> c) -> a -> b -> c
dtwNaive = ...
minimum ([go (x-1) y, go x (y-1), go (x-1) (y-1)] :: [c])
我刚刚更新了我的 dynamic time warping library 以使用 ghc-7.10
编译并偶然发现了一个奇怪的问题:
在ghc-7.8
中编译好的代码:
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE TypeFamilies #-}
dtwNaive :: (Ord c, Fractional c, DataSet a, DataSet b)
=> (Item a -> Item b -> c) -> a -> b -> c
dtwNaive δ as bs = go (len as - 1) (len bs - 1)
where go 0 0 = 0
go _ 0 = 1/0
go 0 _ = 1/0
go x y = δ (ix as x) (ix bs y) + minimum [ go (x-1) y
, go x (y-1)
, go (x-1) (y-1)
]
现在产生以下错误:
src/Data/DTW.hs:112:52:
Couldn't match type ‘c’ with ‘GHC.Exts.Item (t1 c)’
‘c’ is a rigid type variable bound by
the type signature for
dtwNaive :: (Ord c, Fractional c, DataSet a, DataSet b) =>
(Item a -> Item b -> c) -> a -> b -> c
at src/Data/DTW.hs:106:13
Expected type: Int -> [c] -> t1 c
Actual type: Int -> [GHC.Exts.Item (t1 c)] -> t1 c
Relevant bindings include
go :: Int -> Int -> c (bound at src/Data/DTW.hs:109:11)
δ :: Item a -> Item b -> c (bound at src/Data/DTW.hs:108:10)
dtwNaive :: (Item a -> Item b -> c) -> a -> b -> c
(bound at src/Data/DTW.hs:108:1)
In the first argument of ‘minimum’, namely
‘[go (x - 1) y, go x (y - 1), go (x - 1) (y - 1)]’
In the second argument of ‘(+)’, namely
‘minimum [go (x - 1) y, go x (y - 1), go (x - 1) (y - 1)]’
In the expression:
δ (ix as x) (ix bs y)
+ minimum [go (x - 1) y, go x (y - 1), go (x - 1) (y - 1)]
老实说,我很困惑,因为我认为 [c]
与 t ~ []
时的 [GHC.Exts.Item (t c)]
是一回事。我错过了什么?
minimum
的类型跟以前不一样了。旧类型是 Ord a => [a] -> a
。新类型是 (Ord a, Foldable t) => t a -> a
。
作为 minimum
的参数的列表文字有一个 IsList
实例不再明显。您可以通过添加本地注释来解决此问题:
{-# LANGUAGE ScopedTypeVariables #-}
dtwNaive ::
forall a b c.
(Ord c, Fractional c, DataSet a, DataSet b)
=> (Item a -> Item b -> c) -> a -> b -> c
dtwNaive = ...
minimum ([go (x-1) y, go x (y-1), go (x-1) (y-1)] :: [c])