Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static bounds checks on Haskell arrays

Tags:

arrays

haskell

Is there any way to get static checks on Haskell arrays? Let's take this code:

import Data.Array
let a = listArray (0, 10) [-3.969683028665376e+01, 2.209460984245205e+02, -2.759285104469687e+02, 1.383577518672690e+02, -3.066479806614716e+01, 2.506628277459239e+00]

(0, 10) should really be (0, 5), but the compiler accepts the code. The error is only detected at runtime, despite the fact that it could be detected at compile-time.

like image 413
quant_dev Avatar asked Mar 17 '12 18:03

quant_dev


People also ask

Can array bounds checking be performed statically?

Yes, checking if the index is within the bounds of the array.

What does array bounds checking mean?

Array bound checking refers to determining whether all array references in a program are within. their declared ranges. This checking is critical for software verification and validation because. subscripting arrays beyond their declared sizes may produce unexpected results, security holes, or failures.

Does C have bounds checking for arrays?

Bounds checking is easy for arrays because the array subscript syntax specifies both the address calculation and the array within which the resulting pointer should point. With pointers in C, a pointer can be used in a context divorced from the name of the storage region for which it is valid.


1 Answers

This cannot be detected at compile-time, because there's nothing in the list's type that saves its size, so the listArray function cannot possibly perform such checks. Also, if the data came from an external file (for example), it would be very difficult to get the static size checking to work.

You need a dependent type system such as the one you find in Agda for things like this to be possible.

like image 60
dflemstr Avatar answered Oct 19 '22 09:10

dflemstr