Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Type unions (Russ Abbott) 2. Re: Equivalence of Inheritance (Michael Katelman) 3. Re: Equivalence of Inheritance (Bastian Erdn??) ---------------------------------------------------------------------- Message: 1 Date: Tue, 14 Dec 2010 13:43:17 -0800 From: Russ Abbott <russ.abb...@gmail.com> Subject: Re: [Haskell-beginners] Type unions To: Hector Guilarte <hector...@gmail.com> Cc: beginners <beginners@haskell.org> Message-ID: <aanlktin78=zsbc-zdgl6qm0lqrbczmo3f=3ztsyvy...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Thanks. I had assumed that in data A = ... data B = ... data AorB = A | B the A | B in the definition of AorB referred to the types A and B. But now I gather that they are completely unrelated -- except for the fact that they are spelled the same. Thanks. * -- Russ * * * On Tue, Dec 14, 2010 at 1:29 PM, Hector Guilarte <hector...@gmail.com>wrote: > But did you already understand what's happening? > > > data AorB = A | B > > is pretty much like an enumeration the same as > > > data DayOfTheWeek = Monday | Tuesday | Wednesday | ... | Sunday > > it stores no values of any type, you could even have DayOfTheWeek as > a constructor in the same declaration of the data DayOfTheWeek, because > they are > in different scopes: > > > data DayOfTheWeek = Monday | Tuesday | Wednesday | ... | Sunday > | DayOfTheWeek > > and by no means, that A or B on the right hand side of your data AorB are > referencing the > data A nor the data B... > > In the end, you could have: > > data A = Aconstructor Int > data B = Bconstructor Int > data AorB = A A | B B > > f :: Int -> AorB > f x > | even x = A (Aconstructor x) > | otherwise = B (Bconstructor x) > > I just added a Value to each constructor of the data AorB, which happens > to be > named the same as the value they store... > > What I'm about to do is something I haven't tried, but I don't see why it > shouldn't > compile: > > > data Try = Int Int > > in data Try you have a constructor named Int, and it has a value of type > Int, > > I'm trying to explain it the best I can, but I don't know if I managed to > do it > clearly, please let me know if there's something where I wasn't clear > enough. > > Hector > > > On Tue, Dec 14, 2010 at 4:40 PM, Russ Abbott <russ.abb...@gmail.com>wrote: > >> My typo started this most recent confusion. When I wrote >> >> data AorB = A | B >> >> compiles with error. >> That raises the question of what it really means! >> >> >> I meant to say >> >> data AorB = A | B >> >> compiles *without *error. >> That raises the question of what it really means! >> >> * >> -- Russ * >> >> >> On Tue, Dec 14, 2010 at 1:04 PM, Tobias Brandt <tob.bra...@googlemail.com >> > wrote: >> >>> On 14 December 2010 22:02, Hector Guilarte <hector...@gmail.com> wrote: >>> > Tobias you replied at the same time I was answering, you did explained >>> what >>> > is happening, >>> > however, you said something which isn't right. As I mentioned before, >>> "Type >>> > Constructors" and >>> > "Value Constructors" are in different scopes, and so, their names can >>> be >>> > used again... >>> > if you don't believe me, give it a try with this example. >>> >>> I believe you. As I said, I was talking crap :-) >>> >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20101214/f5610642/attachment-0001.htm> ------------------------------ Message: 2 Date: Tue, 14 Dec 2010 15:45:46 -0600 From: Michael Katelman <katel...@uiuc.edu> Subject: Re: [Haskell-beginners] Equivalence of Inheritance To: russ.abb...@gmail.com Cc: beginners <beginners@haskell.org> Message-ID: <aanlktik5uu1mnr2husnawunknafma4mogxcatpwr3...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Without going too exotic, I see two choices. Keeping the single type Person makes what you are asking for, as far as I know, impossible (dependent types). For human readability you could consider type MensGroup = [Person] type WomensGroup = [Person] If you split Person into two types, Man and Woman, there are repercussions for the aggregate group type type Group = [Either Man Woman] -Mike On Tue, Dec 14, 2010 at 3:30 PM, Russ Abbott <russ.abb...@gmail.com> wrote: > Now that this is straightened out, I went back to what I was doing in the > first place and?realized?that I haven't solved my problem. > Given > > data Person = > ?? ? ?Man {name :: String, age :: Int, prostateCondition :: Condition} > ??| Woman {name :: String, age :: Int, ovaryCondition ? ?:: Condition} > > I'd like to define something like this. > > type MensGroup = [Man] > > Is there a way to do something like that? > -- Russ > > > On Tue, Dec 14, 2010 at 1:06 PM, Russ Abbott <russ.abb...@gmail.com> wrote: >> >> That's good. (It's more or less the way I was doing it.) ?What I wanted to >> avoid was this. >> >> getGenderSpecificCondition ( ?Man _ _ cond) = cond >> getGenderSpecificCondition (Woman _ _ cond) = cond >> >> I know it seems like a small thing, but I would like to be able to write >> it like this. >> >> getGenderSpecificCondition p >> ?? | p == (Man _ _ cond) = cond >> ?? | p == (Woman _ _ cond) = cond >> >> But that's not legal syntax. ?A pattern can't appear in that context. But >> this does the job. >> >> getGenderSpecificCondition :: Person -> Condition >> getGenderSpecificCondition p >> ?? | isMan p = prostateCondition p >> ?? | isWoman p = ovaryCondition p >> >> isMan ( ? ? Man _ _ cond) = True >> isMan _ = False >> isWoman (Woman _ _ cond) = True >> isWoman _ = False >> >> That works! prostateCondition and ovaryCondition are both defined on >> Person. (I'm surprised to see that.) >> >> *Person> Group [Man "Harry" 32 OK, Woman "Sally" 29 Good] >> Harry(32, OK) >> Sally(29, Good) >> >> Also >> >> *Person> prostateCondition (Woman "Sally" 29 Good) >> *** Exception: No match in record selector prostateCondition >> *Person> prostateCondition (Man "Harry" 29 Good) >> Good >> >> -- Russ >> >> >> On Tue, Dec 14, 2010 at 12:31 PM, Michael Katelman <katel...@uiuc.edu> >> wrote: >>> >>> Perhaps this? >>> >>> https://gist.github.com/741048 >>> >>> -Mike >>> >>> On Tue, Dec 14, 2010 at 2:27 PM, Russ Abbott <russ.abb...@gmail.com> >>> wrote: >>> > What I'm after is a version of my example that compiles. ?Can you make >>> > one? >>> > >>> > -- Russ >>> > >>> > >>> > On Tue, Dec 14, 2010 at 12:18 PM, Antoine Latter <aslat...@gmail.com> >>> > wrote: >>> >> >>> >> Sorry, I really don't know enough about what you're after to attempt >>> >> that. >>> >> >>> >> But you'll need to change you're signatures of the form: >>> >> >>> >> > function :: Person -> Foo >>> >> >>> >> to something of the form: >>> >> >>> >> > function :: Person p => p -> Foo >>> >> >>> >> Because again, a type class can not be used as a type. >>> >> >>> >> Antoine >>> >> >>> >> On Tue, Dec 14, 2010 at 2:12 PM, Russ Abbott <russ.abb...@gmail.com> >>> >> wrote: >>> >> > What got fouled up is all the adjustments I had to make to the other >>> >> > declarations. >>> >> > Can you?complete?the example so that it compiles using >>> >> > >>> >> > class Person p where ... >>> >> > >>> >> > I'd very much like to see an example that actually compiles. >>> >> > >>> >> > Thanks. >>> >> > -- Russ >>> >> > >>> >> > On Tue, Dec 14, 2010 at 11:58 AM, Antoine Latter >>> >> > <aslat...@gmail.com> >>> >> > wrote: >>> >> >> >>> >> >> On Tue, Dec 14, 2010 at 1:52 PM, Russ Abbott >>> >> >> <russ.abb...@gmail.com> >>> >> >> wrote: >>> >> >> > If gender is a field in a Person type, then a Person must have >>> >> >> > both >>> >> >> > an >>> >> >> > ovaryCondition and a prostateCondition. ?That seems awkward. >>> >> >> > Regarding >>> >> >> > ?? ? class Person p where >>> >> >> > I started down that path but got completely fouled up. >>> >> >> >>> >> >> How did this get fouled up? Every class declaration must take >>> >> >> arguments - here, 'p' is the argument for the class. >>> >> >> >>> >> >> Thanks, >>> >> >> Antoine >>> >> > >>> >> > >>> > >>> > >>> > _______________________________________________ >>> > Beginners mailing list >>> > Beginners@haskell.org >>> > http://www.haskell.org/mailman/listinfo/beginners >>> > >>> > >> > > ------------------------------ Message: 3 Date: Tue, 14 Dec 2010 22:46:30 +0100 From: Bastian Erdn?? <earth...@web.de> Subject: Re: [Haskell-beginners] Equivalence of Inheritance To: beginners@haskell.org Message-ID: <23661ea0-aa0e-4d85-920c-93cea6a8b...@web.de> Content-Type: text/plain; charset=us-ascii On Dec 14, 2010, at 19:11, Russ Abbott wrote: > 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 > > Class `Person' used as a type > > > If I write "(Person p) =>" instead, I get other diagnostics. > > I would very much appreciate seeing how this should be done in Haskell. What about > ---------- Example (multiple files) ------------ > --- Group.hs --- > module Group where > > import Person > > data Group = Group { members :: [Person] } > > instance Show Group where > show group = unlines $ map show $ members group > > --- Person.hs --- > > module Person where > > data Person = Person { > nameP :: p -> String , > ageP :: p -> Int , > getGenderSpecificCondition :: p -> Condition } > > instance Show Person where > show p = nameP p ++ "(" ++ show (ageP p) ++ ", " ++ > show (getGenderSpecificCondition p) ++ ")" > > data Condition = Bad | OK | Good class PersonClass p where toPerson :: p -> Person > --- Man.hs --- > module Man where > > import Person > > data Man = Man { nameM :: String > , ageM :: Int > , prostateCondition :: Condition > } > > instance PersonClass Man where > toPerson (Man n a c) = Person n a c > > --- Woman.hs--- > module Woman where > > import Person > > data Woman = Woman { nameW :: String > , ageW :: Int > , ovaryCondition :: Condition > } > > instance Person Woman where > toPerson (Woman n a c) = Person n a c > > ---------- End example (multiple files) ------------ ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 30, Issue 29 *****************************************