<div dir="ltr"><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">I&#39;m also confused about how to do the equivalence of inheritance in Haskell.  Here is a complete example below.  It doesn&#39;t compile. The error message is </font></font></font><div>

<font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif"><br></font></font></font></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font size="2"><font face="trebuchet ms,sans-serif"><div>

<font class="Apple-style-span" color="#CC0000">Class `Person&#39; used as a type</font></div></font></font></div></blockquote><div><font size="2"><font face="trebuchet ms,sans-serif"><div style="color: rgb(0, 51, 51); ">
<br>
</div></font></font></div><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">If I write &quot;(Person p) =&gt;&quot; instead, I get other diagnostics. </font></font></font></div><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif"><br>

</font></font></font></div><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">I would very much appreciate seeing how this should be done in Haskell.</font></font></font></div><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif"><br>

</font></font></font></div><div><span class="Apple-style-span" style="font-family: &#39;trebuchet ms&#39;, sans-serif; color: rgb(0, 51, 51); ">---------- Example (multiple files) ------------</span></div><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">--- Group.hs ---</font></font></font></div>

<div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif"><div>module Group where</div><div><br></div><div>import Person</div><div><br></div><div>data Group = Group { members :: [Person] }</div><div>
<br>
</div><div>instance Show Group where</div><div>  show group = unlines $ map show $ members group</div><div><br></div><div>--- Person.hs ---</div><div><br></div><div><div>module Person </div><div>   (</div><div>     Condition(Bad, OK, Good)</div>

<div>   , Person</div><div>   )</div><div>where</div><div><br></div><div>class Person where</div><div>  age :: Person -&gt; Int</div><div>  </div><div>  name :: Person -&gt; String</div><div>  </div><div>  getGenderSpecificCondition :: Person -&gt; Condition</div>

<div><br></div><div>instance Show Person where</div><div>  show p = name p ++ &quot;(&quot; ++ age p ++ &quot;, &quot; ++  getGenderSpecificCondition p ++ &quot;)&quot;</div><div>                     </div><div>data Condition = Bad | OK | Good</div>

</div><div><br></div></font></font></font><div dir="ltr"><font><font face="&#39;trebuchet ms&#39;, sans-serif"><font color="#003333">--- Man.hs ---</font></font></font></div><div dir="ltr"><font><font face="&#39;trebuchet ms&#39;, sans-serif"><font color="#003333"><div dir="ltr">

module Man </div><div dir="ltr">     ( age</div><div dir="ltr">     , name</div><div dir="ltr">     , Man (Man)</div><div dir="ltr">     ) </div><div dir="ltr">where</div><div dir="ltr"><br></div><div dir="ltr">import Person</div>

<div dir="ltr"><br></div><div dir="ltr">data Man = Man { name :: String</div><div dir="ltr">                        , age  :: Int</div><div dir="ltr">                        , prostateCondition :: Condition</div><div dir="ltr">

                        }</div><div dir="ltr">                                 </div><div dir="ltr">instance Person Man where</div><div dir="ltr">    getGenderSpecificCondition :: Person -&gt; Condition</div><div dir="ltr">

    getGenderSpecificCondition m = prostateCondition m</div></font></font></font></div><div dir="ltr"><font><font face="&#39;trebuchet ms&#39;, sans-serif"><font color="#003333"><br></font></font></font></div><div dir="ltr">

<font><font face="&#39;trebuchet ms&#39;, sans-serif"><font color="#003333">--- Woman.hs---</font></font></font></div><div dir="ltr"><font><font face="&#39;trebuchet ms&#39;, sans-serif"><font color="#003333"><div dir="ltr">

module Woman </div><div dir="ltr">     ( age</div><div dir="ltr">     , name</div><div dir="ltr">     , Woman (Woman)</div><div dir="ltr">     ) </div><div dir="ltr">where</div><div dir="ltr"><br></div><div dir="ltr">import Person</div>

<div dir="ltr">               </div><div dir="ltr">data Woman = Woman { name :: String</div><div dir="ltr">                                 , age  :: Int</div><div dir="ltr">                                 , ovaryCondition :: Condition</div>

<div dir="ltr">                                 }</div><div dir="ltr">                   </div><div dir="ltr">instance Person Woman where</div><div dir="ltr">    getGenderSpecificCondition :: Person -&gt; Condition</div>
<div dir="ltr">
    getGenderSpecificCondition w = ovaryCondition w</div><div dir="ltr"><br></div><div dir="ltr"><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial; "><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">---------- End example (multiple files) ------------ </font></font></font></div>

<div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif"><br></font></font></font></div><div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">Thanks</font></font></font></div>

<div><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif"><br></font></font></font></div></span></div><i>-- Russ </i></font></font></font></div><br>
<br><br><div class="gmail_quote">On Tue, Dec 14, 2010 at 12:11 AM,  <span dir="ltr">&lt;<a href="mailto:beginners-request@haskell.org">beginners-request@haskell.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Date: Mon, 13 Dec 2010 22:09:25 -0600<br>
From: Antoine Latter &lt;<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>&gt;<br>
Subject: Re: [Haskell-beginners] Equivalent of inheritance in Haskell<br>
To: C K Kashyap &lt;<a href="mailto:ckkashyap@gmail.com">ckkashyap@gmail.com</a>&gt;<br>
Cc: <a href="mailto:beginners@haskell.org">beginners@haskell.org</a><br>
Message-ID:<br>
        &lt;<a href="mailto:AANLkTinE30iTwWs8qBvWAcDoqGvy2T2_qpQqbXZKrCcm@mail.gmail.com">AANLkTinE30iTwWs8qBvWAcDoqGvy2T2_qpQqbXZKrCcm@mail.gmail.com</a>&gt;<br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
On Mon, Dec 13, 2010 at 9:10 PM, C K Kashyap &lt;<a href="mailto:ckkashyap@gmail.com">ckkashyap@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; But there is not a way to easily say (in Haskell) &quot;type A is<br>
&gt;&gt; everything that type B is plus these other things here ...&quot;. Haskell<br>
&gt;&gt; is not an OO language.<br>
&gt;<br>
&gt; This captures what I had in mind. Using compound types seems ok but<br>
&gt; I&#39;d still need to do some mechanical stuff if I had to provide a<br>
&gt; function that works on the compound type which is actually defined for<br>
&gt; a component type.<br>
&gt;<br>
&gt; If I understand you right .. you&#39;d build a &#39;Man&#39; type and &#39;Woman&#39; type<br>
&gt; by using a &#39;Person&#39; type. Lets say, there is a function called getName<br>
&gt; that is Person -&gt; String<br>
&gt; I&#39;d have to mechanically define a function getName :: Man -&gt; String -<br>
&gt; that extracts the person inside and calls getName on it - did I<br>
&gt; understand it right?<br>
&gt; Or would you typically write extract functions that&#39;ll return the<br>
&gt; components and then the user could call the method on the component?<br>
&gt; As in .... getPerson :: Man -&gt; Person ... then call getName on that.<br>
&gt;<br>
&gt; How do you deal with situations like that?<br>
&gt;<br>
<br>
Well, in this case I might just have a person type with a &#39;gender&#39;<br>
field :-) Then I get the polymorphism and code-reuse for free!<br>
<br>
But what you&#39;re talking about is something that OO-style programming<br>
is particularly aligned towards, and functional programming generally<br>
is not.<br>
<br>
One thing people do is use type-classes - this would be a bit like<br>
having &#39;Car&#39; and &#39;Truck&#39; implement the same interface. The simple<br>
building blocks would be duplicated, but the complex application-level<br>
functionality could be written against the typeclass.<br>
<br>
Another approach is with functional lenses - these are libraries that<br>
aim to make updating complex compound types easier. Off the top of my<br>
head I know of fclabels[1], but I know there are others. If you&#39;re<br>
interested in this approach you might be able to email the -cafe<br>
mailing list to ask for more.<br>
<br>
Is there a particular problem you&#39;re trying to solve? we might be able<br>
to take the conversation in a less speculative direction.<br>
<br>
Antoine</blockquote></div></div></div>