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

# 时间区间：关系实例有效期纸面原型

> 状态：Q-007 已关闭：暂不引入 interval 类型。本文保留纸面原型 + 局部 CLI 验证作为证据，不是规范：关系实例有效期用显式时间参数表达已够用，规范性规则见[世界：时间](https://koine.systems/reference/spec/world/#时间)。不引入 interval 关键字，不引入内建 `VALID_FROM` / `VALID_TO`，不改 [未决问题](https://koine.systems/notes/open-questions/)。

## 1. 范围与约定

Q-007 问两件事：`[start, end)` 是否需要独立类型；订阅、雇佣这类关系实例有效期怎么表示。本文用两个业务用例回答：**订阅有效期**和**雇佣有效期**。

约定：有效期是关系上的两个显式 `Timestamp` 参数 `from` / `to`，采用半开区间 `[from, to)`——`from` 含、`to` 不含。这与世界规范里“关系实例的有效期用显式时间参数表达”的立场一致，见[世界：时间](https://koine.systems/reference/spec/world/#时间)。

## 2. 领域模型

```koine
module interval_proto {
  entity Customer
  entity Employee
  entity Company
}

// 计划：订阅档位。枚举值，无身份。
value Plan = { Basic, Pro, Enterprise }

// 订阅有效期：客户在某计划上的有效期。
// key partial (customer, from)：不同 from 是不同行，同一客户可有多个有效期段。
state relation subscribed(customer: Customer, from: Timestamp, plan: Plan, to: Timestamp) {
  key partial (customer, from)
}

// 雇佣有效期：员工在某公司的有效期。
state relation employed(employee: Employee, from: Timestamp, company: Company, to: Timestamp) {
  key partial (employee, from)
}
```

### 2.1 key 选择：`key partial (customer, from)` 与 `key total (customer)`

| key | 含义 | 同一客户能否多行 | 能否存历史 | 后果 |
| --- | --- | --- | --- | --- |
| `key partial (customer, from)` | 每个 `(customer, from)` 至多一行 | 能：不同 `from` 是不同 key | 能 | 需要额外约束防重叠；操作某一段要带 `from` 定位；相同 `from` 的两段会互相替换 |
| `key total (customer)` | 每个 `customer` 恰好一行 | 否：第二段替换第一段 | 否 | 只能表达“当前订阅”；历史必须另放一张带 `from/to` 的关系 |

两个结论直接来自 key 语义（[世界：关系与 key](https://koine.systems/reference/spec/world/#关系与-key)）：

- `key total (customer)` 强制每个客户恰好一行。用它表达有效期，客户续费或重新订阅时旧行被替换，历史丢失；因此它只适合“当前事实”，例如 `current_plan(customer, plan)`。
- `key partial (customer, from)` 把 `from` 放进 key，让“不同开始时间”成为不同行，从而保留多段历史。这是本文两个用例的默认选择。

`key partial (customer, from)` 的代价：

1. **重叠不会自动被阻止**。两段不同 `from` 的有效期可以重叠，需要 axiom 或 transition 的 `require` 约束。
2. **定位一段有效期要带 `from`**。没有关系实例 GUID，`from` 就是那段有效期的身份；见下文 `terminate`。
3. **相同 `from` 的并发段互相替换**。雇佣用例里，若同一员工同一天起在不同公司兼职，`key partial (employee, from)` 会让后一行替换前一行；需要并发段时，key 要含 `company`，且保持前置（`key partial (employee, from, company)`）或退回全参数 key。

## 3. 派生谓词与不变量

```koine
// 任意两个 [from, to) 区间是否重叠。
define intervals_overlap(f1: Timestamp, t1: Timestamp, f2: Timestamp, t2: Timestamp) -> Bool {
  f1 < t2 and f2 < t1
}

// 订阅在时刻 t 是否有效：存在一段覆盖 t 的有效期。
define subscription_active_at(customer: Customer, t: Timestamp) -> Bool {
  exists (p: Plan, f: Timestamp, to: Timestamp).
    subscribed(customer, f, p, to) and f <= t and t < to
}

// 雇佣在时刻 t 是否有效。
define employed_at(employee: Employee, t: Timestamp) -> Bool {
  exists (c: Company, f: Timestamp, to: Timestamp).
    employed(employee, f, c, to) and f <= t and t < to
}

// 区间必须合法：from 早于 to。
axiom SubscriptionIntervalValid {
  forall (c: Customer, p: Plan, f: Timestamp, t: Timestamp).
    subscribed(c, f, p, t) implies f < t
}

// 同一客户的两段订阅不能重叠。
axiom NoOverlappingSubscription {
  forall (c: Customer, p1: Plan, f1: Timestamp, t1: Timestamp, p2: Plan, f2: Timestamp, t2: Timestamp).
    subscribed(c, f1, p1, t1) and subscribed(c, f2, p2, t2) and f1 < f2 implies
      not (f1 < t2 and f2 < t1)
}
```

`NoOverlappingSubscription` 用 `f1 < f2` 区分“同一客户的两段不同有效期”：因为 key 是 `(customer, from)`，同一客户的两行必然 `from` 不同。半开区间重叠条件是 `f1 < t2 and f2 < t1`（相邻不重叠，`t1 == f2` 时为假）。

## 4. 业务动作

```koine
// 客户订阅计划：当前无有效订阅时，开一段从 now() 起 30 天的有效期。
transition subscribe(customer: Customer, plan: Plan) {
  require not exists (p: Plan, f: Timestamp, to: Timestamp).
    subscribed(customer, f, p, to) and f <= now() and now() < to
  subscribed(customer, now(), plan, now() + 30 days)
}

// 雇佣：当前无有效雇佣时，开一段从 now() 起 365 天的有效期。
transition hire(employee: Employee, company: Company) {
  require not exists (c: Company, f: Timestamp, to: Timestamp).
    employed(employee, f, c, to) and f <= now() and now() < to
  employed(employee, now(), company, now() + 365 days)
}

// 终止一段雇佣：用 from 定位要结束的那段，把 to 收口到 now()。
transition terminate(employee: Employee, company: Company, from: Timestamp) {
  require exists (to: Timestamp).
    employed(employee, from, company, to) and from <= now() and now() < to
  employed(employee, from, company, now())
}

// 初始世界：c1、c2 各有一段订阅；e1 有一段雇佣。
state s0 {
  time 2024-01-15T00:00:00Z
  c1: Customer
  c2: Customer
  e1: Employee
  co1: Company

  subscribed(c1, 2024-01-01T00:00:00Z, Basic, 2024-02-01T00:00:00Z)
  subscribed(c2, 2024-01-10T00:00:00Z, Pro, 2024-01-20T00:00:00Z)
  employed(e1, 2023-06-01T00:00:00Z, co1, 2024-06-01T00:00:00Z)
}

init s0
```

`terminate` 展示 `key partial` 的一个直接后果：要结束哪一段，必须把 `from` 作为参数传进来，否则无法定位“那一段”。如果用内建 `VALID_FROM` / `VALID_TO` 字段，同样的定位问题依然存在；显式参数只是让这个身份显式可见。

查询意图：

- `subscription_active_at(c1, 2024-01-15T00:00:00Z)` 为真：c1 的 `[01-01, 02-01)` 覆盖 01-15。
- `subscription_active_at(c1, 2024-02-01T00:00:00Z)` 为假：`to` 不含。
- `employed_at(e1, 2024-03-01T00:00:00Z)` 为真；`employed_at(e1, 2024-07-01T00:00:00Z)` 为假。

## 5. 三类查询

### 5.1 某时刻是否有效

`subscription_active_at` / `employed_at` 把“在 t 是否有效”写成一个普通 Bool 表达式：在当前快照上，用 `f <= t and t < to` 判断 `t` 是否落在某段 `[from, to)` 里。注意这里不需要世界的 `time` 等于 `t`——`from` / `to` 是关系里的普通值，`t` 是查询参数。

现有命令能回答到哪一步：

- **`check`**：只做类型检查和 state/init 的 axiom 验证，不把任意表达式求值并打印成布尔。不能直接回答“c1 在 t 是否有效”，除非把它写进 axiom（那会变成“每个世界都必须成立”的约束，语义不对）。
- **`find --where 'subscription_active_at(c1, 2024-01-15T00:00:00Z)'`**：是“从 start 出发，找一个满足谓词的世界”，不是“在指定世界求值”。若谓词在 `s0` 为真会返回 `s0`（空路径）；为假时会在可达世界里继续找，找到的是另一个世界，找不到是 `not-found`，都不是“在 `s0` 求值”。
- **`run` / `explore`**：与点查无直接关系；它们回放场景或枚举可达世界。

结论：谓词可写、可被 `require` / `axiom` / `find --where` 复用，但**没有“给定世界对表达式求值并返回布尔”的一等命令**，直接点查需要绕路。这是 gap。

本文原型已实测：完整模型 `koine check` 返回 0 条诊断；`find --start s0 --where 'subscription_active_at(c1, 2024-01-15T00:00:00Z)'` 返回 `found`（空路径，即 `s0`）；把时间换成 `2024-02-01T00:00:00Z` 返回 `not-found`，因为半开区间不含 `to`。

### 5.2 同一客户的历史

“同一客户的全部有效期”就是 `subscribed` 里该客户的多行。`key partial (customer, from)` 保证这些行都在；CLI 输出按 key/元组字典序稳定排序（[CLI](https://koine.systems/reference/cli/)），所以同一客户的段天然按 `(customer, from)` 排列。

现有命令能回答到哪一步：

- **`observe --view`** 只做列级过滤，不做行级过滤（[观察：视角](https://koine.systems/reference/spec/observations/#视角)）。想看 c1 的历史，只能拿到整张 `subscribed` 再人工挑出 c1 的行。
- **`find`** 返回世界 + 路径，不返回关系子集。
- 想按客户过滤，只能为每个客户写一个固定名字的 derived relation（例如 `derived relation c1_history(plan: Plan, from: Timestamp, to: Timestamp) { subscribed(c1, from, plan, to) }`），不能参数化成任意客户。

结论：历史数据**可存、可排序输出（全表）**，但“按客户投影/行级过滤”是 gap。

### 5.3 两个有效期是否重叠

“两个给定区间是否重叠”是一个纯谓词 `intervals_overlap`，直接可写。更难的是“同一客户任意两段不重叠”的不变量，以及“找出所有重叠对”。

现有命令能回答到哪一步：

- **`check`**：`NoOverlappingSubscription` 作为 axiom，在 `s0` / `init` 上被验证。单个世界满足与否，`check` 能回答。
- **`run`**：沿场景每步检查 axiom，所以**这一条路径上**每个世界都不重叠，`run` 能保证。
- **`find --where '...'`**：写“存在重叠”的谓词，`found` 给出一个重叠的世界（witness），完整搜索无命中是 `not-found`，边界耗尽是 `unknown`。这能回答“在界内有没有反例”。
- **`explore`**：有界枚举可达世界。axiom 无效的世界不进图，所以图里只含不重叠的世界——但这是**有界**结论，最多 `holds-in-scope`，不是全可达世界证明。

结论：单世界/单路径的重叠检查可写、可被 check/run 强制；找 witness 用 find/explore；**“所有可达世界都不重叠”没有形式化证明，只能 unknown（有界 explore 给 holds-in-scope）**。

自连接去重是显式参数的固有风险：`subscribed(c, p1, f1, t1)` 与 `subscribed(c, p2, f2, t2)` 可能绑到**同一行**（`p1==p2, f1==f2, t1==t2`），此时“重叠”为真却是假阳性。同一客户靠 `f1 < f2` 去重（key 保证两行 `from` 不同）；跨客户（`c1 != c2`）则要另加 `c1 != c2`，且两客户 `from` 相等时不能再用 `f1 < f2` 去重。没有关系实例身份，这个判别必须手工写，容易出错。这是 gap。

## 6. 证据分级

| 结论 | 证据 | 级别 | 说明 |
| --- | --- | --- | --- |
| 显式 `from` / `to` 参数能表达关系实例有效期 | [世界：时间](https://koine.systems/reference/spec/world/#时间) 明文 | proved | 规范直接允许，无内建字段 |
| `key partial (customer, from)` 允许多段历史 | [世界：关系与 key](https://koine.systems/reference/spec/world/#关系与-key) | proved | 不同 `from` → 不同 key → 多行 |
| `key total (customer)` 不能存历史（“能存历史”被证伪） | 同上 | refuted | 每客户一行，第二段替换第一段 |
| as-of 点查可写成 define，可被 require/axiom/find-where 复用 | [定义](https://koine.systems/reference/spec/definitions/)、[形式语义：绑定域](https://koine.systems/reference/spec/semantics/#绑定域) | proved | 构造性定义；原型模型 `koine check` 通过，`find` 边界点已实测 |
| as-of 作为独立命令直接求值 | [CLI](https://koine.systems/reference/cli/) 命令清单 | gap | 无世界表达式求值命令；find 是搜索语义 |
| 同客户历史可存、可全表排序输出 | key 语义 + [CLI](https://koine.systems/reference/cli/) | proved | 数据与排序有保证 |
| 同客户历史按行投影/过滤 | [观察：视角](https://koine.systems/reference/spec/observations/#视角) | gap | observe 只做列级过滤 |
| 两个区间重叠可写成纯谓词 | [运算符：排序](https://koine.systems/reference/spec/operators/#排序) | proved | `f1 < t2 and f2 < t1` |
| 同客户不重叠在单个世界/单条场景路径上强制 | [变化：axiom 检查点](https://koine.systems/reference/spec/transitions/#axiom) | proved | check 验证 state/init，run 逐步验证 |
| 同客户不重叠在全部可达世界上成立 | — | unknown | 需形式化证明或有限域穷举；explore 有界只给 holds-in-scope |
| 自连接区分“同一行” | key 语义 + 无关系实例身份 | gap | 同客户靠 `f1 < f2`，跨客户需 `c1 != c2` 且注意 `from` 相等 |

## 7. 结论：Q-007 已关闭

**决定**：暂不引入 interval 类型。规范性规则见[世界：时间](https://koine.systems/reference/spec/world/#时间)：关系实例有效期用显式 `from` / `to` 参数（半开区间 `[from, to)`），区间运算与 as-of 用 `define` / `derived relation` 派生。

**显式时间参数已覆盖的场景**（本文用例）：点查 as-of、多段历史存储、重叠谓词与不变量、单世界/单路径的不变量强制。这些都是普通 relation + `Timestamp` 参数 + define/axiom 就能表达的。

**显式参数暴露的代价，也是需要 interval 类型才更省力的场景**：

1. **区间运算是手写的**。没有内建 `overlaps` / `contains` / 区间长度；每次都要写 `f1 < t2 and f2 < t1`，半开边界和自匹配去重容易错。
2. **`from < to` 靠 axiom 保证**。编译器不检查区间合法性；漏写 axiom 就允许空/反向区间。
3. **区间不是一等值**。不能作为一个值排序、聚合、返回或进 key；它被拆成两条独立的 `Timestamp` 参数。若未来需要“返回一个区间”“区间参与聚合/key”，显式参数会撞到 define 只返回单值、key 是参数子集这些边界。
4. **关系实例身份靠业务参数充当**。自连接去重、定位某一段都要手工选判别器（`from`），跨客户/并发同起点时判别器会失效。

**重评估条件**：

- 保持“显式 `from` / `to` 参数 + 半开区间约定”作为有效期表达方式，与[世界：时间](https://koine.systems/reference/spec/world/#时间)一致。
- 把本文模型落成 conformance fixture，用 `check` / `run` / `find` / `explore` 做有界验证，确认表达力和上面列出的边界。
- 只有当出现这些证据时才重新评估引入 interval 类型：区间运算频繁且出错、区间需要作为返回值/key/聚合元素、需要编译器强制 `from < to`。届时另写 ADR，并更新 Q-007 的状态。

## 相关

- [未决问题](https://koine.systems/notes/open-questions/)
- [关系语义与历史分析（问题陈述）](https://koine.systems/notes/relation-semantics/)
- [关系语义与历史分析：市场模型原型](https://koine.systems/notes/relation-semantics-prototype/)
- [世界：关系与 key](https://koine.systems/reference/spec/world/#关系与-key)
- [世界：时间](https://koine.systems/reference/spec/world/#时间)
- [变化：transition 与 axiom](https://koine.systems/reference/spec/transitions/)
- [观察：路径与差异](https://koine.systems/reference/spec/observations/)
- [定义](https://koine.systems/reference/spec/definitions/)
- [形式语义：绑定域](https://koine.systems/reference/spec/semantics/#绑定域)
- [CLI](https://koine.systems/reference/cli/)
- [完整例子：Aurora 订阅市场](https://koine.systems/examples/market/)

Source: https://koine.systems/notes/interval-prototype/index.mdx
