Skip to content

Commit 83ed35a

Browse files
gh-5: Add TNS operators.
1 parent 56e8400 commit 83ed35a

3 files changed

Lines changed: 1195 additions & 4 deletions

File tree

docs/SPECIFICATION.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,14 +674,14 @@
674674
675675
- `REPLACE(STR: string, STR: a, STR: b):STR` — Returns a `STR` formed by replacing every occurrence of the substring `a` in `string` with `b`. The `a` argument must be a non-empty string; supplying an empty `a` raises a runtime error.
676676
677+
- `FLIP(INT|STR: obj):INT|STR` — For `INT` input, returns an `INT` whose binary-digit spelling is the reverse of the absolute-value binary spelling of `obj` (sign is preserved). For `STR` input, returns the character-reversed `STR`.
678+
677679
### 12.8 Tensor operations
678680
679681
- `SHAPE(TNS: tensor):TNS` — Returns the tensor's shape as a 1D `TNS` (vector) of `INT` lengths (one entry per dimension).
680682
681683
- `TLEN(TNS: tensor, INT: dim):INT` — Returns the length of the specified 1-based dimension. Errors if `dim` is out of range.
682684
683-
- `FLIP(INT|STR: obj):INT|STR` — For `INT` input, returns an `INT` whose binary-digit spelling is the reverse of the absolute-value binary spelling of `obj` (sign is preserved). For `STR` input, returns the character-reversed `STR`.
684-
685685
- `TFLIP(TNS: obj, INT: dim):TNS` — Returns a new `TNS` with the elements along 1-based dimension `dim` reversed. Errors if `dim` is out of range.
686686
687687
- `SCAT(TNS: src, TNS: dst, TNS: ind):TNS` — Returns a copy of `dst` with a rectangular slice replaced by `src`. `ind` must be a 2D tensor of `INT` pairs with shape `[TLEN(dst, 1), 10]` (binary `10` = decimal 2), i.e., one `[lo, hi]` row per destination dimension (rank; for example `rank = TLEN(SHAPE(dst), 1)`). Indices are 1-based; negatives follow the tensor indexing rules (for example, `-1` is the last element) and `0` is invalid. For each dimension, the inclusive span `hi - lo + 1` must equal the corresponding `src` dimension length, and all bounds must fall within `dst`. Elements outside the slice are copied from `dst` unchanged.
@@ -696,7 +696,7 @@
696696
697697
- Backward-compatible two-argument form: when called as `CONV(x, kernel)` the operator performs the original N-dimensional discrete convolution and returns a tensor with the same shape as `x`. Requirements and semantics from the original behavior remain unchanged: `kernel` must have the same rank as `x`, every kernel dimension length must be odd (so the kernel has a well-defined center), boundary sampling is clamped to the nearest valid index (replicate padding), and tensor element types must be uniformly `INT` or uniformly `FLT` within each tensor. If both tensors are `INT` the output is `INT`, otherwise the output is `FLT`.
698698
699-
- 2-D multi-output extension (keyword form): when called with any of the keywords `stride_w`, `stride_h`, `pad_w`, `pad_h`, or `bias`, and when the input `x` is a 3-D WHC tensor (width, height, channels) and `kernel` is a 4-D tensor of shape `[kw, kh, in_c, out_c]`, `CONV` performs a multi-output 2-D convolution equivalent to the `CONV2D` helper in the standard library. In this mode:
699+
- 2-D multi-output extension (keyword form): when called with any of the keywords `stride_w`, `stride_h`, `pad_w`, `pad_h`, or `bias`, and when the input `x` is a 3-D WHC tensor (width, height, channels) and `kernel` is a 4-D tensor of shape `[kw, kh, in_c, out_c]`, `CONV` performs a multi-output 2-D convolution. In this mode:
700700
701701
- `pad_w` / `pad_h` specify zero-padding added to both sides of the width/height dimensions (padding value = 0). The padded input size is `in_w + 2*pad_w` by `in_h + 2*pad_h`.
702702
@@ -720,7 +720,7 @@
720720
721721
- `MPROD(TNS: t1, ..., TNS: tN):TNS` — Elementwise product across tensors. Shapes must match; elements must be all `INT` or all `FLT` (no mixing).
722722
723-
- `TADD/TSSUB/TMUL/TDIV/TPOW(TNS: x, INT|FLT: y):TNS` — Tensor-scalar arithmetic. Tensor elements and scalar must both be `INT` or both be `FLT` (no mixing). Division by zero is an error.
723+
- `TADD/TSUB/TMUL/TDIV/TPOW(TNS: x, INT|FLT: y):TNS` — Tensor-scalar arithmetic. Tensor elements and scalar must both be `INT` or both be `FLT` (no mixing). Division by zero is an error.
724724
725725
### 12.9 Map operations
726726
@@ -783,12 +783,15 @@
783783
`FUNC` and `THR` values are serialized with enough information to reconstruct them in a new interpreter process:
784784
785785
- `FUNC` values include an identifier, name, parameter list (including default expressions), return type, the function body AST, and a serialized snapshot of the closure environment. The closure snapshot serializes each bound value via `SER` so that the function can be rehydrated elsewhere.
786+
786787
- `THR` values include an identifier plus status metadata (paused/finished/stop/state) and, when available, the serialized block AST and environment captured when the thread was created. The underlying OS thread is not serialized.
787788
788789
- `UNSER(STR: obj):INT|FLT|STR|TNS|MAP|FUNC|THR` ; reverse of `SER`. Given a string produced by `SER`, reconstruct the runtime value.
789790
790791
- For `INT`, `FLT`, `STR`, `TNS`, and `MAP`, reconstruction is exact.
792+
791793
- For `FUNC`, the interpreter rebuilds the function body and closure environment from the serialized definition so the function can be called in a fresh process.
794+
792795
- For `THR`, the interpreter reconstructs a `THR` handle with the recorded metadata. The handle is inert: it does not resume the original background task, but supports `TYPE`, `PAUSED`, `STOP`, and `AWAIT` (which returns immediately).
793796
794797
If the input is not a valid serialization produced by `SER`, `UNSER` raises a runtime error.

0 commit comments

Comments
 (0)