2

One of the serious use case for units of measure is matrix operations: The inner product of a matrix a*b with c*d is only valid when b = c, etc....

However, I dont see any constructs for 'composite units' such as the one that would be required.

Do we have any way of having this checked by the type system in F# ?

nicolas
  • 9,549
  • 3
  • 39
  • 83

1 Answers1

4

disclaimer: sent from phone => can contain errors

I'd say that this check in some way can be achieved using UoM or phantom types:

[<Measure>] type s
type M<[<Measure>]'w, [<Measure>] 'h>() = 
    static member (*) (a : M<'a, 't>, b : M<'t, 'b>) : M<'a, 'b> = failwith "NYI"
let x = M<s ^ 3, s ^ 3>()
let y = M<s ^ 3, s>()
let z  = x * y // M<s ^ 3, s> 

However question is: how convinient it will be to use...

Community
  • 1
  • 1
desco
  • 16,642
  • 1
  • 45
  • 56
  • interesting. i'd really use it, if nothing else inside functions, as I imagine it can be a burden. with matrix calculus, i'd say that it is like the type system in F# : if you get the dimension correct, and the general flow is not absurd, your program is correct 99% of the time. – nicolas Jun 30 '12 at 22:39
  • Without a way to parameterize with a symbol like n instead of static like 3 here, it has limited use. from my pov, allowing such feature would be a massive win for scientific computation. – nicolas Jul 01 '12 at 08:06
  • @Desco I think the problem with UoM is you don't know at runtime the values, so how do you implement a function init():M<'R,'C> ? Since you can't read the dimensions, the user should specify also in values R and C : init(r,c):M<'R,'C> – Gus Jul 02 '12 at 12:16