Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
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 (Tobias Brandt)
2. Re: Type unions (Hector Guilarte)
3. Re: Type unions (Hector Guilarte)
4. Re: Type unions (Tobias Brandt)
5. Re: Equivalence of Inheritance (Russ Abbott)
----------------------------------------------------------------------
Message: 1
Date: Tue, 14 Dec 2010 22:02:20 +0100
From: Tobias Brandt <[email protected]>
Subject: Re: [Haskell-beginners] Type unions
To: Hector Guilarte <[email protected]>
Cc: [email protected], beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Woah, I was talking crap. Ignore my last post.
On 14 December 2010 21:56, Hector Guilarte <[email protected]> wrote:
> Hello,
> Nobody has explained you why that doesn't compile...
> Here's the deal
> Suppose you have a data A which has a?constructor?named B and a Int
>> data A = B Int
> now suppose you have a data C which has a?constructor?named A and a Int
>> data C = A Int
> that compiles because the name of your data type is different from
> the?constructor,
> that is, the names of the data types and the constructors they have are in
> different
> scopes, so for doing what you want, you would need to do:
>> data A =?Aconstructor?Int
>> data B =?Bconstructor?Int
>> data?AorB?= A A ?| B B
> Where the first A is a?constructor?named A and the second references a data
> type A,
> idem for B
> Hope that helps you,
> H?ctor Guilarte
> On Tue, Dec 14, 2010 at 3:39 PM, Russ Abbott <[email protected]> wrote:
>>
>> Is there a way to get this to work?
>>
>> data A = Aconstructor Int
>> data B = Bconstructor Int
>> data AorB = A | B
>> f :: Int -> AorB
>> f x
>> ??| even x ? ? = Aconstructor x
>> ??| otherwise = Bconstructor x
>>
>> ?I get this diagnostic.
>>
>> Couldn't match expected type `AorB' against inferred type `A'
>>
>> Since AorB is A or B, why is this not permitted?
>> If instead I write
>>
>> data AorB = Aconstructor Int | Bconstructor Int
>>
>> everything works out ok. But what if I want separate types for A and B?
>> Thanks,
>> -- Russ
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 2
Date: Tue, 14 Dec 2010 16:32:27 -0430
From: Hector Guilarte <[email protected]>
Subject: Re: [Haskell-beginners] Type unions
To: Tobias Brandt <[email protected]>
Cc: [email protected], beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
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 didn't even try
it, but I asure you it
will compile without any trouble:
data A = C Int
data B = D Int
data AorB = A A | B B
Greeting,
H?ctor Guilarte
On Tue, Dec 14, 2010 at 4:22 PM, Tobias Brandt <[email protected]>wrote:
> On 14 December 2010 21:44, Russ Abbott <[email protected]> wrote:
> > What's confusing is that
> >
> >
> > data AorB = A | B
> >
> > compiles with error.
> > That raises the question of what it really means!
>
> You have to distinguish between type and value constructors.
> On the left hand side of a data declaration you have
> a type constructor (AorB) and possibly some type variables.
> On the right hand side you have value constructors followed
> by their arguments (types or type variables). E.g.:
>
> data TypeConstr a b c = ValueConstr1 a b | ValueConstr2 c | ValueConstr3
> Int
>
> But in your example A and B were already declared as type
> constructors, so they can't be used again as value constructors.
> That's why you get an error. If you remove
>
> data A = ...
> and
> data B = ...
>
> then
>
> data AorB = A | B
>
> compiles.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20101214/f143a278/attachment-0001.htm>
------------------------------
Message: 3
Date: Tue, 14 Dec 2010 16:33:31 -0430
From: Hector Guilarte <[email protected]>
Subject: Re: [Haskell-beginners] Type unions
To: Tobias Brandt <[email protected]>
Cc: [email protected], beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Hahaha don't worry man,
Hope it helps for you too
On Tue, Dec 14, 2010 at 4:32 PM, Tobias Brandt <[email protected]>wrote:
> Woah, I was talking crap. Ignore my last post.
>
> On 14 December 2010 21:56, Hector Guilarte <[email protected]> wrote:
> > Hello,
> > Nobody has explained you why that doesn't compile...
> > Here's the deal
> > Suppose you have a data A which has a constructor named B and a Int
> >> data A = B Int
> > now suppose you have a data C which has a constructor named A and a Int
> >> data C = A Int
> > that compiles because the name of your data type is different from
> > the constructor,
> > that is, the names of the data types and the constructors they have are
> in
> > different
> > scopes, so for doing what you want, you would need to do:
> >> data A = Aconstructor Int
> >> data B = Bconstructor Int
> >> data AorB = A A | B B
> > Where the first A is a constructor named A and the second references a
> data
> > type A,
> > idem for B
> > Hope that helps you,
> > H?ctor Guilarte
> > On Tue, Dec 14, 2010 at 3:39 PM, Russ Abbott <[email protected]>
> wrote:
> >>
> >> Is there a way to get this to work?
> >>
> >> data A = Aconstructor Int
> >> data B = Bconstructor Int
> >> data AorB = A | B
> >> f :: Int -> AorB
> >> f x
> >> | even x = Aconstructor x
> >> | otherwise = Bconstructor x
> >>
> >> I get this diagnostic.
> >>
> >> Couldn't match expected type `AorB' against inferred type `A'
> >>
> >> Since AorB is A or B, why is this not permitted?
> >> If instead I write
> >>
> >> data AorB = Aconstructor Int | Bconstructor Int
> >>
> >> everything works out ok. But what if I want separate types for A and B?
> >> Thanks,
> >> -- Russ
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >>
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20101214/a65586cf/attachment-0001.htm>
------------------------------
Message: 4
Date: Tue, 14 Dec 2010 22:04:31 +0100
From: Tobias Brandt <[email protected]>
Subject: Re: [Haskell-beginners] Type unions
To: Hector Guilarte <[email protected]>
Cc: [email protected], beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 14 December 2010 22:02, Hector Guilarte <[email protected]> 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 :-)
------------------------------
Message: 5
Date: Tue, 14 Dec 2010 13:06:37 -0800
From: Russ Abbott <[email protected]>
Subject: Re: [Haskell-beginners] Equivalence of Inheritance
To: Michael Katelman <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]>wrote:
> Perhaps this?
>
> https://gist.github.com/741048
>
> -Mike
>
> On Tue, Dec 14, 2010 at 2:27 PM, Russ Abbott <[email protected]>
> 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 <[email protected]>
> 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 <[email protected]>
> >> 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 <[email protected]>
> >> > wrote:
> >> >>
> >> >> On Tue, Dec 14, 2010 at 1:52 PM, Russ Abbott <[email protected]>
> >> >> 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
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20101214/f6061f52/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 30, Issue 27
*****************************************