Efficient and type safe flag sets

We have http://www.haskell.org/haskellwiki/EnumSet_EnumMap Is there also an efficient implementation for bit sets that fit into a machine word? This would be useful for foreign function interfaces. E.g. where C defines #define SND_SEQ_PORT_CAP_READ (1<<0) /**< readable from this port */ #define SND_SEQ_PORT_CAP_WRITE (1<<1) /**< writable to this port */ #define SND_SEQ_PORT_CAP_SYNC_READ (1<<2) /**< allow read subscriptions */ #define SND_SEQ_PORT_CAP_SYNC_WRITE (1<<3) /**< allow write subscriptions */ In Haskell we could define module SndSeq data PortCap = Read | Write | SyncRead | SyncWrite bit32 :: Enum a => a -> FlagSet32 a flags32 :: Enum a => [a] -> FlagSet32 a In C we have to write foo (SND_SEQ_PORT_CAP_SYNC_READ | SND_SEQ_PORT_CAP_SYNC_READ) which is efficient but unsafe, because flags of the wrong sort or plain numbers could be passed accidentally. In Haskell we would write foo (flags32 [Read, SyncRead]) which is both efficient and safe, because foo :: FlagSet32 PortCap -> IO () (Given that constant expression evaluation works ...)

On Sunday 04 November 2007 04:15:45 pm Henning Thielemann wrote:
We have http://www.haskell.org/haskellwiki/EnumSet_EnumMap
Is there also an efficient implementation for bit sets that fit into a machine word? This would be useful for foreign function interfaces.
http://www.eecs.tufts.edu/~rdocki01/docs/edison/Data-Edison-Coll-EnumSet.htm...
participants (2)
-
Henning Thielemann
-
Robert Dockins