[Haskell-cafe] [C][enums][newbie] What is natural Haskell representation of such enum?

David Barbour dmbarbour at gmail.com
Mon Jan 23 22:45:50 CET 2012


If you want a simple translation, use Word8 (from Data.Word) for the type
and use Data.Bits for operations on it just like in C++. This would offer
you storage efficiency (if stored as a strict field).

If you want idiomatic Haskell, constructor of the form:

  data ObjectType = Object | Item | Container | Unit | Player, etc.

Then simply put intelligence into the `isContainer` or `toWord8`, etc.
translations. This latter approach will be more extensible in the long run,
since you might find you want some parameterized object types.

Re: OO Classes and Haskell

If your classes are more like `interfaces`, you could use Typeclasses to
model them. Otherwise, look into OOHaskell. But I think your program
architecture will simply be different in idiomatic Haskell than in
idiomatic C++.

Regards,

Dave


On Mon, Jan 23, 2012 at 1:14 PM, Daniel Hlynskyi <abcz2.uprola at gmail.com>wrote:

> Thanks. This and previous email are answers to question I asked. But not
> the answer to question I mean.
> I'll describe the whole task, as Yves Parès suggested.
>
> I'm trying to convert C++ code to Haskell. I have such hierarchy: class
> Object, class Item : Object, class Container : Item. Another one example:
> class Unit : Object, class Player : Unit. Each constructor do things like
> this:
>
> Object::Object()
> {
>     objectType        = TYPEMASK_OBJECT;
>     // ... lots of code ...
> }
>
>
> Item::Item()
> {
>     objectType |= TYPEMASK_ITEM;
>     // ...
> }
>
> Container::Container(): Item()
> {
>     objectType |= (TYPEMASK_ITEM | TYPEMASK_CONTAINER);
>     // ...
> }
>
>
> What is objectType? This field is used when a networksend packet is
> created. In the packet it is 1 byte of flags, so it is in object hierarchy.
>
> So the question was: what type should objectType field have in Haskell? I
> think it must not mimic enum. What the structure have I to use? There is
> one more problem - there may be lots of objects, lots of, so memory
> efficiency is also suggested.
> And side question: what to do with classes? =) Maybe there is simple rule
> to convert OO hierarchy to FP.
>
> 23 січня 2012 р. 12:15 Malcolm Wallace <malcolm.wallace at me.com> написав:
>
>
>> > 2012/1/22 Данило Глинський <abcz2.uprola at gmail.com>
>> > What is natural Haskell representation of such enum?
>> >
>> > enum TypeMask
>> > {
>> >    UNIT,
>> >    GAMEOBJECT,
>> >
>> >    CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT
>> > };
>>
>> I don't think that definition makes any sense in C, because UNIT is 0, so
>> UNIT | GAMEOBJECT == GAMEOBJECT == 1
>>
>> Nevertheless, in Haskell something vaguely similar might be:
>>
>> data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT
>>
>> > // 1-byte flaged enum
>> > enum TypeMask
>> > {
>> >    // ...
>> >    UNIT        = 0x0004,
>> >    GAMEOBJECT  = 0x0008,
>> >    // ...
>> >
>> >    CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT
>> >    WORLDOBJECT = UNIT | PLAYER | GAMEOBJECT | DYNAMICOBJECT | CORPSE
>> >    // ... even more enum combos ...
>> > };
>>
>> import Data.Bits
>> data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT | WORLDOBJECT
>> instance Enum TypeMask where
>>    fromEnum UNIT = 0x4
>>    fromEnum GAMEOBJECT = 0x8
>>    fromEnum CREATURE_OR_GAMEOBJECT = fromEnum UNIT .|. fromEnum GAMEOBJECT
>>    fromEnum WORLDOBJECT = fromEnum UNIT .|. fromEnum PLAYER .|. fromEnum
>> GAMEOBJECT
>>                           .|. fromEnum DYNAMICOBJECT .|. fromEnum CORPSE
>>
>>    toEnum 0x4 = UNIT
>>    toEnum 0x8 = GAMEOBJECT
>>    toEnum _   = error "unspecified enumeration value of type TypeMask"
>>
>> isCreatureOrGameObject :: Int -> Bool
>> isCreatureOrGameObject x = (x .|. fromEnum CREATURE_OR_GAMEOBJECT) /= 0
>>
>> isWorldObject :: Int -> Bool
>> isWorldObject x = (x .|. fromEnum WORLDOBJECT) /= 0
>>
>> -- But fundamentally, this is not an idiomatic Haskell way of doing
>> things.
>> -- The other posts in this thread have shown more Haskell-ish
>> translations.
>>
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120123/cab2e3e4/attachment.htm>


More information about the Haskell-Cafe mailing list