Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More concise Bigarray.Genarray indexing syntax wanted #7757

Closed
vicuna opened this issue Mar 23, 2018 · 9 comments
Closed

More concise Bigarray.Genarray indexing syntax wanted #7757

vicuna opened this issue Mar 23, 2018 · 9 comments

Comments

@vicuna
Copy link

vicuna commented Mar 23, 2018

Original bug ID: 7757
Reporter: nbb
Status: new
Resolution: open
Priority: normal
Severity: feature
Version: 4.06.1
Category: lexing and parsing
Monitored by: @Drup @hcarty

Bug description

In numerical and array-oriented computing it is quite common to manipulate arrays by slicing, subdividing or reshaping them liberally. Under these operations, their dimensionality changes. For instance, an array of shape (4,3,5) may get summed along the dimension 1, resulting in a shape (4,5), then reshaped to one dimension (20), then partitioned in pairs (10,2) etc.

It is therefore often useful to use Bigarray.Genarray.t, to allow for reshaping. Thus Genarray.t should be seen as a workhorse not as a rare high-dimensional exceptional case. For Genarray.t, the basic indexing operation presently has a heavy syntax except for the case where the dimensionality is >3: only then, x.{i,j,k,l} works.

Desired is an indexing syntax for Genarray.t that is a short as possible. For instance, x.?{i; j; k} where ? is either no character, or one, or any of the custom-indexing characters supported in 4.06.

One way to achieve that would be to parse x.?{i; j; ...} as x.?{[|i; j; ...|]} as a general rule. This would disallow arbitrary OCaml code inside the braces. Alternatively, this would only be parsed when the '?' takes a specific value, e.g. %. Another less invasive way might be to specifically attach a new custom indexing syntax to Genarray.

Steps to reproduce

Presently, in the Owl library, custom indexing is used to provide the syntax x.%{[|i;j|]}. This is rather cumbersome for such a common operation. Motivated by this existing use, % would be a preferred choice for a special custom indexing character.

Additional information

for background see here:

http://www.cl.cam.ac.uk/~lw525/owl/chapter/slicing.html

where indexing appears around the middle of the page.

also,

https://discuss.ocaml.org/t/capturing-the-bigarray-indexing-syntax/1762/3

@vicuna
Copy link
Author

vicuna commented Mar 23, 2018

Comment author: nbb

Even nicer would be the syntax x.{i, j, ...} for all dimensionalities for (x:Genarray.t). This would require providing a way of shadowing the Bigarray.Array{1,2,3} syntax. The shadowing could be acceptable in an environment where Genarray.t is used pervasively.

@vicuna
Copy link
Author

vicuna commented Mar 27, 2018

Comment author: nbb

User-defined data types may also benefit from the proposed abbreviated indexing syntax, not only Genarray.t. If a shortened syntax gets included in OCaml, I think it's better to make it user-accessible.

Case in point: in Owl, there is a View.t datatype which is basically a mutable, shared-data copy of a subarray(slice) of an underlying Genarray.t. Clearly, for View.t the same indexing syntax would be needed.

http://www.cl.cam.ac.uk/~lw525/owl/apidoc/owl_view.html

@vicuna
Copy link
Author

vicuna commented Mar 27, 2018

Comment author: @Octachron

To clarify my thoughys: in 4.06, x.%{1;2;3;4} is not syntactically valid. This point is currently fixed in trunk by parsing it as (.%{}) x (1;2;3;4). However, it would be also possible to resurrect the multi-index syntax and parse x.%{1;2;3;4} as (.%{;..;}) x [|1;2;3;4|] (or [1;2;3;4]?). Since the syntax is invalid in 4.06, it will be guaranteed to not break any existing code and it would avoid numerical libraries hijacking the bigarray modules or writing a custom ppx.

@vicuna
Copy link
Author

vicuna commented Mar 29, 2018

Comment author: @Drup

I think this is a nice solution. You should definitely try to squeeze that in before the next release.

@vicuna
Copy link
Author

vicuna commented Mar 29, 2018

Comment author: nbb

is what octachron described specific to the '.%{}' indexing or is it the same for all indexing characters (e.g. also '$.{}')? in principle one could choose one character which gets parsed as [|1;2;3|] and another one which parses as [1;2;3] inside the curly braces. not sure this added complexity is worth while?

@vicuna
Copy link
Author

vicuna commented Mar 29, 2018

Comment author: @Octachron

I don't think differentiating on intermediary characters is worth the added complexity, I think it is better to stick to the rule that the first character of an operator determines its syntactic behavior (even if "**" is an exception to this rule). If it is absolutely necessary to have both list and array multi-index, I would rather distinguish on the parenthesis kind, since being able to read "x .! [1;2]" as "x" ".!" "[1;2]" would be nice.

@nilsbecker
Copy link

this will be resolved when GPR #1726 lands.

@damiendoligez
Copy link
Member

#1726 has landed. What is the status of this feature wish now? Can we close it?

@Octachron
Copy link
Member

We can close it for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants