+ map index (range (l,u)) == [0..rangeSize (l,u)] ++This documents an invariant that was previously only implicit. + +
+ class Ix a where + range :: (a,a) -> [a] + index :: (a,a) -> a -> Int + inRange :: (a,a) -> a -> Bool + rangeSize :: (a,a) -> Int ++
+ class Ix a where + range :: (a,a) -> [a] + index :: (a,a) -> a -> Int + inRange :: (a,a) -> a -> Bool + rangeSize :: (a,a) -> Int + + rangeSize b@(l,h) | null (range b) = 0 + -- NB: replacing "null (range b)" by "not (l <= h)" + -- fails if the bounds are tuples. For example, + -- (1,2) <= (2,1) + -- but the range is nevertheless empty + -- range ((1,2),(2,1)) = [] ++