blob: 7542a56ff8930000a01c78f7a012eb9274f683b3 (
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
|
(.module: {#.doc "Rational numbers."}
[lux #*
[abstract
[equivalence (#+ Equivalence)]
[order (#+ Order)]
[monoid (#+ Monoid)]
[number (#+ Number)]
[codec (#+ Codec)]
[monad (#+ do)]]
[control
["." function]
["p" parser]]
[data
["." error]
["." product]
["." maybe]
[number
["." nat ("#@." decimal)]]
["." text ("#@." monoid)]]
["." math]
["." macro
["." code]
["s" syntax (#+ Syntax syntax:)]]])
(type: #export Ratio
{#numerator Nat
#denominator Nat})
(def: (equalize parameter subject)
(-> Ratio Ratio [Nat Nat])
[(n/* (get@ #denominator subject)
(get@ #numerator parameter))
(n/* (get@ #denominator parameter)
(get@ #numerator subject))])
(def: (normalize (^slots [#numerator #denominator]))
(-> Ratio Ratio)
(let [common (math.n/gcd numerator denominator)]
{#numerator (n// common numerator)
#denominator (n// common denominator)}))
(structure: #export equivalence (Equivalence Ratio)
(def: (= parameter subject)
(and (n/= (get@ #numerator parameter)
(get@ #numerator subject))
(n/= (get@ #denominator parameter)
(get@ #denominator subject)))))
(`` (structure: #export order (Order Ratio)
(def: &equivalence ..equivalence)
(~~ (template [<name> <op>]
[(def: (<name> parameter subject)
(let [[parameter' subject'] (..equalize parameter subject)]
(<op> parameter' subject')))]
[< n/<]
[<= n/<=]
[> n/>]
[>= n/>=]
))
))
(template [<name> <comp>]
[(def: #export (<name> left right)
(-> Ratio Ratio Ratio)
(if (:: ..order <comp> left right)
right
left))]
[min <]
[max >]
)
(def: (- parameter subject)
(let [[parameter' subject'] (..equalize parameter subject)]
(normalize [(n/- parameter' subject')
(n/* (get@ #denominator parameter)
(get@ #denominator subject))])))
(structure: #export number
(Number Ratio)
(def: (+ parameter subject)
(let [[parameter' subject'] (..equalize parameter subject)]
(normalize [(n/+ parameter' subject')
(n/* (get@ #denominator parameter)
(get@ #denominator subject))])))
(def: - ..-)
(def: (* parameter subject)
(normalize [(n/* (get@ #numerator parameter)
(get@ #numerator subject))
(n/* (get@ #denominator parameter)
(get@ #denominator subject))]))
(def: (/ parameter subject)
(let [[parameter' subject'] (..equalize parameter subject)]
(normalize [subject' parameter'])))
(def: (% parameter subject)
(let [[parameter' subject'] (..equalize parameter subject)
quot (n// parameter' subject')]
(..- (update@ #numerator (n/* quot) parameter)
subject)))
(def: (negate (^slots [#numerator #denominator]))
{#numerator denominator
#denominator numerator})
(def: abs function.identity)
(def: (signum x)
{#numerator 1
#denominator 1})
)
(def: separator Text ":")
(structure: #export codec (Codec Text Ratio)
(def: (encode (^slots [#numerator #denominator]))
($_ text@compose (nat@encode numerator) separator (nat@encode denominator)))
(def: (decode input)
(case (text.split-with separator input)
(#.Some [num denom])
(do error.monad
[numerator (nat@decode num)
denominator (nat@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))})))))
(template [<identity> <compose> <name>]
[(structure: #export <name>
(Monoid Ratio)
(def: identity (..ratio <identity>))
(def: compose (:: ..number <compose>))
)]
[0 + addition]
[1 * multiplication]
)
|