better way to create Array defined on all indices

I'd like to have an Array indices values (see http://haskell.org/onlinereport/array.html) defined on [minBound::indices ... maxBound::indices]. The attached does this; however, it also allows duplicate definitions (e.g. in the attached, the value for Term index is defined twice). Is there a better way to do this? Maybe a way that doesn't use all the contexts? Also, is there a way to do it so that the value associated with an index is not redefined? In effect, I'm making a map, but using an Array as the implementation because, I guess, it would be a bit faster. TIA.

On 10/15/08 16:05, Larry Evans wrote:
I'd like to have an Array indices values (see http://haskell.org/onlinereport/array.html) defined on [minBound::indices ... maxBound::indices]. The attached does this; however, it also allows duplicate definitions (e.g. in the attached, the value for Term index is defined twice).
Is there a better way to do this? Maybe a way that doesn't use all the contexts? Also, is there a way to do it so that the value associated with an index is not redefined? The aforementioned array.html has:
_ -> error "Array.!: \ \multiply defined array element") suggesting it should detect redefinitions; however, the library on my system apparently doesn't do that. It will detect missing defintions with the same error found in array.html: [] -> error "Array.!: \ \undefined array element" Is this a bug in my ghc library? I'n on ubuntu and synaptic shows the ghc6 version as: 6.8.2-2ubuntu1

On 10/15/08 23:50, Larry Evans wrote:
On 10/15/08 16:05, Larry Evans wrote:
I'd like to have an Array indices values (see http://haskell.org/onlinereport/array.html) defined on [minBound::indices ... maxBound::indices]. [snip] The aforementioned array.html has:
_ -> error "Array.!: \ \multiply defined array element")
suggesting it should detect redefinitions;
Is this a bug in my ghc library? According to:
http://www.haskell.org/ghc/docs/6.6-latest/html/libraries/base/Data-Array.ht... it's not. It's just implemented differently by libraries:
The Haskell 98 Report further specifies that if any two associations in the list have the same index, the value at that index is undefined (i.e. bottom). However in GHC's implementation, the value at such an index is the value part of the last association with that index in the list. So, if I want multiple definitions diagnosed as an error, I'll have to do it myself :(

Am Mittwoch, 15. Oktober 2008 23:05 schrieb Larry Evans:
I'd like to have an Array indices values (see http://haskell.org/onlinereport/array.html) defined on [minBound::indices ... maxBound::indices]. The attached does this; however, it also allows duplicate definitions (e.g. in the attached, the value for Term index is defined twice).
Is there a better way to do this? Maybe a way that doesn't use all the contexts?
I don't know of a better way to do it, your way is fine. What do you mean by "contexts" here? Of course, Bounded and Ix are necessary to define array_complete. If you want to perhaps leave some array entries undefined, there's no problem with that, as long as you don't try to access an undefined entry later.
Also, is there a way to do it so that the value associated with an index is not redefined?
From GHC's user's guide, section 13.1.1.6: GHC's implementation of array takes the value of an array slot from the last (index,value) pair in the list, and does no checking for duplicates. The reason for this is efficiency, pure and simple. If you want to throw an error for duplicate definitions, you have to scan the eqs list manually before passing it to array.
In effect, I'm making a map, but using an Array as the implementation because, I guess, it would be a bit faster.
TIA.
Cheers, Daniel
participants (2)
-
Daniel Fischer
-
Larry Evans