SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
Experience Report: Type‐checking 
Polymorphic Units for Astrophysics 
Research in Haskell 
Takayuki Muranushi 
Advanced Institute for 
Computational Science, 
RIKEN 
takayuki.muranushi@riken.jp 
Richard A. Eisenberg 
University of Pennsylvania 
eir@cis.upenn.edu
July 23, 
1983 . 
• Air Canada Flight 143, a Boeing 767‐233 was 
departing Montreal to Edmonton. 
• The fuel gauge is not working. 
• The crews use a backup system and manually 
calculated the amount of fuel to be refueled.
Manual calculation 
mass of 
fuel required density of fuel volume to be refueled 
÷ = 
[kg] [pound/L] [*L] 
The correct calculation 
mass of 
fuel required density of fuel volume to be refueled 
÷ = 
[kg] [kg/L] [L] 
Flight 143 took off with 22,300 pounds of fuel to 
Edmonton, where 22,300 kg was actually needed.
• Flight 143 made an emergency landing on 
runway 32L of Gimli abandoned airport. 
• It was “Family Day” festival; go‐carts, campers, 
families and barbecues were on 32L. 
• No one was seriously hurt nor killed. 
Wade H. Nelson (1997)
A same law of physics can be 
represented in many different units 
Mass of 
Density of fuel Volume to be 
fuel required Dimensions Level: 
Mass ÷ Density = 
Volume 
[kg] [kg/L] [L] 
÷ = 
[lb.] ÷ [lb./L] = [L] 
Units Level: 
÷ = refueled 
Quantity Level: 
quantity value = numerical value [ unit ] 
[kg] ÷ [lb./L] = [L]
“units‐of‐measure are to science what 
types are to programming” ‐‐‐ A. J. Kennedy 
• To avoid mistakes like Gimli Glider, we would 
like to use type system to enforce the 
correctness of the dimensions and units in our 
calculations. 
• Such correctness of “laws of physics” is more 
than just about specific set of units; we can 
represent one quantity in many different units, 
but they mean the same quantity.
“units‐of‐measure are to science what 
types are to programming” ‐‐‐ A. J. Kennedy 
“Laws of physics are dimension‐monomorphic 
and unit‐polymorphic” 
‐‐‐ T. Muranushi 
Dimensions Level: 
Mass ÷ Density = Volume 
[kg] ÷ [kg/L] = [L] 
[lb.] ÷ [lb./L] = [L] 
Units Level:
“units‐of‐measure are to science what 
types are to programming” ‐‐‐ A. J. Kennedy 
“Laws of physics are dimension‐monomorphic 
and unit‐polymorphic” 
‐‐‐ T. Muranushi 
• We already have type system of units for many 
languages including C, F#, simulink and of 
course in Haskell; we already have 
polymorphism. Will they blend?
Using `unittyped` by Thijs Alkemade, 
I started an attempt to encode knowledge 
of physics in Haskell.
A unit‐monomorphic quantity function 
refuel :: Fractional f => 
Value Mass KiloGram f 
‐> Value Density KiloGramPerLiter f 
‐> Value Volume Liter f 
refuel gasMass gasDen = gasMass |/| gasDen 
A unit‐polymorphic version? 
refuel :: Fractional f => 
Value Mass uniMass f ‐‐ Here we replace the unit types 
‐> Value Density uniDen f ‐‐ with type variables 
‐> Value Volume uniVol f ‐‐ 
refuel gasMass gasDen = gasMass |/| gasDen 
This code doesn’t compile.
This code 
refuel :: Fractional f => 
Value Mass uniMass f 
‐> Value Density uniDen f 
‐> Value Volume uniVol f 
refuel gasMass gasDen = gasMass |/| gasDen 
needs these annotations to compile: 
refuel :: (Fractional f, 
Convertible' Mass uniMass, 
Convertible' Density uniDen, 
Convertible' Volume uniVol, 
MapNeg negUniDen uniDen, ‐‐ negUniDen = 1 / uniDen 
MapMerge uniMass negUniDen uniVol ‐‐ uniMass * negUniDen = uniVol 
) => 
Value Mass uniMass f 
‐> Value Density uniDen f 
‐> Value Volume uniVol f 
refuel gasMass gasDen = gasMass |/| gasDen
Problem with unit polymorphism in `unittyped` 
too much type constraint!! 
Colors indicate: Type constraints, Types, Values 
refuel :: (Fractional f, 
Convertible' Mass umass, 
Convertible' Density uden, 
Convertible' Volume uvol, 
MapNeg negUden uden, 
MapMerge umass negUden uvol) => 
Value Mass umass f 
gravityPoisson :: 
(Fractional x 
, dimLen ~ LengthDimension 
, dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] 
, dimDen ~ Density 
, dimZhz ~ '[ '(Time, NTwo)] 
, Convertible' dimLen uniLen 
, Convertible' dimPot uniPot 
, Convertible' dimDen uniDen 
, Convertible' dimZhz uniZhz 
, Convertible' dimZhz uniZhz' 
, MapMerge dimLen dimLen dimLen2 
, MapNeg dimLen2 dimLenNeg2 
, MapMerge dimPot dimLenNeg2 dimZhz 
, MapMerge dimDen '[ '(Time, NTwo), '(Length, PThree), 
'(Mass, NOne) ] dimZhz 
, MapMerge uniLen uniLen uniLen2 
, MapNeg uniLen2 uniLenNeg2 
, MapMerge uniPot uniLenNeg2 uniZhz 
, MapMerge uniDen '[ '(Second, NTwo), '(Meter, 
PThree), '((Kilo Gram), NOne) ] uniZhz' 
) => 
(forall s. AD.Mode s => 
Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot 
uniPot (AD s x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimDen 
uniDen x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimZhz 
uniZhz x)) 
gravityPoisson gravitationalPotential density r 
= laplacian gravitationalPotential r |‐| (4 *| pi |*| 
density r |*| g) 
‐> Value Density uden f 
‐> Value Volume uvol f 
refuel gasMass gasDen = gasMass |/| gasDen 
gravityPoisson :: 
(Fractional x 
, dimLen ~ LengthDimension 
, dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] 
, dimDen ~ Density 
, dimZhz ~ '[ '(Time, NTwo)] 
, Convertible' dimLen uniLen 
, Convertible' dimPot uniPot 
, Convertible' dimDen uniDen 
, Convertible' dimZhz uniZhz 
, Convertible' dimZhz uniZhz' 
, MapMerge dimLen dimLen dimLen2 
, MapNeg dimLen2 dimLenNeg2 
, MapMerge dimPot dimLenNeg2 dimZhz 
, MapMerge dimDen '[ '(Time, NTwo), '(Length, PThree), '(Mass, NOne) ] dimZhz 
, MapMerge uniLen uniLen uniLen2 
, MapNeg uniLen2 uniLenNeg2 
, MapMerge uniPot uniLenNeg2 uniZhz 
, MapMerge uniDen '[ '(Second, NTwo), '(Meter, PThree), '((Kilo Gram), NOne) ] uniZhz' 
) => 
(forall s. AD.Mode s => 
Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimDen uniDen x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimZhz uniZhz x)) 
gravityPoisson gravitationalPotential density r 
= laplacian gravitationalPotential r |‐| (4 *| pi |*| density r |*| g) 
gravitationalPotentialToDensity :: 
forall x 
dimLen dimDen dimDen' dimLen2 dimNegLen2 dimZhz dimNegGC dimPot 
uniLen uniDen uniDen' uniLen2 uniNegLen2 uniZhz uniNegGC uniPot . 
(Fractional x 
, dimLen ~ LengthDimension 
, dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] 
, dimDen ~ Density 
, MapEq dimDen' dimDen 
, Convertible' dimLen uniLen 
, Convertible' dimPot uniPot 
, Convertible' dimZhz uniZhz 
, Convertible' dimDen' uniDen' 
, Convertible' dimDen uniDen 
, MapMerge dimLen dimLen dimLen2 
, MapNeg dimLen2 dimNegLen2 
, MapMerge dimPot dimNegLen2 dimZhz 
, MapNeg '[ '(Time, NTwo), '(Length, PThree), '(Mass, NOne) ] dimNegGC 
, dimNegGC ~ '[ '(Time, PTwo), '(Length, NThree), '(Mass, POne) ] 
, MapMerge dimZhz dimNegGC dimDen' 
, MapMerge uniLen uniLen uniLen2 
, MapNeg uniLen2 uniNegLen2 
, MapMerge uniPot uniNegLen2 uniZhz 
, MapNeg '[ '(Second, NTwo), '(Meter, PThree), '((Kilo Gram), NOne) ] uniNegGC 
, uniNegGC ~ '[ '(Second, PTwo), '(Meter, NThree), '((Kilo Gram), POne) ] 
, MapMerge uniZhz uniNegGC uniDen' 
) => 
(forall s. AD.Mode s => 
Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimDen uniDen x)) 
gravitationalPotentialToDensity gravitationalPotential r 
= to (undefined :: Value dimDen uniDen x) $ 
(laplacian gravitationalPotential r |/| (4 *| pi |*| g) :: Value dimDen' uniDen' x) 
hydrostatic :: 
forall x 
dimLen dimPre dimDen dimAcc dimGpr dimNegLen dimNegDen dimAcc' 
uniLen uniPre uniDen uniAcc uniGpr uniNegLen uniNegDen uniAcc' . 
( Fractional x 
, dimLen ~ LengthDimension 
, dimPre ~ Pressure 
, dimDen ~ Density 
, dimAcc ~ Acceleration 
, dimNegDen ~ '[ '(Length, PThree), '(Mass, NOne) ] 
, dimGpr ~ '[ '(Length, NTwo), '(Mass, POne) , '(Time, NTwo) ] 
, Convertible' dimLen uniLen 
, Convertible' dimPre uniPre 
, Convertible' dimGpr uniGpr 
, Convertible' dimDen uniDen 
, Convertible' dimAcc uniAcc 
, Convertible' dimAcc' uniAcc' 
, MapNeg dimLen dimNegLen 
, MapMerge dimPre dimNegLen dimGpr 
, MapNeg dimDen dimNegDen 
, MapMerge dimGpr dimNegDen dimAcc' 
, MapEq dimAcc' dimAcc 
, MapNeg uniLen uniNegLen 
, MapMerge uniPre uniNegLen uniGpr 
, MapNeg uniDen uniNegDen 
, MapMerge uniGpr uniNegDen uniAcc' 
) => 
(forall s. AD.Mode s => 
Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPre uniPre (AD s x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> Value dimDen uniDen x) 
‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc x)) 
hydrostatic pressure density externalAcc r 
= compose $ ¥i ‐> to (undefined :: Value dimAcc uniAcc x) $ 
(externalAcc r ! i) |+| (gradP r ! i) |/| (density r) 
where 
gradP :: Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimGpr uniGpr x) 
gradP = grad pressure 
pressureToAcc :: 
forall x 
dimLen dimPre dimDen dimAcc dimGpr dimNegLen dimNegDen dimAcc' 
uniLen uniPre uniDen uniGpr uniNegLen uniNegDen uniAcc' . 
( Fractional x 
, dimLen ~ LengthDimension 
, dimPre ~ Pressure 
, dimDen ~ Density 
, dimAcc ~ Acceleration 
, dimNegDen ~ '[ '(Length, PThree), '(Mass, NOne) ] 
, dimGpr ~ '[ '(Length, NTwo), '(Mass, POne) , '(Time, NTwo) ] 
, Convertible' dimLen uniLen 
, Convertible' dimPre uniPre 
, Convertible' dimGpr uniGpr 
, Convertible' dimDen uniDen 
, Convertible' dimAcc' uniAcc' 
, Convertible' dimAcc uniAcc' 
, MapNeg dimLen dimNegLen 
, MapMerge dimPre dimNegLen dimGpr 
, MapNeg dimDen dimNegDen 
, MapMerge dimGpr dimNegDen dimAcc' 
, MapEq dimAcc' dimAcc 
, MapNeg uniLen uniNegLen 
, MapMerge uniPre uniNegLen uniGpr 
, MapNeg uniDen uniNegDen 
, MapMerge uniGpr uniNegDen uniAcc' 
) => 
(forall s. AD.Mode s => 
Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPre uniPre (AD s x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> Value dimDen uniDen x) 
‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc' x)) 
pressureToAcc pressure density r 
= compose $ ¥i ‐> to (undefined :: Value dimAcc uniAcc' x) $ 
(gradP r ! i) |/| (density r) 
where 
gradP :: Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimGpr uniGpr x) 
gradP = grad pressure 
gravitationalPotentialToAcc :: 
forall x 
dimLen dimPot dimAcc' dimNegLen dimAcc 
uniLen uniPot uniAcc' uniNegLen 
( Fractional x 
, dimLen ~ LengthDimension 
, dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] 
, dimAcc' ~ '[ '(Length, POne), '(Time, NTwo)] 
, dimAcc ~ Acceleration 
, Convertible' dimLen uniLen 
, Convertible' dimPot uniPot 
, Convertible' dimAcc' uniAcc' 
, Convertible' dimAcc uniAcc' 
, MapNeg dimLen dimNegLen 
, MapMerge dimPot dimNegLen dimAcc' 
, MapEq dimAcc' dimAcc 
, MapNeg uniLen uniNegLen 
, MapMerge uniPot uniNegLen uniAcc' 
) => 
(forall s. AD.Mode s => 
Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) 
‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc' x)) 
gravitationalPotentialToAcc pot r = 
(fmap $ to (undefined :: Value dimAcc uniAcc x)) $ 
grad pot r 
We need at least two 
type constraints per one 
arithmetic operator, in 
order to encode type‐level 
unit calculations.
↑ ✈ Problem 
↓ ✈ Solution
Our solution:
Quantity representation in `unittyped` 
Type constructor takes (dimensions) (units) (numerical value) 
λ> :t 88 *| mile |/| hour 
… :: Fractional f => 
Value '[ '(Length, POne), '(Time, NOne)] 
'[ '(Mile, POne), '(Hour, NOne)] f 
Quantity representation in `units` 
Type constructor takes 
(dimensions) (map from dimensions to units) (numerical value ) 
λ> :t 88 % mile :/ hour :: Fractional f => Qu Velocity SI f 
… :: Fractional f => 
Qu '[ '(Length, One), '(Time, MOne)] 
'[ '(Length, Meter), '(Time, Second), …] f
System of Units as type argument 
• The map from dimensions to units represents 
a system of units; e.g. SI system, CGS 
(centimeter–gram–second) system, etc. 
λ> 88 % Miles :/ Hour :: Qu Velocity SI Float 
39.33952 m/s 
λ> :info SI 
type SI = MkLCSU 
'[(Length, Meter), (Mass, Kilo :@ Gram), (Time, Second), 
(Current, Ampere), (Temperature, Kelvin), 
(AmountOfSubstance, Mole), 
(LuminousIntensity, Lumen)] 
λ> :info CGS 
type CGS = MkLCSU 
'[(Length, Centi :@ Meter), (Mass, Gram), (Time, Second)]
[Def] A coherent system of unit ℓ 
ℓ = {u1, u2, … , un}∪ {u1 
p u2 
q …un 
r | p,q, … , r ∈ } 
base units units derived by products of base units 
• A Joule (1 [kg/m2s2]) is the coherent derived unit of 
energy in SI 
• An erg (1 [g/cm2s2] = 10-7J) is the coherent derived 
unit of energy in centimeter‐gram‐second system 
1:1 mapping between dimensions and units 
[kg] ÷ [kg/m3] = [m3] 
[SI mass]÷ [SI density] = [SI volume]
Unit‐polymorphic calculations in `units` 
refuel :: Fractional f => 
Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f 
refuel gasMass gasDen = gasMass |/| gasDen 
• local coherent system of unit ℓ is the only one, 
unconstrained, type variable 
• The computation is nondimensionalized; can be 
carried out without details units. 
[kg] ÷ [kg/m3] = [m3] 
[SI mass]÷ [SI density] = [SI volume]
• Unit polymorphism with fundeps gave rise to 
overwhelming complexes of constrained type 
variables 
refuel :: (Fractional f, 
Convertible' Mass umass, 
Convertible' Density uden, 
Convertible' Volume uvol, 
MapNeg negUden uden, 
MapMerge umass negUden uvol) => 
Value Mass umass f 
‐> Value Density uden f 
‐> Value Volume uvol f 
refuel gasMass gasDen = gasMass |/| gasDen 
• Take local coherent system of unit ℓ as only one 
free variable 
refuel :: Fractional f => 
Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f 
refuel gasMass gasDen = gasMass |/| gasDen
An application: 
Avoid over/underflow
Exercise: How many Newtons is the Lennard-Jones 
force F between two argon atoms at distance 4Å, 
where 
Solution #1: 
ljForce :: Energy ℓ Float ‐> Length ℓ Float 
‐> Length ℓ Float ‐> Force ℓ Float 
ljForce eps sigma r 
= (24 *| eps |*| sigma |ˆ pSix) |/| (r |ˆ pSeven) 
|‐|(48 *| eps |*| sigma |ˆ pTwelve) |/| (r |ˆ pThirteen) 
λ> let sigmaAr = 3.4e‐8 % Meter 
epsAr = 1.68e‐21 % Joule 
r = 4.0e‐8 % Meter 
λ> (ljForce epsAr sigmaAr r :: Force SI Float) # Newton 
NaN
Exercise: How many Newtons is the Lennard-Jones 
force F between two argon atoms at distance 4Å, 
where 
Solution #2: 
ljForce :: Energy ℓ Float ‐> Length ℓ Float 
‐> Length ℓ Float ‐> Force ℓ Float 
ljForce eps sigma r 
= (24 *| eps |*| sigma |ˆ pSix) |/| (r |ˆ pSeven) 
|‐|(48 *| eps |*| sigma |ˆ pTwelve) |/| (r |ˆ pThirteen) 
type CU = MkLCSU '[ '(Length, Angstrom), 
'(Mass, ProtonMass), '(Time, Pico :@ Second)] 
λ> (ljForce epsAr sigmaAr r :: Force CU Float) # Newton 
9.3407324e‐14
Astrophysics research in Haskell 
• A 27‐page astrophysics paper has been 
written in Haskell; its quantitative reasoning is 
powered by the units library.
↑ ✈ Experience 
↓ ✈ Conclusion
When you design type system of units 
consider unit polymorphism 
because, with unit polymorphism 
• We can faithfully express unit‐independent 
nature of laws of physics. 
• We can write quantity expressions, which 
users can later interpret in unit systems of 
their choice. 
• We can avoid overflows/ underflows by 
appropriately choosing system of units.
• An easy way to implement unit 
polymorphism is to take local coherent 
system of unit ℓ as only one free variable 
refuel :: Fractional f => 
Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f 
refuel gasMass gasDen = gasMass |/| gasDen
In the paper 
• Extensibility 
• Quantity combinators 
• Value‐level units 
• Protect numerical values from manipulation 
Things came after paper 
• defaultLCSU 
• Template Haskell
Comments 
• Haskell is such a cool language that allows 
something like `units` at all. Its type system is 
so programmable that these features can be built 
on top of, instead of being integrated in (like F#) 
or externally analyzed (like C) 
• As far as we know, `units` is the only practical 
system that supports unit polymorphism. 
• Heartfelt thanks to all people’s work that enabled 
GHC 7.8, and to Richard who implemented 
`units`.
↑ ✈ Thanks! 
↓ ✈ Questions? 
cabal install units 
and enjoy unit polymorphism! 
refuel :: Fractional f => 
Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f 
refuel gasMass gasDen = gasMass |/| gasDen

Weitere ähnliche Inhalte

Ähnlich wie Type-checking Polymorphic Units for Astrophysics Research in Haskell

Randomness conductors
Randomness conductorsRandomness conductors
Randomness conductors
wtyru1989
 
Manual de ingenieria civil
Manual de ingenieria civilManual de ingenieria civil
Manual de ingenieria civil
Jorge Eduardo RJ
 

Ähnlich wie Type-checking Polymorphic Units for Astrophysics Research in Haskell (20)

EES Functions and Procedures for Forced convection heat transfer
EES Functions and Procedures for Forced convection heat transferEES Functions and Procedures for Forced convection heat transfer
EES Functions and Procedures for Forced convection heat transfer
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
Presentation.pdf
Presentation.pdfPresentation.pdf
Presentation.pdf
 
Average Polynomial Time Complexity Of Some NP-Complete Problems
Average Polynomial Time Complexity Of Some NP-Complete ProblemsAverage Polynomial Time Complexity Of Some NP-Complete Problems
Average Polynomial Time Complexity Of Some NP-Complete Problems
 
Randomness conductors
Randomness conductorsRandomness conductors
Randomness conductors
 
KZ Spatial Waves Separations
KZ Spatial Waves SeparationsKZ Spatial Waves Separations
KZ Spatial Waves Separations
 
Fourier_Pricing_ICCF_2022.pdf
Fourier_Pricing_ICCF_2022.pdfFourier_Pricing_ICCF_2022.pdf
Fourier_Pricing_ICCF_2022.pdf
 
Traectory recent
Traectory recentTraectory recent
Traectory recent
 
CFD_notes.pdf
CFD_notes.pdfCFD_notes.pdf
CFD_notes.pdf
 
Boiling and Condensation heat transfer -- EES Functions and Procedures
Boiling and Condensation heat transfer -- EES Functions and ProceduresBoiling and Condensation heat transfer -- EES Functions and Procedures
Boiling and Condensation heat transfer -- EES Functions and Procedures
 
CGI2018 keynote - fluids simulation
CGI2018 keynote - fluids simulationCGI2018 keynote - fluids simulation
CGI2018 keynote - fluids simulation
 
Low-rank tensor approximation (Introduction)
Low-rank tensor approximation (Introduction)Low-rank tensor approximation (Introduction)
Low-rank tensor approximation (Introduction)
 
Presentation
PresentationPresentation
Presentation
 
S. Duplij, A q-deformed generalization of the Hosszu-Gluskin theorem
S. Duplij, A q-deformed generalization of the Hosszu-Gluskin theoremS. Duplij, A q-deformed generalization of the Hosszu-Gluskin theorem
S. Duplij, A q-deformed generalization of the Hosszu-Gluskin theorem
 
Manual de ingenieria civil
Manual de ingenieria civilManual de ingenieria civil
Manual de ingenieria civil
 
Cheatsheet recurrent-neural-networks
Cheatsheet recurrent-neural-networksCheatsheet recurrent-neural-networks
Cheatsheet recurrent-neural-networks
 
Subquad multi ff
Subquad multi ffSubquad multi ff
Subquad multi ff
 
S08 chap6 web
S08 chap6 webS08 chap6 web
S08 chap6 web
 
Generalized Nonlinear Models in R
Generalized Nonlinear Models in RGeneralized Nonlinear Models in R
Generalized Nonlinear Models in R
 

Kürzlich hochgeladen

Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Sérgio Sacani
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Sérgio Sacani
 
biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
1301aanya
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
Silpa
 
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxTHE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
ANSARKHAN96
 

Kürzlich hochgeladen (20)

Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
PSYCHOSOCIAL NEEDS. in nursing II sem pptx
PSYCHOSOCIAL NEEDS. in nursing II sem pptxPSYCHOSOCIAL NEEDS. in nursing II sem pptx
PSYCHOSOCIAL NEEDS. in nursing II sem pptx
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
 
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIACURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptx
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditions
 
Zoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdfZoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdf
 
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICEPATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
 
Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS ESCORT SERVICE In Bhiwan...
Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS  ESCORT SERVICE In Bhiwan...Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS  ESCORT SERVICE In Bhiwan...
Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS ESCORT SERVICE In Bhiwan...
 
Factory Acceptance Test( FAT).pptx .
Factory Acceptance Test( FAT).pptx       .Factory Acceptance Test( FAT).pptx       .
Factory Acceptance Test( FAT).pptx .
 
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate ProfessorThyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxTHE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
 

Type-checking Polymorphic Units for Astrophysics Research in Haskell

  • 1. Experience Report: Type‐checking Polymorphic Units for Astrophysics Research in Haskell Takayuki Muranushi Advanced Institute for Computational Science, RIKEN takayuki.muranushi@riken.jp Richard A. Eisenberg University of Pennsylvania eir@cis.upenn.edu
  • 2. July 23, 1983 . • Air Canada Flight 143, a Boeing 767‐233 was departing Montreal to Edmonton. • The fuel gauge is not working. • The crews use a backup system and manually calculated the amount of fuel to be refueled.
  • 3. Manual calculation mass of fuel required density of fuel volume to be refueled ÷ = [kg] [pound/L] [*L] The correct calculation mass of fuel required density of fuel volume to be refueled ÷ = [kg] [kg/L] [L] Flight 143 took off with 22,300 pounds of fuel to Edmonton, where 22,300 kg was actually needed.
  • 4. • Flight 143 made an emergency landing on runway 32L of Gimli abandoned airport. • It was “Family Day” festival; go‐carts, campers, families and barbecues were on 32L. • No one was seriously hurt nor killed. Wade H. Nelson (1997)
  • 5. A same law of physics can be represented in many different units Mass of Density of fuel Volume to be fuel required Dimensions Level: Mass ÷ Density = Volume [kg] [kg/L] [L] ÷ = [lb.] ÷ [lb./L] = [L] Units Level: ÷ = refueled Quantity Level: quantity value = numerical value [ unit ] [kg] ÷ [lb./L] = [L]
  • 6. “units‐of‐measure are to science what types are to programming” ‐‐‐ A. J. Kennedy • To avoid mistakes like Gimli Glider, we would like to use type system to enforce the correctness of the dimensions and units in our calculations. • Such correctness of “laws of physics” is more than just about specific set of units; we can represent one quantity in many different units, but they mean the same quantity.
  • 7. “units‐of‐measure are to science what types are to programming” ‐‐‐ A. J. Kennedy “Laws of physics are dimension‐monomorphic and unit‐polymorphic” ‐‐‐ T. Muranushi Dimensions Level: Mass ÷ Density = Volume [kg] ÷ [kg/L] = [L] [lb.] ÷ [lb./L] = [L] Units Level:
  • 8. “units‐of‐measure are to science what types are to programming” ‐‐‐ A. J. Kennedy “Laws of physics are dimension‐monomorphic and unit‐polymorphic” ‐‐‐ T. Muranushi • We already have type system of units for many languages including C, F#, simulink and of course in Haskell; we already have polymorphism. Will they blend?
  • 9. Using `unittyped` by Thijs Alkemade, I started an attempt to encode knowledge of physics in Haskell.
  • 10. A unit‐monomorphic quantity function refuel :: Fractional f => Value Mass KiloGram f ‐> Value Density KiloGramPerLiter f ‐> Value Volume Liter f refuel gasMass gasDen = gasMass |/| gasDen A unit‐polymorphic version? refuel :: Fractional f => Value Mass uniMass f ‐‐ Here we replace the unit types ‐> Value Density uniDen f ‐‐ with type variables ‐> Value Volume uniVol f ‐‐ refuel gasMass gasDen = gasMass |/| gasDen This code doesn’t compile.
  • 11. This code refuel :: Fractional f => Value Mass uniMass f ‐> Value Density uniDen f ‐> Value Volume uniVol f refuel gasMass gasDen = gasMass |/| gasDen needs these annotations to compile: refuel :: (Fractional f, Convertible' Mass uniMass, Convertible' Density uniDen, Convertible' Volume uniVol, MapNeg negUniDen uniDen, ‐‐ negUniDen = 1 / uniDen MapMerge uniMass negUniDen uniVol ‐‐ uniMass * negUniDen = uniVol ) => Value Mass uniMass f ‐> Value Density uniDen f ‐> Value Volume uniVol f refuel gasMass gasDen = gasMass |/| gasDen
  • 12. Problem with unit polymorphism in `unittyped` too much type constraint!! Colors indicate: Type constraints, Types, Values refuel :: (Fractional f, Convertible' Mass umass, Convertible' Density uden, Convertible' Volume uvol, MapNeg negUden uden, MapMerge umass negUden uvol) => Value Mass umass f gravityPoisson :: (Fractional x , dimLen ~ LengthDimension , dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] , dimDen ~ Density , dimZhz ~ '[ '(Time, NTwo)] , Convertible' dimLen uniLen , Convertible' dimPot uniPot , Convertible' dimDen uniDen , Convertible' dimZhz uniZhz , Convertible' dimZhz uniZhz' , MapMerge dimLen dimLen dimLen2 , MapNeg dimLen2 dimLenNeg2 , MapMerge dimPot dimLenNeg2 dimZhz , MapMerge dimDen '[ '(Time, NTwo), '(Length, PThree), '(Mass, NOne) ] dimZhz , MapMerge uniLen uniLen uniLen2 , MapNeg uniLen2 uniLenNeg2 , MapMerge uniPot uniLenNeg2 uniZhz , MapMerge uniDen '[ '(Second, NTwo), '(Meter, PThree), '((Kilo Gram), NOne) ] uniZhz' ) => (forall s. AD.Mode s => Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimDen uniDen x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimZhz uniZhz x)) gravityPoisson gravitationalPotential density r = laplacian gravitationalPotential r |‐| (4 *| pi |*| density r |*| g) ‐> Value Density uden f ‐> Value Volume uvol f refuel gasMass gasDen = gasMass |/| gasDen gravityPoisson :: (Fractional x , dimLen ~ LengthDimension , dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] , dimDen ~ Density , dimZhz ~ '[ '(Time, NTwo)] , Convertible' dimLen uniLen , Convertible' dimPot uniPot , Convertible' dimDen uniDen , Convertible' dimZhz uniZhz , Convertible' dimZhz uniZhz' , MapMerge dimLen dimLen dimLen2 , MapNeg dimLen2 dimLenNeg2 , MapMerge dimPot dimLenNeg2 dimZhz , MapMerge dimDen '[ '(Time, NTwo), '(Length, PThree), '(Mass, NOne) ] dimZhz , MapMerge uniLen uniLen uniLen2 , MapNeg uniLen2 uniLenNeg2 , MapMerge uniPot uniLenNeg2 uniZhz , MapMerge uniDen '[ '(Second, NTwo), '(Meter, PThree), '((Kilo Gram), NOne) ] uniZhz' ) => (forall s. AD.Mode s => Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimDen uniDen x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimZhz uniZhz x)) gravityPoisson gravitationalPotential density r = laplacian gravitationalPotential r |‐| (4 *| pi |*| density r |*| g) gravitationalPotentialToDensity :: forall x dimLen dimDen dimDen' dimLen2 dimNegLen2 dimZhz dimNegGC dimPot uniLen uniDen uniDen' uniLen2 uniNegLen2 uniZhz uniNegGC uniPot . (Fractional x , dimLen ~ LengthDimension , dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] , dimDen ~ Density , MapEq dimDen' dimDen , Convertible' dimLen uniLen , Convertible' dimPot uniPot , Convertible' dimZhz uniZhz , Convertible' dimDen' uniDen' , Convertible' dimDen uniDen , MapMerge dimLen dimLen dimLen2 , MapNeg dimLen2 dimNegLen2 , MapMerge dimPot dimNegLen2 dimZhz , MapNeg '[ '(Time, NTwo), '(Length, PThree), '(Mass, NOne) ] dimNegGC , dimNegGC ~ '[ '(Time, PTwo), '(Length, NThree), '(Mass, POne) ] , MapMerge dimZhz dimNegGC dimDen' , MapMerge uniLen uniLen uniLen2 , MapNeg uniLen2 uniNegLen2 , MapMerge uniPot uniNegLen2 uniZhz , MapNeg '[ '(Second, NTwo), '(Meter, PThree), '((Kilo Gram), NOne) ] uniNegGC , uniNegGC ~ '[ '(Second, PTwo), '(Meter, NThree), '((Kilo Gram), POne) ] , MapMerge uniZhz uniNegGC uniDen' ) => (forall s. AD.Mode s => Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> (Value dimDen uniDen x)) gravitationalPotentialToDensity gravitationalPotential r = to (undefined :: Value dimDen uniDen x) $ (laplacian gravitationalPotential r |/| (4 *| pi |*| g) :: Value dimDen' uniDen' x) hydrostatic :: forall x dimLen dimPre dimDen dimAcc dimGpr dimNegLen dimNegDen dimAcc' uniLen uniPre uniDen uniAcc uniGpr uniNegLen uniNegDen uniAcc' . ( Fractional x , dimLen ~ LengthDimension , dimPre ~ Pressure , dimDen ~ Density , dimAcc ~ Acceleration , dimNegDen ~ '[ '(Length, PThree), '(Mass, NOne) ] , dimGpr ~ '[ '(Length, NTwo), '(Mass, POne) , '(Time, NTwo) ] , Convertible' dimLen uniLen , Convertible' dimPre uniPre , Convertible' dimGpr uniGpr , Convertible' dimDen uniDen , Convertible' dimAcc uniAcc , Convertible' dimAcc' uniAcc' , MapNeg dimLen dimNegLen , MapMerge dimPre dimNegLen dimGpr , MapNeg dimDen dimNegDen , MapMerge dimGpr dimNegDen dimAcc' , MapEq dimAcc' dimAcc , MapNeg uniLen uniNegLen , MapMerge uniPre uniNegLen uniGpr , MapNeg uniDen uniNegDen , MapMerge uniGpr uniNegDen uniAcc' ) => (forall s. AD.Mode s => Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPre uniPre (AD s x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> Value dimDen uniDen x) ‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc x)) hydrostatic pressure density externalAcc r = compose $ ¥i ‐> to (undefined :: Value dimAcc uniAcc x) $ (externalAcc r ! i) |+| (gradP r ! i) |/| (density r) where gradP :: Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimGpr uniGpr x) gradP = grad pressure pressureToAcc :: forall x dimLen dimPre dimDen dimAcc dimGpr dimNegLen dimNegDen dimAcc' uniLen uniPre uniDen uniGpr uniNegLen uniNegDen uniAcc' . ( Fractional x , dimLen ~ LengthDimension , dimPre ~ Pressure , dimDen ~ Density , dimAcc ~ Acceleration , dimNegDen ~ '[ '(Length, PThree), '(Mass, NOne) ] , dimGpr ~ '[ '(Length, NTwo), '(Mass, POne) , '(Time, NTwo) ] , Convertible' dimLen uniLen , Convertible' dimPre uniPre , Convertible' dimGpr uniGpr , Convertible' dimDen uniDen , Convertible' dimAcc' uniAcc' , Convertible' dimAcc uniAcc' , MapNeg dimLen dimNegLen , MapMerge dimPre dimNegLen dimGpr , MapNeg dimDen dimNegDen , MapMerge dimGpr dimNegDen dimAcc' , MapEq dimAcc' dimAcc , MapNeg uniLen uniNegLen , MapMerge uniPre uniNegLen uniGpr , MapNeg uniDen uniNegDen , MapMerge uniGpr uniNegDen uniAcc' ) => (forall s. AD.Mode s => Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPre uniPre (AD s x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> Value dimDen uniDen x) ‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc' x)) pressureToAcc pressure density r = compose $ ¥i ‐> to (undefined :: Value dimAcc uniAcc' x) $ (gradP r ! i) |/| (density r) where gradP :: Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimGpr uniGpr x) gradP = grad pressure gravitationalPotentialToAcc :: forall x dimLen dimPot dimAcc' dimNegLen dimAcc uniLen uniPot uniAcc' uniNegLen ( Fractional x , dimLen ~ LengthDimension , dimPot ~ '[ '(Time, NTwo), '(Length, PTwo)] , dimAcc' ~ '[ '(Length, POne), '(Time, NTwo)] , dimAcc ~ Acceleration , Convertible' dimLen uniLen , Convertible' dimPot uniPot , Convertible' dimAcc' uniAcc' , Convertible' dimAcc uniAcc' , MapNeg dimLen dimNegLen , MapMerge dimPot dimNegLen dimAcc' , MapEq dimAcc' dimAcc , MapNeg uniLen uniNegLen , MapMerge uniPot uniNegLen uniAcc' ) => (forall s. AD.Mode s => Vec3 (Value dimLen uniLen (AD s x)) ‐> Value dimPot uniPot (AD s x)) ‐> (Vec3 (Value dimLen uniLen x) ‐> Vec3 (Value dimAcc uniAcc' x)) gravitationalPotentialToAcc pot r = (fmap $ to (undefined :: Value dimAcc uniAcc x)) $ grad pot r We need at least two type constraints per one arithmetic operator, in order to encode type‐level unit calculations.
  • 13. ↑ ✈ Problem ↓ ✈ Solution
  • 15. Quantity representation in `unittyped` Type constructor takes (dimensions) (units) (numerical value) λ> :t 88 *| mile |/| hour … :: Fractional f => Value '[ '(Length, POne), '(Time, NOne)] '[ '(Mile, POne), '(Hour, NOne)] f Quantity representation in `units` Type constructor takes (dimensions) (map from dimensions to units) (numerical value ) λ> :t 88 % mile :/ hour :: Fractional f => Qu Velocity SI f … :: Fractional f => Qu '[ '(Length, One), '(Time, MOne)] '[ '(Length, Meter), '(Time, Second), …] f
  • 16. System of Units as type argument • The map from dimensions to units represents a system of units; e.g. SI system, CGS (centimeter–gram–second) system, etc. λ> 88 % Miles :/ Hour :: Qu Velocity SI Float 39.33952 m/s λ> :info SI type SI = MkLCSU '[(Length, Meter), (Mass, Kilo :@ Gram), (Time, Second), (Current, Ampere), (Temperature, Kelvin), (AmountOfSubstance, Mole), (LuminousIntensity, Lumen)] λ> :info CGS type CGS = MkLCSU '[(Length, Centi :@ Meter), (Mass, Gram), (Time, Second)]
  • 17. [Def] A coherent system of unit ℓ ℓ = {u1, u2, … , un}∪ {u1 p u2 q …un r | p,q, … , r ∈ } base units units derived by products of base units • A Joule (1 [kg/m2s2]) is the coherent derived unit of energy in SI • An erg (1 [g/cm2s2] = 10-7J) is the coherent derived unit of energy in centimeter‐gram‐second system 1:1 mapping between dimensions and units [kg] ÷ [kg/m3] = [m3] [SI mass]÷ [SI density] = [SI volume]
  • 18. Unit‐polymorphic calculations in `units` refuel :: Fractional f => Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f refuel gasMass gasDen = gasMass |/| gasDen • local coherent system of unit ℓ is the only one, unconstrained, type variable • The computation is nondimensionalized; can be carried out without details units. [kg] ÷ [kg/m3] = [m3] [SI mass]÷ [SI density] = [SI volume]
  • 19. • Unit polymorphism with fundeps gave rise to overwhelming complexes of constrained type variables refuel :: (Fractional f, Convertible' Mass umass, Convertible' Density uden, Convertible' Volume uvol, MapNeg negUden uden, MapMerge umass negUden uvol) => Value Mass umass f ‐> Value Density uden f ‐> Value Volume uvol f refuel gasMass gasDen = gasMass |/| gasDen • Take local coherent system of unit ℓ as only one free variable refuel :: Fractional f => Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f refuel gasMass gasDen = gasMass |/| gasDen
  • 20. An application: Avoid over/underflow
  • 21. Exercise: How many Newtons is the Lennard-Jones force F between two argon atoms at distance 4Å, where Solution #1: ljForce :: Energy ℓ Float ‐> Length ℓ Float ‐> Length ℓ Float ‐> Force ℓ Float ljForce eps sigma r = (24 *| eps |*| sigma |ˆ pSix) |/| (r |ˆ pSeven) |‐|(48 *| eps |*| sigma |ˆ pTwelve) |/| (r |ˆ pThirteen) λ> let sigmaAr = 3.4e‐8 % Meter epsAr = 1.68e‐21 % Joule r = 4.0e‐8 % Meter λ> (ljForce epsAr sigmaAr r :: Force SI Float) # Newton NaN
  • 22. Exercise: How many Newtons is the Lennard-Jones force F between two argon atoms at distance 4Å, where Solution #2: ljForce :: Energy ℓ Float ‐> Length ℓ Float ‐> Length ℓ Float ‐> Force ℓ Float ljForce eps sigma r = (24 *| eps |*| sigma |ˆ pSix) |/| (r |ˆ pSeven) |‐|(48 *| eps |*| sigma |ˆ pTwelve) |/| (r |ˆ pThirteen) type CU = MkLCSU '[ '(Length, Angstrom), '(Mass, ProtonMass), '(Time, Pico :@ Second)] λ> (ljForce epsAr sigmaAr r :: Force CU Float) # Newton 9.3407324e‐14
  • 23. Astrophysics research in Haskell • A 27‐page astrophysics paper has been written in Haskell; its quantitative reasoning is powered by the units library.
  • 24. ↑ ✈ Experience ↓ ✈ Conclusion
  • 25. When you design type system of units consider unit polymorphism because, with unit polymorphism • We can faithfully express unit‐independent nature of laws of physics. • We can write quantity expressions, which users can later interpret in unit systems of their choice. • We can avoid overflows/ underflows by appropriately choosing system of units.
  • 26. • An easy way to implement unit polymorphism is to take local coherent system of unit ℓ as only one free variable refuel :: Fractional f => Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f refuel gasMass gasDen = gasMass |/| gasDen
  • 27. In the paper • Extensibility • Quantity combinators • Value‐level units • Protect numerical values from manipulation Things came after paper • defaultLCSU • Template Haskell
  • 28. Comments • Haskell is such a cool language that allows something like `units` at all. Its type system is so programmable that these features can be built on top of, instead of being integrated in (like F#) or externally analyzed (like C) • As far as we know, `units` is the only practical system that supports unit polymorphism. • Heartfelt thanks to all people’s work that enabled GHC 7.8, and to Richard who implemented `units`.
  • 29. ↑ ✈ Thanks! ↓ ✈ Questions? cabal install units and enjoy unit polymorphism! refuel :: Fractional f => Qu Mass ℓ f ‐> Qu Density ℓ f ‐> Qu Volume ℓ f refuel gasMass gasDen = gasMass |/| gasDen