# Any RFC for Units of Measure?

**URL:** <https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154>\
**Category:** Uncategorized\
**Created:** [March 27, 2018, 4:44pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154 "2018-03-27T16:44:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [March 27, 2018, 4:44pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/1 "2018-03-27T16:44:41Z")

</div>

I am thinking of starting a RFC regarding [units of measure](http://typesatwork.imm.dtu.dk/material/TaW_Paper_TypesAtWork_Kennedy.pdf), as may be found in F#. A quick search didn’t show any previous RFC on the topic, which I found a bit surprising. Am I right that there is none? Otherwise, I’d be happy to take part in an existing conversation.

---

<div class="post-metadata">

**Author:** ![repax](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/repax/32/1342_2.png) [@repax](https://internals.rust-lang.org/u/repax)\
**Post date:** [March 27, 2018, 6:10pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/2 "2018-03-27T18:10:07Z")

</div>

This is interesting. In F#, `float<m/s/s>` is a parameterised `float` – just like `list` is taking a type parameter in `list<int>`.

However, in Rust division is defined as a function in a trait (`std::ops::Div`). Can the idea be translated into Rust, and can it be generalised to utilise the trait system?

I sometimes use _newtyping_ to further “subtype” values but it has a usability cost, but with Rust’s affine type system it does provide some extra safety and security benefits. Specifically, it limits how values of the selected types can be constructed, combined, and how long they remain accessible.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [March 27, 2018, 6:15pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/3 "2018-03-27T18:15:03Z")

</div>

Does the [uom](https://crates.io/crates/uom) crate do what you want? There may be other similar crates too. The question for an RFC is whether this _needs_ to be part of the standard library, which I doubt.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [March 27, 2018, 6:53pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/4 "2018-03-27T18:53:06Z")

</div>

I’m hoping const generics becomes powerful enough that we don’t need this in the language, and we can just make a library like [www.boost.org/libs/units](http://www.boost.org/libs/units) that allows arbitrary rational powers naturally.

---

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [March 27, 2018, 6:59pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/5 "2018-03-27T18:59:49Z")

</div>

@cuviper uom is a nice library, but doesn’t even get close to the power of what F#'s type system can achieve.

@scottmcm Is there a RFC for const generics that you believe would make it powerful enough to represent it? I agree that the only necessary components are static rationals (which we can probably have) and a form of ad-hoc type normalization to automatically determine e.g. that `km*h` is the same thing as `h*km` and that either can be divided by `km` to obtain `h`.

Oh, and the need for a base dimension in boost is very much non-compositional, so rather problematic.

---

<div class="post-metadata">

**Author:** ![Boddlnagg](https://avatars.discourse-cdn.com/v4/letter/b/aeb1de/32.png) [@Boddlnagg](https://internals.rust-lang.org/u/Boddlnagg)\
**Post date:** [March 27, 2018, 7:46pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/6 "2018-03-27T19:46:46Z")

</div>

Have you seen paholg’s [dimensioned](https://crates.io/crates/dimensioned) crate? It can already do quite a lot of what F# can offer, working around the lack of const generics by implementing their own type level integers.

---

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [March 27, 2018, 7:58pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/7 "2018-03-27T19:58:19Z")

</div>

Unless I’m mistaken, dimensioned won’t let you define custom units that interact properly with existing units, e.g. seconds per lap, dollars per gallon, etc.

Also, what kind of error messages do you get with dimensioned? Unless I’m mistaken, they are going to be very, very messy.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [March 27, 2018, 10:25pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/8 "2018-03-27T22:25:17Z")

</div>

Reading the paper, it seems F#'s units-of-measure has a key feature that a mere crate like `dimensioned` could never provide: **unification.**

You will never be able to convince rustc that the product of `Units<f64, A>` and `Units<f64, B>` has the same type as the product of `Units<f64, B>` and `Units<f64, A>`.

---

<div class="post-metadata">

**Author:** ![leonardo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leonardo/32/1344_2.png) [@leonardo](https://internals.rust-lang.org/u/leonardo)\
**Post date:** [March 27, 2018, 10:35pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/9 "2018-03-27T22:35:27Z")

</div>

> [@Yoric](#):
>
> as may be found in F#.

F# handling of units is nice, but I seem to remember it has some problems. If you want to design a language feature this small language could give you some ideas:

> **[Frink](http://futureboy.us/frinkdocs/)**
>
> A practical calculating tool, unit
> conversion utility, and programming language designed to make physical
> calculations simple. It tracks units of measure (feet, meters, kilograms,
> watts, etc.) through all calculations, and helps ensure that...

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [March 27, 2018, 10:39pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/10 "2018-03-27T22:39:34Z")

</div>

> [@ExpHP](#):
>
> You will never be able to convince rustc that the product of Units\<f64, A\> and Units\<f64, B\> has the same type as the product of Units\<f64, B\> and Units\<f64, A\>.

Can't you just write two impls: `impl Mul<Units<f64, B>> for Units<f64, A> { .. }`and `impl Mul<Units<f64, A>> for Units<f64, B> { .. }` with the same associated `Output` type?

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [March 27, 2018, 10:41pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/11 "2018-03-27T22:41:12Z")

</div>

FWIW the [`rink`](https://crates.io/crates/rink) crate claims to be similar to frink. (But I’m not familiar with either.)

---

<div class="post-metadata">

**Author:** ![leonardo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leonardo/32/1344_2.png) [@leonardo](https://internals.rust-lang.org/u/leonardo)\
**Post date:** [March 27, 2018, 10:42pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/12 "2018-03-27T22:42:51Z")

</div>

The Rust ecosystem is getting bigger than expected :-]

---

<div class="post-metadata">

**Author:** ![jmst](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jmst](https://internals.rust-lang.org/u/jmst)\
**Post date:** [March 27, 2018, 11:20pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/13 "2018-03-27T23:20:26Z")

</div>

> [@ExpHP](#):
>
> You will never be able to convince rustc that the product of Units\<f64, A\> and Units\<f64, B\> has the same type as the product of Units\<f64, B\> and Units\<f64, A\>.

Yeah, this is the fundamental problem of supporting units of measure in type systems.

I think it might be kind of possible (but I haven't tried it) to do it in either current Rust or current Rust + specialization by giving each quantity an unique typenum number, and then choosing the product order so that the one with the minimal number comes first.

Obviously this doesn't extend very well to third-party crates since those numbers must be unique, although one could perhaps encode the crate name as a type and use it as part of the comparison.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [March 27, 2018, 11:20pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/14 "2018-03-27T23:20:59Z")

</div>

> Can’t you just write two impls

No way, no how; sorry for the confusion, but A and B were meant to be type parameters.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [March 28, 2018, 12:15am UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/15 "2018-03-28T00:15:01Z")

</div>

> [@jmst](#):
>
> I think it might be kind of possible (but I haven’t tried it) to do it in either current Rust or current Rust + specialization by giving each quantity an unique typenum number, and then choosing the product order so that the one with the minimal number comes first.

This isn't what I mean. `dimensioned` already does this. For any two monomorphic units `A` and `B`, `Units<f64, A> * Units<f64, B>` and `Units<f64, B> * Units<f64, A>` **already are the same type.** (unit systems in `dimensioned` use type-level fixed-size arrays of integers; they are not extensible, so your other concerns are avoided)

But if you have a generic function with two type parameters `A` and `B`, then no matter what you do or how many where bounds you have, these two products will never be 100% interchangeable.

**...or so I was going to say.** When trying to construct an example, I rediscovered just how finicky unification in rust can feel. Much to my surprise, the compiler accepted the following function and all reasonable variations of it (swapping the order of various Prods or multiplications, as long as the bounds still suggest that `Prod<A, B> = Prod<B, A>`).

```rust
use std::ops::Mul;
type Prod<A, B> = <A as Mul<B>>::Output;

fn foo<A, B>(a: A, b: B) -> Prod<A, Prod<B, A>>
where
    B: Copy + Mul<A>,
    A: Copy + Mul<B, Output=Prod<B, A>>,
    A: Mul<Prod<A, B>>,
{ a * (b * a) }

```

That said:

1. Having to use `where` bounds to make `Prod<A, B>` and `Prod<B, A>` interchangeable affects your public signature, and in turn, all generic code that uses your function. If you need to rely on some other property of abelian groups in an update to the function then you are S.O.L with respect to backwards compatibility.

2. Just to show how crazy unification in rust is, here's one that does fail. Apparently, the trait bound `A: Mul<B, Output = AB>` does not make `AB` and `Prod<A, B>` interchangeable.

```rust
use std::ops::Mul;
type Prod<A, B> = <A as Mul<B>>::Output;

// error[E0277]: the trait bound `A: std::ops::Mul<AB>` is not satisfied
fn foo<A, B, AB>(a: A, b: B) -> Prod<Prod<A, Prod<B, A>>, B>
where
    B: Copy + Mul<A, Output = AB>,
    A: Copy + Mul<B, Output = AB>,
    A: Mul<Prod<A, B>>,
    Prod<A, AB>: Mul<B>,
{ a * (b * a) * b }

```

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [March 28, 2018, 1:27am UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/16 "2018-03-28T01:27:43Z")

</div>

Your last example can be rewritten as:

```rust
fn foo<A, B>(a: A, b: B) -> Prod<Prod<A, Prod<B, A>>, B>
where
    B: Copy + Mul<A>,
    A: Copy + Mul<B>,
    A: Mul<Prod<B, A>>,
    Prod<A, Prod<B, A>>: Mul<B>,
{ a * (b * a) * b }

```

But, yeah, its quite restricting and unergonomic to say at least. Also it will not allow to write the following function body with potential reordering of operations:

```rust
{
    match value {
        A => a1 * (b1 * a2) * b2,
        B => (a1 * b1) * a2 * b2,
        C => (a1 * a2 * a3 / a4) * b1 * b2,
    }
}

```

---

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [March 28, 2018, 3:31am UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/17 "2018-03-28T03:31:17Z")

</div>

@leonardo If I read frink’s docs correctly, its units of measure system is just as limited as dimensioned or uom.

Also, could you elaborate on the problems with F#'s units of measure system?

---

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [March 28, 2018, 3:32am UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/18 "2018-03-28T03:32:46Z")

</div>

And yes, the F# type system has a nice equivalence relation/normalization among units of measure, and unification for units of measure type variables, which cannot be implemented in today’s Rust as far as I can tell.

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [March 28, 2018, 7:59am UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/19 "2018-03-28T07:59:57Z")

</div>

Boost.Units (and the Units ISO C++ proposal) is pretty nice and to implement it the only thing one needs AFAICT is specialization. How is this better ?

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [March 28, 2018, 12:05pm UTC](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154/20 "2018-03-28T12:05:34Z")

</div>

While I haven’t stared too hard at Boost.Units, I rather doubt that any solution that is sufficient for C++ is going to be sufficient for Rust, because of the fundemental differences between C++ templates and rust generics. Unification is not a problem in C++.

[Next page](https://internals.rust-lang.org/t/any-rfc-for-units-of-measure/7154.md?page=2)
