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 style>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="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font:normal normal normal 12px/normal &#39;Bitstream Vera Sans Mono&#39;,Courier,monospace;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:1.4;background-color:rgb(255,255,255)">
<div class="line" id="LC46" 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 class="n" 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 class="o" 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 class="n" 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 class="p" 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 class="p" 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 class="line" id="LC47" 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 class="p" 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 class="line" id="LC48" 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 class="n" 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 class="o" 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 class="n" 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 class="p" 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 class="line" id="LC49" 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 class="line" id="LC49" 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 class="p" 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="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font:normal normal normal 12px/normal &#39;Bitstream Vera Sans Mono&#39;,Courier,monospace;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:1.4;background-color:rgb(255,255,255)">
<div class="line" id="LC232" 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 class="n" 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 class="o" 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 class="n" 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 class="p" 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 class="line" id="LC233" 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 class="p" 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 class="line" id="LC234" 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 class="n" 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 class="o" 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 class="n" 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 class="p" 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 class="line" id="LC235" 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 class="line" id="LC235" 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 class="line" id="LC235" 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="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font:normal normal normal 12px/normal &#39;Bitstream Vera Sans Mono&#39;,Courier,monospace;font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:1.4;background-color:rgb(255,255,255)">
<div class="line" id="LC25" 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 class="n" 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 class="o" 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 class="n" 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 class="p" 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 class="o" 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 class="n" 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 class="p" 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 class="line" id="LC26" 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 class="p" 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 class="line" id="LC27" 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 class="n" 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 class="o" 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 class="p" 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 class="n" 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 class="o" 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 class="n" 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 class="p" 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 class="line" id="LC28" 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 class="line" id="LC28" 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 class="p" 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 class="Apple-interchange-newline">What is <span class="n" style="font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:16px;white-space:pre;background-color:rgb(255,255,255);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 class="n" style="font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:16px;white-space:pre;background-color:rgb(255,255,255);margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left: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"><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">So the question was: what type should </span></font><span class="n" style="background-color:rgb(255,255,255);font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:16px;white-space:pre;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="background-color:rgb(255,255,255);font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:16px;white-space:pre"> 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="background-color:rgb(255,255,255);font-family:&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace;font-size:12px;line-height:16px;white-space:pre">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"><br></span></font><div class="gmail_quote">23 січня 2012 р. 12:15 Malcolm Wallace <span dir="ltr">&lt;<a href="mailto:malcolm.wallace@me.com">malcolm.wallace@me.com</a>&gt;</span> написав:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
&gt; 2012/1/22 Данило Глинський &lt;<a href="mailto:abcz2.uprola@gmail.com">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 class="im"><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><br></div>