Skip to content

[Feature] Shape function to replace rows and columns - #147

Open
Sir-NoChill wants to merge 1 commit into
masterfrom
spec/gazprea/shape
Open

Sir-NoChill wants to merge 1 commit into
masterfrom
spec/gazprea/shape

Conversation

@Sir-NoChill

Copy link
Copy Markdown
Collaborator

This PR obliterates the length, rows and columns functions in favour of a generic 'shape' function that returns the shape of an array/vector type. It does not cross vector boundaries, so vectors always return their flat shape. It returns the full descriptor of statically shaped arrays, so shape(integer[2][3][4]) = [2 3 4]; // yes, that is invalid. Assume a variable of that type. This makes the runtime a bit cleaner and more aligned to what we currently have working for us.

This branch implements a change to the gazprea specification
eliminating the following functions:
- `length()`
- `rows()`
- `columns()`

And replaces them with a generic function that works for arbitrary
ranks: `shape()`.

The goal is to have a generic way to query any 'shaped' entity in
gazprea without needing to case on the exact rank and type.

The signature:

```gazprea
function shape(T shaped) returns integer[*];
```

The function takes as arguments an n-d array or vector and returns
the longest contiguous static shape of the shaped element. For
example:

```gazprea
integer[1][2][3] a;
shape(a) -> std_output; // prints '[1 2 3]'

integer[*] b = [[1, 2], [3, 4]];
shape(b) -> std_output; // prints '[2 2]'

vector<integer[2][3]> c;
c.append([[2, 3, 4], [4, 6, 8]]);
shape(c) -> std_output; //prints '[1 2 3]'

vector<vector<integer[*]>> d = [[[1, 2]]];
shape(d) -> std_output; // prints '[1]', vectors
   // generally cannot look inside other vectors to determine
   // shape, since multiple vectors could lead to a ragged
   // array, which is not efficiently expressible in this
   // format
```

docs(gazprea): clarify shape() semantics for nested collections

Add explicit examples and documentation clarifying that shape() reports
only the immediate collection's dimensions, not nested element types:

- shape(vector<integer[2][3]>) returns [length], not [length, 2, 3]
- shape(vector<integer>[3][2]) returns [3, 2], the array extents
- To query element shapes, index first then call shape: shape(a[0][0])

This simplifies the implementation and clarifies the contract: shape()
returns the dimensions of what you pass it, nothing more. Nested
dimensions are accessed by indexing into the collection.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant