-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuantity.idr
53 lines (37 loc) · 1.52 KB
/
Quantity.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Quantity
import public Dimension
%access public export
%default total
Quantity : Dimensions -> Type
Quantity = Dimensioned Double
interface Quantifiable val (dim : Dimensions) | val where
quantify : val -> Quantity dim
baseUnit : (n : String) -> Quantity [(Dim n 1)]
baseUnit n = WithDim 1 (makeDimension n)
dimensionless : Double -> Quantity []
dimensionless n = WithDim n []
Quantifiable Double [] where
quantify val = dimensionless val
Quantifiable Integer [] where
quantify val = dimensionless (fromInteger val)
Quantifiable (Quantity dim) dim where
quantify val = val
asNumber : Quantity [] -> Double
asNumber (WithDim n []) = n
infixr 10 #
infixr 7 ??
(??) : Quantity n -> Quantity n -> Double
(??) (WithDim a _) (WithDim b _) = a / b
syntax [quantity] "in" [units] = quantity ?? units
(+) : (Quantifiable t1 d, Quantifiable t2 d) => t1 -> t2 -> Quantity d
x + y = case (quantify x, quantify y) of
((WithDim a d), (WithDim b d)) => WithDim (a + b) d
(-) : (Quantifiable t1 d, Quantifiable t2 d) => t1 -> t2 -> Quantity d
x - y = case (quantify x, quantify y) of
((WithDim a d), (WithDim b d)) => WithDim (a - b) d
(*) : (Quantifiable t1 d1, Quantifiable t2 d2) => t1 -> t2 -> Quantity (d1 * d2)
x * y = case (quantify x, quantify y) of
((WithDim a d1), (WithDim b d2)) => WithDim (a*b) (d1*d2)
(/) : (Quantifiable t1 d1, Quantifiable t2 d2) => t1 -> t2 -> Quantity (d1 / d2)
x / y = case (quantify x, quantify y) of
((WithDim a d1), (WithDim b d2)) => WithDim (a / b) (d1 / d2)