aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/type/unit.lux
blob: ff6d3bb3abad4ea431c592f08991d486b08521f8 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
## TODO: Write tests ASAP.
(.module:
  [lux #*
   [abstract
    [monad (#+ Monad do)]
    [equivalence (#+ Equivalence)]
    [order (#+ Order)]
    [enum (#+ Enum)]]
   [control
    ["p" parser
     ["s" code (#+ Parser)]]]
   [data
    [text
     ["%" format (#+ format)]]]
   [macro
    ["." code]
    [syntax (#+ syntax:)
     ["|.|" export]
     ["|.|" annotations]]]
   [math
    [number
     ["i" int]
     ["." ratio (#+ Ratio)]]]
   [type
    abstract]])

(abstract: #export (Qty unit)
  Int
  
  (def: #export in
    (All [unit] (-> Int (Qty unit)))
    (|>> :abstraction))

  (def: #export out
    (All [unit] (-> (Qty unit) Int))
    (|>> :representation)))

(signature: #export (Scale s)
  (: (All [u] (-> (Qty u) (Qty (s u))))
     scale)
  (: (All [u] (-> (Qty (s u)) (Qty u)))
     de_scale)
  (: Ratio
     ratio))

(type: #export Pure
  (Qty []))

(type: #export (Per d n)
  (-> d n))

(type: #export (Inverse u)
  (|> Pure (Per u)))

(type: #export (Product p s)
  (|> s (Per (Inverse p))))

(def: #export pure
  (-> Int Pure)
  in)

(template [<name> <tag>]
  [(def: <name>
     (-> Text Text)
     (|>> (format "{" kind "@" module "}")
          (let [[module kind] (name_of <tag>)])))]
  
  [unit_name  #..Unit]
  [scale_name #..Scale]
  )

(syntax: #export (unit:
                   {export |export|.parser}
                   {name s.local_identifier}
                   {annotations (p.default |annotations|.empty |annotations|.parser)})
  (wrap (list (` (type: (~+ (|export|.write export)) (~ (code.local_identifier name))
                   (~ (|annotations|.write annotations))
                   (primitive (~ (code.text (unit_name name))))))
              (` (def: (~+ (|export|.write export)) (~ (code.local_identifier (format "@" name)))
                   (~ (code.local_identifier name))
                   (:assume [])))
              )))

(def: ratio^
  (Parser Ratio)
  (s.tuple (do p.monad
             [numerator s.int
              _ (p.assert (format "Numerator must be positive: " (%.int numerator))
                          (i.> +0 numerator))
              denominator s.int
              _ (p.assert (format "Denominator must be positive: " (%.int denominator))
                          (i.> +0 denominator))]
             (wrap [(.nat numerator) (.nat denominator)]))))

(syntax: #export (scale:
                   {export |export|.parser}
                   {name s.local_identifier}
                   {(^slots [#ratio.numerator #ratio.denominator]) ratio^}
                   {annotations (p.default |annotations|.empty |annotations|.parser)})
  (let [g!scale (code.local_identifier name)]
    (wrap (list (` (type: (~+ (|export|.write export)) ((~ g!scale) (~' u))
                     (~ (|annotations|.write annotations))
                     (primitive (~ (code.text (scale_name name))) [(~' u)])))
                (` (structure: (~+ (|export|.write export)) (~ (code.local_identifier (format "@" name)))
                     (..Scale (~ g!scale))
                     (def: (~' scale)
                       (|>> ..out
                            (i.* (~ (code.int (.int numerator))))
                            (i./ (~ (code.int (.int denominator))))
                            ..in))
                     (def: (~' de_scale)
                       (|>> ..out
                            (i.* (~ (code.int (.int denominator))))
                            (i./ (~ (code.int (.int numerator))))
                            ..in))
                     (def: (~' ratio)
                       [(~ (code.nat numerator)) (~ (code.nat denominator))])))
                ))))

(template [<name> <op>]
  [(def: #export (<name> param subject)
     (All [unit] (-> (Qty unit) (Qty unit) (Qty unit)))
     (|> subject out (<op> (out param)) in))]

  [u/+ i.+]
  [u/- i.-]
  )

(def: #export (u// param subject)
  (All [p s] (-> (Qty p) (Qty s) (|> (Qty s) (Per (Qty p)))))
  (function (_ input)
    (|> (out subject)
        (i.* (out input))
        (i./ (out param))
        in)))

(def: #export (u/* param subject)
  (All [p s] (-> (Qty p) (Qty s) (Product (Qty p) (Qty s))))
  (function (_ input)
    (|> subject
        out
        (i.* (out (input param)))
        in)))

(def: #export (re_scale from to quantity)
  (All [si so u] (-> (Scale si) (Scale so) (Qty (si u)) (Qty (so u))))
  (let [[numerator denominator] (ratio./ (\ from ratio)
                                         (\ to ratio))]
    (|> quantity
        out
        (i.* (.int numerator))
        (i./ (.int denominator))
        in)))

(scale: #export Kilo [+1         +1,000])
(scale: #export Mega [+1     +1,000,000])
(scale: #export Giga [+1 +1,000,000,000])

(scale: #export Milli [        +1,000 +1])
(scale: #export Micro [    +1,000,000 +1])
(scale: #export Nano  [+1,000,000,000 +1])

(unit: #export Gram)
(unit: #export Meter)
(unit: #export Litre)
(unit: #export Second)

(structure: #export equivalence (All [unit] (Equivalence (Qty unit)))
  (def: (= reference sample)
    (i.= (out reference) (out sample))))

(structure: #export order (All [unit] (Order (Qty unit)))
  (def: &equivalence ..equivalence)
  
  (def: (< reference sample)
    (i.< (out reference) (out sample))))

(structure: #export enum (All [unit] (Enum (Qty unit)))
  (def: &order ..order)
  (def: succ (|>> ..out inc ..in))
  (def: pred (|>> ..out dec ..in)))