length :: forall f a n. Foldable f => Semiring n => (f a) -> n
`length` is not specific to any one type and it's a complete waste to think of it as something to do with strings in general. They should all just be foldable and that'll give way more.Likewise, they could all just be functors and `map` is free.
What happens if you feel the urge to define several Semirings (or Monoids) on the data type?
For the second question, traditional (+,×,...) is a Semiring for integers; so is (&,|,...) (bitwise boolean operations). How can you define both Semiring implementations without getting them confused?
There is another reply to my comment that I haven't digested yet. Maybe it answers the second.
And I'm wrong about that. You need a zero and a one, right. What would that structure be?
An extremely heavily-commented approach to defining equality for things like Double (where there are multiple sensible ways to compare things) will probably be a better introduction to these ideas:
https://github.com/mrkgnao/noether/blob/master/library/Noeth...
https://github.com/mrkgnao/noether/blob/master/library/Noeth...
The rest of the repo implements abstract algebraic structures along similar lines.
Here is what monoids look like in this formalism at present. It's a bit involved: first I use a fine-grained separation into highly polymorphic Magma and Neutral classes:
https://github.com/mrkgnao/noether/blob/master/library/Noeth...
And there are "strategies" for combining those pieces of structure, whose use looks like
type instance MonoidS (op :: BinaryNumeric) Rational
= DeriveMonoid_Semigroup_Neutral op Rational
Which means "derive the preferred monoid structure with operation op" (MonoidS op) on Rational from the preferred semigroup structure and preferred neutral element on it with that operation.https://github.com/mrkgnao/noether/blob/master/library/Noeth...
You can always use some other monoid structure for the same combination of type and operation (!) if you like. For instance, here's a demo of using the self-action of a ring on itself (i.e. considering a ring as a module over itself) to compute something, even if your preferred choice of action has been declared to be something else (or hasn't been defined at all).
https://github.com/mrkgnao/noether/blob/master/library/Noeth...
This says "here, use the self-action derived from the multiplicative magma structure on R".
A list of ASCI chars obviously does (the length of the list), but I'm not sure about Data.Text.Text.
https://hackage.haskell.org/package/text-1.2.2.2/docs/Data-T...
https://hackage.haskell.org/package/bytestring-0.10.8.1/docs...
If it's just counting how many u8's in a slice, that's trivial. But storing the total number of variable-length graphemes isn't. I assume that's why Data.Text's length function is O(n), and other languages that have UTF-16 or UTF-8 strings also have length functions that are linear.
This in theory. It is readily apparent from the Data.Text page at https://hackage.haskell.org/package/text-1.2.2.2/docs/Data-T... that the authors care about performance only selectively ("fusion" of buffer allocations is fun, relatively messy data structures to cache important data such as string length are not fun), and that they don't care enough about Unicode to add serious string-level abstractions over the Unicode tables exposed in the Char type.
Data.Text seems to recognize symbols made of several codepoints (like emoji) as one but still counts diacritics and combining characters as different symbols.
Or are you suggesting that we specialize the TCP receive function to only receive UTF-8 strings, so we don't have to convert when displaying to the user?
See: http://blog.ezyang.com/2016/09/the-base-of-a-string-theory-f...
By the way the link to the caesar cipher is broken.