purescript / purescript-prelude

The PureScript Prelude
BSD 3-Clause "New" or "Revised" License
161 stars 88 forks source link

[generics-rep] Newtypes for deriving via #241

Open kl0tl opened 4 years ago

kl0tl commented 4 years ago

With deriving via and some newtypes we could simulate defaults methods and alleviate a bit the implementation of typeclasses with generic defaults.

For instance implementing Show, Enum and Bounded:

module Example where

import Data.Enum (class Enum)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Bounded (genericBottom, genericTop)
import Data.Generic.Rep.Enum (genericSucc, genericPred)
import Data.Generic.Rep.Show (genericShow)

data ABC = A | B | C

derive instance eqABC :: Eq ABC
derive instance ordABC :: Ord ABC
derive instance genericABC :: Generic ABC _

instance showABC :: Show ABC where
  show = genericShow

instance boundedABC :: Bounded ABC where
  top = genericTop
  bottom = genericBottom

instance enumABC :: Enum ABC where
  succ = genericSucc
  pred = genericPred

Could be a bit more compact:

 module Example where

 import Data.Enum (class Enum)
 import Data.Generic.Rep (class Generic)
-import Data.Generic.Rep.Bounded (genericBottom, genericTop)
+import Data.Generic.Rep.Bounded (GenericBounded)
-import Data.Generic.Rep.Enum (genericSucc, genericPred)
+import Data.Generic.Rep.Enum (GenericEnum)
-import Data.Generic.Rep.Show (genericShow)
+import Data.Generic.Rep.Show (GenericShow)

 data ABC = A | B | C

derive instance eqABC :: Eq ABC
derive instance ordABC :: Ord ABC
derive instance genericABC :: Generic ABC _

+derive via (GenericShow ABC) instance showABC :: Show ABC
-instance showABC :: Show ABC where
-  show = genericShow

+derive via (GenericBounded ABC) instance boundedABC :: Bounded ABC
-instance boundedABC :: Bounded ABC where
-  top = genericTop
-  bottom = genericBottom

+derive via (GenericEnum ABC) instance enumABC :: Enum ABC
-instance enumABC :: Enum ABC where
-  succ = genericSucc
-  pred = genericPred

Writing those newtypes is straightforward and I gladly volonteer. The only annoyance is how to name them: GenericBounded isn’t taken but GenericShow and GenericEnum refer to the typeclasses implemented by the generic representation.

Would it be acceptable to rename the typeclasses to GenericRep* and keep Generic* for the newtypes? Or has someone a better naming scheme to propose?

hdgarrood commented 4 years ago

I'd quite like to retain the current class names so that we can avoid a breaking change. How about a Using prefix for newtypes? We could potentially even only provide a single newtype here and hang all of the instances onto it:

newtype UsingGeneric a = UsingGeneric a
instance showGeneric :: Generic a => Show (UsingGeneric a) where [...]
instance boundedGeneric :: Generic a => Bounded (UsingGeneric a) where [...]
instance enumGeneric :: Generic a => Enum (UsingGeneric a) where [...]

and then:

data ABC = ABC
derive instance genericABC :: Generic ABC _
derive via (UsingGeneric ABC) instance showABC :: Show ABC
derive via (UsingGeneric ABC) instance boundedABC :: Bounded ABC
derive via (UsingGeneric ABC) instance enumABC :: Enum ABC