Say there is a sum type that has a layer of nesting, like so:
data LandVehicle = Car | Truck
deriving (Eq, Enum, Bounded)
data MarineVehicle = Submarine | Tugboat
deriving (Eq, Enum, Bounded)
data Vehicle = Land LandVehicle | Marine MarineVehicle
Is there some generic version of enumerate that will produce a list containing all four possible values of Vehicle?
Land Car
Land Truck
Marine Submarine
Marine Tugboat
There’s a similar question on StackOverflow that makes reference to rolling one’s own implementation with Typeable/Data. But does there exist a package that provides this functionality out of the box?
This looks like what you’re searching for. It also has a universeGeneric function. Handrolling your own with generics-sop or even GHC.Generics is a good exercise and should be pretty easy to get through, though.
Very cool! I handrolled an implementation with TH, but I wish I’d known about these packages! Do any of these packages work with GADTs? e.g. I’d like something like:
data Type a where
Int :: Type Int
Bool :: Type Bool
Text :: Type Text
-- Some from some package
everything = [Some Int, Some Bool, Some Text]
generic-enumerations probably should in theory to the extent you can get a Generic instance for a GADT in the first place, which I understand is quite difficult. I’ve never tried to make a Generic instance for a GADT, but I found this article on the matter: How to derive Generic for (some) GADTs using QuantifiedConstraints - Ryan Scott
Finitary is lovely, to the extent I wish it was in base - "class Enum done right" is a very good description. Just worth noting the GPLv3 license can be a bit scary.