I'm trying to do some shallow printing on datatype names but I'm not sure how to do this with Generics-SOP.
For the cases where I have a List of my newtype, I can print out what I need fairly easily:
class GTypeName f where
gtypeName :: g f -> String
instance (HasDatatypeInfo a) => GTypeName (SOP I ('[ '[], '[a, [a]] ])) where
gtypeName _ = "(Array " ++ name ++ ")"
where
name = datatypeName $ datatypeInfo (Proxy @ a)
As you can see, I'm matching on the List kind and using the inner a's Datatype Info to get the name out. But I don't know how to handle the case where I want to get the actual top level constructor name itself.
In GHC Generics I did the following:
instance (GTypeName f) => GTypeName (D1 m f) where -- peer inside
gtypeName _ = gtypeName (Proxy @ f)
-- go into the Cons constructed type
instance {-# OVERLAPPING #-} (GTypeName f) => GTypeName (C1 ('MetaCons ":" g s) f) where
gtypeName _ = gtypeName (Proxy @ f)
-- return the constructor name here
instance (KnownSymbol n) => GTypeName (C1 ('MetaCons n g s) f) where
gtypeName _ = symbolVal (Proxy @ n)
-- take the left side, as this is the result of a type product from Cons
instance (GTypeName a) => GTypeName (a :*: b) where
gtypeName _ =
gtypeName (Proxy @ a)
-- match on array and take it out here
instance (GTypeName b) => GTypeName ((C1 ('MetaCons "[]" g s) f) :+: b) where
gtypeName _ = "(Array " ++ gtypeName (Proxy @ b) ++ ")"
This is ultimately used with a newtype and some data types like so:
newtype Status = Status Text
newtype OpenRequest = OpenRequest
{ path :: Path
}
data Route req res = Route
{ method :: Method
, url :: Url
}
open :: Route OpenRequest Success
open = Route {method = POST, url = Url "/api/open"}