forked from CakeML/candle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candle_nums.ml
303 lines (248 loc) · 6.61 KB
/
candle_nums.ml
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
(*
This module attempts to provide a compatibility interface for the
OCaml num library, which HOL Light uses.
*)
module type NUM = sig
val num_of_int : int -> num
val int_of_num : num -> int
val string_of_num : num -> string
val denominator : num -> num
val numerator : num -> num
val abs_num : num -> num
val floor_num : num -> num
val ( +/ ) : num -> num -> num
val ( -/ ) : num -> num -> num
val ( */ ) : num -> num -> num
val ( // ) : num -> num -> num
val add_num : num -> num -> num
val sub_num : num -> num -> num
val mul_num : num -> num -> num
val div_num : num -> num -> num
val minus_num : num -> num
(* These operations are for integers (unit denominator) *)
val quo_num : num -> num -> num
val mod_num : num -> num -> num
val ( </ ): num -> num -> bool
val ( >/ ) : num -> num -> bool
val ( <=/ ) : num -> num -> bool
val ( >=/ ) : num -> num -> bool
val ( =/ ) : num -> num -> bool
val ( <>/ ) : num -> num -> bool
val le_num : num -> num -> bool
val lt_num : num -> num -> bool
val ge_num : num -> num -> bool
val gt_num : num -> num -> bool
val eq_num : num -> num -> bool
val min_num : num -> num -> num
val max_num : num -> num -> num
val compare : num -> num -> order
val gcd_num : num -> num -> num
val ( **/) : num -> num -> num
val power_num : num -> num -> num
val is_integer_num : num -> bool;;
end;;
type num =
| Int of int
| Rat of rat
;;
let pp_num n =
match n with
| Int i -> pp_int i
| Rat r -> pp_rat r
;;
module Num (* : NUM*) = struct
type num = num;;
let denominator n =
match n with
| Int i -> Int 1
| Rat r -> Int (Rat.denominator r)
;;
let numerator n =
match n with
| Int i -> n
| Rat r -> Int (Rat.numerator r)
;;
let num_fix n =
match n with
| Int i -> n
| Rat r ->
if Rat.denominator r = 1 then
Int (Rat.numerator r)
else if Rat.denominator r = 0 then
failwith "num_fix: division by zero"
else n
;;
let abs_num n =
match n with
| Int i -> Int (abs i)
| Rat r -> Rat (Rat.(/) (Rat.fromInt (abs (Rat.numerator r)))
(Rat.fromInt (Rat.denominator r)))
;;
let sign_num n =
let sign i = if i < 0 then -1 else if i > 0 then 1 else 0 in
match n with
| Int i -> sign i
| Rat r -> sign (Rat.numerator r)
;;
(* The Rat type operations normalize results *)
let norm n = num_fix n
;;
let num_of_int i = Int i
;;
let int_of_num n =
match n with
| Int i -> i
| Rat r -> Rat.numerator r / Rat.denominator r
;;
let string_of_num n =
match n with
| Int i -> string_of_int i
| Rat r ->
let n = Rat.numerator r in
let d = Rat.denominator r in
string_of_int n ^ "/" ^ string_of_int d
;;
let minus_num n =
match n with
| Int i -> Int (-i)
| Rat r -> Rat (rat_minus r)
;;
let (+/) x y =
match x, y with
| Int i, Int j -> Int (i + j)
| Int i, Rat r -> Rat (Rat.(+) (Rat.fromInt i) r)
| Rat r, Int i -> Rat (Rat.(+) r (Rat.fromInt i))
| Rat i, Rat j -> Rat (Rat.(+) i j)
;;
let (+/) x y = num_fix (x +/ y);;
let add_num = (+/);;
let (-/) x y =
match x, y with
| Int i, Int j -> Int (i - j)
| Int i, Rat r -> Rat (Rat.(-) (Rat.fromInt i) r)
| Rat r, Int i -> Rat (Rat.(-) r (Rat.fromInt i))
| Rat i, Rat j -> Rat (Rat.(-) i j)
;;
let (-/) x y = num_fix (x -/ y);;
let sub_num = (-/);;
let ( */) x y =
match x, y with
| Int i, Int j -> Int (i * j)
| Int i, Rat r -> Rat (Rat.( * ) (Rat.fromInt i) r)
| Rat r, Int i -> Rat (Rat.( * ) r (Rat.fromInt i))
| Rat i, Rat j -> Rat (Rat.( * ) i j)
;;
let ( */) x y = num_fix (x */ y);;
let mul_num = ( */);;
let (//) x y =
match x, y with
| Int i, Int j -> Rat (Rat.(/) (Rat.fromInt i) (Rat.fromInt j))
| Int i, Rat r -> Rat (Rat.(/) (Rat.fromInt i) r)
| Rat r, Int i -> Rat (Rat.(/) r (Rat.fromInt i))
| Rat i, Rat j -> Rat (Rat.(/) i j)
;;
let (//) x y = num_fix (x // y);;
let div_num = (//);;
let quo_num x y =
match x, y with
| Int i, Int j ->
let q = i / j in
let r = i mod j in
Int (if r >= 0 then q else if j > 0 then q - 1 else q + 1)
| Int _, Rat _ ->
let y = abs_num y in
Int (int_of_num (x // y))
| _ ->
Int (int_of_num (x // y))
;;
let is_integer_num n =
match n with
| Int _ -> true
| _ -> false;;
let mod_num x y =
match x, y with
| Int i, Int j ->
let r = i mod j in
Int (if r >= 0 then r else if j > 0 then r + j else r - j)
| _ -> x -/ (y */ quo_num x y)
;;
let power_num b e =
let rec pow b e =
if e < 1 then
Int 1
else if e mod 2 <> 0 then b */ pow b (e - 1)
else let p = pow b (e / 2) in
p */ p in
pow b (int_of_num e)
;;
let ( **/) = power_num;;
let floor_num n =
match n with
| Int i -> n
| Rat r -> Int (Rat.numerator r / Rat.denominator r)
;;
let compare x y =
match x, y with
| Int i, Int j -> Int.compare i j
| Int i, Rat r -> Rat.compare (Rat.fromInt i) r
| Rat r, Int j -> Rat.compare r (Rat.fromInt j)
| Rat i, Rat j -> Rat.compare i j
;;
let ( </) x y = compare x y = Less;;
let ( <=/) x y = compare x y <> Greater;;
let ( >/) x y = compare x y = Greater;;
let ( >=/) x y = compare x y <> Less;;
let ( =/) x y = compare x y = Equal;;
let ( <>/) x y = compare x y <> Equal;;
let lt_num = ( </);;
let le_num = ( <=/);;
let gt_num = ( >/);;
let ge_num = ( >=/);;
let eq_num = ( =/);;
let min_num x y = if x <=/ y then x else y;;
let max_num x y = if x >=/ y then x else y;;
let gcd_num x y =
match x, y with
| Int i, Int j -> Int (abs (Int.gcd i j))
;;
let succ_num n = Int 1 +/ n;;
end;; (* struct *)
(* There's no 'open': *)
let num_of_int = Num.num_of_int;;
let int_of_num = Num.int_of_num;;
let string_of_num = Num.string_of_num;;
let denominator = Num.denominator;;
let numerator = Num.numerator;;
let minus_num = Num.minus_num;;
let abs_num = Num.abs_num;;
let floor_num = Num.floor_num;;
let ( +/ ) = Num.( +/);;
let ( -/ ) = Num.( -/);;
let ( */ ) = Num.( */);;
let ( // ) = Num.( //);;
let add_num = Num.add_num ;;
let sub_num = Num.sub_num ;;
let mul_num = Num.mul_num ;;
let div_num = Num.div_num ;;
(* These operations are for integers (unit denominator) *)
let quo_num = Num.quo_num;;
let mod_num = Num.mod_num;;
let ( </ ) = Num.( </);;
let ( >/ ) = Num.( >/);;
let ( <=/ ) = Num.( <=/);;
let ( >=/ ) = Num.( >=/);;
let ( =/ ) = Num.( =/);;
let ( <>/ ) = Num.( <>/);;
let lt_num = Num.lt_num;;
let le_num = Num.le_num;;
let gt_num = Num.gt_num;;
let ge_num = Num.ge_num;;
let eq_num = Num.eq_num;;
let min_num = Num.min_num;;
let max_num = Num.max_num;;
let compare = Num.compare;;
let gcd_num = Num.gcd_num;;
let ( **/) = Num.( **/);;
let power_num = Num.power_num;;
let is_integer_num = Num.is_integer_num;;
let succ_num = Num.succ_num;;