Types depending on tuples.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Types depending on tuples.

Mike Meyer
I'm doing some geometry in 2 and 3 dimensions. To do this, I've got Vector2d and Vector3d classes, which are basically just wrappers around 2 and 3-tuples of doubles:

type Vector2d = (Double, Double)
type Vector3d = (Double, Double, Double)

I've also got a typeclass Vector so I can do polymorphic things on them:

type Vector3d = (Double, Double, Double)

(Ok, polymorphic thing).

I now have a need to convert lists of Vector's to a list of lists of Doubles. I'd like that to be part of the Vector typeclass, but - well, declaring it is interesting.

I can't just add a method "toLists :: a -> [Double]", because (according to ghc), there's no match between the expected type 'Double' and the actual type '[t1]'. 

Can I declare this typeclass?

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Types depending on tuples.

Mike Izbicki
You never actually provided the Vector type class, so it's difficult
to tell what exactly you're trying to do.

On Tue, Aug 26, 2014 at 12:46 AM, Mike Meyer <[hidden email]> wrote:

> I'm doing some geometry in 2 and 3 dimensions. To do this, I've got Vector2d
> and Vector3d classes, which are basically just wrappers around 2 and
> 3-tuples of doubles:
>
> type Vector2d = (Double, Double)
> type Vector3d = (Double, Double, Double)
>
> I've also got a typeclass Vector so I can do polymorphic things on them:
>
> type Vector3d = (Double, Double, Double)
>
> (Ok, polymorphic thing).
>
> I now have a need to convert lists of Vector's to a list of lists of
> Doubles. I'd like that to be part of the Vector typeclass, but - well,
> declaring it is interesting.
>
> I can't just add a method "toLists :: a -> [Double]", because (according to
> ghc), there's no match between the expected type 'Double' and the actual
> type '[t1]'.
>
> Can I declare this typeclass?
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Types depending on tuples.

Mike Izbicki
This works just fine for me:

class Vector a where
    toLists :: a -> [Double]

instance Vector (Double,Double,Double) where
    toLists (a,b,c)=[a,b,c]

On Tue, Aug 26, 2014 at 1:17 AM, Mike Meyer <[hidden email]> wrote:

> It's there, but in the text:
>
> class Vector a where
>    toLists :: a -> [Double]
>
>
> On Tue, Aug 26, 2014 at 2:59 AM, Mike Izbicki <[hidden email]> wrote:
>>
>> You never actually provided the Vector type class, so it's difficult
>> to tell what exactly you're trying to do.
>>
>> On Tue, Aug 26, 2014 at 12:46 AM, Mike Meyer <[hidden email]> wrote:
>> > I'm doing some geometry in 2 and 3 dimensions. To do this, I've got
>> > Vector2d
>> > and Vector3d classes, which are basically just wrappers around 2 and
>> > 3-tuples of doubles:
>> >
>> > type Vector2d = (Double, Double)
>> > type Vector3d = (Double, Double, Double)
>> >
>> > I've also got a typeclass Vector so I can do polymorphic things on them:
>> >
>> > type Vector3d = (Double, Double, Double)
>> >
>> > (Ok, polymorphic thing).
>> >
>> > I now have a need to convert lists of Vector's to a list of lists of
>> > Doubles. I'd like that to be part of the Vector typeclass, but - well,
>> > declaring it is interesting.
>> >
>> > I can't just add a method "toLists :: a -> [Double]", because (according
>> > to
>> > ghc), there's no match between the expected type 'Double' and the actual
>> > type '[t1]'.
>> >
>> > Can I declare this typeclass?
>> >
>> > _______________________________________________
>> > Haskell-Cafe mailing list
>> > [hidden email]
>> > http://www.haskell.org/mailman/listinfo/haskell-cafe
>> >
>
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Types depending on tuples.

Mike Meyer
Weird. I thought that's what I tried. Must have done something else.

Thanks!


On Tue, Aug 26, 2014 at 3:31 AM, Mike Izbicki <[hidden email]> wrote:
This works just fine for me:

class Vector a where
    toLists :: a -> [Double]

instance Vector (Double,Double,Double) where
    toLists (a,b,c)=[a,b,c]

On Tue, Aug 26, 2014 at 1:17 AM, Mike Meyer <[hidden email]> wrote:
> It's there, but in the text:
>
> class Vector a where
>    toLists :: a -> [Double]
>
>
> On Tue, Aug 26, 2014 at 2:59 AM, Mike Izbicki <[hidden email]> wrote:
>>
>> You never actually provided the Vector type class, so it's difficult
>> to tell what exactly you're trying to do.
>>
>> On Tue, Aug 26, 2014 at 12:46 AM, Mike Meyer <[hidden email]> wrote:
>> > I'm doing some geometry in 2 and 3 dimensions. To do this, I've got
>> > Vector2d
>> > and Vector3d classes, which are basically just wrappers around 2 and
>> > 3-tuples of doubles:
>> >
>> > type Vector2d = (Double, Double)
>> > type Vector3d = (Double, Double, Double)
>> >
>> > I've also got a typeclass Vector so I can do polymorphic things on them:
>> >
>> > type Vector3d = (Double, Double, Double)
>> >
>> > (Ok, polymorphic thing).
>> >
>> > I now have a need to convert lists of Vector's to a list of lists of
>> > Doubles. I'd like that to be part of the Vector typeclass, but - well,
>> > declaring it is interesting.
>> >
>> > I can't just add a method "toLists :: a -> [Double]", because (according
>> > to
>> > ghc), there's no match between the expected type 'Double' and the actual
>> > type '[t1]'.
>> >
>> > Can I declare this typeclass?
>> >
>> > _______________________________________________
>> > Haskell-Cafe mailing list
>> > [hidden email]
>> > http://www.haskell.org/mailman/listinfo/haskell-cafe
>> >
>
>


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Types depending on tuples.

Tom Ellis
On Tue, Aug 26, 2014 at 03:54:41AM -0500, Mike Meyer wrote:
> Weird. I thought that's what I tried. Must have done something else.

You said "need to convert lists of Vector's to a list of lists of                                                                              
Doubles" ([[Double]]).  This is just list of Doubles ([Double]).
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Types depending on tuples.

John Lato-2


On Aug 26, 2014 6:16 AM, "Tom Ellis" <[hidden email]> wrote:
>
> On Tue, Aug 26, 2014 at 03:54:41AM -0500, Mike Meyer wrote:
> > Weird. I thought that's what I tried. Must have done something else.
>
> You said "need to convert lists of Vector's to a list of lists of
> Doubles" ([[Double]]).  This is just list of Doubles ([Double]).

This operates on a single Vector. You can just map this function over a list to do the requested conversion.


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Types depending on tuples.

Tom Ellis
On Tue, Aug 26, 2014 at 12:36:38PM -0700, John Lato wrote:

> On Aug 26, 2014 6:16 AM, "Tom Ellis" <
> [hidden email]> wrote:
> >
> > On Tue, Aug 26, 2014 at 03:54:41AM -0500, Mike Meyer wrote:
> > > Weird. I thought that's what I tried. Must have done something else.
> >
> > You said "need to convert lists of Vector's to a list of lists of
> > Doubles" ([[Double]]).  This is just list of Doubles ([Double]).
>
> This operates on a single Vector. You can just map this function over a
> list to do the requested conversion.

Sure, I'm just saying that given that the original error was apparently

    no match between the expected type 'Double' and the actual type '[t1]'

this may be the source of the problem.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe