[Haskell-cafe] bounded ranges

Henning Thielemann lemming at henning-thielemann.de
Thu Jul 22 18:38:43 EDT 2010


On Thu, 22 Jul 2010, Chad Scherrer wrote:

> Hello cafe,
>
> I'm trying to do some things with bounded indices so I can carry
> around arrays (well, Vectors, really) without needing to refer to the
> bounds.
>
> For example, if I know my indices are Bool values, I can do
>
>> rangeSize (minBound, maxBound :: Bool)
> 2
>
> I'd like to be able to do this in general, but...
>> :t rangeSize (minBound, maxBound)
> <interactive>:1:11:
>    Ambiguous type variable `a' in the constraints:
>      `Bounded a'
>        arising from a use of `minBound' at <interactive>:1:11-18
>      `Ix a' arising from a use of `rangeSize' at <interactive>:1:0-29
>    Probable fix: add a type signature that fixes these type variable(s)
>
> I thought it might help to put it into a module and do a better job
> with the type, like this:
>
> bdRangeSize :: (Ix i, Bounded i) => i -> Int
> bdRangeSize _ = rangeSize (minBound, maxBound :: i)


bdRangeSize x = rangeSize (minBound, maxBound `asTypeOf` x)



More information about the Haskell-Cafe mailing list