Hello,<br><br>The first solution by Alexis needs the languages extension for multiparameter type classes to be on. So you have to add, e.g.<br><br>{-# LANGUAGE MultiParamTypeClasses #-} <br><br>at the beginning of your source file.<br><br>The second solution is preferable, of course.<br><br>--<br>Thanks, Artem<br><div class="gmail_quote"><div dir="ltr">ср, 13 июня 2018 г. в 6:37, Alexis King <<a href="mailto:lexi.lambda@gmail.com">lexi.lambda@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sure, it can certainly be done:<br>
<br>
class CombineLines a b where<br>
(++) :: a -> b -> Lines<br>
<br>
instance CombineLines Line Line where { ... }<br>
instance CombineLines Line Lines where { ... }<br>
instance CombineLines Lines Line where { ... }<br>
instance CombineLines Lines Lines where { ... }<br>
<br>
Of course, whether or not such a class is an especially useful thing is<br>
another matter entirely. Other encodings might be more helpful in<br>
practice. Perhaps something like this is closer to what you really want:<br>
<br>
class ToLines a where<br>
toLines :: a -> Lines<br>
<br>
instance ToLines Line where<br>
toLines = lineToLines<br>
instance ToLines Lines where<br>
toLines = id<br>
<br>
(++) :: (ToLines a, ToLines b) => a -> b -> Lines<br>
x ++ y = toLines x `addLines` toLines y<br>
<br>
Or perhaps none of these are really all that helpful in practice, and<br>
the overloading isn’t really worth it.<br>
<br>
<br>
> On Jun 12, 2018, at 22:58, Hilco Wijbenga <<a href="mailto:hilco.wijbenga@gmail.com" target="_blank">hilco.wijbenga@gmail.com</a>> wrote:<br>
> <br>
> Hi all,<br>
> <br>
> Given definitions of Line and Lines is it possible to define a type<br>
> class (or maybe multiple type classes) that would allow for the<br>
> following:<br>
> <br>
> (++) :: Line -> Line -> Lines<br>
> (++) :: Line -> Lines -> Lines<br>
> (++) :: Lines -> Line -> Lines<br>
> (++) :: Lines -> Lines -> Lines<br>
> <br>
> I.e. is there a way to overload (++) such that it supports each of<br>
> these 4 combinations? (Let's ignore that (++) already exists for the<br>
> moment.)<br>
> <br>
> Cheers,<br>
> Hilco<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>