[Haskell-cafe] Managing a sum type with a lot of constructors

David Turner dct25-561bs at mythic-beasts.com
Thu Aug 24 07:45:18 UTC 2017


Hi,

We have a sum type of 151 constructors (and growing) in one project. There
is a somewhat natural grouping of the constructors into 24 groups (with
between 1 and 32 constructors in each group) so we used that to break it
down into the two levels:

data Group1 = Ctor1 Int | Ctor2 Bool | ...
data Group2 = Ctor5 String | Ctor6 Double | ...
...
data A = Group1 Group1 | Group2 Group2 | ...

This wasn't enough, so then we cheated and wrote some code-gen. The
datatype is described as data (think YAML or JSON) and then there's a short
program which generates the declarations, including Haddock comments, and
various useful functions such as somewhat-custom JSON serialisation. Each
group gets its own module, which gives faster recompilation on changes. We
could have used TemplateHaskell, except we wouldn't have got such nice
Haddock docs (and, ew, TemplateHaskell) and it would all have had to have
been in one module.

Hope that helps,

David



On 24 August 2017 at 02:29, Adam Flott <adam at adamflott.com> wrote:

> I have a sum type with a lot of constructors and I'm not sure how to
> represent
> the type with maintainability in mind. For example,
>
>     data A = A1
>            | A2 Int
>            | A3 Text Int32 Bool
>            | ...
>            | A100 Bool
>
> Every inner type is concrete. There are 100+ constructors with no sign of
> ever
> getting reduced.
>
> What technique would you recommend to keep the sum type approach but not
> having
> to define them all in one spot? I'm thinking 1 inner type + 1 function to
> construct per file (if that's possible).
> _______________________________________________
> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170824/795ed3a0/attachment.html>


More information about the Haskell-Cafe mailing list