generics-sop icon indicating copy to clipboard operation
generics-sop copied to clipboard

Generic generic

Open mikeplus64 opened this issue 6 years ago • 6 comments

The reason this is useful is to be able to combine Reps of different types, and pretend that they are actually a Generic type on their own.

mikeplus64 avatar Jul 04 '18 16:07 mikeplus64

nice!

phadej avatar Jul 04 '18 16:07 phadej

I'd rather do more general instance:

instance (All SListI a, f ~ I) => Generic (SOP f a) where
   ....

I don't think that Generic (SOP f a) would make any sense for anything else than f ~ I.

phadej avatar Jul 04 '18 16:07 phadej

Done :-)

mikeplus64 avatar Jul 05 '18 00:07 mikeplus64

I've been thinking about this instance in the past, but I've always been hesitating because it isn't actually giving you a generic representation of a SOP. Could you explain your use case in more detail?

kosmikus avatar Jul 05 '18 05:07 kosmikus

My exact use-case: squeal-postgresql allows users to run insertions of arbitrary types so long as their Rep matches the expected Rep for the particular table. However, you don't always actually want your Haskell types to have, e.g., internal IDs in them. That is, if you have a type:

data Location = Location { locType :: Text, locCentre :: Coord, ... }

the corresponding table in your schema may have a serial primary key. With this instance, we can keep the idiomatic Location type (i.e., don't add a strange "ID" field to it), but still be able to use it for queries/insertions; instead of having traversePrepared_ (insertRow_ #location (Set (param @1) As #locId :* ...)) [UglyLocation { locId = XXX, ..}], we can have traversePrepared_ (insertRow_ #location (Set (param @1) As #locId :* ...)) [XXX :* from Location {..}].

mikeplus64 avatar Jul 05 '18 05:07 mikeplus64

I see. I think I'd feel slightly more comfortable to define a different newtype-wrapper around SOP and provide the instance for that. This should cover your use case. I'm not sure though. I could certainly be convinced to just accept your PR as it is now. My primary worry that it might become easier to confuse original types with their representations if we add this kind of instance.

kosmikus avatar Jul 10 '18 08:07 kosmikus