类型类参数上的Haskell类型强制执行
问题描述:
我试图沿着一本机器学习书,并了解一些关于未来内容的内容,我试图让我的代码具有一般性。类型类参数上的Haskell类型强制执行
这是我的代码。我最终会拥有其他DataSet实例,但这是我现在所拥有的。
data SupervisedDataSet x y = SupervisedDataSet [([x], y)] deriving (Show)
class DataSet a where
augment :: x -> a -> a --Augment each input vector, making x the head.
instance DataSet (SupervisedDataSet x y) where
augment v (SupervisedDataSet ds) =·
let xsys = unzip ds in
SupervisedDataSet $ zip (map (v:) $ fst xsys) (snd xsys)
我试图与augment
的第一个参数执行的SupervisedDataSet
第一个参数的类型由GHC类型检查的要求。
Perceptron.hs:16:7:
Couldn't match type `x1' with `x'
`x1' is a rigid type variable bound by
the type signature for
agument :: x1 -> SupervisedDataSet x y -> SupervisedDataSet x y
at Perceptron.hs:14:3
`x' is a rigid type variable bound by
the instance declaration at Perceptron.hs:13:37
Expected type: SupervisedDataSet x1 y
Actual type: SupervisedDataSet x y
In the expression:
SupervisedDataSet $ zip (map (v :) $ fst xsys) (snd xsys)
In the expression:
let xsys = unzip ds
in SupervisedDataSet $ zip (map (v :) $ fst xsys) (snd xsys)
我明白为什么我收到错误,我只是不知道如何解决它。任何想法,将不胜感激。谢谢
感谢您的时间。
答
class DataSet a where
augment :: x -> a -> a
可以写成
class DataSet a where
augment :: forall x . x -> a -> a
尝试,而不是
data SupervisedDataSet x y = SupervisedDataSet [([x], y)] deriving (Show)
class DataSet f where
augment :: a -> f a b -> f a b
instance DataSet SupervisedDataSet where
augment v (SupervisedDataSet ds) =
let xsys = unzip ds in
SupervisedDataSet $ zip (map (v:) $ fst xsys) (snd xsys)
太好了!这工作,但我打算增加一个实例UnsupervisedDataSet只采用一个参数类型。在我修改我的问题之前,我将自己处理它。我有一些想法。 – 2013-04-05 04:23:21
我设法使用这个作为我需要的基础。感谢一束。 – 2013-04-05 04:39:49