Ra is dead
Ra is dead

Reputation: 569

Haskell type enforcement on typeclass parameters

I am trying to follow along a machine learning book and knowing a bit about the future content I am trying make my code generalisable.

Here is my code. I will eventually have other instances of DataSet, but this is all I have for now.

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)  

I am trying to enforce the type of the first parameter of SupervisedDataSet with the first parameter of augment as requested by the type checker in 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)

I understand why I am receiving the error, I just don't know how to fix it. Any ideas would be appreciated. Thanks

Thanks for your time.

Upvotes: 1

Views: 107

Answers (1)

ScootyPuff
ScootyPuff

Reputation: 1335

class DataSet a where
  augment :: x -> a -> a

could be written as

class DataSet a where
  augment :: forall x . x -> a -> a

Try instead

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)

Upvotes: 2

Related Questions