[Haskell-cafe] OverloadedStrings, but with compile-time invariants

Henning Thielemann lemming at henning-thielemann.de
Fri Dec 8 16:08:53 UTC 2023


On Fri, 8 Dec 2023, Max Ulidtko wrote:

> Consider a type called NonEmptyText which is a (wrapper around) Text, 
> but with an added invariant that the contained text is never the empty 
> string "".

Maybe that is too much to expect from a string literal syntax?

I have no convincing solution for your string example, but I got a nice 
one for general (non-Char) non-empty lists. Analogous to your example you 
might ask for overloaded list syntax for NonEmpty lists. We are so used to 
the list syntax with comma separation, that we have mostly forgotten about 
the list constructor (:). Instead of [1,2,3] we could write 1:2:3:[]. With 
this syntax it is also easy to write a type checked NonEmpty list this 
way: 1:|2:3:[]. With my non-empty package you can even write lists with 
any required minimum number of elements, like 1!:2!:3:4:5:[].

https://wiki.haskell.org/List_notation


More information about the Haskell-Cafe mailing list