ITT8060 Advanced Programming · Autumn 2026
Tallinn University of Technology
Full prose version with runnable examples: notes.html
ITT8060[<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> 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.
ITT8060Inside 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.
ITT8060Look 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>
ITT8060A 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.
ITT8060The 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.
ITT8060One 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"
ITT8060JSON 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.
ITT8060A 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.
ITT8060A 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.
ITT8060A 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.
ITT8060A 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
ITT8060The 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