[Haskell-cafe] "Bounds checking" pragma?

Artyom Kazak artyom.kazak at gmail.com
Thu Nov 10 00:35:07 CET 2011


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 #-}

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...

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



More information about the Haskell-Cafe mailing list