Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 337 lines (277 sloc) 8.952 kB
fccc685 Initial open-source release
MLstate authored
1 /*
5a3ed84 @Aqua-Ye [enhance] stdlib: added succ and pred to the Int module
Aqua-Ye authored
2 Copyright © 2011, 2012 MLstate
fccc685 Initial open-source release
MLstate authored
3
3143758 @Aqua-Ye [doc] lib: changed files headers to MIT license
Aqua-Ye authored
4 This file is part of Opa.
fccc685 Initial open-source release
MLstate authored
5
3143758 @Aqua-Ye [doc] lib: changed files headers to MIT license
Aqua-Ye authored
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
fccc685 Initial open-source release
MLstate authored
7
3143758 @Aqua-Ye [doc] lib: changed files headers to MIT license
Aqua-Ye authored
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
fccc685 Initial open-source release
MLstate authored
9
3143758 @Aqua-Ye [doc] lib: changed files headers to MIT license
Aqua-Ye authored
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fccc685 Initial open-source release
MLstate authored
11 */
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
12
fccc685 Initial open-source release
MLstate authored
13 /**
14 * This modules defines operations on integers, which are guaranteed to be at
15 * least 31 bits wide (including the sign bit)
16 *
17 * @category data
18 * @destination public
19 * @author Valentin Gatien-Baron, 2010 (documentation)
20 * @author Nicolas Pelletier, 2010
21 * @stability stable
22 **/
23
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
24 /**
25 * {1 About this module}
26 *
27 * {1 Where should I start?}
28 *
29 * {1 What if I need more?}
30 */
31
32 /**
33 * {1 Types defined in this module}
34 */
fccc685 Initial open-source release
MLstate authored
35
36 /**
37 * Order type label for int and float
38 */
39 type Int.order = Order.default
40 type Float.order = Order.default
41
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
42 /**
43 * {1 Interface}
44 */
45
fccc685 Initial open-source release
MLstate authored
46 Int = {{
47
48 /**
49 * {2 Numeric operations}
50 */
51
52 /**
53 * The standard addition
54 */
55 add = %% BslPervasives.int_add %% : int, int -> int
56
57 /**
58 * The standard subtracion
59 */
60 sub = %% BslPervasives.int_sub %% : int, int -> int
61
62 /**
63 * The standard multiplication
64 */
65 mul = %% BslPervasives.int_mul %% : int, int -> int
66
67 /**
68 * The standard quotient
69 */
70 div = %% BslPervasives.int_div %% : int, int -> int
71
72 /**
73 * {2 Order}
74 */
75 order = Order.make(ordering) : order(int, Int.order)
76
77 /**
78 * Compare two int
79 */
80 compare_raw = %% BslPervasives.compare_int %%
81 ordering = %% BslNumber.Int.ordering %%
82
83 compare(x,y): Order.comparison =
84 @opensums(ordering(x,y))
85
86
87 `!=` = %% BslPervasives.int_cmp_neq %% : int, int -> bool
88 `==` = %% BslPervasives.int_cmp_eq %% : int, int -> bool
89 `<` = %% BslPervasives.int_cmp_lneq %% : int, int -> bool
90 `<=` = %% BslPervasives.int_cmp_leq %% : int, int -> bool
91 `>` = %% BslPervasives.int_cmp_gneq %% : int, int -> bool
92 `>=` = %% BslPervasives.int_cmp_geq %% : int, int -> bool
93
94 `+` = %% BslPervasives.int_add %%
95 `-` = %% BslPervasives.int_sub %%
96 `*` = %% BslPervasives.int_mul %%
97 `/` = %% BslPervasives.int_div %%
98
99 /**
5a3ed84 @Aqua-Ye [enhance] stdlib: added succ and pred to the Int module
Aqua-Ye authored
100 * Add 1 to the int given in parameter
101 */
102 succ(i) = i+1
103
104 /**
105 * Subtract 1 to the int given in parameter
106 */
107 pred(i) = i-1
108
109 /**
fccc685 Initial open-source release
MLstate authored
110 * Check equality of two int
111 */
112 equals = %% BslPervasives.int_cmp_eq %% : int, int -> bool
113
114 /**
115 * {2 Conversion functions}
116 */
117
118 /**
119 * Returns the string representation of the integer
120 */
121 to_string = String.of_int : int -> string
122
123 /**
124 * Parse a string as an int
125 * The usage of this function is discouraged, as it will exit with an error
126 * when the input is incorrect
127 * It should be used only in parsers, where you can be sure that the string is
128 * valid
129 */
130 of_string = %% BslNumber.Int.of_string %% : string -> int
131
d841d6d @arthuraa [enhance] stdlib: bind bypasses.
arthuraa authored
132 of_string_opt = %% BslNumber.Int.of_string_opt %% : string -> option(int)
133
fccc685 Initial open-source release
MLstate authored
134 /**
135 * Converts a float to int
136 */
137 of_float = %% BslNumber.Int.of_float %% : float -> int
138
139 /**
140 * Return the ascii value (later utf-8 value) of the first char of the string
141 */
142 of_utf8 = %% bslPervasives.int_of_first_char %% : string -> int
143
144 /**
145 * Return the float representation of an integer
146 */
147 to_float= Float.of_int
148
149 /**
150 * Convert the integer to its string representation in hexadecimal
151 */
152 to_hex =
153 int_to_1hex(int) =
3a864c8 @nrs135 [debug] stdlib: Experimental 53-bit pack/unpack.
nrs135 authored
154 //if int<10 then string_of_int(int) <-- problems with node
155 //else
fccc685 Initial open-source release
MLstate authored
156 match int:int with
3a864c8 @nrs135 [debug] stdlib: Experimental 53-bit pack/unpack.
nrs135 authored
157 | 0 -> "0"
158 | 1 -> "1"
159 | 2 -> "2"
160 | 3 -> "3"
161 | 4 -> "4"
162 | 5 -> "5"
163 | 6 -> "6"
164 | 7 -> "7"
165 | 8 -> "8"
166 | 9 -> "9"
fccc685 Initial open-source release
MLstate authored
167 | 10 -> "A"
168 | 11 -> "B"
169 | 12 -> "C"
170 | 13 -> "D"
171 | 14 -> "E"
172 | 15 -> "F"
173 | _ -> @fail
174 (i ->
175 rec aux(i, s) = if i > 0
176 then aux(i / 16, int_to_1hex(mod(i, 16))^s)
177 else s
178 aux(i, "")
179 ) : int -> string
180
181 /**
182 *
183 **/
184 fold = (f, init, n ->
185 rec aux(i, acc) = if n == i then acc else aux(i + 1, f(acc, i))
186 aux(0, init)
187 ) : ('a, int -> 'a), 'a, int -> 'a
188
189 /**
190 *
191 **/
192 repeat = (f,init,n ->
193 fold(acc, _index -> f(acc), init, n)
194 ) : ('a -> 'a), 'a, int -> 'a
195
196 max(a:int, b:int) =
197 match compare_raw(a,b) with
198 | -1 -> b
199 | _ -> a
200
201 min(a:int, b:int) =
202 match compare_raw(a,b) with
203 | 1 -> b
204 | _ -> a
205
206 abs = %% BslNumber.Math.abs_i %% : int -> int
207
208 }}
209
210 Float =
211 {{
212
213 /** UNSAFE
214 * try to convert a string into another type
215 * if you want to do that, that should mean the string come from a parser
216 * but if this is the case, you probably already get the wanted type
217 * in case of the string is not a representation this type the function throw an error
218 */
219 of_string= %% BslNumber.Float.of_string %%
d841d6d @arthuraa [enhance] stdlib: bind bypasses.
arthuraa authored
220 of_string_opt = %% BslNumber.Float.of_string_opt %%
fccc685 Initial open-source release
MLstate authored
221 of_int= %% BslNumber.Float.of_int %%
222 to_int = Int.of_float
223 to_string = String.of_float
224 to_formatted_string = %% BslNumber.Float.to_formatted_string %%
225 infinity = 1. / 0.
226 NaN = 0.0 / 0.0
227 abs = %% BslNumber.Math.abs_f %% : float -> float
228 equals = `==`
229 `<` = %% BslNumber.Float.lt%%
230 `>` = %% BslNumber.Float.gt%%
231 `!=` = %% BslNumber.Float.neq%%
232 `==` = %% BslNumber.Float.eq%%
233 `>=` = %% BslNumber.Float.geq%%
234 `<=` = %% BslNumber.Float.leq%%
235 compare_raw = %% Bslpervasives.compare_float %%
236 compare = %% BslNumber.Float.comparison %%
237 `+` = %% Bslpervasives.float_add %%
238 `-` = %% Bslpervasives.float_sub %%
239 `*` = %% Bslpervasives.float_mul %%
240 `/` = %% Bslpervasives.float_div %%
241
242
243 eq(a, b) =
244 if not(Math.is_normal(a) && Math.is_normal(b)) then
245 a == b
246 else
247 // we divide by zero only when a=0 and b=0, in which case v is NaN and eq(a,b) is true
248 v = abs(a - b) / max(abs(a), abs(b))
249 Math.is_NaN(v) || v < 0.000001
250
251 /**
252 * A total order on floating-point numbers
253 *
254 * Note: In this order, special number [NaN] ("not a number" is larger than the largest floating point number, but smaller than positive infinity)
255 */
256 ordering(a:float,b:float): Order.ordering =
257 match compare_raw(a,b) with
258 | -1 -> {lt}
259 | 0 -> {eq}
260 | 1 -> {gt}
261 | _ -> @fail
262
263 order = Order.make(ordering) : order(float, Float.order)
264
265 ceil : float -> float = %% BslNumber.Float.ceil %%
266 floor : float -> float = %% BslNumber.Float.floor %%
267 round : float -> int = %% BslNumber.Float.round %%
268
269 max(a:float, b:float) =
270 match compare_raw(a,b) with
271 | -1 -> b
272 | _ -> a
273
274 min(a:float, b:float) =
275 match compare_raw(a,b) with
276 | 1 -> b
277 | _ -> a
278
279 }}
280
281
282 Bitwise = {{
283
284 /**
285 * {2 Bitwise operations}
286 *
287 * {b Warning}: Low-level operations with different semantics on the server
288 * and on the client. On the server, they work on 64-bits integers; on the
289 * client, they work on 32-bits integers.
290 */
291
292 /**
293 * Bitwise logical and.
294 **/
295 land = %% BslNumber.Int.op_land %% : int, int -> int
296
297 /**
298 * Bitwise logical or.
299 **/
300 lor = %% BslNumber.Int.op_lor %% : int, int -> int
301
302 /**
303 * Bitwise logical xor.
304 **/
305 lxor = %% BslNumber.Int.op_lxor %% : int, int -> int
306
307 /**
308 * Bitwise logical negation.
309 **/
310 lnot = %% BslNumber.Int.op_lnot %% : int -> int
311
312 /**
313 * [n lsl m] shifts n to the left by m bits.
314 * The result is unspecified if [m < 0] or [m >= bitsize],
315 * where bitsize is 32 on a 32-bit platform and 64 on a 64-bit platform.
316 *
317 **/
318 lsl = %% BslNumber.Int.op_lsl %% : int, int -> int
319
320 /**
321 * [Bitwise.lsr(n, m)] shifts n to the right by m bits.
322 * This is a logical shift:
323 * zeroes are inserted regardless of the sign of n .
324 * The result is unspecified if [m < 0] or [m >= bitsize]
325 *
326 **/
327 lsr = %% BslNumber.Int.op_lsr %% : int, int -> int
328
329 /**
330 * [Bitwise.asr(n, m)] shifts n to the right by m bits.
331 * This is an arithmetic shift: the sign bit of n is replicated.
332 * The result is unspecified if [m < 0] or [m >= bitsize].
333 **/
334 asr = %% BslNumber.Int.op_asr %% : int, int -> int
335
336 }}
Something went wrong with that request. Please try again.