<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Andrew Kennedy's Blog : Types</title><link>http://blogs.msdn.com/andrewkennedy/archive/tags/Types/default.aspx</link><description>Tags: Types</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Units of Measure in F#: Part Three, Generic Units</title><link>http://blogs.msdn.com/andrewkennedy/archive/2008/09/04/units-of-measure-in-f-part-three-generic-units.aspx</link><pubDate>Thu, 04 Sep 2008 13:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8885070</guid><dc:creator>andrewkennedy</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/andrewkennedy/comments/8885070.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewkennedy/commentrss.aspx?PostID=8885070</wfw:commentRss><description>&lt;P&gt;In the first two articles in this series, we saw how to declare base and derived units, introduce constants with units, define our own conversion functions, and have F# check the units for us.&lt;/P&gt;
&lt;P&gt;But what if we're writing code that doesn't care what units it is working in? That's the subject of today's article.&lt;/P&gt;
&lt;P&gt;Let's start simple. What is the type of &lt;FONT face="courier new"&gt;fun x -&amp;gt; x*x&lt;/FONT&gt;? Well, multiplication is overloaded, so F# defaults to integers:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_2.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb.png" width=173 height=82 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;But if we annotate the argument, we can define squaring for all kinds of floats (hovering over the last of these to show its type):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_6.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_2.png" width=320 height=121 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;This looks painful! What if we don't tell F# what the units are, using the underscore notation to say "find me its units"?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_8.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_3.png" width=263 height=83 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;F# has inferred a &lt;EM&gt;generic&lt;/EM&gt; unit type for squaring. The notation &lt;FONT face="courier new"&gt;'u&lt;/FONT&gt; looks like a type parameter, but is in fact a &lt;EM&gt;unit-of-measure &lt;/EM&gt;parameter that can be instantiated at any units. Let's use it on masses, lengths and speeds:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_10.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_4.png" width=216 height=115 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;F# can infer generic unit-of-measure types with type annotations required only to resolve overloading. Here are some simple examples, tested out in F# Interactive:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_12.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_5.png" width=378 height=176 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Here's one that requires a bit of head-scratching to understand:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_14.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_6.png" width=396 height=80 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_6.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Returning to reality, suppose we want to sum the elements of a list. In true functional style, we use one of the &lt;A href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29" mce_href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29"&gt;fold&lt;/A&gt; operators.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_16.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_7.png" width=336 height=80 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Oops - we don't seem to have a nice generic type! The reason is simple: unless units are specified, constants, including zero, are assumed to have no units, i.e. to be dimensionless. So instead, let's give &lt;FONT face="courier new"&gt;0.0&lt;/FONT&gt; some units, but not tell F# what they are, by writing &lt;FONT face="courier new"&gt;0.0&amp;lt;_&amp;gt;&lt;/FONT&gt;:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_18.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_8.png" width=343 height=78 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_8.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's better!&lt;/P&gt;
&lt;P&gt;Now we can go wild and write all sorts of statistical stuff.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_20.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_9.png" width=811 height=409 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_9.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And here are the beautiful types that are inferred for these functions:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_22.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_10.png" width=622 height=185 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_10.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Summing up: &lt;/STRONG&gt;we've seen how to write &lt;EM&gt;code&lt;/EM&gt; that is &lt;EM&gt;generic &lt;/EM&gt;(a.k.a. as &lt;EM&gt;polymorphic&lt;/EM&gt;) in units-of-measure.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Next time&lt;EM&gt;, &lt;/EM&gt;&lt;/STRONG&gt;we'll write &lt;EM&gt;types&lt;/EM&gt; that are generic in units-of-measure. This makes the feature &lt;EM&gt;extensible&lt;/EM&gt;: if &lt;FONT face="courier new"&gt;float&lt;/FONT&gt;s with units aren't what you want, just define your own types!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8885070" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units+of+Measure/default.aspx">Units of Measure</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Types/default.aspx">Types</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units/default.aspx">Units</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Generics/default.aspx">Generics</category></item><item><title>Units of Measure in F#: Part Two, Unit Conversions</title><link>http://blogs.msdn.com/andrewkennedy/archive/2008/09/02/units-of-measure-in-f-part-two-unit-conversions.aspx</link><pubDate>Tue, 02 Sep 2008 11:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8888469</guid><dc:creator>andrewkennedy</dc:creator><slash:comments>20</slash:comments><comments>http://blogs.msdn.com/andrewkennedy/comments/8888469.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewkennedy/commentrss.aspx?PostID=8888469</wfw:commentRss><description>&lt;P&gt;In today's article I'll show you how to use different systems of units, convert between units, and interface with non-unit-aware code. &lt;/P&gt;
&lt;H3&gt;PowerPack goodies&lt;/H3&gt;
&lt;P&gt;First, though, let's have a look at some handy definitions provided in the F# PowerPack. To access this DLL, you'll need to reference if from your project. If you're working in Visual Studio, right-click on References in the Solution Explorer window.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_12.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb.png" width=293 height=93 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Now select FSharp.PowerPack and click OK.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_14.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_5.png" width=472 height=56 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;If you're working in FSharp Interactive, type &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_16.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_6.png" width=194 height=21 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_6.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Now you'll have at your disposal the complete set of SI &lt;A href="http://en.wikipedia.org/wiki/SI_base_unit" mce_href="http://en.wikipedia.org/wiki/SI_base_unit"&gt;base&lt;/A&gt; and &lt;A href="http://en.wikipedia.org/wiki/SI_derived_unit" mce_href="http://en.wikipedia.org/wiki/SI_derived_unit"&gt;derived&lt;/A&gt; units, defined &lt;FONT face="Courier new"&gt;Microsoft.FSharp.Math.SI&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_20.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_8.png" width=558 height=171 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_8.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Also available are various &lt;A href="http://physics.nist.gov/cuu/Constants/index.html" mce_href="http://physics.nist.gov/cuu/Constants/index.html"&gt;physical constants&lt;/A&gt;, defined in &lt;FONT face="courier new"&gt;Microsoft.FSharp.Math.PhysicalConstants&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_24.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_24.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_10.png" width=753 height=263 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_10.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Multiple unit systems&lt;/H3&gt;
&lt;P&gt;So for physicists, at least, there is no excuse to go non-metric. But what if you insist? No problem! Here is the example from Part One, using feet instead of metres as the unit of length.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_26.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_26.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_11.png" width=608 height=235 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_11.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if you need to convert between feet and metres? First, define a conversion factor.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_6.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_2.png" width=271 height=25 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;What are the units of &lt;FONT face="Courier new"&gt;feetPerMetre&lt;/FONT&gt;? Answer: feet per metre (doh!), or &lt;FONT face="courier new"&gt;ft/m&lt;/FONT&gt; for short. Now we can convert distances...&lt;BR&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_8.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_3.png" width=605 height=80 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;...and speeds...&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_10.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_4.png" width=527 height=83 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;...and we can convert back the other way by multiplying instead of dividing:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_30.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_30.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_13.png" width=649 height=29 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_13.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;As far as F# is concerned, &lt;FONT face="courier new"&gt;ft&lt;/FONT&gt; and &lt;FONT face="courier new"&gt;m&lt;/FONT&gt; have nothing to do with each other. It's up to you, the programmer, to define appropriate conversion factors. But the presence of units on the conversion factors makes mistakes much less likely. For example, what happens if I divide instead of multiply above? The type of the result suggests that something is awry, and will probably lead to a compile-time error later in the code:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_32.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_32.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_14.png" width=658 height=77 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_14.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;It's probably a good idea to package up conversion factors with the unit-of-measure to which they relate. A convenient way to do this is to add a &lt;EM&gt;static member&lt;/EM&gt; to the unit-of-measure "type":&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_34.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_34.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_15.png" width=338 height=68 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_15.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Now we can just write &lt;FONT face="courier new"&gt;ft.perMetre.&lt;/FONT&gt;&lt;/P&gt;
&lt;H3&gt;Interfacing non-unit-aware code&lt;/H3&gt;
&lt;P&gt;In Part One, we saw how to use syntax such as &lt;FONT face="courier new"&gt;2.0&amp;lt;s&amp;gt;&lt;/FONT&gt; to introduce units-of-measure into the types of floating-point values. But what if a quantity is stored in a file, or entered by the user through a GUI, or in a web form? In that case it'll probably start out life as a string, to be parsed and converted into a &lt;FONT face="Courier New"&gt;float&lt;/FONT&gt;. How can we convert a vanilla &lt;FONT face="courier new"&gt;float&lt;/FONT&gt; into, say, a &lt;FONT face="courier new"&gt;float&amp;lt;s&amp;gt;&lt;/FONT&gt;? Easy: just multiply by &lt;FONT face="courier new"&gt;1.0&amp;lt;s&amp;gt;&lt;/FONT&gt;! Here's an example:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_22.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_7.png" width=366 height=111 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;If we want to convert back to a vanilla &lt;FONT face="courier new"&gt;float&lt;/FONT&gt;, say, to pass to a non-unit-aware .NET method, we just divide by &lt;FONT face="courier new"&gt;1.0&amp;lt;s&amp;gt;&lt;/FONT&gt;:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_28.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_28.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_9.png" width=539 height=31 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoUnitConversions_F004/image_thumb_9.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H3&gt;Dimensionless quantities&lt;/H3&gt;
&lt;P&gt;But hang on a minute - what's going on with that last example? The variable &lt;FONT face="Courier New"&gt;timeInSeconds&lt;/FONT&gt; has type &lt;FONT face="courier new"&gt;float&amp;lt;s&amp;gt;&lt;/FONT&gt;, and we divided it by &lt;FONT face="courier new"&gt;1.0&amp;lt;s&amp;gt;&lt;/FONT&gt; which has type &lt;FONT face="courier new"&gt;float&amp;lt;s&amp;gt;&lt;/FONT&gt;. So the units cancel out, producing units which we write simply as the digit &lt;FONT face="courier new"&gt;1&lt;/FONT&gt;. Hence the type of &lt;FONT face="courier new"&gt;timeInSeconds / 1.0&amp;lt;s&amp;gt;&lt;/FONT&gt; is &lt;FONT face="courier new"&gt;float&amp;lt;1&amp;gt;&lt;/FONT&gt;. Such a quantity is called &lt;A href="http://en.wikipedia.org/wiki/Dimensionless_number" mce_href="http://en.wikipedia.org/wiki/Dimensionless_number"&gt;dimensionless&lt;/A&gt;. Conveniently, F# defines the ordinary &lt;FONT face="courier new"&gt;float&lt;/FONT&gt; type to be an alias for &lt;FONT face="courier new"&gt;float&amp;lt;1&amp;gt;,&lt;/FONT&gt;using the definition &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="courier new"&gt;type float = float&amp;lt;1&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;which makes use of overloading on the &lt;EM&gt;arity&lt;/EM&gt; (= number of parameters) of the type. (Overloading is used to similar good effect with the non-generic .NET type &lt;FONT face="courier new"&gt;System.Collections.IEnumerable&lt;/FONT&gt; and its generic variant &lt;FONT face="courier new"&gt;System.Collections.Generic.IEnumerable&lt;/FONT&gt;).&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Summing up&lt;/STRONG&gt;,&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;we've learnt about unit conversions of various kinds, between different unit systems, and between ordinary floats and floats-with-units.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Next time &lt;/STRONG&gt;we'll look at &lt;EM&gt;generic &lt;/EM&gt;unit types, or: what is a good type for &lt;FONT face="courier new"&gt;fun x -&amp;gt; x*x&lt;/FONT&gt;?&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8888469" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units+of+Measure/default.aspx">Units of Measure</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Types/default.aspx">Types</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units/default.aspx">Units</category></item><item><title>Units of Measure in F#: Part One, Introducing Units</title><link>http://blogs.msdn.com/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx</link><pubDate>Fri, 29 Aug 2008 20:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8881307</guid><dc:creator>andrewkennedy</dc:creator><slash:comments>24</slash:comments><comments>http://blogs.msdn.com/andrewkennedy/comments/8881307.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewkennedy/commentrss.aspx?PostID=8881307</wfw:commentRss><description>&lt;P&gt;Do you remember NASA's Mars Climate Orbiter? It was &lt;A href="http://www.cnn.com/TECH/space/9909/30/mars.metric.02/" mce_href="http://www.cnn.com/TECH/space/9909/30/mars.metric.02/"&gt;lost&lt;/A&gt; in September 1999 because of a confusion between &lt;A href="http://en.wikipedia.org/wiki/Metric_system" mce_href="http://en.wikipedia.org/wiki/Metric_system"&gt;metric&lt;/A&gt; and so-called "&lt;A href="http://en.wikipedia.org/wiki/English_unit" mce_href="http://en.wikipedia.org/wiki/English_unit"&gt;English&lt;/A&gt;" &lt;A href="http://en.wikipedia.org/wiki/Units_of_measurement" mce_href="http://en.wikipedia.org/wiki/Units_of_measurement"&gt;units of measurement&lt;/A&gt;. The &lt;A href="http://mars.jpl.nasa.gov/msp98/news/mco991110.html" mce_href="http://mars.jpl.nasa.gov/msp98/news/mco991110.html"&gt;report&lt;/A&gt; into the disaster made many recommendations. In all likelihood, 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.&lt;/P&gt;
&lt;P&gt;Of course, the languages that NASA used had no language or tool support for units. Many people have suggested ways of extending programming languages with support for static checking of units-of-measure. It's even possible to abuse the rich type systems of existing languages such as &lt;A href="http://tuoml.sourceforge.net/" mce_href="http://tuoml.sourceforge.net/"&gt;C++&lt;/A&gt; and &lt;A href="http://code.google.com/p/dimensional/" mce_href="http://code.google.com/p/dimensional/"&gt;Haskell&lt;/A&gt; to achieve it, but at some cost in usability. More recently, Sun's &lt;A href="http://research.sun.com/projects/plrg/Fortress/overview.html" mce_href="http://research.sun.com/projects/plrg/Fortress/overview.html"&gt;Fortress&lt;/A&gt; programming language has included type system support for &lt;A href="http://en.wikipedia.org/wiki/Physical_quantity" mce_href="http://en.wikipedia.org/wiki/Physical_quantity"&gt;dimensions&lt;/A&gt; and units. It also happens to have been the subject of my &lt;A href="http://research.microsoft.com/~akenn/units/ProgrammingLanguagesAndDimensions.pdf" mce_href="http://research.microsoft.com/~akenn/units/ProgrammingLanguagesAndDimensions.pdf"&gt;PhD thesis&lt;/A&gt; from a few years ago. So it's particularly exciting for me that a feature which I studied &lt;EM&gt;in theory&lt;/EM&gt; in 1995 will now get used &lt;EM&gt;in practice&lt;/EM&gt; in F#.&lt;/P&gt;
&lt;P&gt;As recently announced in the September 2008 F# CTP (Community Technical Preview), the &lt;A href="http://msdn.microsoft.com/fsharp" mce_href="http://msdn.microsoft.com/fsharp"&gt;F#&lt;/A&gt; programming language now has full support for static checking &lt;EM&gt;and inference&lt;/EM&gt; of units-of-measure. In this series of articles I'll gently introduce the feature. (If you're not familiar with F#, look &lt;A class="" href="http://msdn.microsoft.com/en-gb/fsharp/cc835246.aspx" mce_href="http://msdn.microsoft.com/en-gb/fsharp/cc835246.aspx"&gt;here&lt;/A&gt;.) We've already been testing out the units-of-measure feature inside Microsoft and I'm amazed at the diversity of applications that are turning up. Of course, there are the obvious applications to scientific computing, and games (which are all about physics, after all), but we're seeing applications in &lt;A href="http://research.microsoft.com/research/mlp/" mce_href="http://research.microsoft.com/research/mlp/"&gt;machine learning&lt;/A&gt;, finance, search (think click rates, etc) and others.&lt;/P&gt;
&lt;P&gt;We'll start more traditionally, with units for mass, length and time. As it's the 21st century I'll use the modern incarnation of the metric system, the &lt;A href="http://en.wikipedia.org/wiki/SI" mce_href="http://en.wikipedia.org/wiki/SI"&gt;International System of Units&lt;/A&gt;, abbreviated &lt;A href="http://www.bipm.org/en/si/" mce_href="http://www.bipm.org/en/si/"&gt;SI&lt;/A&gt; from the French "Le &lt;STRONG&gt;S&lt;/STRONG&gt;ystème &lt;STRONG&gt;I&lt;/STRONG&gt;nternational d'Unités". &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_4.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 25px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_1.png" width=163 height=64 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_1.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here, the &lt;FONT face="Courier New"&gt;Measure&lt;/FONT&gt; attribute tells F# that &lt;FONT face="Courier New"&gt;kg&lt;/FONT&gt;, &lt;FONT face="courier new"&gt;s&lt;/FONT&gt; and &lt;FONT face="courier new"&gt;m&lt;/FONT&gt; aren't really types in the usual sense of the word, but are used to build units-of-measure. (You'll see in Part Two of this series of articles that they can have static members just like ordinary types in F#, but other than that they are quite different). &lt;/P&gt;
&lt;P&gt;Now let's introduce some constants with their units, which we can do simply by tacking the units onto a floating-point number, in between angle brackets:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_12.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 25px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_5.png" width=433 height=42 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Notice how conventional notation for units is used, with / for dividing, and ^ for powers. Juxtaposition just means "multiply", and negative powers can be used in place of division, so you might prefer to express the units of acceleration slightly differently:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_7.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_7.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb.png" width=433 height=27 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Or I might invite the wrath of my old physics teacher by writing it this way:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_16.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_2.png" width=417 height=27 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Now we can do some physics! If I jump out of my window, at what speed will I hit the ground?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_14.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 25px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_6.png" width=579 height=25 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_6.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;You could try this in F# Interactive and see the result, or print it out. (It's about 8.3 metres per second. Ouch!) But more interestingly, hover the cursor over the variable and look at its type:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 25px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_8.png" width=585 height=77 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_8.png"&gt;&lt;/P&gt;
&lt;P&gt;Magic! Units-of-measure are not just handy comments-on-constants: they are there &lt;EM&gt;in the types&lt;/EM&gt; of values, and, what's more, the F# compiler knows the &lt;EM&gt;rules of units&lt;/EM&gt;. To be more precise: the built-in type &lt;FONT face="courier new"&gt;float&lt;/FONT&gt; takes an optional unit-of-measure &lt;EM&gt;parameter&lt;/EM&gt;, written in angle brackets, in a similar way that types such as &lt;FONT face="courier new"&gt;IEnumerable&lt;/FONT&gt; take a &lt;EM&gt;type&lt;/EM&gt; parameter, as in &lt;FONT face="courier new"&gt;IEnumerable&amp;lt;int&amp;gt;&lt;/FONT&gt;. When values of floating-point type are multiplied, the units are multiplied too; when they are divided, the units are divided too, and when taking square roots, the same is done to the units. So by the rule for multiplication, the expression inside &lt;FONT face="courier new"&gt;sqrt&lt;/FONT&gt; above must have units &lt;FONT face="courier new"&gt;m^2/s^2&lt;/FONT&gt;, and therefore the units of &lt;FONT face="courier new"&gt;speedOfImpact&lt;/FONT&gt; must be&lt;FONT face="courier new"&gt; m/s&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;What if I make a mistake?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_20.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 25px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_9.png" width=802 height=56 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_9.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;I've tried to add a height to an acceleration, so F# tells me exactly what I've done wrong. The units don't match up, and it tells me so!&lt;/P&gt;
&lt;P&gt;Now let's do a little more physics. What force does the ground exert on me to maintain my stationary position?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_22.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 25px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_10.png" width=383 height=103 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_10.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We've just applied &lt;A href="http://en.wikipedia.org/wiki/Newton%27s_laws_of_motion" mce_href="http://en.wikipedia.org/wiki/Newton%27s_laws_of_motion"&gt;Newton's Second Law of motion&lt;/A&gt;. Being such a &lt;A href="http://en.wikipedia.org/wiki/Great_man_theory" mce_href="http://en.wikipedia.org/wiki/Great_man_theory"&gt;Great Man&lt;/A&gt;, &lt;A href="http://en.wikipedia.org/wiki/Isaac_Newton" mce_href="http://en.wikipedia.org/wiki/Isaac_Newton"&gt;Sir Isaac&lt;/A&gt; had a unit named after him: the &lt;A href="http://en.wikipedia.org/wiki/Newton" mce_href="http://en.wikipedia.org/wiki/Newton"&gt;newton&lt;/A&gt;, the SI unit of force. So instead of the cumbersome &lt;FONT face="courier new"&gt;kg m/s^2&lt;/FONT&gt; we can introduce a &lt;EM&gt;derived unit&lt;/EM&gt; and just write &lt;FONT face="courier new"&gt;N&lt;/FONT&gt;, the standard symbol for newtons.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_24.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_24.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px 0px 0px 20px; BORDER-RIGHT-WIDTH: 0px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_11.png" width=426 height=51 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartOneIntroducingUnits_A131/image_thumb_11.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Derived units are just like type aliases: as far as F# is concerned, &lt;FONT face="courier new"&gt;N&lt;/FONT&gt; and &lt;FONT face="courier new"&gt;kg m/s^2&lt;/FONT&gt; mean exactly the same thing.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Summing up. &lt;/STRONG&gt;We've seen how to introduce &lt;EM&gt;base units&lt;/EM&gt;, how to introduce &lt;EM&gt;derived units&lt;/EM&gt;, and how to introduce &lt;EM&gt;constants with units&lt;/EM&gt;. Everything else has followed automatically: F# checks that units are used consistently, it rejects code with unit errors, and it infers units for code that's unit-correct.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Next time &lt;/STRONG&gt;we'll have a look at multiple unit systems, converting between units, and interfacing with non-unit-aware code.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8881307" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units+of+Measure/default.aspx">Units of Measure</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Types/default.aspx">Types</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units/default.aspx">Units</category></item></channel></rss>