[Haskell-cafe] "Bounds checking" pragma?

Daniel Fischer daniel.is.fischer at googlemail.com
Thu Nov 10 00:08:30 CET 2011


On Thursday 10 November 2011, 00:35:07, Artyom Kazak wrote:
> Hello!
> 
> The (!) operator is short and nice. Unfortunately, when doing heavy
> computing, we have to use "unsafeAt" instead. It looks ugly and it is
> ugly, also.
> 
> Some compilers for imperative languages like Free Pascal have an option
> to turn on/off bounds checking for arrays. Wouldn't it be nice to have
> such option in GHC? Is it possible?
> 
> There is a problem: Haskell has a lot of array libraries. The only
> solution I see is a new FLAG pragma:
> 
> 	(!) :: Array i a -> i -> a
>          --definition
> 
>          {-# FLAG "boundsCheck"     (!) = unsafeAt #-}

There's a problem here, unsafeAt uses an Int index into the array, while 
(!) uses the declared index type. Even skipping the bounds check, you'd 
still have to calculate the Int index for the replacement of (!).

> 
> It is similar to RULES pragma, but only fires when flag is set. To set
> the flag you need to complile with option -flags="boundsCheck". Also,
> the mantainers of vector library, bytestring library, repa library and
> so on will have to include such pragmas in their code.
> 
> I don't know about C++ preprocessor, though. Maybe this is already
> solvable with #define's...

#ifdef OMIT_BOUNDS_CHECK
{-# RULES
"ArrayIndex"  arr ! i = unsafeAt arr (unsafeIndex (bounds arr) i) 
 #-}
#endif

> 
> Anyway, I have to say it once again: unsafeAt is ugly and Haskell is
> beautiful. Why high-performance code should be ugly?

(?) = unsafeAt




More information about the Haskell-Cafe mailing list