Andrew Kennedy: F# Units of Measure
- Posted: Sep 22, 2009 at 2:00 PM
- 46,222 Views
- 19 Comments
Download
How do I download the videos?
- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”
Why should I download videos from Channel9?
- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!
Which version should I choose?
- If you want to view the video on your PC, Xbox or Media Center, download the High Quality WMV file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality WMV file.
- If you have a Zune, WP7, iPhone, iPad, or iPod device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.
Right click “Save as…”
- High Quality WMV (PC, Xbox, MCE)
- MP3 (Audio only)
- MP4 (iPod, Zune HD)
- Mid Quality WMV (Lo-band, Mobile)
- WMV (WMV Video)
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/
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.
Follow the Discussion
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.
Thanks, I find this very interesting. I imagine it can be helpful in many situations.
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!!!
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!!!
One of my favorite F# features. Thanks for this great interview!
(and unfortunately keithfl is right about the unreadable code
)
interesting
so this is strictly a compile time feature? its not "generics" really, its sort of compile time generics?
thats the impression i got
--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)
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.
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.
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
Again, we will work this out.
C
very cool.. we do alot of physics calculations for measuremnts on lasers so this could be very useful
are you making any large changes in beta2 to this or is the beta1 functionality kinda stable?
Thanks for your reply ...
again ... a very interesting feature and a awesome interview!
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.
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!
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.)
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.
>> 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!
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
Sorry you having a bad experience. We will look into this.
C
Comments open.
C
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
What David said!
Remove this comment
Remove this thread
close