How far can you take overloading with type classes?

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

How far can you take overloading with type classes?

Hilco Wijbenga
Hi all,

Given definitions of Line and Lines is it possible to define a type
class (or maybe multiple type classes) that would allow for the
following:

(++) :: Line -> Line -> Lines
(++) :: Line -> Lines -> Lines
(++) :: Lines -> Line -> Lines
(++) :: Lines -> Lines -> Lines

I.e. is there a way to overload (++) such that it supports each of
these 4 combinations? (Let's ignore that (++) already exists for the
moment.)

Cheers,
Hilco
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: How far can you take overloading with type classes?

Alexis King
Sure, it can certainly be done:

  class CombineLines a b where
    (++) :: a -> b -> Lines

  instance CombineLines Line Line where { ... }
  instance CombineLines Line Lines where { ... }
  instance CombineLines Lines Line where { ... }
  instance CombineLines Lines Lines where { ... }

Of course, whether or not such a class is an especially useful thing is
another matter entirely. Other encodings might be more helpful in
practice. Perhaps something like this is closer to what you really want:

  class ToLines a where
    toLines :: a -> Lines

  instance ToLines Line where
    toLines = lineToLines
  instance ToLines Lines where
    toLines = id

  (++) :: (ToLines a, ToLines b) => a -> b -> Lines
  x ++ y = toLines x `addLines` toLines y

Or perhaps none of these are really all that helpful in practice, and
the overloading isn’t really worth it.


> On Jun 12, 2018, at 22:58, Hilco Wijbenga <[hidden email]> wrote:
>
> Hi all,
>
> Given definitions of Line and Lines is it possible to define a type
> class (or maybe multiple type classes) that would allow for the
> following:
>
> (++) :: Line -> Line -> Lines
> (++) :: Line -> Lines -> Lines
> (++) :: Lines -> Line -> Lines
> (++) :: Lines -> Lines -> Lines
>
> I.e. is there a way to overload (++) such that it supports each of
> these 4 combinations? (Let's ignore that (++) already exists for the
> moment.)
>
> Cheers,
> Hilco

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: How far can you take overloading with type classes?

Artem Pelenitsyn
Hello,

The first solution by Alexis needs the languages extension for multiparameter type classes to be on. So you have to add, e.g.

{-# LANGUAGE MultiParamTypeClasses #-}

at the beginning of your source file.

The second solution is preferable, of course.

--
Thanks, Artem
ср, 13 июня 2018 г. в 6:37, Alexis King <[hidden email]>:
Sure, it can certainly be done:

  class CombineLines a b where
    (++) :: a -> b -> Lines

  instance CombineLines Line Line where { ... }
  instance CombineLines Line Lines where { ... }
  instance CombineLines Lines Line where { ... }
  instance CombineLines Lines Lines where { ... }

Of course, whether or not such a class is an especially useful thing is
another matter entirely. Other encodings might be more helpful in
practice. Perhaps something like this is closer to what you really want:

  class ToLines a where
    toLines :: a -> Lines

  instance ToLines Line where
    toLines = lineToLines
  instance ToLines Lines where
    toLines = id

  (++) :: (ToLines a, ToLines b) => a -> b -> Lines
  x ++ y = toLines x `addLines` toLines y

Or perhaps none of these are really all that helpful in practice, and
the overloading isn’t really worth it.


> On Jun 12, 2018, at 22:58, Hilco Wijbenga <[hidden email]> wrote:
>
> Hi all,
>
> Given definitions of Line and Lines is it possible to define a type
> class (or maybe multiple type classes) that would allow for the
> following:
>
> (++) :: Line -> Line -> Lines
> (++) :: Line -> Lines -> Lines
> (++) :: Lines -> Line -> Lines
> (++) :: Lines -> Lines -> Lines
>
> I.e. is there a way to overload (++) such that it supports each of
> these 4 combinations? (Let's ignore that (++) already exists for the
> moment.)
>
> Cheers,
> Hilco

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.