[Haskell-cafe] Restrict values in type

Luke Clifton ltclifton at gmail.com
Mon Jan 13 04:09:17 UTC 2014


Hi Carter,

Yes, I have seen the diagrams project, and in fact am hoping to use them
when I actually get to rendering.

Perhaps, I should have provided more info to try and explain why I am doing
this.

I am trying to implement a Gerber file viewer (and maybe editor... we'll
see)

http://www.ucamco.com/Portals/0/Public/The_Gerber_File_Format_Specification.pdf

I am using parsec to parse the gerber format and build my gerber data type
so that I can make modifications to it, and write it back out.

I'll take a closer look at diagrams source and see if I can come up with
some inspiration.

Thanks,

Luke



On Mon, Jan 13, 2014 at 11:55 AM, Carter Schonwald <
carter.schonwald at gmail.com> wrote:

> Hey Luke,
> have you seen the diagrams project? http://projects.haskell.org/diagrams/
> they've struggled through some of the same problems, and they've worked
> very hard to write a power user friendly expressive DSL lib for that
> problem domain.
> check it out!
> -Carter
>
>
> On Sun, Jan 12, 2014 at 10:38 PM, Luke Clifton <ltclifton at gmail.com>wrote:
>
>> Hi,
>>
>> 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.
>>
>> 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.
>>
>> data Image = Image [Stroke]
>>
>> data Stroke = Line Point Point PenShape
>>     | Arc Point Point Point PenShape
>>     | Spot Point PenShape
>>
>> data PenShape = Circle Float
>>     | Rectangle Float Float
>>     | ArbitraryPen -- Stuff (not relevant)
>>
>> And this is all great and works.
>>
>> 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.
>>
>> What is the best way of enforcing this in the type system.
>>
>> 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).
>>
>> 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.
>>
>> I have been looking at DataKinds and GADTs, but I can't quite figure out
>> if they actually help me here at all.
>>
>> I'm sure there is a way to do this, I'm just not googling properly.
>>
>> What I want to write is...
>>
>> data Image = Image [Stroke]
>>
>> data Stroke = Line Point Point (Circle or Rectangle)
>>     | Arc Point Point Point Circle
>>     | Spot Point PenShape
>>
>> data PenShape = Circle Float
>>     | Rectangle Float Float
>>     | ArbitraryPen -- Stuff (not relevant)
>>
>> Regards,
>>
>> Luke
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140113/d9e8e76f/attachment.html>


More information about the Haskell-Cafe mailing list