---
title: "运算符"
---

> Documentation Index
> Fetch the complete documentation index at: https://koine.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# 运算符

> 特性：K-009

本文件定义 Koine 的内置运算符、相等、全序和算术规则。运算符表是**封闭**的：用户不能定义或扩展运算符；所有类型规则在检查期确定。

运算符不是第二套语义：它只是内置函数的前缀/中缀写法。关系调用 `R(args)` 仍是谓词，不是运算符。

## 总则

- 不做用户定义重载，不做方法分派，不做隐式类型转换。
- 只有数值字面量可以按期望类型定型；变量之间必须显式转换。
- 表达式无副作用；运算符不抛异常。类型错误、除零、溢出都是诊断值。
- 关系不是值：关系不能参与 `==`、算术或排序。关系调用返回 `Bool`；函数式读取只对 `key total` 的真子集有效。
- 运算符只作用于同一类型。`alias` 在检查前展开；`newtype` 与底层类型是不同类型。

## 相等

`==` 只允许两边是同一类型 `T`，结果是 `Bool`。`T` 不能是 relation。

| 类型 | `==` 的语义 |
| --- | --- |
| entity | 身份相同：同一实体类型、同一实体 |
| value / newtype | 类型相同且值相等；newtype 与其底层类型不相等 |
| 产品值 | 声明的产品类型相同，字段逐个相等 |
| 枚举 | 声明的枚举类型相同，成员相同 |
| Bool / Int | 值相等 |
| Decimal | 数值相等；按 canonical 值比较 |
| Text | Unicode 码点序列完全相同；不做 normalization、大小写折叠 |
| Date / Time | 值相等 |
| Timestamp | 同一时间点；固定偏移归一化到 UTC 后比较 |
| Duration | 时长相等 |
| quantity | dimension 相同且 canonical magnitude 相等；unit 不进入身份 |

规则：

- `10 Kilometer == 10000 Meter` 为真。
- `1.0 == 1.00` 为真；`Decimal` 没有 `-0`，canonical 形式去掉尾随零。
- `Amount(1) == Decimal(1)` 是类型错误；newtype 是 nominal。
- 关系不能比较。要比较关系，用 `forall` / `exists` / `derived relation` 写成语义条件。
- 0.1 不提供 `!=`；写 `not (a == b)`。

## 排序

`<`、`<=`、`>`、`>=` 只允许同一类型、且该类型有全序；结果是 `Bool`。

| 允许 | 不允许 |
| --- | --- |
| Int、Decimal | Bool、Text |
| Date、Time、Timestamp、Duration | entity、产品值、枚举 |
| 同 dimension 的 quantity | relation |

规则：

- 比较不可链式：`a < b < c` 是错误，必须写 `a < b and b < c`。
- Text 不做排序。字典序、locale、大小写和 Unicode normalization 是显式函数的事，不是 `<` 的隐式语义。
- 产品和枚举没有固有顺序；需要顺序时用显式字段或 `define`。

## 布尔

`not`、`and`、`or`、`implies` 只接受 `Bool`，返回 `Bool`。

- 没有 truthiness：`1 and true`、`"x" or false` 都是类型错误。
- `and` / `or` / `implies` 的短路是求值优化，不改变语义；表达式本身没有副作用。
- `implies` 定义为 `not a or b`，右结合。
- 不提供位运算、`xor` 或 `nand`。

## 算术

下表的 `i` 是 `Int`，`d` 是 `Decimal`，`q` 是 quantity，`n` 是 `Int`。

| 表达式 | 结果 | 条件 |
| --- | --- | --- |
| `i1 + i2`、`i1 - i2`、`i1 * i2` | Int | 任意精度，精确 |
| `-i` | Int | |
| `d1 + d2`、`d1 - d2`、`d1 * d2` | Decimal | 精确；结果 canonical |
| `-d` | Decimal | |
| `d1 / d2` | Decimal | 仅当商是有限十进制；否则诊断 |
| `q1 + q2`、`q1 - q2` | Quantity | 同 dimension |
| `q1 * q2` | Quantity | dimension 相加 |
| `q1 / q2` | Quantity 或 Decimal | dimension 相减；同 dimension 得 Decimal；商必须有限 |
| `q ^ n` | Quantity | `n` 是正整数；dimension 指数乘 `n` |
| `Decimal * Quantity`、`Quantity * Decimal` | Quantity | 只缩放，dimension 不变 |
| `Duration + Duration`、`Duration - Duration` | Duration | |
| `-Duration` | Duration | |
| `Duration * Decimal`、`Decimal * Duration` | Duration | 缩放 |
| `Timestamp + Duration`、`Duration + Timestamp` | Timestamp | |
| `Timestamp - Duration` | Timestamp | |
| `Timestamp - Timestamp` | Duration | |

不定义：

- `Int / Int`。整数除法有截断/取整歧义；需要比率时显式转 `Decimal`，需要整数商时用后续的 `div` / `mod`。
- `Date` / `Time` 的加减。它们只比较；要时间算术，先显式转 `Timestamp`。
- `Timestamp + Timestamp`、`Duration * Duration`、`Duration / Duration`。
- `Quantity / Decimal`、`Decimal / Quantity`。量纲缩放用乘法。

## Decimal 与除法

`Decimal` 是任意精度十进制数：

- 由符号、任意精度整数系数和非负 scale 表示。
- `+`、`-`、`*` 精确；结果去掉尾随零，不保留 `-0`。
- 相等和 key 都按 canonical 数值；`1.0`、`1.00`、`1` 作为 Decimal 是同一个值。
- 没有 `NaN`、`Infinity` 或负零。
- 实现可以限制系数位数或 scale，但超出时必须报 `koine.eval.decimal-limit`，不能静默截断或回绕。这个限制是实现契约，不是语言语义。

`/` 只在商是**有限十进制**时有值。对 `a / b`：

- `b` 为 0：诊断 `koine.eval.division-by-zero`。
- 商不是有限十进制：诊断 `koine.eval.non-terminating-division`，并建议改用 `divide`。
- 例如 `1.0 / 2.0` 合法；`1.0 / 3.0` 不合法。

需要显式舍入时使用内置函数：

```koine
divide(a: Decimal, b: Decimal, scale: Int, rounding: Rounding) -> Decimal
```

- `scale` 是保留的小数位数，0..18。
- `rounding` 是内置枚举：`HalfEven`、`HalfUp`、`Down`、`Up`、`Floor`、`Ceiling`。
- 没有默认 rounding；调用必须写出来。
- `b` 为 0 报 `koine.eval.division-by-zero`。
- 结果仍然受实现位数限制，超出报 `koine.eval.decimal-limit`。

`to(value, unit)` 的精确形式同样要求商有限；需要舍入时用：

```koine
to(value: Quantity, unit: UnitExpr, scale: Int, rounding: Rounding) -> Decimal
```

## 文本

`Text` 只支持 `==` 和 `not (==)`。

- 相等按 Unicode 码点序列，不做 normalization、大小写折叠、trim 或 locale 处理。
- 不支持 `<`、`<=`、`>`、`>=`。
- 大小写、子串、正则、排序等操作后续以显式函数加入；它们不属于 K-009。

## 显式数值转换

`Int` 和 `Decimal` 不隐式转换。

- 整数字面量 `42` 在期望 `Int` 的上下文是 Int，在期望 `Decimal` 的上下文是 Decimal；没有上下文时默认 Int。
- 带小数点的字面量 `42.0` 是 Decimal。
- `Int` 变量转 `Decimal`：`to_decimal(i)`。
- `Decimal` 转 `Int`：`to_int(d, rounding)`，按给定 rounding 舍入到整数；没有默认 rounding。
- `1 + 2.5` 是类型错误；写 `1.0 + 2.5` 或 `to_decimal(i) + 2.5`。

## 优先级与结合性

从低到高：

| 优先级 | 运算符 | 结合性 |
| --- | --- | --- |
| 1 | `implies` | 右 |
| 2 | `or` | 左 |
| 3 | `and` | 左 |
| 4 | `not` | 前缀 |
| 5 | `==`、`<=`、`<`、`>=`、`>` | 非结合 |
| 6 | `+`、`-` | 左 |
| 7 | `*`、`/` | 左 |
| 8 | 一元 `-` | 前缀 |
| 9 | 调用 `f(...)`、字段读取 `e.f` | 左 |

- `not a == b` 解析为 `not (a == b)`。
- `a < b < c` 是错误；比较运算符非结合。
- 括号可以覆盖优先级。
- `and` / `or` 混用时按上表；风格上建议加括号，但这不是语义要求。
- 单位表达式的 `*`、`/`、`^` 有自己的语法，不参与值运算符优先级；见[量与单位](https://koine.systems/reference/spec/units/)。

## 内置函数

以下不是运算符，但属于同一层的封闭内置表：

| 函数 | 签名 | 说明 |
| --- | --- | --- |
| `now()` | `() -> Timestamp` | 当前世界时间 |
| `to` | `(Quantity, UnitExpr) -> Decimal` | 精确显示换算；商必须有限 |
| `to` | `(Quantity, UnitExpr, Int, Rounding) -> Decimal` | 显式舍入的显示换算 |
| `divide` | `(Decimal, Decimal, Int, Rounding) -> Decimal` | 显式除法 |
| `to_decimal` | `(Int) -> Decimal` | 精确转换 |
| `to_int` | `(Decimal, Rounding) -> Int` | 显式舍入转换 |

## 诊断

| 码 | 场景 |
| --- | --- |
| `koine.type.operator-mismatch` | 运算符两边类型不匹配 |
| `koine.type.mixed-numeric` | Int 与 Decimal 混用，建议显式转换 |
| `koine.type.operator-not-defined` | 该类型没有这个运算符，例如 Text `<`、entity `+` |
| `koine.parse.chained-comparison` | 比较链，例如 `a < b < c` |
| `koine.eval.division-by-zero` | 除数为 0 |
| `koine.eval.non-terminating-division` | 商不是有限十进制，建议 `divide` |
| `koine.eval.decimal-limit` | 超出实现的 Decimal 位数/scale 限制 |

诊断是值；transition 遇到这些诊断时该步不成立，`scenario` 报告第几步。

## 示例

```koine
define refundable(order: Order) -> Bool {
  status(order) == Paid and not refunded(order)
}

define rate_to(from: Currency, to: Currency) -> Decimal {
  divide(exchange_rate(from), exchange_rate(to), 8, HalfEven)
}

define days_since(order: Order) -> Duration {
  now() - paid_at(order)
}
```

```text
1 + 2.5                 -- 错误：Int 与 Decimal 混用
"a" < "b"               -- 错误：Text 没有排序
1 < 2 < 3               -- 错误：比较不可链式
1.0 / 3.0               -- 错误：商不是有限十进制
1.0 / 0.0               -- 错误：除零
10 Kilometer == 10000 Meter  -- 合法，true
```

## 相关

- [世界](https://koine.systems/reference/spec/world/#值类型)：类型和字面量。
- [量与单位](https://koine.systems/reference/spec/units/)：dimension、unit expression 和 canonical 值。
- [形式语义](https://koine.systems/reference/spec/semantics/)：类型与求值规则。
- [错误码](https://koine.systems/reference/errors/)：诊断码表。
- [ADR-0023](https://koine.systems/notes/decisions/0023-operator-table/)：为什么采用封闭表和显式除法。

Source: https://koine.systems/reference/spec/operators/index.mdx
