summaryrefslogtreecommitdiff
path: root/docs/hyper-operators.md
blob: bb05e2025d28dc85a33716f011925c8f3cc5de19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Hyper RPN Operators

Hyper operators operate on **all** values currently on the stack, rather than just the top one or two. They pop every value, reduce them left-associatively, and push a single result back.

The syntax uses square brackets: `[+]`, `[*]`, `[-]`, `[/]`, `[^]`, `[%]`, `[lg]`, `[log]`, `[ln]`.

## How Hyper Operators Work

```
Input:  1  2  3  4  5  [+]

  Step  Stack
  ----  -----
   1    [1]
   2    [1, 2]
   3    [1, 2, 3]
   4    [1, 2, 3, 4]
   5    [1, 2, 3, 4, 5]
   [+]  [15]

Result: 15
```

All five values are consumed, summed left-to-right (`((((1 + 2) + 3) + 4) + 5)`), and the single result is pushed back.

### Requirements

- At least **two** values must be on the stack, or the operator returns an error.
- After execution, exactly one value remains on the stack.

## Arithmetic Hyper Operators

### `[+]` Sum

Adds all stack values left-associatively.

```
$ gt '1 2 3 4 5 [+]'
15
```

With two operands, behaves identically to binary `+`:

```
$ gt '10 20 [+]'
30
```

### `[*]` Product

Multiplies all stack values left-associatively. Result is always unitless.

```
$ gt '2 3 4 [*]'
24
$ gt '1 2 3 [*]'
6
```

### `[-]` Subtraction

Subtracts all stack values left-associatively from the first.

```
$ gt '10 3 2 [-]'
5
$ gt '100 10 20 30 [-]'
40
```

### `[/]` Division

Divides all stack values left-associatively from the first. Result is always unitless.

```
$ gt '100 5 2 [/]'
10
$ gt '1000 10 10 [/]'
10
```

### `[^]` Power

Raises left-associatively. The first value is the base, subsequent values are successive exponents. Result is always unitless.

```
$ gt '2 3 2 [^]'
64
```

Equivalently: `(2 ^ 3) ^ 2` = `8 ^ 2` = 64.

With two operands, behaves identically to binary `^`:

```
$ gt '2 10 [^]'
1024
```

### `[%]` Modulo

Computes modulo left-associatively.

```
$ gt '100 7 3 [%]'
2
```

Equivalently: `100 % 7` = 2, then `2 % 3` = 2.

```
$ gt '10 3 2 2 [%]'
0
```

## Logarithmic Hyper Operators

Logarithmic hyper operators compute the **sum** of the log function applied to each stack value. All input values must be positive.

### `[lg]` Base-2 Logarithm Sum

```
$ gt '2 4 8 [lg]'
6
```

Equivalently: `log2(2) + log2(4) + log2(8)` = `1 + 2 + 3` = 6.

```
$ gt '1 2 3 4 5 [lg]'
6.907
```

### `[log]` Base-10 Logarithm Sum

```
$ gt '10 100 [log]'
3
```

Equivalently: `log10(10) + log10(100)` = `1 + 2` = 3.

### `[ln]` Natural Logarithm Sum

```
$ gt '2.718281828 7.389 [ln]'
2.999992408
```

Equivalently: `ln(2.718281828) + ln(7.389)` ≈ `1 + 2` ≈ 3.

## Metric-Aware Behavior

Some hyper operators are metric-aware, meaning they understand units and can operate on values with compatible units.

### Metric-aware operators: `[+]`, `[-]`, `[%]`

These operators:

1. **Validate** that all operands share the same metric category (or are unitless, which is always compatible).
2. **Convert** all values to the result metric's base units before computing.
3. **Push** the result with the first non-unitless metric (or unitless if all inputs are unitless).

```
$ gt '1km 2km 3km [+]'
6
```

Result is in kilometers: `1 + 2 + 3 = 6 km`.

```
$ gt '1km 500m [+]'
1.5
```

`500m` is converted to `0.5km`, then `1km + 0.5km = 1.5km`.

```
$ gt '10m 3m 2m [%]'
1
```

`10 % 3 = 1`, then `1 % 2 = 1 m`.

### Non-metric operators: `[*]`, `[/]`, `[^]`, `[lg]`, `[log]`, `[ln]`

These operators use raw numeric values and always produce unitless (Cool) results, regardless of input units:

- `[*]` — multiplying meters by meters would yield square meters, a different category, so the result is unitless.
- `[/]` — dividing two like units yields a ratio, which is unitless.
- `[^]` — exponents are inherently unitless.
- `[lg]`, `[log]`, `[ln]` — logarithms require dimensionless inputs, so the raw numeric value is used and the result is unitless.

### Mixed unitless and unit values

Unitless (Cool) values can be combined with any metric category — they "absorb" and take on the category of the other operands:

```
$ gt '0 5km [+]'
5
```

## Examples

### Batch aggregation

```
$ gt '12 15 18 14 16 [+]'
75
```

### Running total with metric conversion

```
$ gt '5km 2km 1000m [+]'
8
```

Result is in km: `5 + 2 + 1 = 8 km`.

### Compute a product

```
$ gt '2 5 10 [*]'
100
```

### Cascading subtraction

```
$ gt '100 10 20 30 5 [-]'
35
```

`100 - 10 - 20 - 30 - 5 = 35`.

### Repeated division

```
$ gt '1000 2 2 2 2 [/]'
62.5
```

`1000 / 2 / 2 / 2 / 2 = 62.5`.

### Log sum for information theory

Sum of log2 values for entropy calculations:

```
$ gt '0.25 0.5 0.25 [lg]'
-3
```

`log2(0.25) + log2(0.5) + log2(0.25)` = `-2 + -1 + -2` = -5.

## Edge Cases

### Division by zero

```
$ gt '10 5 0 [/]'
Error: division by zero
```

### Modulo by zero

```
$ gt '100 7 0 [%]'
Error: modulo by zero
```

### Non-positive log inputs

```
$ gt '0 [lg]'
Error: log2 undefined for non-positive numbers

$ gt '-1 [ln]'
Error: ln undefined for non-positive numbers
```

### Incompatible metrics

Mixing different metric categories (e.g., length and weight) in a metric-aware operator returns an error.

### Insufficient operands

All hyper operators require at least two values:

```
$ gt '5 [*]'
Error: insufficient operands for [*]: need at least 2 values

$ gt '[+]'
Error: stack is empty
```

## Summary Table

| Operator | Description                    | Metric-aware | Result metric |
|----------|--------------------------------|--------------|---------------|
| `[+]`    | Sum all values                 | Yes          | First non-Cool|
| `[*]`    | Multiply all values            | No           | Cool (unitless)|
| `[-]`    | Subtract all values            | Yes          | First non-Cool|
| `[/]`    | Divide all values              | No           | Cool (unitless)|
| `[^]`    | Power (left-associative)       | No           | Cool (unitless)|
| `[%]`    | Modulo (left-associative)      | Yes          | First non-Cool|
| `[lg]`   | Sum of log2 for all values     | No           | Cool (unitless)|
| `[log]`  | Sum of log10 for all values    | No           | Cool (unitless)|
| `[ln]`   | Sum of ln for all values       | No           | Cool (unitless)|