Proposal: Allow gunfold for Data.Map, Data.IntMap, etc.

Edward Kmett ekmett at gmail.com
Wed Aug 29 19:34:46 CEST 2012


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20120829/39e6131e/attachment.htm>


More information about the Libraries mailing list