Slide 1

Slide 1 text

Types for units of measure More type power to you

Slide 2

Slide 2 text

Quantity and Units 101

Slide 3

Slide 3 text

Physical Quantity A physical quantity is a property of a material or system that can be quantified by measurement. A physical quantity can be expressed as the combination of a magnitude and a unit.

Slide 4

Slide 4 text

Kitty White • Height • Stack of 5 apples • Weight • 3 apples

Slide 5

Slide 5 text

SI The International System of Units 9th edition 2019 It is most widely used system of measurement.

Slide 6

Slide 6 text

International System of Quantities Base quantity Symbol for dimension SI base unit SI unit symbol length L meter m mass M kilogram kg time T second s electric current I ampere A thermodynamic temperature Θ kelvin K amount of substance N mole mol luminous intensity J candela cd

Slide 7

Slide 7 text

Vector space [1] [] [1] [] [ ∙ ] [] [2] [] [] [] [] [] [] [−1]

Slide 8

Slide 8 text

Dimensional Analysis analysis of the relationships between different quantities

Slide 9

Slide 9 text

Dimensional Homogeneity • The most basic rule of dimensional analysis is dimensional homogeneity. • Only commensurable quantities (physical quantities having the same dimension) may be compared, equated, added, or subtracted. • In other words, any physically meaningful equation will have the same dimensions on its left and right sides. • This can be used to spot errors in formulae or calculations.

Slide 10

Slide 10 text

However, the dimensions form an abelian group under multiplication, so: • One may take ratios of incommensurable quantities (quantities with different dimensions), and multiply or divide them. • 1 + 2 is meaningless. • However, 1 ÷ 2 is fine.

Slide 11

Slide 11 text

An Abelian Group generated by MKS []: L:length, M:mass, T:time. , , ∈ ℕ. 100 = []: length 010 = []: mass 001 = []: time 10−1 = [/]: velocity

Slide 12

Slide 12 text

Meta-Programming CTSC (Compile Time Sanity Checking)

Slide 13

Slide 13 text

In practice • This code calculates volume, but... • Correct code // calc volume of a box double volume = x * y; // ^~~~~~ Oh... // calc volume of a box double volume = x * y * z;

Slide 14

Slide 14 text

Homogeneity checking at compile time • We already know type checking! • We need types to distinguish different quantities.

Slide 15

Slide 15 text

Q. How to Type A. Phantom Types C++ example: • a is a quantity which has int value 2 with dim L. • length is a phantom type. • It allows us to distinguish for each quantities. • Type params that are used only at compile time are called phantom types. quantity a = 2;

Slide 16

Slide 16 text

In practice (again) • Even if you code a meaningless expression, you will get a compile error. • A diagnostic message will then be displayed. // calc volume of a box quantity volume = x * y; // ^~~~~~~~~~~~~~~ ERROR! // compiler will show you diagnostic msg // (The message will be buried in a super long type name, but...)

Slide 17

Slide 17 text

Internal representation • Internal representation would be following form: Q. Is coefficient in ℕ? A. YES if only for four arithmetic operations, but NO if you want to allow power roots. quantity quantity<((M,1),(L,1),(T,-2)), double>

Slide 18

Slide 18 text

Conclusion • Units can be embedded in types as phantom types • This allows for sanity checks at compile time • If the sanity check fails, a compile error occurs • Get long error messages • These error messages are like your life • And nobody loves you

Slide 19

Slide 19 text

Continued on Tech Blog... Advanced topics in types and meta-programming

Slide 20

Slide 20 text

References • https://www.bipm.org/utils/common/pdf/si-brochure/SI-Brochure- 9.pdf • https://en.wikipedia.org/wiki/Physical_quantity • https://en.wikipedia.org/wiki/Dimension • https://en.wikipedia.org/wiki/Dimensional_analysis • https://en.wikipedia.org/wiki/Homogeneity_(physics) • https://en.wikipedia.org/wiki/Finitely_generated_abelian_group • https://github.com/LoliGothick/mitama-dimensional