Units of measure and type providers

ITT8060 Advanced Programming · Autumn 2026

Tallinn University of Technology

Full prose version with runnable examples: notes.html

ITT8060

Numbers with units

[<Measure>] type m            // metres
[<Measure>] type s            // seconds

let distanceTravelled (speed: float<m/s>) (time: float<s>) : float<m> =
  speed * time

A unit of measure is written in angle brackets after a number. It becomes part of the number's type.

Units are multiplied and divided along with the numbers: distanceTravelled 10.0<m/s> 60.0<s> is a float<m>.

ITT8060

Checked by the compiler, gone at run time

> 1.0<m> + 1.0<s>;;
error FS0001: The unit of measure 's' does not match the unit of measure 'm'

A wrong unit is a type error. The program does not compile.

let runtimeType = (box 2.0<m>).GetType().Name

"Double": units are erased. They cost nothing at run time, and a running program cannot ask a number for its unit.

ITT8060

Data outside the type system

Inside an F# program everything has a type.

Most data does not live inside the program: JSON from web services, CSV files, databases, workbooks.

None of them know about F# types.

A program has to get the data across that border.

ITT8060

Three ways across

Look it up by name. No types. A misspelt name fails at run time.

let tempByName = observation.GetProperty("temperature").AsFloat ()

Write the type by hand. Typed afterwards, but the record and the conversion are ours to write and to keep in step.

A type provider. The type is made from a sample of the data. A misspelt name is a compile-time error.

type ObservationJson = JsonProvider<observationJson>
ITT8060

What a type provider is

A component that the compiler loads while it is checking the program.

It is given static parameters in angle brackets: a sample, a file, the address of a service.

It answers with types. In the editor they are there at once: type a dot and look.

ITT8060

Compile time and run time

The provider runs at compile time. What it needs, it needs then: the sample, the file, the service.

The data is read at run time, by a library that comes with the provider.

let runtimeTypeOfProvided = provided.GetType().FullName

"FSharp.Data.Runtime.BaseTypes.JsonDocument": the provided type is erased. It exists for the compiler and the editor only.

ITT8060

Inferred from a sample

One observation of the sample has no humidity, and one temperature is a whole number:

val humidityOf: o: JsonProvider<...>.Root -> Option<int>
val temperatureOf: o: JsonProvider<...>.Root -> decimal

The provider knows the sample, not the service.

Data that differs from the sample fails when it is read:

"failed: '/humidity' is missing"

ITT8060

The providers of this lecture

JSON and CSV on a sample. The sample is text in the script.

The World Bank. The provider asks the service which countries and indicators there are, while the code is being checked.

Weather observations from open data. Three rows of the service's answer are the sample. The data is loaded when the program runs.

ITT8060

Units on data

A unit in the data is text: "°C", "MJ/m2". The compiler knows nothing about it.

We attach the measure where the data enters the program. From there on the compiler checks.

let mjPerKwh : float<MJ/kWh> = wPerKw * sPerH / jPerMj

3.6: the units of the three constants multiply out to megajoules per kilowatt-hour, or the line would not compile.

ITT8060

Data frames

A series maps keys to values. A frame is a collection of series that share their row keys.

let meanTemp : float = dataFrame?hourlyTemp |> Stats.mean

Gained: columns by name, missing values, ready-made statistics.

Given up: the units and the record type. The name of a column is a string again.

ITT8060

How a provider is written

A provider tells the compiler which types and members there are, and gives it the code that stands for each member.

Code as a value is a quotation:

let quoted = <@ 1 + 2 * 3 @>

let twice (e: Expr<int>) = <@ %e + %e @>

twice quoted is the code ((1 + (2 * 3)) + (1 + (2 * 3))): a function from code to code.

ITT8060

Design considerations

A sample or a schema? A sample says what was seen. A schema says what can occur.

What happens when the source changes?

A provider that asks a service: the code stops compiling.

A sample in the script: the code compiles, and fails when it reads.

A program that is built on a provider is built on the provider's source.

ITT8060

Summary

  1. A unit of measure is part of a number's type: the compiler checks it, and at run time it is gone.
  2. A type provider gives types to data that lives outside the program, while the program is being compiled.
  3. A provider infers types from a sample; data that differs from the sample fails when it is read.
  4. Provided types are erased: they exist for the compiler and the editor, not in the running program.
  5. A data frame gives up static types for columns by name, missing values and aggregates.
ITT8060

Bonus homework, reading and next week

The bonus homework is out this week.

Reading: Microsoft Learn, "Units of measure" and "Type providers"; the documentation of FSharp.Data and Deedle.

The account of type providers follows slides by Tomas Petricek.

Next week: functional programming and Lean.

ITT8060