'How can I write test cases for instances in Haskell

I have an instance of Num for a type I created called Vec:

instance Num Vec where
  (+) (Vec x) (Vec y) = Vec (zipWith (+) x y)

And I am trying to write a test case for it like I normally do. Normally, I test functions and not instances. I usually do it this way this way:

spec :: Spec
spec = do
  describe "(+)" $ do
    it "produces (Vec [2])" $
      Vec [1] + Vec [1] `shouldBe` Vec [2]

Since the code above doesn't produce any results, does this mean I'd have to create functions for (+) in order to test it? How can I do it for instances?



Solution 1:[1]

This is not an answer to the question but it is too long for a comment.

It seems like Vec uses pointwise lifting of Num.

This corresponds to using Applicative lifting (Ap) over ZipList:

{-# Language DerivingVia #-}

import Control.Applicative (ZipList(..))
import Data.Monoid         (Ap(..))

-- >> Vec [1] + Vec [1]
-- Vec [2]
newtype Vec = Vec [Int]
  deriving Num
  via Ap ZipList Int

You can also use deriving newtype IsList if you want to use list syntax for Vec.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iceland_jack