支持/集成语言测量单位的策略是什么?

发布于 2024-12-05 06:19:08 字数 469 浏览 0 评论 0原文

我想从纯粹的语言设计角度来看,SI 单位的“实现”需要哪些“功能”(语义上和句法上)。

如果有人声称一种语言对测量单位有很大的支持,那么通常会期望哪种“功能”?

  • 只是像特殊文字或语法糖之类的东西?
  • 使单元类型安全的特殊约定(但无需昂贵的运行时包装)?
  • 用于分数计算的特殊数学模式?
  • 单位之间的自动转换和强制?

例如,F# 集成了对该语言中测量单位的支持。它与 Java 的一个库相比有何改进?

应将哪些功能内置到语言中以提高单元的可用性?哪些功能不一定与测量单位相关,但可以使实现更好?

I wonder from purely language-design point of view which "features" (semantically and syntactically) an "implementation" of SI units would require.

Which "functionality" is generally expected if someone claims that a language has great support for units of measurements?

  • Just something like special literals or syntactic sugar?
  • Special conventions which make units typesafe (but without costly runtime wrapping)?
  • A special math mode for computations with fractions?
  • Automatic conversions and coercion between units?

For instance F# has integrated support for units of measurements in the language. How does it improve over e. g. a library for Java?

Which features should be built into the languages to improve usability of units? Which features are not necessarily related to units of measurement but make an implementation nicer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

余厌 2024-12-12 06:19:08

F# 相对于 Java UOM 库的优势很简单——类型安全。如果您尝试添加 3.4.,您将收到编译时错误。

在 F# 中,UOM 在类型检查后被删除,因为它是唯一具有此类功能的 .NET 语言。

回复评论,这里是一个简单的分数类型及其用法:

type Fraction<[<Measure>] 'a>(a : int, b : int) =
    member __.Divisor = a
    member __.Dividend = b
    member __.AsFloat = float a / float b
    static member (*) (a : Fraction<'a>, b : Fraction<'b>) : Fraction<'a * 'b> =
        Fraction(a.Divisor * b.Divisor, a.Dividend * b.Dividend)

type [<Measure>] m
type [<Measure>] kg
type [<Measure>] s

let a = Fraction<m / s>(4, 3)
let b = Fraction<kg>(2, 5)
let ``a times b`` = a * b

返回的值是 Fraction 类型,其 AsFloat 值为 0.5333333333

F#'s advantage over a Java UOM library is simple - type safety. You will get compile-time errors if you attempt to add 3.<s> and 4.<m / s>.

In F#, UOM are erased after the type-checking, because it is the only .NET language with such feature.

In reply to comment, here is a simple fraction type and its usage:

type Fraction<[<Measure>] 'a>(a : int, b : int) =
    member __.Divisor = a
    member __.Dividend = b
    member __.AsFloat = float a / float b
    static member (*) (a : Fraction<'a>, b : Fraction<'b>) : Fraction<'a * 'b> =
        Fraction(a.Divisor * b.Divisor, a.Dividend * b.Dividend)

type [<Measure>] m
type [<Measure>] kg
type [<Measure>] s

let a = Fraction<m / s>(4, 3)
let b = Fraction<kg>(2, 5)
let ``a times b`` = a * b

And the value returned is of type Fraction<kg m/s> and its AsFloat value is 0.5333333333.

江南月 2024-12-12 06:19:08

从语言设计的角度来看,能够:

  1. 使用任意标签(不仅仅是字符串或其他原始标签,而是任意结构化标签)标记数据类型
  2. 标签执行任意编译时计算
  3. 对给定操作数标签和运算符的 (函数),计算应用于标记操作数的函数结果的标签

这一切都可以完成 并完成更多的10 年前 使用 C++ 模板元编程。

Haskell 还有一个实现,它使用 Haskell 的(有限)能力对类型类实例执行编译时计算。但它并不完整(据我所知,没有分数幂,也没有非 SI 单位)。

From a language-design point of view, it's enough to be able to :

  1. tag data types with arbitrary labels (not just string or other primitive labels, but arbitrarily structured labels)
  2. perform arbitrary compile-time calculations on labels
  3. given operand labels and an operator (function), calculate the label of the result of the function applied to labelled operands

This all can be done and was done more than 10 years ago with C++ template metaprogramming.

There's also an implementation for Haskell that uses Haskell's (limited) ability to perform compile-time calculations over typeclass instances. It's not as complete though (AFAIK no fractional powers and no non-SI units).

醉殇 2024-12-12 06:19:08

迷人。我在 google 上搜索了这个叮当声,“我确信我可以在 Smalltalk 中轻松地做到这一点”,并偶然发现了 Frink http://futureboy .us/frinkdocs/,它针对 JVM 和 Android。

跟踪测量单位(英尺、米、吨、美元、瓦等)
完成所有计算并允许您进行加、减、乘、
毫不费力地划分它们,并确保答案出来
即使您混合使用加仑和升等单位,也是正确的。

当然,关键是转化

使用巨大的内置数据文件在数千种单位类型之间进行单位转换。

和不确定性

支持区间算术(也称为区间计算)
计算,允许您自动计算误差范围和
所有计算中都存在不确定性。

我确信您可以使用 Java 中的类来完成此操作,当然也可以使用 Ada 来完成此操作,但这会需要大量(抱歉)输入。

Fascinating. I googled this tinkling,"I am sure I could do this quite easily in Smalltalk," and stumbled upon Frink http://futureboy.us/frinkdocs/ which targets the JVM and Android.

Tracks units of measure (feet, meters, tons, dollars, watts, etc.)
through all calculations and allows you to add, subtract, multiply,
and divide them effortlessly, and makes sure the answer comes out
correct, even if you mix units like gallons and liters.

and of course key to this is conversion

Unit Conversion between thousands of unit types with a huge built-in data file.

and uncertainty

Supports Interval Arithmetic (also known as Interval Computations) in
calculations, allowing you to automagically calculate error bounds and
uncertainties in all of your calculations.

I'm sure you could do this with classes in Java and definitely with Ada, but that would be a lot of (I'm sorry) typing.

黑白记忆 2024-12-12 06:19:08

20 世纪 80 年代,ACM SIGPLAN 公告中有一篇关于此的好论文。这个想法是类型安全加上你在高中物理中学到的单元分析:例如,速度是英里/小时,时间是小时,因此将它们相乘得出以英里为单位的距离,并且类型系统强制执行该距离这样你就不会犯类别错误。语言或元语言将有办法指定所有这些。另一个例子,我最近在Java中做了一些货币工作,其中CurrencyValue除以CurrencyValue得到一个BigDecimal,即汇率;一个CurrencyValue乘以或除以一个BigDecimal得到另一个CurrencyValue,等等。

这基本上就是你过去做旧的多项选择PSSC物理考试的方式:忘记物理,只记住宇宙的一些常数,然后推理问题给定指定数量的单位和所需答案的单位,那么您可以自己构造所需的公式。

There was a good paper in the ACM SIGPLAN Notices about this in the 1980s. The idea was type safety plus the kind of unit analysis you learn in high school physics: for example, a speed is miles/hour, a time is hours, so multiplying them together gives a distance in miles, and the type system enforces that for you so you can't make category mistakes. The language or meta-language would have ways to specify all that. As another example, I recently did some currency work in Java where a CurrencyValue divided by a CurrencyValue gave a BigDecimal, i.e. an exchange rate; a CurrencyValue multipled or divided by a BigDecimal gave another CurrencyValue, etc.

This is basically how you used to do the old multi-choice PSSC Physics exams: forget the physics, just memorize a few constants of the universe, then just reason about the problem given the units of the specified quantities and the units of the answer required, then you can construct the formula required yourself.

唐婉 2024-12-12 06:19:08

Java 库 (unitsofmeasurement.org) 的后续版本现在可以在 unitsofmeasurement.github.io 下找到。它是 Java 测量单位标准 JSR 363 的所在地。一探究竟。

The successor to the library for Java (unitsofmeasurement.org) can now be found under unitsofmeasurement.github.io. It is home to the Java standard for Units of Measurement, JSR 363. Check it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文