[Haskell-cafe] heterogeneous container smart constructor question

Henning Thielemann lemming at henning-thielemann.de
Tue Sep 5 21:40:50 UTC 2023


On Tue, 5 Sep 2023, Zoran BoĆĄnjak wrote:

> The example I gave was simplified. A real problem contains several other 
> complications which makes this simple solution not practical.

I have the package non-empty, that provides the types NonEmpty.T, 
Optional.T and Empty.T. With them you can encode any pattern of allowed 
list lengths. NonEmpty means "list length forbidden", Optional means "list 
length permitted", Empty means "maximum allowed length".

In your example length 2, 5, 8 were allowed, so the bit pattern of allowed 
lengths is 001001001, which you can encode by

type B0 = NonEmpty.T
type B1 = Optional.T

type SpecialList = B0 (B0 (B1 (B0 (B0 (B1 (B0 (B0 (B1 Empty.T))))))))


https://hackage.haskell.org/package/non-empty

Maybe you can adapt this idea to your problem.


More information about the Haskell-Cafe mailing list