---
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.

# 量与单位

## quantity

> 特性：K-028

**quantity 是带量纲的值类型。**

```koine
quantity Length

quantity Mass

quantity Time

quantity Acceleration = Length / Time^2

quantity Force = Mass * Acceleration
```

规则：

- base quantity 没有右侧，定义一个量纲基。
- derived quantity 用 `*`、`/`、`^` 表达量纲等式。
- quantity 值没有身份，按 canonical 值比较。
- quantity 可以出现在 relation、define、transition、state 中。
- quantity 的依赖不能成环。
- quantity 是值层的新类别，不是 `value` 声明。
- dimension-only：同 dimension 即同类型；quantity 名是别名/文档，不是 nominal 身份。

## unit

> 特性：K-029

**unit 是某个 quantity 的命名尺度。**

```koine
unit Meter : Length = 1

unit Centimeter : Length = 0.01 Meter

unit Decimeter : Length = 0.1 Meter

unit Kilometer : Length = 1000 Meter

unit Second : Time = 1

unit Kilogram : Mass = 1

unit Newton : Force = Kilogram * Meter / Second^2
```

规则：

- `unit` 右侧是 unit expression；支持 `*`、`/`、`^`、括号和数值系数。
- unit expression 解析为 `{ dimension, factor }`；factor 是正有理数。
- 纯数值 RHS 表示目标 quantity 的 canonical scale。
- unit 不是类型，也不是值；它有独立命名空间。
- 用户可以在任意可见 quantity 上增加 unit，不需要修改 quantity 声明。
- unit 依赖不能成环。
- 同一 quantity 的任意两个 unit 通过 canonical factor 换算，不需要 pairwise conversion。

## unit literal

> 特性：K-031

quantity literal 是：

```text
number unit-expression
```

例如：

```koine
10 Kilometer
10 Meter / Second^2
10 Kilogram * Meter / Second^2
```

- unit expression 的 `*`、`/`、`^` 属于单位，不属于值运算。
- 对 literal 做值运算要加括号：

```koine
(10 Meter) / (2 Second)
```

- literal 在 lowering 阶段 canonical 化：

```text
10 Kilometer
-> { dim: Length, magnitude: 10000 }
```

- unit 不进入值身份；`10 Kilometer` 与 `10000 Meter` 相等。
- relation key、`diff`、查询按 canonical 值。
- 原始 unit expression 的 span 保留到诊断层。

## dimension

> 特性：K-030

**dimension 是 quantity 的类型级指数向量。**

| 表达式 | 量纲规则 |
| --- | --- |
| `q1 + q2`、`q1 - q2`、比较 | 要求同一 dimension |
| `q1 * q2` | 指数相加 |
| `q1 / q2` | 指数相减；同 dimension 得到 `Decimal` |
| `q^n` | 指数乘以 n |
| `Decimal * q` | 只缩放，dimension 不变 |
| `sum` / `min` / `max` | 要求同一 dimension |

`F = ma` 直接由 dimension 规则检查：

```koine
quantity Force = Mass * Acceleration

define force(mass: Mass, acceleration: Acceleration) -> Force {
  mass * acceleration
}
```

量纲运算由语言内置，不是用户自定义运算符重载。`q1 / q2` 的 magnitude 遵循[运算符](https://koine.systems/reference/spec/operators/#decimal-与除法)：商必须是有限十进制，否则用显式 `divide` / `to` 舍入。

## 显示

`to(value, unit-expression)` 是显式显示查询：

```koine
to(service_radius(seller), Meter)
```

它返回该 unit 下的 magnitude，不改变语义值；不是隐式转换。商不是有限十进制时，用 `to(value, unit, scale, rounding)` 显式舍入；见[运算符](https://koine.systems/reference/spec/operators/#decimal-与除法)。

## 连续单位与用户自定义

```koine
unit Meter : Length = 1

unit Centimeter : Length = 0.01 Meter

unit Decimeter : Length = 0.1 Meter

unit Kilometer : Length = 1000 Meter

unit Furlong : Length = 201.168 Meter
```

`Centimeter -> Kilometer` 不需要单独声明；两者都通过 canonical Meter 组合。

unit 目录不是语言内置表。用户可以定义业务 quantity，也可以给已有 quantity 增加 unit。

## 边界

- 货币不是静态 unit；汇率是动态世界数据，用 relation/define。
- 温度等仿射单位需要 point/delta 模型，不在本规范内。
- `conversion` 不再用于物理单位；K-008 标记 `deprecated`。
- 当前原型覆盖 unit expression、unit literal、canonical 化、quantity 乘法和比较；任意表达式中的 unit literal、affine 仍是后续工作；Decimal 精度与舍入见[运算符](https://koine.systems/reference/spec/operators/#decimal-与除法)。

## 相关

- [ADR-0021](https://koine.systems/notes/decisions/0021-quantity-unit-dimension/)
- [ADR-0022](https://koine.systems/notes/decisions/0022-unit-expressions/)
- [世界](https://koine.systems/reference/spec/world/)
- [形式语义](https://koine.systems/reference/spec/semantics/)
- [语法摘要](https://koine.systems/reference/spec/grammar/)

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