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 (aditya siram) 2. Re: Type unions (aditya siram) 3. Re: Type unions (Tobias Brandt) 4. Re: Type unions (Russ Abbott) 5. Re: Type unions (Tobias Brandt) 6. Re: Type unions (Hector Guilarte) ---------------------------------------------------------------------- Message: 1 Date: Tue, 14 Dec 2010 14:29:51 -0600 From: aditya siram <aditya.si...@gmail.com> Subject: Re: [Haskell-beginners] Type unions To: russ.abb...@gmail.com Cc: beginners <beginners@haskell.org> Message-ID: <aanlktinp8d=eetcdpvm2bmjn1fm2ueg2=xqxjw4mj...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Does this help? data A = A Int f :: Int -> Either A B f x | even x = Left $ A x | | otherwise = Right $ B x | -deech On Tue, Dec 14, 2010 at 2:09 PM, Russ Abbott <russ.abb...@gmail.com> 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 > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > ------------------------------ Message: 2 Date: Tue, 14 Dec 2010 14:30:51 -0600 From: aditya siram <aditya.si...@gmail.com> Subject: Re: [Haskell-beginners] Type unions To: russ.abb...@gmail.com Cc: beginners <beginners@haskell.org> Message-ID: <aanlkti=f2i6kfi+9z7dpyfzwjtmvx=1ycthtyh86u...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Sorry I had a typo. Here's working code: data A = A Int data B = B Int f :: Int -> Either A B f x | even x = Left $ A x | otherwise = Right $ B x -deech On Tue, Dec 14, 2010 at 2:29 PM, aditya siram <aditya.si...@gmail.com> wrote: > Does this help? > > > data A = A Int > > f :: Int -> Either A B > f x > ?| even x = Left $ A x ? ? | > ?| otherwise = Right $ B x | > > -deech > > On Tue, Dec 14, 2010 at 2:09 PM, Russ Abbott <russ.abb...@gmail.com> 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 >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > ------------------------------ Message: 3 Date: Tue, 14 Dec 2010 21:35:21 +0100 From: Tobias Brandt <tob.bra...@googlemail.com> Subject: Re: [Haskell-beginners] Type unions To: russ.abb...@gmail.com Cc: beginners <beginners@haskell.org> Message-ID: <aanlktikuiyjseqx3hb+trnghm+yhs3fw22evmb=lx...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On 14 December 2010 21:26, Russ Abbott <russ.abb...@gmail.com> wrote: > Isn't "Either" the same thing as AorB in > > data AorB = Aconstructor Int | Bconstructor Int > > I want two separate types A and B along with a third type which is their > Union. Is that not possible? That's exactly what either is: data A = ... data B = ... f :: Int -> Either A B > In my actual case, I have more than two types. ?So I would like a way to > take the union of an arbitrarily number of types. > > data Union = A1 | A2 | ... > > where each of A1, A2, ... has its own data declaration. You can create a new data type: data MyUnion = First A1 | Second A2 | Third A3 and use it like this: f :: Int -> MyUnion ------------------------------ Message: 4 Date: Tue, 14 Dec 2010 12:44:32 -0800 From: Russ Abbott <russ.abb...@gmail.com> Subject: Re: [Haskell-beginners] Type unions To: Tobias Brandt <tob.bra...@googlemail.com> Cc: beginners <beginners@haskell.org> Message-ID: <aanlktinqylsem8xkytu-mymxedxac_tewmpfrf3zr...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" I guess the point is that you can't put type names into the declaration of some other type. data A = ... data B = ... -- No good data AorB = A | B f :: Int -> AorB f x | even x = Aconstructor x | otherwise = Bconstructor x -- OK data AorB = AType A | BType B f :: Int -> AorB f x | even x = AType $ Aconstructor x | otherwise = BType $ Bconstructor x What's confusing is that data AorB = A | B compiles with error. That raises the question of what it really means! *-- Russ* *** * ** On Tue, Dec 14, 2010 at 12:35 PM, Tobias Brandt <tob.bra...@googlemail.com>wrote: > On 14 December 2010 21:26, Russ Abbott <russ.abb...@gmail.com> wrote: > > Isn't "Either" the same thing as AorB in > > > > data AorB = Aconstructor Int | Bconstructor Int > > > > I want two separate types A and B along with a third type which is their > > Union. Is that not possible? > > That's exactly what either is: > > data A = ... > data B = ... > > f :: Int -> Either A B > > > In my actual case, I have more than two types. So I would like a way to > > take the union of an arbitrarily number of types. > > > > data Union = A1 | A2 | ... > > > > where each of A1, A2, ... has its own data declaration. > > You can create a new data type: > > data MyUnion = First A1 | Second A2 | Third A3 > > and use it like this: > > f :: Int -> MyUnion > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20101214/524f6d2f/attachment-0001.htm> ------------------------------ Message: 5 Date: Tue, 14 Dec 2010 21:52:58 +0100 From: Tobias Brandt <tob.bra...@googlemail.com> Subject: Re: [Haskell-beginners] Type unions To: russ.abb...@gmail.com Cc: beginners <beginners@haskell.org> Message-ID: <aanlktinp2y2myfeovoik2adno-su4n2xdwa+gfghf...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On 14 December 2010 21:44, Russ Abbott <russ.abb...@gmail.com> 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. ------------------------------ Message: 6 Date: Tue, 14 Dec 2010 16:26:49 -0430 From: Hector Guilarte <hector...@gmail.com> Subject: Re: [Haskell-beginners] Type unions To: russ.abb...@gmail.com Cc: beginners <beginners@haskell.org> Message-ID: <aanlktimoq5rn1scuae5gqg421nctox56d5ndz58kb...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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 <russ.abb...@gmail.com> 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 > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20101214/1ae1d262/attachment.htm> ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 30, Issue 26 *****************************************