Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/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: How to best handle classes of types, interactions and listing of different types? (Silent Leaf) 2. Re: How to best handle classes of types, interactions and listing of different types? (Alex Belanger) 3. Re: How to best handle classes of types, interactions and listing of different types? (Silent Leaf) ---------------------------------------------------------------------- Message: 1 Date: Thu, 26 May 2016 15:44:51 +0200 From: Silent Leaf <silent.le...@gmail.com> To: Daniel Bergey <ber...@alum.mit.edu>, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] How to best handle classes of types, interactions and listing of different types? Message-ID: <cagfccjp14uhyuhasmmx0dw_mnvj9vbdt58ef-6s7rn2r0cn...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I understand your example. Still it's useless if we need the value of the multiplied vectors, more than just a list of their lengths after the operation, unless i missed something. At any rate; I'm trying to create a representation of mathematical sets. The tricky part being, they're precisely capable to handle any kind of content, including themselves. But I think I went too overboard, trying to handle the idea of sets that could contain strictly any type (at once). In practice, the basic elements will be rather similar, and known enough at least so that I can merely use ADTs for the various "types" of elements, and same for sets themselves. If needed I can create two instances of monoids, one for And, one for Or, using newtype wrappers. It's vaguely a hassle but anyway it'll only be useful if i have to create functions that could work on both monoids (separately), which would be interesting enough so i don't merely duplicate the job. There's also the possibility i need to use existing functions already using monoids... For now I think i'll be ok with ADTs and one common wrapper for all elements. Thanks still, I'll think about your idea, it's rather interesting. Le mardi 24 mai 2016, Daniel Bergey <ber...@alum.mit.edu> a ?crit : > On 2016-05-23 at 12:06, Silent Leaf <silent.le...@gmail.com> wrote: >> Say there's a class, many types that can instantiate it. Then, i in fact need to be able >> to concatenate (mappend would be ideal!), make lists of values of different types, all >> instantiating the class. > > In most cases, when Haskell beginners want to make a list that contains > several types from a single type class, there's a better way to organize > the code. If you post your code, I'll try to suggest a specific > solution. > > In general, try to find a simple data type that captures the same fields > & functions as an unknown type that is part of the type class. Here's > an example. > > We have a type class for vectors in a metric space, and instances for > 2D, 3D, etc. > >> class Metric v where >> length :: v -> Double >> (*^) :: Double -> v -> v > > This class has the law: s * length v == length (s *^ v) > > Instead of a heterogeneous list [ V2 1 2, V3 3 4 5, V4 6 7 8 9], we make > a list that just has the length of each vector, and lets us multiply > those lengths by a scalar. In this case, we don't even need to write a > new data type, the type is simply Double. We can write: > >> [ length (V2 1 2), length (V3 3 4 5), length (V4 6 7 8 9) ] > > And (*) gives us what Metric does with (*^). Of course, with your > class, it's probably not so obvious how to transform the class this way. > > It's certainly possible to make a record with functions as members, > moving in the object-oriented direction. Existential types (with a > class constraint inside the data constructor) are even more rarely used. > > cheers, > bergey > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160526/25cd4a98/attachment-0001.html> ------------------------------ Message: 2 Date: Thu, 26 May 2016 09:53:57 -0400 From: Alex Belanger <i.caught....@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] How to best handle classes of types, interactions and listing of different types? Message-ID: <cadsky2wtybv7fhs_cf7mcgcwp4s5ywlfdwy4rufejab2sk9...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Monoid would fit the picture as far as "concatenating" the same type go. What you could do is have constructors that defines which types can be concatenated together then only that wrapper type implements the final Monoid instance. On May 26, 2016 9:44 AM, "Silent Leaf" <silent.le...@gmail.com> wrote: I understand your example. Still it's useless if we need the value of the multiplied vectors, more than just a list of their lengths after the operation, unless i missed something. At any rate; I'm trying to create a representation of mathematical sets. The tricky part being, they're precisely capable to handle any kind of content, including themselves. But I think I went too overboard, trying to handle the idea of sets that could contain strictly any type (at once). In practice, the basic elements will be rather similar, and known enough at least so that I can merely use ADTs for the various "types" of elements, and same for sets themselves. If needed I can create two instances of monoids, one for And, one for Or, using newtype wrappers. It's vaguely a hassle but anyway it'll only be useful if i have to create functions that could work on both monoids (separately), which would be interesting enough so i don't merely duplicate the job. There's also the possibility i need to use existing functions already using monoids... For now I think i'll be ok with ADTs and one common wrapper for all elements. Thanks still, I'll think about your idea, it's rather interesting. Le mardi 24 mai 2016, Daniel Bergey <ber...@alum.mit.edu> a ?crit : > On 2016-05-23 at 12:06, Silent Leaf <silent.le...@gmail.com> wrote: >> Say there's a class, many types that can instantiate it. Then, i in fact need to be able >> to concatenate (mappend would be ideal!), make lists of values of different types, all >> instantiating the class. > > In most cases, when Haskell beginners want to make a list that contains > several types from a single type class, there's a better way to organize > the code. If you post your code, I'll try to suggest a specific > solution. > > In general, try to find a simple data type that captures the same fields > & functions as an unknown type that is part of the type class. Here's > an example. > > We have a type class for vectors in a metric space, and instances for > 2D, 3D, etc. > >> class Metric v where >> length :: v -> Double >> (*^) :: Double -> v -> v > > This class has the law: s * length v == length (s *^ v) > > Instead of a heterogeneous list [ V2 1 2, V3 3 4 5, V4 6 7 8 9], we make > a list that just has the length of each vector, and lets us multiply > those lengths by a scalar. In this case, we don't even need to write a > new data type, the type is simply Double. We can write: > >> [ length (V2 1 2), length (V3 3 4 5), length (V4 6 7 8 9) ] > > And (*) gives us what Metric does with (*^). Of course, with your > class, it's probably not so obvious how to transform the class this way. > > It's certainly possible to make a record with functions as members, > moving in the object-oriented direction. Existential types (with a > class constraint inside the data constructor) are even more rarely used. > > cheers, > bergey > _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160526/675d69e2/attachment-0001.html> ------------------------------ Message: 3 Date: Thu, 26 May 2016 17:20:13 +0200 From: Silent Leaf <silent.le...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org>, i.caught....@gmail.com Subject: Re: [Haskell-beginners] How to best handle classes of types, interactions and listing of different types? Message-ID: <CAGFccjNDP1PxFjjRYnZ1Y7vBSOs+-a9HM=r4qlw6jjuewkg...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hey, not a bad idea at all! I'll think about it, thanks! Le jeudi 26 mai 2016, Alex Belanger <i.caught....@gmail.com> a ?crit : > Monoid would fit the picture as far as "concatenating" the same type go. What you could do is have constructors that defines which types can be concatenated together then only that wrapper type implements the final Monoid instance. > > On May 26, 2016 9:44 AM, "Silent Leaf" <silent.le...@gmail.com> wrote: > > I understand your example. Still it's useless if we need the value of the multiplied vectors, more than just a list of their lengths after the operation, unless i missed something. > > At any rate; I'm trying to create a representation of mathematical sets. The tricky part being, they're precisely capable to handle any kind of content, including themselves. But I think I went too overboard, trying to handle the idea of sets that could contain strictly any type (at once). In practice, the basic elements will be rather similar, and known enough at least so that I can merely use ADTs for the various "types" of elements, and same for sets themselves. > If needed I can create two instances of monoids, one for And, one for Or, using newtype wrappers. It's vaguely a hassle but anyway it'll only be useful if i have to create functions that could work on both monoids (separately), which would be interesting enough so i don't merely duplicate the job. > There's also the possibility i need to use existing functions already using monoids... > For now I think i'll be ok with ADTs and one common wrapper for all elements. > > Thanks still, I'll think about your idea, it's rather interesting. > > Le mardi 24 mai 2016, Daniel Bergey <ber...@alum.mit.edu> a ?crit : >> On 2016-05-23 at 12:06, Silent Leaf <silent.le...@gmail.com> wrote: >>> Say there's a class, many types that can instantiate it. Then, i in fact need to be able >>> to concatenate (mappend would be ideal!), make lists of values of different types, all >>> instantiating the class. >> >> In most cases, when Haskell beginners want to make a list that contains >> several types from a single type class, there's a better way to organize >> the code. If you post your code, I'll try to suggest a specific >> solution. >> >> In general, try to find a simple data type that captures the same fields >> & functions as an unknown type that is part of the type class. Here's >> an example. >> >> We have a type class for vectors in a metric space, and instances for >> 2D, 3D, etc. >> >>> class Metric v where >>> length :: v -> Double >>> (*^) :: Double -> v -> v >> >> This class has the law: s * length v == length (s *^ v) >> >> Instead of a heterogeneous list [ V2 1 2, V3 3 4 5, V4 6 7 8 9], we make >> a list that just has the length of each vector, and lets us multiply >> those lengths by a scalar. In this case, we don't even need to write a >> new data type, the type is simply Double. We can write: >> >>> [ length (V2 1 2), length (V3 3 4 5), length (V4 6 7 8 9) ] >> >> And (*) gives us what Metric does with (*^). Of course, with your >> class, it's probably not so obvious how to transform the class this way. >> >> It's certainly possible to make a record with functions as members, >> moving in the object-oriented direction. Existential types (with a >> class constraint inside the data constructor) are even more rarely used. >> >> cheers, >> bergey >> > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160526/3cd0cec6/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 95, Issue 31 *****************************************