0

This is a follow up question to this question. Somebody suggested there that it would make more sense to make the type network an instance of vectorSpace.

newtype Network = Network [( Matrix Double, Vector Double)]

instance AdditiveGroup Network where
  (Network n1) ^+^ (Network n2) = Network $ zipWith (\(m,v) (n,w) -> (m+n,v+w)) n1 n2
  (Network n1) ^-^ (Network n2) = Network $ zipWith (\(m,v) (n,w) -> (m-n,v-w)) n1 n2

instance VectorSpace Network where
  type Scalar Network = Double
  lambda *^ (Network n) = Network $ map (\ (m,v) -> (lambda*m,lambda*v)) n

But when I try to compile I get an error saying that i wrote an illegal instance for 'Scalar'. Could somebody explain me what I did wrong?

Community
  • 1
  • 1
AndyM
  • 51
  • 5
  • You should include the error message in the future. It makes it a lot easier and faster to figure out what's going on. – David Young Jul 22 '16 at 20:12

1 Answers1

0

The error comes from the fact that being able to declare type in a class and instance is not standard Haskell, it is part of the type families extension. Getting rid of that error isn't tough, you just have to add the following language pragma at the top of your file:

{-# LANGUAGE TypeFamilies #-}

However, then you might have a problem with the fact that you haven't defined zeroV or negateV in your AdditiveGroup instance. I'm not sure how you will define those... You may even get away without defining them (you'll get warnings though, and something may crash at runtime).

Alec
  • 31,829
  • 7
  • 67
  • 114