[Haskell-cafe] selectively allow alternatives in a sum type

Olaf Klinke olf at aatal-apotheke.de
Wed Aug 31 20:48:02 UTC 2022


> Trees that grow is essentially this, but using type families.
> 
>     data Foo ksi
>         = LeftFoo !(XLeftFoo ksi) !A
>         | RightFoo !(XRightFoo ksi) !B
> 
>     type family XLeftFoo ksi :: Type
>     type family XRightFoo ksi :: Type
> 
> Then you can define
> 
>     data Both
>     type instance XLeftFoo  Both = ()
>     type instance XRightFoo Both = ()
> 
> or
> 
>     data OnlyLeft
>     type instance XLeftFoo  OnlyLeft = ()
>     type instance XRightFoo OnlyLeft = Void
> 
> ---
> 
> Third option is to simply have parametrized data:
> 
>    data Foo b
>        = LeftFoo !A
>        | RIghtFoo !b
> 
>    type FooBoth = Foo B
>    type FooOnlyLeft = Foo Void
> 
> ---
> 
> Sometimes I prefer a higher-kinded data approach, especially if there is
> very little variants needed, and they are "uniform" (e.g. second variant
> doesn't have some fields).
> Yet, sometimes simple parameterized data is simplest approach
> (especially as you get stuff for free by deriving Traversable!).
> 
> On the other hand, if type is big and have many uniform extension points
> (even if there are few different combinations), then HKD becomes
> boilerplate heavy as well.
> The drawback of TTG, is that writing polymorphic code (i.e. which work
> for any or some `ksi`s) is not very fun: a lot of equality constraints etc.
> 
> - Oleg

So I re-discovered Trees that Grow sans the trick of using type
families to name particular combinations of parameters. Hooray Najd,
and thanks Oleg for bringing this up. 

Olaf



More information about the Haskell-Cafe mailing list