The numhask-range package

[ Tags: bsd3, library, project ] [ Propose Tags ]

Numbers that represent ranges of all sorts.

[Skip to Readme]


Versions 0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.1.0, 0.1.1
Dependencies adjunctions (>=4.3 && <5), base (>=4.7 && <4.11), distributive (==0.5.*), numhask (>=0.0.9), protolude (>=0.1.8 && <0.3), QuickCheck (>=2.8 && <3), semigroupoids (>=5.1 && <6) [details]
License BSD3
Copyright Tony Day
Author Tony Day
Category project
Home page
Source repository head: git clone
Uploaded Sun Aug 27 00:09:45 UTC 2017 by tonyday567
Distributions LTSHaskell:0.0.4, NixOS:0.1.1, Stackage:0.1.1
Downloads 270 total (21 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-08-27 [all 1 reports]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for numhask-range-0.1.1

[back to package description]


Build Status Hackage lts nightly

A Space represents an interval over a type. The main instance of a Space, a Range, consists of a lower and upper value, though lower > upper is allowed, and leads to a useful definition of a negative space. A Rect is a wrapped Compose Pair Range and so a two-dimensional Space.

spatial zeros and ones

This library emerged as a bridge between chart-unit, an effort to create a clean, minimalist chart api, and numhask, an effort to create clean, minimalist numerical classes.

If you ask yourself what a chart is, sifting through the cruft of accumulated practice, nomenclature and usage, digging deep for charting's essence, and if you ask the question in haskell, here's what you find:


To a first approximation, charting is transforming unit squares and placing them on a physical XY plane, such as a screen, or on graph paper. A rectangle is a distended square; a line is a very thin rectangle; a histogram is a series of rectangles, and axes are nothing more than a collection of squares. The main thing on a chart that isn't a square is text, but even then we use square pixels to render.


As a well-meaning, but eternally confused student of category theory, I had learnt to pay attention to the simplest thing I could find within a problem domain. To quote from the haddock, a unitSquare is "a square with its center at the origin and sides of length 1, oriented parallel to the axes." When we first learn to chart, the origin of a graph is usually at the bottom left, and only moves to the center once we learn our negative numbers. The origin for html/svg/css is at top left, however, and the y-axis heads down not up. So what makes this co-ordinate system the right one?

Reducing down to the one dimension case, the diagrams unit boils down to a range along a dimension of -0.5 to 0.5, or Range -0.5 0.5. Length is 1 and the mid-point is 0, so if we define Range -0.5 0.5 as one, the multiplicative unit, we get the very neat:

mid one == zero
width one == one

which absolutely nails the correct co-ordinate system, once you see how it easily it can extend to the two-dimensional case:

mid (one :: Rect a) == zero :: Pair a
width (one :: Rect a) == one :: Pair a


As a card carrying member of the + and <> should be the same thing committee, I gravitated towards a monoidally additive definition:

zero = Range infinity neginfinity
(+) (Range l u) (Range l' u') = Range (min l l') (max u u')
(<>) = (+)
mempty = zero

Known as a convex hull union, this operation is the bread-and-butter of charting. If you have an object at Range 2 3 and one at Range 0 1, then you're going to have to draw over Range 0 3 to get it all on the page.

It's very similar to a tropical semiring, which sets infinity as zero, min as +, and + as , often summarised as (infinity,min,+) versus the usual (0,+,) semiring. Reading up on star-semirings here, I suspect that an operation that doesn't fill in the holes, that remembers contiguous and non-contiguous intervals in a space, will complete this mempty and plus definition to form a star-semiring. But the unification of charting and regular expressions is another tale.


If spatial one and zero are the inspiration of the library, then NumHask.Space is the perspiration. The Space class are all the various bits and bobs that made up earlier versions of chart operations, refactored a hundred times and slowly reduced to a managable and coherent class.

The Space class came out of common functionality between Range and Rect. If current trends continue, Space will consume the remaining components of the Range class. To effect this, however, requires the number heirarchy to be defined for the Space class, which currently leads to compiler whining about orphans, ambiguity and undecidables. It may be that the consumption of Range ideas will lead to the necessity of wrapping Space in a newtype and that wrapper name may best be Range. The grind continues.