nick8325 / quickcheck

Automatic testing of Haskell programs.
Other
713 stars 119 forks source link

newtype deriving Function #271

Closed BebeSparkelSparkel closed 4 years ago

BebeSparkelSparkel commented 4 years ago

It doesn't seem possible to newtype derive an instance of Function. Is there a way to allow this?

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype GenericData = GenericData Word deriving (Show, Eq, Arbitrary, Function)
    • Couldn't match type ‘Word’ with ‘GenericData’
        arising from the coercion of the method ‘function’
          from type ‘forall b.
                     (Word -> b) -> Word Test.QuickCheck.Function.:-> b’
            to type ‘forall b.
                     (GenericData -> b) -> GenericData Test.QuickCheck.Function.:-> b’
    • When deriving the instance for (Function GenericData)
    |
121 | newtype GenericData = GenericData Word deriving (Show, Eq, Arbitrary, Function)
    |
phadej commented 4 years ago

No.

We'll need to coerce Word :-> c to GenericData :-> c, but that's not possible as Fun isn't coercible in the first argument:

 Test.QuickCheck> :i Fun
type role Fun nominal representational

Use either functionMap with explicit instance definition or

{-# LANGUAGE DeriveAnyClass, DerivingStrategies, DeriveGeneric, ... #-}
newtype GenericData = GenericData Word
  deriving stock (Show, Eq, Generic)
  deriving newtype (Arbitrary)
  deriving anyclass (Function)
BebeSparkelSparkel commented 4 years ago

Works great thanks