[Haskell-beginners] How to best handle classes of types, interactions and listing of different types?
Silent Leaf
silent.leaf0 at gmail.com
Thu May 26 15:20:13 UTC 2016
Hey, not a bad idea at all! I'll think about it, thanks!
Le jeudi 26 mai 2016, Alex Belanger <i.caught.air at 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.leaf0 at 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 <bergey at alum.mit.edu> a écrit :
>> On 2016-05-23 at 12:06, Silent Leaf <silent.leaf0 at 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 at 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-0001.html>
More information about the Beginners
mailing list