[GHC] #14716: indexM-style accessor for arrays?

#14716: indexM-style accessor for arrays? -------------------------------------+------------------------------------- Reporter: Zemyla | Owner: (none) Type: feature | Status: new request | Priority: normal | Milestone: Component: libraries | Version: 8.2.2 (other) | Keywords: array | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Pretty much everything I've seen being done with basic `Array`s says something along the lines of "You have to force the argument; otherwise, you get a thunk that holds a reference to the array". An example of that is `fromArray` in `Data.Sequence`. But it really shouldn't be this way. The `Array` from `primitive` and the `Vector`s from `vector` have ways of obtaining a value from an array that doesn't leave a thunk hanging around. The basic prototype is {{{#!hs indexArrayM :: Monad m => Array a -> Int -> m a }}} And having this sort of thing available for standard `Array`s would be useful because `Array` is one of the first packages users see when they look for something like arrays in Haskell. Also, it's tagged `Trustworthy`, unlike `primitive` or `vector`, and there are references to them in `containers`, which is again a package lots of people use. It'd also mean that the `Functor`, `Foldable`, and `Traversable` instances don't leak memory. As a sort-of-related side note, we should also have a function `(!?)`, which functions like the similarly-named function in `vector`, and has the signature {{{#!hs (!?) :: (IArray a e, Ix i) => a i e -> i -> Maybe e }}} This would, in addition to not leaving a thunk when the `Maybe` is deconstructed, also eliminate a common use pattern when it comes to arrays: The user of `(!)` checks the bounds of the array, does something if it's outside, then passes the index to `(!)` which... checks the bounds again. Having a function which works like `lookup` on `Map`s would be a boon here. Having this work for all `IArray`s, not just `Array`, would require a new function in the `IArray` typeclass, which might break existing packages that derive a new `IArray`. However, the function added could be along the lines of: {{{#!hs unsafeAtM :: (Applicative f, Ix i) => a i e -> i -> f e unsafeAtM a i f = pure (unsafeAt a i) }}} which would be a default that, while it doesn't grant the benefits of using a monadic-type accessor, would ensure the code doesn't break while the packages that use/derive it are upgraded. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14716 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14716: indexM-style accessor for arrays? -------------------------------------+------------------------------------- Reporter: Zemyla | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: libraries | Version: 8.2.2 (other) | Resolution: | Keywords: array Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * cc: core-libraries-committee@… (added) Comment: Question for the Core Libraries Committee: What is the status of `Data.Array`? As part of the Report it seems like this module subtree would fall under the CLC's purview. Do additions require a libraries proposal? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14716#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14716: indexM-style accessor for arrays? -------------------------------------+------------------------------------- Reporter: Zemyla | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: libraries | Version: 8.2.2 (other) | Resolution: | Keywords: array Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by bgamari): Regarding the proposed idea, it seems quite reasonable to me. In general I would love to see `Data.Array`'s interface fleshed out to approach the completeness of, say, `vector`. It's currently needlessly painful to use. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14716#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14716: indexM-style accessor for arrays? -------------------------------------+------------------------------------- Reporter: Zemyla | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: libraries | Version: 8.2.2 (other) | Resolution: | Keywords: array Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by ekmett): From a personal opinion perspective, it seems to me like a good idea to help bring the APIs a little closer together here, with no particularly good reason not to do so coming to mind. Ben, `array` falls under the purview of the CLC. It is currently maintained by Dan Doel. He also maintains `vector` and `primitive`. https://wiki.haskell.org/Library_submissions#The_Libraries Zemyla, the library proposal process is the best way to get feedback on your proposal from the community at large, but you'll want to get Dan on board, as the change would run through him as maintainer. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14716#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14716: indexM-style accessor for arrays? -------------------------------------+------------------------------------- Reporter: Zemyla | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: libraries | Version: 8.2.2 (other) | Resolution: | Keywords: array Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by hvr): Somewhat related, in https://github.com/haskell/bytestring/pull/146 there's a minor bit of name-finding struggle on whether to introduce a `indexMaybe` or a `(!?)` verb for the total version of `index`... -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14716#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC