[Haskell-cafe] EnumSet and EnumMap

Chris Kuklewicz haskell at list.mightyreason.com
Sun Feb 25 06:35:13 EST 2007


Yitzchak Gale wrote:
> Chris Kuklewicz wrote:
>> I have posted
>> this useful wrapper on the Haskell wiki at
>> http://haskell.org/haskellwiki/EnumSet_EnumMap
> 
> In my opinion, this is important enough that IntMap
> and IntSet should be deprecated from the standard
> libraries in favor of EnumMap and EnumSet.

I disagree.

> 
> Besides the obvious advantage of a huge increase
> in generality at no performance cost (I think), there
> is another reason.
> 

There is a performance cost.  Each use of Data.List.map in that code is a
performance cost.  And if toEnum/fromEnum is non-trivial there is a performance
cost.

For instance, I have abandoned (EnumMap Char) and made a specific CharMap module
that uses GHC.Base.unsafeChr to avoid the bounds checks.

If you want zero performance penalty you need to stop using Enum and create an
UnsafeEnum type class that does casting without bounds checking, and knows how
to cast things like "[Char]" to "[Int]" and back without touching each element.

> The difference in interface been the Int and non-Int
> versions of Set and Map forces you to make an
> early decision about which to use. That decision
> get pervasively hard-wired into your code.

This is true.  The solution for real application is to adopt a type class
interface to collections, for which a few examples exist.

> 
> Chris' idea fixes that problem.

EnumMap and EnumSet increase type safety but not always efficiency.

> 
> Regards,
> Yitz



More information about the Libraries mailing list