Proposal: Allow gunfold for Data.Map, Data.IntMap, etc.
José Pedro Magalhães
jpm at cs.uu.nl
Sat Sep 1 13:14:15 CEST 2012
On Wed, Aug 29, 2012 at 6:34 PM, Edward Kmett <ekmett at 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
> 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)
> Libraries mailing list
> Libraries at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Libraries