"Bounds checking" pragma?

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?

Hi, Am Donnerstag, den 10.11.2011, 01:35 +0200 schrieb Artyom Kazak:
Anyway, I have to say it once again: unsafeAt is ugly and Haskell is beautiful. Why high-performance code should be ugly?
It does not have to be ugly. Just write (!!) = unsafeAt in some common module of yours, and you have a nice fast array access. I don’t think a flag would be very helpful, because you might have some places where you want the safe access, and others where you know that it is safe to skip the check. Greetings, Joachim -- Joachim "nomeata" Breitner mail@joachim-breitner.de | nomeata@debian.org | GPG: 0x4743206C xmpp: nomeata@joachim-breitner.de | http://www.joachim-breitner.de/

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

2011/11/10 Daniel Fischer
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 (!).
#ifdef OMIT_BOUNDS_CHECK {-# RULES "ArrayIndex" arr ! i = unsafeAt arr (unsafeIndex (bounds arr) i) #-} #endif
Thanks!
(?) = unsafeAt
Yes, but (!) was taken as an example. There is a lot of other functions doing bounds checking... Creating replacements for every single function is rather tedious. The flag pragma also has other uses. For example, we can introduce an "overflow" flag (check every operation with Int's for overflow) , which may be useful for debugging. In release version this flag will be turned off.
participants (3)
-
Artyom Kazak
-
Daniel Fischer
-
Joachim Breitner