dmwit / universe

Classes for types where we know all the values
BSD 3-Clause "New" or "Revised" License
37 stars 17 forks source link

Should we offer a Generic helper? #40

Closed treeowl closed 5 years ago

treeowl commented 5 years ago

To a first approximation,

class GUniverse f where
  gUniverse :: [f a]

instance GUniverse U1 where
  gUniverse = [U1]

instance GUniverse V1 where
  gUniverse = []

instance (GUniverse f, GUniverse g) => GUniverse (f :*: g) where
  gUniverse = map (\(x,y) -> x :*: y) $ gUniverse +*+ gUniverse

instance (GUniverse f, GUniverse g) => GUniverse (f :+: g) where
  gUniverse = map L1 gUniverse +++ map R1 gUniverse

instance GUniverse f => GUniverse (M1 i c f) where
  gUniverse = map M1 gUniverse

instance Universe c => GUniverse (K1 i c) where
  gUniverse = map K1 universe

universeGeneric :: (Generic a, GUniverse (Rep a)) => [a]
universeGeneric = map to gUniverse
phadej commented 5 years ago

Yes. Don't making it too clever has a change to work with recursive types, at the cost of not-so fair value production.

treeowl commented 5 years ago

@phadej, I'm having trouble parsing that sentence, unfortunately.

phadej commented 5 years ago

If we don't try to make it too clever, the generic implementation might work for recursive types.

treeowl commented 5 years ago

Yes, indeed. It should be a bit more clever, though. In particular, it should do something more "sum-of-products"-like. I'd expect it to interleave fairly among the constructors, and diagonally across the fields. The rough draft above is gratuitously unfair.

phadej commented 5 years ago

Offered in #41

phadej commented 5 years ago

And release is cut: https://hackage.haskell.org/package/universe-base-1.1 + others packages