Skip to content
This repository
tag: v3512
Fetching contributors…

Cannot retrieve contributors at this time

file 336 lines (277 sloc) 8.952 kb
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/*
Copyright © 2011, 2012 MLstate

This file is part of Opa.

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
*/

/**
* This modules defines operations on integers, which are guaranteed to be at
* least 31 bits wide (including the sign bit)
*
* @category data
* @destination public
* @author Valentin Gatien-Baron, 2010 (documentation)
* @author Nicolas Pelletier, 2010
* @stability stable
**/

/**
* {1 About this module}
*
* {1 Where should I start?}
*
* {1 What if I need more?}
*/

/**
* {1 Types defined in this module}
*/

/**
* Order type label for int and float
*/
type Int.order = Order.default
type Float.order = Order.default

/**
* {1 Interface}
*/

Int = {{

  /**
* {2 Numeric operations}
*/

  /**
* The standard addition
*/
  add = %% BslPervasives.int_add %% : int, int -> int

  /**
* The standard subtracion
*/
  sub = %% BslPervasives.int_sub %% : int, int -> int

  /**
* The standard multiplication
*/
  mul = %% BslPervasives.int_mul %% : int, int -> int

  /**
* The standard quotient
*/
  div = %% BslPervasives.int_div %% : int, int -> int

  /**
* {2 Order}
*/
  order = Order.make(ordering) : order(int, Int.order)

  /**
* Compare two int
*/
  compare_raw = %% BslPervasives.compare_int %%
  ordering = %% BslNumber.Int.ordering %%

  compare(x,y): Order.comparison =
     @opensums(ordering(x,y))


  `!=` = %% BslPervasives.int_cmp_neq %% : int, int -> bool
  `==` = %% BslPervasives.int_cmp_eq %% : int, int -> bool
  `<` = %% BslPervasives.int_cmp_lneq %% : int, int -> bool
  `<=` = %% BslPervasives.int_cmp_leq %% : int, int -> bool
  `>` = %% BslPervasives.int_cmp_gneq %% : int, int -> bool
  `>=` = %% BslPervasives.int_cmp_geq %% : int, int -> bool

  `+` = %% BslPervasives.int_add %%
  `-` = %% BslPervasives.int_sub %%
  `*` = %% BslPervasives.int_mul %%
  `/` = %% BslPervasives.int_div %%

  /**
* Add 1 to the int given in parameter
*/
  succ(i) = i+1

  /**
* Subtract 1 to the int given in parameter
*/
  pred(i) = i-1

  /**
* Check equality of two int
*/
  equals = %% BslPervasives.int_cmp_eq %% : int, int -> bool

  /**
* {2 Conversion functions}
*/

  /**
* Returns the string representation of the integer
*/
  to_string = String.of_int : int -> string

  /**
* Parse a string as an int
* The usage of this function is discouraged, as it will exit with an error
* when the input is incorrect
* It should be used only in parsers, where you can be sure that the string is
* valid
*/
  of_string = %% BslNumber.Int.of_string %% : string -> int

  of_string_opt = %% BslNumber.Int.of_string_opt %% : string -> option(int)

  /**
* Converts a float to int
*/
  of_float = %% BslNumber.Int.of_float %% : float -> int

  /**
* Return the ascii value (later utf-8 value) of the first char of the string
*/
  of_utf8 = %% bslPervasives.int_of_first_char %% : string -> int

  /**
* Return the float representation of an integer
*/
  to_float= Float.of_int

  /**
* Convert the integer to its string representation in hexadecimal
*/
  to_hex =
    int_to_1hex(int) =
      //if int<10 then string_of_int(int) <-- problems with node
      //else
        match int:int with
        | 0 -> "0"
        | 1 -> "1"
        | 2 -> "2"
        | 3 -> "3"
        | 4 -> "4"
        | 5 -> "5"
        | 6 -> "6"
        | 7 -> "7"
        | 8 -> "8"
        | 9 -> "9"
        | 10 -> "A"
        | 11 -> "B"
        | 12 -> "C"
        | 13 -> "D"
        | 14 -> "E"
        | 15 -> "F"
        | _ -> @fail
    (i ->
      rec aux(i, s) = if i > 0
        then aux(i / 16, int_to_1hex(mod(i, 16))^s)
        else s
      aux(i, "")
    ) : int -> string

  /**
*
**/
  fold = (f, init, n ->
    rec aux(i, acc) = if n == i then acc else aux(i + 1, f(acc, i))
    aux(0, init)
  ) : ('a, int -> 'a), 'a, int -> 'a

  /**
*
**/
  repeat = (f,init,n ->
    fold(acc, _index -> f(acc), init, n)
  ) : ('a -> 'a), 'a, int -> 'a

  max(a:int, b:int) =
    match compare_raw(a,b) with
      | -1 -> b
      | _ -> a

  min(a:int, b:int) =
    match compare_raw(a,b) with
      | 1 -> b
      | _ -> a

  abs = %% BslNumber.Math.abs_i %% : int -> int

}}

Float =
{{

  /** UNSAFE
* try to convert a string into another type
* if you want to do that, that should mean the string come from a parser
* but if this is the case, you probably already get the wanted type
* in case of the string is not a representation this type the function throw an error
*/
  of_string= %% BslNumber.Float.of_string %%
  of_string_opt = %% BslNumber.Float.of_string_opt %%
  of_int= %% BslNumber.Float.of_int %%
  to_int = Int.of_float
  to_string = String.of_float
  to_formatted_string = %% BslNumber.Float.to_formatted_string %%
  infinity = 1. / 0.
  NaN = 0.0 / 0.0
  abs = %% BslNumber.Math.abs_f %% : float -> float
  equals = `==`
  `<` = %% BslNumber.Float.lt%%
  `>` = %% BslNumber.Float.gt%%
  `!=` = %% BslNumber.Float.neq%%
  `==` = %% BslNumber.Float.eq%%
  `>=` = %% BslNumber.Float.geq%%
  `<=` = %% BslNumber.Float.leq%%
  compare_raw = %% Bslpervasives.compare_float %%
  compare = %% BslNumber.Float.comparison %%
  `+` = %% Bslpervasives.float_add %%
  `-` = %% Bslpervasives.float_sub %%
  `*` = %% Bslpervasives.float_mul %%
  `/` = %% Bslpervasives.float_div %%


  eq(a, b) =
    if not(Math.is_normal(a) && Math.is_normal(b)) then
      a == b
    else
      // we divide by zero only when a=0 and b=0, in which case v is NaN and eq(a,b) is true
      v = abs(a - b) / max(abs(a), abs(b))
      Math.is_NaN(v) || v < 0.000001

  /**
* A total order on floating-point numbers
*
* Note: In this order, special number [NaN] ("not a number" is larger than the largest floating point number, but smaller than positive infinity)
*/
  ordering(a:float,b:float): Order.ordering =
     match compare_raw(a,b) with
       | -1 -> {lt}
       | 0 -> {eq}
       | 1 -> {gt}
       | _ -> @fail

  order = Order.make(ordering) : order(float, Float.order)

  ceil : float -> float = %% BslNumber.Float.ceil %%
  floor : float -> float = %% BslNumber.Float.floor %%
  round : float -> int = %% BslNumber.Float.round %%

  max(a:float, b:float) =
    match compare_raw(a,b) with
      | -1 -> b
      | _ -> a

  min(a:float, b:float) =
    match compare_raw(a,b) with
      | 1 -> b
      | _ -> a

}}


Bitwise = {{

  /**
* {2 Bitwise operations}
*
* {b Warning}: Low-level operations with different semantics on the server
* and on the client. On the server, they work on 64-bits integers; on the
* client, they work on 32-bits integers.
*/

  /**
* Bitwise logical and.
**/
  land = %% BslNumber.Int.op_land %% : int, int -> int

  /**
* Bitwise logical or.
**/
  lor = %% BslNumber.Int.op_lor %% : int, int -> int

  /**
* Bitwise logical xor.
**/
  lxor = %% BslNumber.Int.op_lxor %% : int, int -> int

  /**
* Bitwise logical negation.
**/
  lnot = %% BslNumber.Int.op_lnot %% : int -> int

  /**
* [n lsl m] shifts n to the left by m bits.
* The result is unspecified if [m < 0] or [m >= bitsize],
* where bitsize is 32 on a 32-bit platform and 64 on a 64-bit platform.
*
**/
  lsl = %% BslNumber.Int.op_lsl %% : int, int -> int

  /**
* [Bitwise.lsr(n, m)] shifts n to the right by m bits.
* This is a logical shift:
* zeroes are inserted regardless of the sign of n .
* The result is unspecified if [m < 0] or [m >= bitsize]
*
**/
  lsr = %% BslNumber.Int.op_lsr %% : int, int -> int

  /**
* [Bitwise.asr(n, m)] shifts n to the right by m bits.
* This is an arithmetic shift: the sign bit of n is replicated.
* The result is unspecified if [m < 0] or [m >= bitsize].
**/
  asr = %% BslNumber.Int.op_asr %% : int, int -> int

}}
Something went wrong with that request. Please try again.