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).<div><br></div><div>If you want idiomatic Haskell, constructor of the form:</div>
<div><br></div><div>  data ObjectType = Object | Item | Container | Unit | Player, etc.</div><div><br></div><div>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.</div>
<div><br></div><div>Re: OO Classes and Haskell </div><div><br></div><div>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++. </div>
<div><br></div><div>Regards,</div><div><br></div><div>Dave</div><div><br></div><div><div><br></div><div><div class="gmail_quote">On Mon, Jan 23, 2012 at 1:14 PM, Daniel Hlynskyi <span dir="ltr">&lt;<a href="mailto:abcz2.uprola@gmail.com">abcz2.uprola@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks. This and previous email are answers to question I asked. But not the answer to question I mean.<div>I&#39;ll describe the whole task, as <span>Yves Parès</span> suggested.</div>
<div><br></div><div>I&#39;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:</div>

<div><br></div><div><pre style="padding-left:0px;padding-right:0px;line-height:1.4;padding-top:0px;font:normal normal normal 12px/normal &#39;Bitstream Vera Sans Mono&#39;,Courier,monospace;font-size:12px;margin-bottom:0px;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;margin-top:0px;padding-bottom:0px">
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Object</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">::</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Object</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">(</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">)</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">{</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="line-height:1.4">    </span><span style="line-height:1.4;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">objectType</span><span style="line-height:1.4">        </span><span style="line-height:1.4;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">=</span><span style="line-height:1.4"> </span><span style="line-height:1.4;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">TYPEMASK_OBJECT</span><span style="line-height:1.4;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">;</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em">    // ... lots of code ...</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em">

<span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">}</span></div></pre><div><br></div><pre style="padding-left:0px;padding-right:0px;line-height:1.4;padding-top:0px;font:normal normal normal 12px/normal &#39;Bitstream Vera Sans Mono&#39;,Courier,monospace;font-size:12px;margin-bottom:0px;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;margin-top:0px;padding-bottom:0px">
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Item</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">::</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Item</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">()</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">{</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em">    <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">objectType</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">|=</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">TYPEMASK_ITEM</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">;</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="line-height:1.4">    // ...</span></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em">}</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em">

<br></div></pre><pre style="padding-left:0px;padding-right:0px;line-height:1.4;padding-top:0px;font:normal normal normal 12px/normal &#39;Bitstream Vera Sans Mono&#39;,Courier,monospace;font-size:12px;margin-bottom:0px;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;margin-top:0px;padding-bottom:0px">
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Container</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">::</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Container</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">()</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">:</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">Item</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">()</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">{</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em">    <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">objectType</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">|=</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">(</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">TYPEMASK_ITEM</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-weight:bold">|</span> <span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">TYPEMASK_CONTAINER</span><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">);</span></div>

<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="line-height:1.4">    // ...</span></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:1em"><span style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">}</span></div>

</pre><br>What is <span style="padding-right:0px;padding-left:0px;padding-top:0px;margin-bottom:0px;padding-bottom:0px;line-height:16px;margin-right:0px;font-size:12px;white-space:pre-wrap;margin-left:0px;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;margin-top:0px">objectType</span><span style="padding-right:0px;padding-left:0px;padding-top:0px;margin-bottom:0px;padding-bottom:0px;line-height:16px;margin-right:0px;font-size:12px;white-space:pre-wrap;margin-left:0px;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;margin-top:0px">? 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.</span></div>

<div><font face="&#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace"><span style="font-size:12px;line-height:16px;white-space:pre-wrap"><br></span></font></div><div><font face="&#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace"><span style="font-size:12px;line-height:16px;white-space:pre-wrap">So the question was: what type should </span></font><span style="padding-right:0px;padding-left:0px;padding-top:0px;margin-bottom:0px;padding-bottom:0px;line-height:16px;margin-right:0px;font-size:12px;white-space:pre-wrap;margin-left:0px;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;margin-top:0px">objectType</span><span style="line-height:16px;font-size:12px;white-space:pre-wrap;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace"> 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.</span></div>

<div><span style="line-height:16px;font-size:12px;white-space:pre-wrap;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace">And side question: what to do with classes? =) Maybe there is simple rule to convert OO hierarchy to FP.</span></div>

<div><font face="&#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace"><span style="font-size:12px;line-height:16px;white-space:pre-wrap"><br></span></font><div class="gmail_quote">23 січня 2012 р. 12:15 Malcolm Wallace <span dir="ltr">&lt;<a href="mailto:malcolm.wallace@me.com" target="_blank">malcolm.wallace@me.com</a>&gt;</span> написав:<div>
<div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br>
&gt; 2012/1/22 Данило Глинський &lt;<a href="mailto:abcz2.uprola@gmail.com" target="_blank">abcz2.uprola@gmail.com</a>&gt;<br>
&gt; What is natural Haskell representation of such enum?<br>
&gt;<br>
&gt; enum TypeMask<br>
&gt; {<br>
&gt;    UNIT,<br>
&gt;    GAMEOBJECT,<br>
&gt;<br>
&gt;    CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT<br>
&gt; };<br>
<br>
</div>I don&#39;t think that definition makes any sense in C, because UNIT is 0, so UNIT | GAMEOBJECT == GAMEOBJECT == 1<br>
<br>
Nevertheless, in Haskell something vaguely similar might be:<br>
<br>
data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT<br>
<div><br>
&gt; // 1-byte flaged enum<br>
&gt; enum TypeMask<br>
&gt; {<br>
&gt;    // ...<br>
&gt;    UNIT        = 0x0004,<br>
&gt;    GAMEOBJECT  = 0x0008,<br>
&gt;    // ...<br>
&gt;<br>
&gt;    CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT<br>
&gt;    WORLDOBJECT = UNIT | PLAYER | GAMEOBJECT | DYNAMICOBJECT | CORPSE<br>
&gt;    // ... even more enum combos ...<br>
&gt; };<br>
<br>
</div>import Data.Bits<br>
data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT | WORLDOBJECT<br>
instance Enum TypeMask where<br>
    fromEnum UNIT = 0x4<br>
    fromEnum GAMEOBJECT = 0x8<br>
    fromEnum CREATURE_OR_GAMEOBJECT = fromEnum UNIT .|. fromEnum GAMEOBJECT<br>
    fromEnum WORLDOBJECT = fromEnum UNIT .|. fromEnum PLAYER .|. fromEnum GAMEOBJECT<br>
                           .|. fromEnum DYNAMICOBJECT .|. fromEnum CORPSE<br>
<br>
    toEnum 0x4 = UNIT<br>
    toEnum 0x8 = GAMEOBJECT<br>
    toEnum _   = error &quot;unspecified enumeration value of type TypeMask&quot;<br>
<br>
isCreatureOrGameObject :: Int -&gt; Bool<br>
isCreatureOrGameObject x = (x .|. fromEnum CREATURE_OR_GAMEOBJECT) /= 0<br>
<br>
isWorldObject :: Int -&gt; Bool<br>
isWorldObject x = (x .|. fromEnum WORLDOBJECT) /= 0<br>
<br>
-- But fundamentally, this is not an idiomatic Haskell way of doing things.<br>
-- The other posts in this thread have shown more Haskell-ish translations.<br>
<br>
<br>
</blockquote></div></div></div><br></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div></div>