RFC: template-haskell & Data.Map vs. Prelude.lookup use

Richard Eisenberg eir at cis.upenn.edu
Fri Apr 25 13:33:45 UTC 2014


On Apr 25, 2014, at 4:16 AM, Herbert Valerio Riedel <hvriedel at gmail.com> wrote:

> Hello Richard,
> 
> On 2014-04-24 at 15:04:55 +0200, Richard Eisenberg wrote:
>> That map seems to store the set of variables during printing TH, for
>> the purposes of disambiguating identifiers with the same name but
>> different uniques. If blatting out a whole lot of program text, I
>> could imagine the Map getting somewhat sizeable.
> 
> When does printing TH actually occur? If it doesn't occur during regular
> compilation, do we ever need to pretty print large amounts of TH?

I guess it depends on who “we” are. In the process of routine TH hackery, I would say that printing is uncommon -- normally the TH is processed and then just spliced, without user interaction. But, I can conceive of a library that does some processing and then prints.

> 
>> But, it seems to only need the lookup and insert operations... 

Good observation.

> 
> actually, it uses a specific combination of lookup+insert, so that it
> would suffice to have the single operation in the style of Python's
> dict.setdefault(), i.e.
> 
>  findWithDefaultInsert :: Ord k => k -> v -> Map k v -> (v, Map k v)
>  findWithDefaultInsert k default m
>    | Just v <- Map.lookup k m = (v, m)
>    | otherwise = default `seq` (default, Map.insert k default m)
> 
>> is there a simpler data structure that has only these operations
>> efficiently?
> 



More information about the ghc-devs mailing list