Skip to content

Shapes

About shapes

There are two kinds of types: basic and compound.

Structures

Basic types

A basic type is a collection of items with a homogeneous type. Therefore, the data of a basic type is stored in sequential memory, called vector.

Here is an example of an integer vector which contains three integers: 1 2 3:i64.

+-----+-----+-----+
|  1  |  2  |  3  |
+-----+-----+-----+

Compound Types

A compound type, which is a collection of items which may have non-homogeneous types, is a list-based type. A list consists of many cells. A cell can be either a vector or a sub-list.

Here is an example of a list with ("abc"; 1 2 3) below. (type: list<?>)

+--------+--------+
| cell 0 | cell 1 |
+--------+--------+
  /         \
+-----+  +-----+-----+-----+
|"abc"|  |  1  |  2  |  3  |
+-----+  +-----+-----+-----+

A special case: all list cell has the same type: (1 2;3 4 5) (type: list<i64>)

+--------+--------+
| cell 0 | cell 1 |
+--------+--------+
  /           \
+-----+-----+-----+-----+-----+
|  1  |  2  |  3  |  4  |  5  |
+-----+-----+-----+-----+-----+

Note: Logically, two cells are separate, however, they are placed sequentially in memory.

Shape Rules

Conventions

Alias Name Type Notes
S scalar vector length = 1
V non-scalar vector length \neq 1
L list list two lists: L1, L2
op primitive operator

Monadic Elementwise

  • Return the same length as the input operand
op(S) => S
op(V) => V

Reduction

  • If list, reduce on the first level of the list
op(S) => 1
op(V) => 1
op(L) => length of L

Dyadic Elementwise

  • Both operands operate on vector types
op(S, S) => S
op(S, V) => V
op(V, S) => V
op(V, V) => V  //length agree on both sides

Dyadic List Based

  • With two inputs: L1 and L2
each_left (L1, L2) => L1
each_right(L1, L2) => L2
each_item (L1, L2) => L1  //length agree on both sides
  • If neither L1 nor L2 is list, the list-based operation is ignored;
  • If one side (left/right) is chosen and the corresponding operand is a list, the first-level items of the list are iterated in order;
  • If one side (left/right) is chosen and the corresponding operand is not a list, the operand is processed as a whole.

Shape Append

  • append: concatenate two items
append(S, S) => V
append(S, V) => V
append(V, S) => V
append(V, V) => V
append(L, S) => L
append(L, V) => L
append(S, L) => L
append(V, L) => L
append(L, L) => L

Shape Left

  • Return left shape

Shape Right

  • Return right shape

Shape Left Value

  • The length of the return shape is determined by the value of the left parameter.