physics: dimensions, quantities and constants

[ bsd3, library, science ] [ Propose Tags ]

Library to work with typed SI dimensions and common physical functions and constants.

[Skip to Readme]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS]
Change log
Dependencies base (>=4.8 && <5) [details]
License BSD-3-Clause
Copyright 2018, Florian Knupfer
Author Florian Knupfer
Category Science
Source repo head: git clone
Uploaded by knupfer at 2018-10-15T16:19:58Z
Distributions NixOS:
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 672 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-10-15 [all 1 reports]

Readme for physics-

[back to package description]


physics is a library for working with quantities in a performant and type safe manner. It provides some common constants and functions to work on quantities.


Quantities do not have an instance of Num to prevent multiplication without changing the units. Special functions are provided which substitute common operations like >+<, >-<, >/<, >*<.

The naming convention is that operators get on quantity facing sides a greater than or an less than sign. If you want to multiply a quantity with a number you can use *<.

Planck units

physics supports planck units, albeit for most cases they are not recommended. The standard used are SI units.

import Physics.Units
import qualified Physics.Units.Planck as P

main = do
  print lightspeed
  -- 299792458 m s⁻¹
  print $ fromSI lightspeed
  -- 1.0 mₚ sₚ⁻¹
  print . fromPlanck $ P.metre >/< P.second
  -- 2.99792458e8 m s⁻¹


Constants are divided into measured constants (which are sorted by increasing precision), exact constants (lightspeed for example) and derived constants. All derived constants are defined in terms of measured and exact constants and not simply as a number.

Because of this, updating constants is quite easy, only a small number of measured constants need to be updated when more precise measurements are available.


physics strives to be as performant as possible. Quantities are newtypes and operators are defined in terms of num operators.

Example usage

{-# LANGUAGE TypeOperators #-}

module Main where

import Physics.Units

main :: IO ()
main = do
  print $ fmap round momentum
  -- 992 m kg s⁻¹
  print $ fmap round kineticEnergy
  -- 5176 m² kg s⁻²

mass :: Kilogram Double
mass = 95 *< kilogram

time :: Second Double
time = 9.58 *< second

distance :: Metre Double
distance = 100 *< metre

speed :: (Metre >/< Second) Double
speed = distance >/< time

momentum :: (Newton >*< Second) Double
momentum = mass >*< speed

kineticEnergy :: Joule Double
kineticEnergy = 1/2 *< mass >*< square speed

-- This would be a type error:
-- kineticEnergy = 1/2 *< mass >*< speed


physics works with GHC 7.10.3 or newer. Some features like showing an Unit or nicer type errors are only available on newer ghcs.