<div dir="ltr"><font color="#003333"><font size="2"><font face="trebuchet ms,sans-serif">I'm also confused about how to do the equivalence of inheritance in Haskell. Here is a complete example below. It doesn'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' 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 "(Person p) =>" 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: 'trebuchet ms', 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 -> Int</div><div> </div><div> name :: Person -> String</div><div> </div><div> getGenderSpecificCondition :: Person -> Condition</div>
<div><br></div><div>instance Show Person where</div><div> show p = name p ++ "(" ++ age p ++ ", " ++ getGenderSpecificCondition p ++ ")"</div><div> </div><div>data Condition = Bad | OK | Good</div>
</div><div><br></div></font></font></font><div dir="ltr"><font><font face="'trebuchet ms', sans-serif"><font color="#003333">--- Man.hs ---</font></font></font></div><div dir="ltr"><font><font face="'trebuchet ms', 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 -> Condition</div><div dir="ltr">
getGenderSpecificCondition m = prostateCondition m</div></font></font></font></div><div dir="ltr"><font><font face="'trebuchet ms', sans-serif"><font color="#003333"><br></font></font></font></div><div dir="ltr">
<font><font face="'trebuchet ms', sans-serif"><font color="#003333">--- Woman.hs---</font></font></font></div><div dir="ltr"><font><font face="'trebuchet ms', 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 -> 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"><<a href="mailto:beginners-request@haskell.org">beginners-request@haskell.org</a>></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 <<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>><br>
Subject: Re: [Haskell-beginners] Equivalent of inheritance in Haskell<br>
To: C K Kashyap <<a href="mailto:ckkashyap@gmail.com">ckkashyap@gmail.com</a>><br>
Cc: <a href="mailto:beginners@haskell.org">beginners@haskell.org</a><br>
Message-ID:<br>
<<a href="mailto:AANLkTinE30iTwWs8qBvWAcDoqGvy2T2_qpQqbXZKrCcm@mail.gmail.com">AANLkTinE30iTwWs8qBvWAcDoqGvy2T2_qpQqbXZKrCcm@mail.gmail.com</a>><br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
On Mon, Dec 13, 2010 at 9:10 PM, C K Kashyap <<a href="mailto:ckkashyap@gmail.com">ckkashyap@gmail.com</a>> wrote:<br>
>><br>
>> But there is not a way to easily say (in Haskell) "type A is<br>
>> everything that type B is plus these other things here ...". Haskell<br>
>> is not an OO language.<br>
><br>
> This captures what I had in mind. Using compound types seems ok but<br>
> I'd still need to do some mechanical stuff if I had to provide a<br>
> function that works on the compound type which is actually defined for<br>
> a component type.<br>
><br>
> If I understand you right .. you'd build a 'Man' type and 'Woman' type<br>
> by using a 'Person' type. Lets say, there is a function called getName<br>
> that is Person -> String<br>
> I'd have to mechanically define a function getName :: Man -> String -<br>
> that extracts the person inside and calls getName on it - did I<br>
> understand it right?<br>
> Or would you typically write extract functions that'll return the<br>
> components and then the user could call the method on the component?<br>
> As in .... getPerson :: Man -> Person ... then call getName on that.<br>
><br>
> How do you deal with situations like that?<br>
><br>
<br>
Well, in this case I might just have a person type with a 'gender'<br>
field :-) Then I get the polymorphism and code-reuse for free!<br>
<br>
But what you'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 'Car' and 'Truck' 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'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'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>