[Haskell-cafe] naturally, length :: a -> Int

Johannes Waldmann johannes.waldmann at htwk-leipzig.de
Tue Mar 2 14:53:34 UTC 2021

Dear Cafe,

thanks for ideas and arguments.

> [Ben Franksen] I would support a move Int->Word in all libraries
> where that makes sense.

I find it hard to think of a library where that does NOT make sense...

The following is somewhat related but actually orthogonal:
I mentioned that numbers often arise as cardinalities
(so they are natural, by definition) and serve as pointers.
The next step is then: pointer type safety. Vincent Hanquez' Foundation
https://hackage.haskell.org/package/foundation-0.0.25  defines

class (IsList c, Item c ~ Element c) => Collection c where
  length :: c -> CountOf (Element c)
class ( ... Collection c) => Sequential c where
  take :: CountOf (Element c) -> c -> c

with   newtype CountOf ty (and there's also   newtype Offset ty)

I tend to agree. The phantom type argument for CountOf
would catch errors like adding the number of students
to the number of exercises (in my application).

(Well and the next step after that would be to have
the size of the collection in the (dependent) type as well.)

Current implementation is  newtype CountOf ty = CountOf Int
and the author adds (and that was also the point I was making)
"Int is a terrible backing type which is hard  to get away from,
considering that GHC/Haskell are mostly using  this for [counting
and] offset. Trying to bring some sanity by a lightweight wrapping."

NB: I found out about this from Michael Snoyman's
(very much entertaining, and too much true)

Best regards, J.W.

More information about the Haskell-Cafe mailing list