业务背景
服务距离、重量、时长和物理公式不应该依赖 pairwise 转换表。quantity 定义量纲,unit 定义同一量纲的尺度,unit literal 在构造时 canonical 化;公式按量纲检查。
quantity
quantity Length
quantity Mass
quantity Time
quantity Acceleration = Length / Time^2
quantity Force = Mass * AccelerationLength、Mass、Time 是 base quantity;Acceleration 和 Force 是 derived quantity。
unit 与 unit expression
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,支持
*、/、^和括号。 Newton的定义直接展示kg·m/s²,不需要魔法 factor。- factor 是精确有理数;unit 依赖不能成环。
- 用户也可以给已有 quantity 增加单位,不修改原声明:
unit Furlong : Length = 201.168 Meterunit literal 与 canonical 化
entity Seller
state relation service_radius(seller: Seller, value: Length) {
key total (seller)
}
state s0 {
time 2024-01-01T00:00:00Z
s1: Seller
service_radius(s1, 10 Kilometer)
}10 Kilometer 在构造时 canonical 化:
10 Kilometer -> { dim: Length, magnitude: 10000 }10 Kilometer 和 10000 Meter 是同一个值,unit 不进入值身份。relation key、比较和 diff 都按 canonical 值。
复合 unit 也是一个 literal:
10 Meter / Second^2
10 Kilogram * Meter / Second^2unit expression 的 *、/、^ 属于单位;要对 literal 做值运算,用括号分隔:
(10 Meter) / (2 Second)公式
define force(mass: Mass, acceleration: Acceleration) -> Force {
mass * acceleration
}量纲检查:
Mass × Acceleration
= Mass × Length / Time^2
= Force运行时只保留 canonical dimension + magnitude;force(2 Kilogram, 3 MeterPerSecondSquared) 得到 6 的 Force 值。
显示
to(value, unit-expression) 是显式显示查询:
to(service_radius(seller), Meter)它返回该 unit 下的 magnitude,不改变语义值。
边界
- 货币不是静态 unit;汇率是动态世界数据,用 relation/define。
- 温度等仿射单位需要 point/delta 模型,不在本规范内。
conversion不再用于物理单位;K-008 已弃用。- 当前原型覆盖 unit expression、unit literal、canonical 化、quantity 乘法和比较;完整 Decimal 精度和任意表达式中的 unit literal 仍是后续工作。