fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
344 stars 20 forks source link

Allow units of measure in sequence ranges #535

Open marklam opened 7 years ago

marklam commented 7 years ago

I propose we extend sequence expressions to handle ranges involving units of measure. Currently we cannot do the following:

let maximumDistance = 3<km>
let minimumDistance = 1<km>
let trials = [ minimumDistance .. maximumDistance ]

The existing way of approaching this problem in F# is to generate a range from bare integers and then map a multiplication to add the dimension

Pros and Cons

The advantages of making this adjustment to F# are

The disadvantages of making this adjustment to F# are

Extra informtion

Estimated cost (XS, S, M, L, XL, XXL): M? (because of the added complication of min .. step .. max)

Related suggestions: (put links to reated suggestions here) Allow units of measure in for loops #534

Affadavit (must be submitted)

Please tick this by placing a cross in the box:

Please tick all that apply:

gusty commented 7 years ago

Looks like the problem is that units of measure don't support the GenericOne operator, possibly because being a measure, which is basically a multiplication, 'zero' is an absolute value but 'one' is relative to the measure.

Since the (..) operator uses the GenericOne it doesn't work, of course you can write this:

let trials = [ minimumDistance .. 1<km> .. maximumDistance ]

Alternatively, or if this suggestion doesn't get approved, you can suggest to add GenericOne to a library that offers language extensions, like FSharpPlus which will redefine some generic operators, like (..) so the code you posted would work, but will be interesting to understand the reason why it wasn't added to F# in first place.

marklam commented 7 years ago

Cool, I didn't know that adding measure to the steps would work!

dsyme commented 7 years ago

@gmpl You are correct about the original reasons for not allowing GenericOne on a generic unit

However there's no reason not to allow it on non-generic units. So we should really just approve the suggestion I think and find a way to make it happen

charlesroddie commented 2 years ago

These extensions are being considered rather ad-hoc in order to make certain patterns shorter, irrespective of whether there is a correspondence to mathematical/physical units and whether the brevity gives a false sense of simplicity.

  1. int<'m> does not have anything like a mathematical One. If there were a multiplicative identity in int<'m>, call it o, then o*o=o, but o*o is in int<'m^2> and o is in int<'m> so this is impossible. So GenericOne should not exist for int<'m>.
  2. The existence of a multiplicative identity does not imply the existence of a natural successor function. Natural successor functions exist in integers and natural numbers but not in real numbers, while multiplicative identities exist in all of these. In this regard the fact that for x = 1. to 3. currently doesn't compile is correct and the fact that [1. .. 3.] currently compiles is a mistake.
  3. int<'m> exists in F# but not in regular use of units, and the semantics are unclear. integers<'m> is more like a subset of reals<'m> than than a projection of integers. The code [1<km> .. 3<km>], means "start with 1km, notice that we are only dealing with a set where valid items are multiples of 1km, find the nearest greater valid item in this set", which is conceptually more complex than [1..3] |> List.map LanguagePrimitives.Int32WithMeasure<km>. The latter is nearer to the mathematical formulation { (f i)km : i in Z }, where f is the natural injection from integers to reals.