2009-06-06

ANNOUNCE: numtype 1.0 — Type-level (low cardinality) integers

Since its inception my dimensional library has been built around a unary type-level representation of integers (NumTypes) defined in the Numeric.NumType module. This module has proven itself useful outside the context of dimensional and after dragging my feet for a long time I’ve finally gotten around packaging it up in its own library: numtype.

The Numeric.NumType module is completely self-contained (only imports Prelude) and is heavily commented in a narrative manner inspired by Oleg Kiselyov’s expositions. I believe it provides a good case study for type-level programming with multi-parameter type classes (MPTCs) and functional dependencies.

Addition, subtraction, division, and multiplication of NumTypes is supported. NumTypes have no value-level representation but can be converted to any Num instance with the toNum function.

The numtype library has two significant short-comings:

  • Minimal haddocks — as with my dimensional library the literate Haskell source code is the documentation. The flip-side is that the code is very well-commented.

  • Due to the unary implementation the practical size of the NumTypes is severely limited, making them unsuitable for large-cardinality applications. If you will be working with integers beyond (–20, 20) this package probably isn’t for you.

(If the second bullet is a show-stopper Edward Kmett’s type-int library may be a better choice. Peter Gavin’s tfp library also provides type-level integers but uses type families instead of MPTCs and fundeps. I cannot vouch for either of these libraries as I haven’t used them.)

Numtype version 1.0 can be downloaded from Hackage or the dimensional project page. I’ve also updated dimensional to version 0.8 with the Numeric.NumType module removed and Julian year and century units added. Enjoy!

2 comments:

  1. There's also 'type-level' from Oleg & co and the 'typical' package. Methinks we (the Hackage community) need to do some standardization/merging.

    ReplyDelete
  2. Porges: to my understanding (from the package description) type-level is limited to natural numbers and the same appears to be true of typical (and e.g. the HNats of Oleg's HList). That being said, I agree that a de-facto standard for this type of stuff would be nice. If I had found another implementation back when I wrote Numeric.NumType (in 2006) I wouldn't have bothered writing my own.

    ReplyDelete