Why is Data.Generic.Rep.Show not implemented like this

33 views
Skip to first unread message

rvdalen

unread,
Nov 21, 2017, 11:04:41 AM11/21/17
to purescript
Hi everyone,

I have had a look at Generic programming in PureScript and tried to implement the GenericShow functionality
myself.  I came up with the following code:

=============================
import Prelude
import Data.Generic.Rep
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)


class GenericShow a where
  genericShow' :: a -> String


instance genericShowNoConstructors :: GenericShow NoConstructors where
  genericShow' a = genericShow' a


instance genericShowNoArgs :: GenericShow NoArguments where
  genericShow' _ = ""


instance genericShowSum :: (GenericShow a, GenericShow b) => GenericShow (Sum a b) where
  genericShow' (Inl l) = genericShow' l
  genericShow' (Inr r) = genericShow' r


instance genericShowProduct
  :: (GenericShow a, GenericShow b)
  => GenericShow (Product a b)
  where
    genericShow' (Product l r) = (genericShow' l) <> ", " <> (genericShow' r)


instance genericShowConstructor
  :: (GenericShow a, IsSymbol name)
  => GenericShow (Constructor name a)
  where
    genericShow' (Constructor a) =
      case genericShow' a of
        ""   -> ctor
        args -> "(" <> (ctor <> args) <> ")"
      where
        ctor :: String
        ctor = reflectSymbol (SProxy :: SProxy name)


instance genericShowArgument :: Show a => GenericShow (Argument a) where
  genericShow' (Argument a) = show a


instance genericShowRec :: (GenericShow a) => GenericShow (Rec a) where
  genericShow' (Rec a) = "{ " <> (genericShow' a) <> " }"


instance genericShowFieldsField :: (Show a, IsSymbol name) => GenericShow (Field name a) where
  genericShow' (Field a) = reflectSymbol (SProxy :: SProxy name) <> ": " <> show a


-- | A `Generic` implementation of the `show` member from the `Show` type class.
genericShow :: forall a rep. Generic a rep => GenericShow rep => a -> String
genericShow o = genericShow' (from o)


=============================

It seems to work ok with my few test cases.  But looking at the actual implementation in `Data.Generic.Rep.Show`,
there are type classes like: `GenericShowArgs` and `GenericShowFields `, which looks like it is not needed.
Am I missing some scenario in my implementation?  Or is the code equivalent?

Regards
--Rouan

Phil Freeman

unread,
Nov 21, 2017, 1:03:02 PM11/21/17
to purescript
Hi Rouan,

Maybe I just overcomplicated it. Could you please open a PR, and we can compare the two there?

Thanks!

--Phil.


--
You received this message because you are subscribed to the Google Groups "purescript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to purescript+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/purescript.
For more options, visit https://groups.google.com/d/optout.

rvdalen

unread,
Nov 21, 2017, 1:12:34 PM11/21/17
to purescript
Hi Phil,

No problem.  I will open a pull request. :)

Regards
--Rouan.
To unsubscribe from this group and stop receiving emails from it, send an email to purescript+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages