<div dir="ltr">You can have a heterogeneous list of items that implement a typeclass if you have a wrapper that uses ExistentialQuantification. See <a href="http://www.haskell.org/haskellwiki/Heterogenous_collections">http://www.haskell.org/haskellwiki/Heterogenous_collections</a><div>
<br></div><div>I don't have enough experience with the type system to properly answer your actual question though.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Jan 12, 2014 at 7:38 PM, Luke Clifton <span dir="ltr"><<a href="mailto:ltclifton@gmail.com" target="_blank">ltclifton@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi,</div><div><br></div><div>I'm quite new to Haskell, and have been loving exploring it. I've always been a huge fan of languages that let me catch errors at compile time, finding dynamic languages like Python a nightmare to work in. I'm finding with Haskell I can take this compile time checking even further than most static languages and it has gotten me rather excited. So I was wondering if there is a Haskell way of solving my problem.</div>

<div><br></div>I'm trying to represent an image made up of a list of strokes. Strokes are either lines, arcs or spots, and can be made using different pen shapes.<div><br></div><div>data Image = Image [Stroke]<br><div>

<br></div><div>data Stroke = Line Point Point PenShape</div><div>    | Arc Point Point Point PenShape</div><div>    | Spot Point PenShape</div><div><br></div><div>data PenShape = Circle Float</div><div>    | Rectangle Float Float</div>

<div>    | ArbitraryPen -- Stuff (not relevant)</div><div><br></div><div>And this is all great and works.</div><div><br></div><div>But now I have a problem. I want to extend this such that Arc strokes are only allowed to have the Circle pen shape, and Lines are only allowed to have the Rectangle or Circle pen shapes.</div>

<div><br></div><div>What is the best way of enforcing this in the type system.</div></div><div><br></div><div>I could make more Strokes like LineCircle, LineRectangle, Arc, PointCircle, PointRectangle, PointArbitrary and get rid of the PenShape type altogether. But this doesn't really feel good to me (and seems like the amount of work I have to do is bigger than it needs to be, especially if I added more basic pen shapes).</div>

<div><br></div><div>I thought about making the different PenShapes different types, using typeclasses and making Stroke an algebraic data type, but then my strokes would be of different types, and I wouldn't be able to have a list of strokes.</div>

<div><br></div><div>I have been looking at DataKinds and GADTs, but I can't quite figure out if they actually help me here at all.</div><div><br></div><div>I'm sure there is a way to do this, I'm just not googling properly.</div>

<div><br></div><div>What I want to write is...</div><div><br></div><div>data Image = Image [Stroke]<br><div><br></div><div>data Stroke = Line Point Point (Circle or Rectangle)</div><div>    | Arc Point Point Point Circle</div>

<div>    | Spot Point PenShape</div><div><br></div><div>data PenShape = Circle Float</div><div>    | Rectangle Float Float</div><div>    | ArbitraryPen -- Stuff (not relevant)</div></div><div><br></div><div>Regards,</div>

<div><br></div><div>Luke</div></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>