blob: 2aa332dc6fc6ac4f1956b179781f62d2704e9e2c (
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
|
(.module: {#.doc "Rational arithmetic."}
[lux #*
[math]
[control
[equivalence (#+ Equivalence)]
[order]
number
codec
monad
["p" parser]]
[data
[number ("nat/" Codec<Text,Nat>)]
[text ("text/" Monoid<Text>)
format]
["E" error]
[product]
[maybe]]
["." macro
[code]
["s" syntax (#+ syntax: Syntax)]]])
(type: #export Ratio
{#numerator Nat
#denominator Nat})
(def: (normalize (^slots [#numerator #denominator]))
(-> Ratio Ratio)
(let [common (math.n/gcd numerator denominator)]
{#numerator (n// common numerator)
#denominator (n// common denominator)}))
(def: #export (* param input)
(-> Ratio Ratio Ratio)
(normalize [(n/* (get@ #numerator param)
(get@ #numerator input))
(n/* (get@ #denominator param)
(get@ #denominator input))]))
(def: #export (/ param input)
(-> Ratio Ratio Ratio)
(normalize [(n/* (get@ #denominator param)
(get@ #numerator input))
(n/* (get@ #numerator param)
(get@ #denominator input))]))
(def: #export (+ param input)
(-> Ratio Ratio Ratio)
(normalize [(n/+ (n/* (get@ #denominator input)
(get@ #numerator param))
(n/* (get@ #denominator param)
(get@ #numerator input)))
(n/* (get@ #denominator param)
(get@ #denominator input))]))
(def: #export (- param input)
(-> Ratio Ratio Ratio)
(normalize [(n/- (n/* (get@ #denominator input)
(get@ #numerator param))
(n/* (get@ #denominator param)
(get@ #numerator input)))
(n/* (get@ #denominator param)
(get@ #denominator input))]))
(def: #export (% param input)
(-> Ratio Ratio Ratio)
(let [quot (n// (n/* (get@ #denominator input)
(get@ #numerator param))
(n/* (get@ #denominator param)
(get@ #numerator input)))]
(- (update@ #numerator (n/* quot) param)
input)))
(def: #export (= param input)
(-> Ratio Ratio Bit)
(and (n/= (get@ #numerator param)
(get@ #numerator input))
(n/= (get@ #denominator param)
(get@ #denominator input))))
(do-template [<name> <op>]
[(def: #export (<name> param input)
(-> Ratio Ratio Bit)
(and (<op> (n/* (get@ #denominator input)
(get@ #numerator param))
(n/* (get@ #denominator param)
(get@ #numerator input)))))]
[< n/<]
[<= n/<=]
[> n/>]
[>= n/>=]
)
(do-template [<name> <comp>]
[(def: #export (<name> left right)
(-> Ratio Ratio Ratio)
(if (<comp> left right)
right
left))]
[min <]
[max >]
)
(structure: #export _ (Equivalence Ratio)
(def: = ..=))
(structure: #export _ (order.Order Ratio)
(def: eq Equivalence<Ratio>)
(def: < ..<)
(def: <= ..<=)
(def: > ..>)
(def: >= ..>=))
(structure: #export _ (Number Ratio)
(def: + ..+)
(def: - ..-)
(def: * ..*)
(def: / ../)
(def: % ..%)
(def: (negate (^slots [#numerator #denominator]))
{#numerator denominator
#denominator numerator})
(def: abs id)
(def: (signum x)
{#numerator +1
#denominator +1}))
(def: separator Text ":")
(def: part-encode
(-> Nat Text)
(|>> nat/encode (text.split +1) maybe.assume product.right))
(def: part-decode
(-> Text (E.Error Nat))
(|>> (format "+") nat/decode))
(structure: #export _ (Codec Text Ratio)
(def: (encode (^slots [#numerator #denominator]))
($_ text/compose (part-encode numerator) separator (part-encode denominator)))
(def: (decode input)
(case (text.split-with separator input)
(#.Some [num denom])
(do E.Monad<Error>
[numerator (part-decode num)
denominator (part-decode denom)]
(wrap (normalize {#numerator numerator
#denominator denominator})))
#.None
(#.Left (text/compose "Invalid syntax for ratio: " input)))))
(syntax: #export (ratio numerator {?denominator (p.maybe s.any)})
{#.doc (doc "Rational literals."
(ratio numerator denominator)
"The denominator can be omitted if it's 1."
(ratio numerator))}
(wrap (list (` ((~! normalize) {#..numerator (~ numerator)
#..denominator (~ (maybe.default (' +1)
?denominator))})))))
|