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

# 语法摘要

本文是声明形式的速查，不是完整文法。规范定义见[语言规范](https://koine.systems/reference/spec/)的各节；两者冲突时以规范节为准。

Koine 的声明是平的。

## 声明

```text
entity Name
value Name = { A, B, C }
value Name = { field: Type, ... }
value Name = { field: Type, ... } where condition
value Name = Type
value Name = Type where self op value
alias Name = Type
relation name(param: Type, ...)
state relation name(param: Type, ...) { key total (param) }
state relation name(param: Type, ...) { key partial (param) }
transition name(param: Type, ...) { ... }
axiom Name { formula }
define name(param: Type) -> Type { expr }
derived relation name(param: Type, ...) { formula }
derived relation name(param: Type, ...) key total (param) { formula }
derived relation name(param: Type, ...) key partial (param) { formula }
view name = { relation, ... }
init Name
init { ... }
state Name { ... }
scenario Name { ... }
module Name { ... }
quantity Name
quantity Name = dimension-expr
unit Name : Quantity = unit-expr
```

## 量纲与单位表达式

```text
number
Name
Name * Name
Name / Name
Name ^ integer
number unit-expr
(unit-expr)
```

## 值构造与字段

```text
Type { field: expr, ... }
expr.field
number unit-expr
```

## transition body

```text
require condition
relation(args)
not relation(args)
time = now() + duration
```

## 条件

运算符的类型规则、相等/排序、Decimal 除法和优先级见[运算符](https://koine.systems/reference/spec/operators/)。

```text
relation(args)
relation(key) == value
not condition
condition and condition
condition or condition
condition implies condition
condition == condition
condition <= condition
forall (x: Type, y: Type). condition
exists (x: Type). condition
```

## 聚合

```text
sum(expr for x: Type where condition)
count(x: Type where condition)
min(expr for x: Type where condition)
max(expr for x: Type where condition)
any(expr for x: Type where condition)
all(expr for x: Type where condition)
```

## 字面量

```text
42
-1
100.50
"text"
true
false
2024-01-01
12:30:00
2024-01-01T12:30:00Z
30 days
Draft
```

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