+1


Pedro

On Wed, Aug 29, 2012 at 6:34 PM, Edward Kmett <ekmett@gmail.com> wrote:
I would like to propose improving the Data instances for a number of currently completely opaque data types in the containers package, by using virtual constructors.

The instance for Data.Map already uses fromList for gfoldl, it just stops there.

Extending it to be able to gunfold and mention the name of that constructor would enable generic traversal libraries like uniplate, etc. to work over the contents of the Map, rather than bailing out in fear or crashing at the sight of a mkNoRepType.

An example of the changes for Data.Map are highlighted below.

instance (Data k, Data a, Ord k) => Data (Map k a) where
  gfoldl f z m   = z fromList `f` toList m
  toConstr _     = fromListConstr
  gunfold k z c  = case constrIndex c of
    1 -> k (z fromList)
    _ -> error "gunfold"
  dataTypeOf _   = mapDataType
  dataCast2 f    = gcast2 f

fromListConstr :: Constr
fromListConstr = mkConstr mapDataType "fromList" [] Prefix

mapDataType :: DataType
mapDataType = mkDataType "Data.Map.Map" [fromListConstr]

I've used this approach for years on my own libraries to great effect.

Discussion Period: 3 Weeks

(I added a week for ICFP)

-Edward

_______________________________________________
Libraries mailing list
Libraries@haskell.org
http://www.haskell.org/mailman/listinfo/libraries