Andrew Kennedy: F# Units of Measure

Download this episode

Download Video

Description

Floating point values in F# can have associated units of measure, which are typically used to indicate length, volume, mass, and so on. The built-in type float takes an optional unit-of-measure parameter, written in angle brackets, in a similar way that types such as IEnumerable take a type parameter, as in IEnumerable<int>.

By using quantities with units, you enable the compiler to verify that arithmetic relationships have the correct units, which helps prevent programming errors like the one that led to NASA's Mars Climate Orbiter being lost in September 1999. This was due to confusion between metric and so-called "English" units of measurement.  The accident could have been prevented if the NASA engineers had been able to annotate their program code with units, and then employed static analysis tools or language-level type-checking to detect and fix any unit errors.

Andrew Kennedy is an MSR research scientist who implemented units of measure for F#. What did this involve? How does it work, exactly? What's next? Meet Andrew and learn all about F#'s latest language feature, units of measure.

Enjoy.

Information sources: http://msdn.com and http://blogs.msdn.com/andrewkennedy/

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      exoteric

      I was very impressed when seeing units of measure in F# the first time and believe it's a great selling point for the language.

    • User profile image
      Richard.Hein

      Thanks, I find this very interesting.  I imagine it can be helpful in many situations. 

    • User profile image
      keithfl

      Very interesting ...and deep ... another nice interview Charles!

       

      We've seen Don Syme several times on C9 ... its nice to finally see Andrew Kennedy!

       

      However ...  the code is completely unreadable!!! Perplexed C'mon cant you guys ever use a larger font? Try 14 please!! Or Just use the new Zoom feature in the VS2010 editor!!

       

      I'd like to actually see the code!!!

       

       

    • User profile image
      Novox

      One of my favorite F# features. Thanks for this great interview!

      (and unfortunately keithfl is right about the unreadable code Expressionless )

    • User profile image
      aL_

      interesting Smiley so this is strictly a compile time feature? its not "generics" really, its sort of compile time generics?

      thats the impression i got Smiley

        

      --edit--

      also, is this just for floats? can a unit be any type? for instance if you have a class Person, could you create a unit of measure for Employee, manager and so on? (just as an example)

    • User profile image
      akenn

      Yes, this is strictly a compile-time feature, so units errors get caught at development time, and there is no runtime performance hit.

       

      It's for any types you like. Built-in types float, float32, decimal, and int can be parameterized on units, but you can build your own, e.g. vectors, complex numbers, matrices, or whatever. My blog (http://blogs.msdn.com/andrewkennedy) has more info.

    • User profile image
      akenn

      Ah - sorry about the unreadable code. I had been assuming that the screen would be captured in high-res. A lot of the examples that I used are also discussed on my blog. The Lunar Lander code was written by Phil Trelford but I'll put it back together and post it here shortly.

    • User profile image
      Charles

      My apologies for the poor resolution of the code demo on-screen. The way that we captured this is the problem. We are working on a solution to this general problem. See, we don't expect interviewees to have to install screen capture software on their laptops in order to record a demo in the studio. That said, we aren't yet sure of a practical solution that also affords least effort on the part of our guests. Working it out.

       

      Thank you in advance, Andrew, for sharing the code in project file format. The domo code should be much easier to read in Visual Studio or a text editor Smiley

       

      Again, we will work this out.

       

      C

    • User profile image
      aL_

      very cool.. we do alot of physics calculations for measuremnts on lasers so this could be very useful Smiley are you making any large changes in beta2 to this or is the beta1 functionality kinda stable?

    • User profile image
      keithfl

      Thanks for your reply ... Angel again ... a very interesting feature and a awesome interview!

       

    • User profile image
      sokhaty

      Measurements in F# is an exciting feature. No doubt about it. I was really impressed when I learned about it being added to the language.

      It surely addresses a lot of potential issues with measurement mismatch. 

       

      Yet, run time support for measurements still makes a lot of sense.

      If you are reading data from external sources at run-time (files, sensors, or web services), you'd still have to implemet all the measurement tracking and conversion yourself. If this can be married to contract somehow, then application would just have to tell the framework that it expects mass to be in kilos, and if input feed turns out to be pounds then conversion would happen under the scene.

       

      Another presumably useful feature would be to declare measures off of classes/types. If for example, I'm counting my chicken, I don't want to be able to inadvertently add this to the count of eggs, unless I explicitly coersed chicken and eggs to be "things".

       

      Anyhow, compile time support is a very good start.

    • User profile image
      dPruitt

      This is a feature that I have been waiting for.

       

      How is the UOM information able to be persited?  If I were serialize an object with UOM would the UOM be included in the XML?

       

      Also I read last year that this feature would also automatically take care of some of the unit conversion, say within a calculation.  Is this still possible or will we have to manage the unit conversion ourselves?

       

      Awsome stuff!

    • User profile image
      akenn

      It's fairly stable. Defining your own overloaded methods on unit-parameterized types is slightly easier, as type inference has been improved. For the product release, there will be a bunch of functions to "cast" into units (e.g. one with type float -> float<'u>) which are intended for interop with non-unit-aware code. (e.g. you might have a numeric library that you want to wrap with a units API.)

    • User profile image
      akenn

      The units-of-measure information is not persisted, as it is "erased" from types before runtime. (It's a purely static feature.)

       

      As for unit conversions, there are no plans for automatic conversions for this release of F#: you must define your own conversion functions.

    • User profile image
      Alex O

      >> See, we don't expect interviewees to have to install screen capture software on their laptops in order to record a demo in the studio. >> That said, we aren't yet sure of a practical solution that also affords least effort on the part of our guests

      Just Remote Desktop into the guest's laptop while recording the screencast on the client-side.

       

      Rant: why is Channel9 reliably not working with Firefox browsers?! Impossible to view videos or post replies. Bummer!

    • User profile image
      Charles

      Not sure about the FF issues. Can you be more specific? You could also run FF's dev tools and send the script error details Smiley

       

      Sorry you having a bad experience. We will look into this.

      C

    • User profile image
      Charles

      Comments open.

      C

    • User profile image
      David Phillip Oster

      C++ has had this a feature for decades, implemented as a template-library evaluated at compile time. See:  "The SI Library of Unit-Based Computation."
      Fermi National Accelerator Laboratory FERMILAB-Conf-98/328 
      "Introduction to the SI Library of Unit-Based Computation" by 
      Walter E. Brown 
      October 1998
      Presented at the International Conference on Computing in High Energy Physics (CHEP ’98), Chicago, Illinois, August 31-September 4, 1998
      When I got my copy, it was linked from  www.fnal.gov/fermitools
       
       

    • User profile image
      Vivek

      What David said!

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.