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

# 世界

## 世界

```koine
relation placed_by(order: Order, customer: Customer)

relation line_in(line: Line, order: Order)

state relation status(order: Order, value: OrderStatus) {
  key total (order)
}

state s0 {
  time 2024-01-01T00:00:00Z
  c1: Customer
  o1: Order
  l1: Line
  placed_by(o1, c1)
  line_in(l1, o1)
  status(o1, Draft)
}
```

```mermaid
flowchart LR
  c1["c1: Customer"] ---|placed_by| o1["o1: Order"]
  l1["l1: Line"] ---|line_in| o1
  o1 ---|status| draft["Draft"]
```

这张图是上面代码块里 s0 的快照，不是 ER 图：节点是实例和值，边是本节的 relation，没有实体属性、方向和基数。快照图的边标签必须来自同一节的 koine 代码块声明，`docs:check` 会校验。结构本身不说明作用：placed_by/line_in 支撑“订单属于谁、订单里有哪些行”的归属查询；status 支撑“订单现在处于什么状态”的判断。

**世界是业务投影内的完整快照。**

- 关系是元组集合：无序、无重复、无 null。
- 没列出的元组就是假，不是默认省略。
- 每个状态关系在世界里取完整外延。
- 世界有一个时间属性。

三层投影：

| 投影 | 决定什么 | 对应构造 |
| --- | --- | --- |
| 本体投影 | 哪些实体和关系被建模 | entity、relation |
| 时间投影 | 哪些时刻被快照 | state、transition |
| 视角投影 | 哪些角色看到哪些关系 | view |

数据的不确定性用值表示，不用世界集合表示。例如"是否支付未知"应该是状态枚举的一个值 `Unknown`，不是两个可能世界。

### 关系与 key

> 特性：K-004, K-005

关系是元组集合，key 是主键。可以类比表，但关系没有列顺序、没有 null；声明描述集合，世界给出元组。

```koine
relation tagged(order: Order, tag: Tag)        // key = (order, tag)

state relation status(order: Order, value: OrderStatus) {
  key partial (order)                          // key = order
}
```

| 规则 | 内容 |
| --- | --- |
| 每个关系都有 key | 不写时默认全部参数 |
| key 是参数子集 | 可以单列，也可以联合 |
| `key total (a)` | 每个 a 必须有一行 |
| `key partial (a)` | 每个 a 可以没有行；默认 |
| total/partial 只对真子集有效 | 全部参数是 key 时写了报错 |

`key total` 是所有世界（`state` / `init` / transition 目标）的结构不变量；缺行报 `koine.state.missing-total-key`，不是普通 false。

key 覆盖哪些参数，决定一个 key 对几行：

- key 是全部参数：一个 key 可以有多行。
- key 是真子集：一个 key 只有一行。写新值替换旧值。

### 状态

> 特性：K-006

`state` 命名一个具体世界，可作为 `init` 或 `scenario` 的起点。state 只声明这个快照里有哪些实体和元组；世界的同一性由内容决定。

```koine
state s0 {
  time 2024-01-01T00:00:00Z
  o1: Order
  status(o1, Draft)
}
```

state 必须完整，并且满足所有 axiom 和 total key。`init` 可以引用已有 state，也可以直接声明匿名世界；见[变化](https://koine.systems/reference/spec/transitions/#init)。

### 实体、值与别名

> 特性：K-001, K-002, K-003

| 声明 | 含义 | 类型安全 |
| --- | --- | --- |
| `entity Order` | 有身份的东西 | 是 |
| `value Amount = Decimal` | 无身份的新类型 | 是：Amount 与 Decimal 不同 |
| `value Currency = { USD, CNY }` | 枚举 | 是 |
| `alias ReportAmount = Amount` | 完全等价的别名 | 否 |
| `alias Buyer = Customer` | 同一实体类型的另一个名字 | 否：不创建新类型或身份 |
| `value Money = { amount: Decimal, currency: Currency }` | 产品值，字段一起出现 | 是：与字段组合不同的类型 |

alias 可以指向 entity 或 value，不能指向 relation。alias 是透明类型同义词：不创建新类型，也不创建新身份。

```koine
entity Customer

alias Buyer = Customer

state s0 {
  b1: Buyer
}
```

规则：

- `b1: Buyer` 声明的是 Customer 实体；alias 不新增实体身份。
- alias 在 state、relation、transition 的类型检查前展开到 canonical 类型。
- alias 可以链式；不允许成环。重复名报错。
- alias 不是角色；不同规则的角色用 relation/entity 表达，不用 alias。

见 [ADR-0018](https://koine.systems/notes/decisions/0018-entity-alias/)。

实体和值的区别：

| | 实体 | 值 |
| --- | --- | --- |
| 有身份 | 是 | 否 |
| 可比较 | 只有同一性 | 按值比较 |
| 例子 | 订单、客户、发票 | 金额、状态、日期 |

### 产品值

> 特性：K-027

产品值把一个无身份的值声明为若干命名字段：

```koine
value Currency = { USD, CNY, EUR }

value Money = {
  amount: Decimal,
  currency: Currency
} where amount >= 0
```

构造和读取：

```koine
order_money(o1, Money { amount: 100.00, currency: CNY })

amount_of(order_money(o1))   // order_money(o1) 返回 Money，再读字段
```

规则：

- 产品值没有身份，按字段结构比较；字段不可变。
- 字段类型必须是已知类型；字段名在同一产品值内唯一。
- 构造必须提供全部字段；字段读取是纯投影。
- 产品值可以带 `where` 精化；构造时检查。
- 产品值可以作为 relation 的列、define 的参数/返回值、transition 的绑定。
- 产品值不能包含方法、relation、transition；函数仍然是顶层 `define`。
- 不引入 variant、递归和模式匹配；这不是完整 ADT。

见 [ADR-0020](https://koine.systems/notes/decisions/0020-product-values/)。

### 值类型

| 类型 | 语义 | 字面量 |
| --- | --- | --- |
| `Int` | 任意精度整数 | `42`、`-1` |
| `Decimal` | 任意精度十进制数；运算见[运算符](https://koine.systems/reference/spec/operators/) | `100.50`、`0.001` |
| `Text` | Unicode 文本 | `"hello"` |
| `Bool` | 布尔 | `true`、`false` |
| `Date` | 本地日期 | `2024-01-01` |
| `Time` | 本地时间 | `12:30:00` |
| `Timestamp` | 时间点 + 固定偏移 | `2024-01-01T12:30:00Z` |
| `Duration` | 时长 | `30 days` |
| 枚举 | 有限值集合 | `Draft`、`Paid` |

规则：

- 比较只在同类型之间；相等、排序和数值转换见[运算符](https://koine.systems/reference/spec/operators/)。
- `Int` 与 `Decimal` 不隐式转换；整数字面量按期望类型定型。
- 物理单位和量纲见[量与单位](https://koine.systems/reference/spec/units/)；货币汇率是显式业务计算。
- 时区只支持固定偏移，不建模夏令时。
- `Date` 和 `Time` 不带时区；只支持比较，转 `Timestamp` 必须显式给偏移。

### 精化

精化给 newtype 加谓词：

```koine
value Amount = Decimal where self >= 0
```

构造 `Amount(100.00)` 时检查 `100.00 >= 0`；不满足则返回诊断。

物理单位转换不再使用 `conversion`；使用 `quantity` / `unit`，见[量与单位](https://koine.systems/reference/spec/units/)。K-008 已标记为 `deprecated`，ADR-0010 保留为历史决策。

### 运算符

Koine 的运算符是封闭的内置表：用户不能定义或扩展运算符，不做隐式转换；关系不是值，不能参与运算符。完整规则见[运算符](https://koine.systems/reference/spec/operators/)。

### 时间

> 特性：K-007

世界有一个 `time` 属性，类型 `Timestamp`。

```koine
init {
  time 2024-01-01T00:00:00Z
  o1: Order
  status(o1, Paid)
  paid_at(o1, 2024-01-01T00:00:00Z)
}
```

读取：

```koine
now()                        // 当前世界的时间
now() - paid_at(order)       // 时间差，返回 Duration
```

推进：

```koine
transition wait(d: Duration) {
  time = now() + d
}
```

规则：

- 时间是显式推进的。没有 `wait`，时间不动。
- 事件时间用普通关系记录，例如 `paid_at(order, value: Timestamp)`。
- 关系实例的有效期用显式时间参数表达，例如 `subscribed(customer, plan, from, to)`；没有内建的生效/失效字段。
- 暂不引入 interval 类型。有效期用显式 `from` / `to` 参数，约定半开区间 `[from, to)`；区间运算与 as-of 查询用 `define` / `derived relation` 派生，不做内建区间运算符。只有出现“区间作为返回值、key 或聚合元素”或“需要编译器强制 `from < to`”等证据时才重新评估，届时另写 ADR。
- 时间条件用比较，例如 `now() - paid_at(order) <= 30 days`。
- axiom 检查当前世界，不检查历史。"30 天内退款"必须记录 `refunded_at`，用 `refunded_at - paid_at <= 30 days` 检查。
- 时间算术只走 `Timestamp`：`Timestamp ± Duration`、`Timestamp - Timestamp -> Duration`。`Date` / `Time` 只比较，不参与加减；见[运算符](https://koine.systems/reference/spec/operators/)。

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