[Haskell-cafe] bounded ranges

Sebastian Fischer sebf at informatik.uni-kiel.de
Thu Jul 22 19:01:03 EDT 2010


An alternative to the `asTypeOf` idiom, which is also Haskell 98, is  
to use a newtype instead of a dummy argument:

     newtype RangeSize i = RangeSize { getRangeSize :: Int }

     boundedRangeSize :: Ix i => (i,i) -> RangeSize i
     boundedRangeSize = RangeSize . rangeSize

     bdRangeSize :: (Ix i, Bounded i) => RangeSize i
     bdRangeSize = boundedRangeSize (minBound, maxBound)

Now you can call

     ghci> getRangeSize (bdRangeSize :: RangeSize Bool)
     2

without language extensions and avoid the extra applications due to  
the dummy argument and the `asTypeOf` call.

Sebastian


-- 
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)





More information about the Haskell-Cafe mailing list