cc01071bd624d69cd00550509794a60d9297a7e2
[Faustine.git] / interpretor / basic.ml
1 (**
2 Module: Basic
3 Description: basic data type in the vectorial faust interpreter.
4 @author WANG Haisheng
5 Created: 31/05/2013 Modified: 17/07/2013
6 *)
7
8 open Types;;
9 open Aux;;
10
11 exception Convert_Error of string;;
12 exception Basic_operation of string;;
13
14
15 (* MACRO *)
16
17 let faust_max = 2147483647;;
18 let faust_min = -2147483648;;
19 let faust_bits = 32;;
20
21 (* Functional operations *)
22
23 let fun_unary oper f = fun x -> oper (f x);;
24 let fun_binary oper f g = fun x -> oper (f x) (g x);;
25 let fun_ternary oper f g h = fun x -> oper (f x) (g x) (h x);;
26
27 (* basic operations *)
28
29 let memorize : int -> (index -> basic) -> (index -> basic) =
30 fun size ->
31 fun vec ->
32 let memory = Array.create size Error in
33 let filled = Array.create size false in
34 let vec_mem : index -> basic =
35 fun i ->
36 if i >= 0 && i < size then (
37 if filled.(i) then
38 memory.(i)
39 else
40 let result = vec i in
41 let () = memory.(i) <- result in
42 let () = filled.(i) <- true in
43 result)
44 else raise (Invalid_argument "vector overflow.") in
45 vec_mem;;
46
47 class vector : int -> (index -> basic) -> vector_type =
48 fun (size_init : int) ->
49 fun (vec_init : index -> basic) ->
50 object
51 val s = size_init
52 val vec = memorize size_init vec_init
53 method size = s
54 method nth = vec
55 end;;
56
57 let rec basic_to_int : basic -> int =
58 fun v ->
59 match v with
60 |N i -> i
61 |R f -> int_of_float f
62 |Vec vec ->
63 raise (Convert_Error "basic_to_int : vector.")
64 |Zero -> 0
65 |Error -> raise (Convert_Error "basic_to_int : Error");;
66
67
68 let basic_to_float : basic -> float =
69 fun v ->
70 match v with
71 |N i -> float_of_int i
72 |R f -> f
73 |Vec vec ->
74 raise (Convert_Error "basic_to_float : vector.")
75 |Zero -> 0.
76 |Error -> 0.;;
77
78
79 let basic_to_float_array : basic -> float array =
80 fun v ->
81 match v with
82 |Vec vec ->
83 let basics : basic array =
84 Array.init vec#size vec#nth in
85 Array.map basic_to_float basics
86 |_ -> [| (basic_to_float v)|];;
87
88
89 let rec basic_to_string : basic -> string =
90 fun (v : basic) ->
91 match v with
92 |N i1 -> string_of_int i1
93 |R f1 -> string_of_float f1
94 |Vec vec ->
95 let basics : basic array =
96 Array.init vec#size vec#nth in
97 let strings = Array.to_list
98 (Array.map basic_to_string basics) in
99 String.concat "," strings
100 |Zero -> "0"
101 |Error -> "0";;
102
103 let basic_of_float : float -> basic = fun f -> R f;;
104
105 let rec basic_of_float_array : float array -> basic =
106 fun (data : float array) ->
107 let n = Array.length data in
108 if n = 0 then
109 raise (Convert_Error "basic_of_float_array : empty.")
110 else if n = 1 then basic_of_float data.(0)
111 else
112 let vec = Array.get (Array.map basic_of_float data) in
113 Vec (new vector n vec);;
114
115 let basic_to_bool : basic -> bool =
116 fun b ->
117 match b with
118 | N i ->
119 if i = 1 then true
120 else if i = 0 then false
121 else raise (Convert_Error "basic_to_bool : only for 0 or 1.")
122 | Zero -> false
123 | _ -> raise (Convert_Error "basic_to_bool : only for 0 or 1.");;
124
125 let basic_of_bool : bool -> basic =
126 fun tof -> if tof then N 1 else N 0;;
127
128
129 (* VALUE OPERATIONS *)
130
131 let rec basic_normalize : basic -> basic =
132 fun b ->
133 let n = 2. ** float_of_int (faust_bits) in
134 match b with
135 |N i ->
136 if i > faust_max then
137 N (i - int_of_float
138 (n *. floor (((float_of_int i) +. n/.2.)/.n)))
139 else if i < faust_min then
140 N (i + int_of_float
141 (n *. floor ((n/.2. -. (float_of_int i) -. 1.)/.n)))
142 else N i
143 |R f ->
144 if f > float_of_int (faust_max) then
145 R (f -. (n *. floor ((f +. n/.2.)/.n)))
146 else if f < float_of_int (faust_min) then
147 R (f +. (n *. floor ((n/.2. -. f -. 1.)/.n)))
148 else R f
149 |Vec vec ->
150 Vec (new vector vec#size
151 (fun_unary basic_normalize vec#nth))
152 |Zero -> Zero
153 |Error -> Error;;
154
155
156 let rec basic_add : basic -> basic -> basic =
157 fun b1 -> fun b2 ->
158 match (b1, b2) with
159 | (Zero, _) -> b2
160 | (_, Zero) -> b1
161 | (Vec vec1, Vec vec2) ->
162 if vec1#size = vec2#size then
163 Vec (new vector vec1#size
164 (fun_binary basic_add vec1#nth vec2#nth))
165 else raise (Basic_operation "vector size not matched.")
166 | (Vec vec1, _) -> raise (Basic_operation "vec1 +~ sca2")
167 | (N i1, N i2) -> basic_normalize (N (i1 + i2))
168 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) +. f2))
169 | (N i1, Vec vec2) -> raise (Basic_operation "i1 +~ vec2")
170 | (N i1, Error) -> Error
171 | (R f1, N i2) -> basic_normalize (R (f1 +. (float_of_int i2)))
172 | (R f1, R f2) -> basic_normalize (R (f1 +. f2))
173 | (R f1, Vec vec2) -> raise (Basic_operation "f1 +~ vec2")
174 | (R f1, Error) -> Error
175 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
176 | (Error, _) -> Error;;
177
178
179 let (+~) b1 b2 = basic_add b1 b2;;
180
181
182 let rec basic_neg : basic -> basic =
183 fun b ->
184 match b with
185 |N i -> N (-i)
186 |R f -> R (-.f)
187 |Vec vec -> Vec (new vector vec#size (fun_unary basic_neg vec#nth))
188 |Zero -> Zero
189 |Error -> Error;;
190
191
192 let basic_sub : basic -> basic -> basic =
193 fun b1 ->
194 fun b2 ->
195 basic_add b1 (basic_neg b2);;
196
197
198 let (-~) b1 b2 = basic_sub b1 b2;;
199
200
201 let rec basic_mul : basic -> basic -> basic =
202 fun b1 ->
203 fun b2 ->
204 match (b1, b2) with
205 | (Vec vec1, Vec vec2) ->
206 if vec1#size = vec2#size then
207 Vec (new vector vec1#size
208 (fun_binary basic_mul vec1#nth vec2#nth))
209 else raise (Basic_operation "vector size not matched.")
210 | (Vec vec1, Zero) ->
211 Vec (new vector vec1#size
212 (fun_unary (basic_mul Zero) vec1#nth))
213 | (Vec vec1, _) -> raise (Basic_operation "vec1 *~ sca2")
214 | (N i1, N i2) -> basic_normalize (N (i1 * i2))
215 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) *. f2))
216 | (N i1, Vec vec2) -> raise (Basic_operation "i1 *~ vec2")
217 | (N i1, Zero) -> N 0
218 | (N i1, Error) -> Error
219 | (R f1, N i2) -> basic_normalize (R (f1 *. (float_of_int i2)))
220 | (R f1, R f2) -> basic_normalize (R (f1 *. f2))
221 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
222 | (R f1, Zero) -> R 0.
223 | (R f1, Error) -> Error
224 | (Zero, N i2) -> N 0
225 | (Zero, R f2) -> R 0.
226 | (Zero, Vec vec2) ->
227 Vec (new vector vec2#size
228 (fun i -> basic_mul Zero (vec2#nth i)))
229 | (Zero, Zero) -> Zero
230 | (Zero, Error) -> Error
231 | (Error, Vec vec2) -> raise (Basic_operation "Error *~ vec2")
232 | (Error, _) -> Error;;
233
234
235 let ( *~ ) b1 b2 = basic_mul b1 b2;;
236
237
238 let rec basic_recip : basic -> basic =
239 fun v ->
240 match v with
241 |N i -> basic_recip (R (float_of_int i))
242 |R f -> if f = 0. then Error else R (1./.f)
243 |Vec vec -> Vec (new vector vec#size
244 (fun_unary basic_recip vec#nth))
245 |Zero -> Error
246 |Error -> R 0.;;
247
248
249 let basic_div : basic -> basic -> basic =
250 fun b1 ->
251 fun b2 ->
252 basic_mul b1 (basic_recip b2);;
253
254
255 let (/~) b1 b2 = basic_div b1 b2;;
256
257
258 let rec basic_zero : basic -> basic =
259 fun v ->
260 match v with
261 |N i -> N 0
262 |R f -> R 0.
263 |Vec vec -> Vec (new vector vec#size
264 (fun_unary basic_zero vec#nth))
265 |Zero -> Zero
266 |Error -> R 0.;;
267
268
269 let rec basic_power : basic -> basic -> basic =
270 fun b1 ->
271 fun b2 ->
272 match (b1, b2) with
273 | (Vec vec1, Vec vec2) ->
274 if vec1#size = vec2#size then
275 Vec (new vector vec1#size
276 (fun_binary basic_power vec1#nth vec2#nth))
277 else raise (Basic_operation "vector size not matched.")
278 | (Vec vec1, Zero) ->
279 let vec_zeros = Vec (new vector vec1#size (fun i -> Zero)) in
280 basic_power b1 vec_zeros
281 | (Vec vec1, _) -> raise (Basic_operation "vec1 *~ sca2")
282 | (N i1, _) -> basic_power (R (float_of_int i1)) b2
283 | (R f1, N i2) -> basic_power b1 (R (float_of_int i2))
284 | (R f1, R f2) -> basic_normalize (R (f1 ** f2))
285 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
286 | (R f1, Zero) -> R 1.
287 | (R f1, Error) -> Error
288 | (Zero, N i2) -> basic_power b1 (R (float_of_int i2))
289 | (Zero, R f2) -> R 0.
290 | (Zero, Vec vec2) ->
291 let vec_zeros = Vec (new vector vec2#size (fun i -> Zero)) in
292 basic_power vec_zeros b2
293 | (Zero, Zero) -> basic_power (R 0.) (R 0.)
294 | (Zero, Error) -> Error
295 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
296 | (Error, _) -> Error;;
297
298
299 let rec basic_logic :
300 (bool -> bool -> bool) -> basic -> basic -> basic =
301 fun oper -> fun b1 -> fun b2 ->
302 match (b1, b2) with
303 | (Vec vec1, Vec vec2) ->
304 if vec1#size = vec2#size then
305 Vec (new vector vec1#size
306 (fun_binary (basic_logic oper) vec1#nth vec2#nth))
307 else raise (Basic_operation "vector size not matched.")
308 | (Vec vec1, Zero) ->
309 let vec_zeros = Vec (new vector vec1#size (fun i -> Zero)) in
310 basic_logic oper b1 vec_zeros
311 | (Vec vec1, _) -> raise (Basic_operation "vec1 logic sca2")
312 | (N i1, N i2) -> basic_of_bool (oper (basic_to_bool b1)
313 (basic_to_bool b2))
314 | (N i1, R f2) ->
315 raise (Basic_operation "Float shouldn't be in logical oper.")
316 | (N i1, Vec vec2) -> raise (Basic_operation "f1 logic vec2")
317 | (N i1, Zero) -> basic_logic oper b1 (N 0)
318 | (N i1, Error) -> Error
319 | (R f1, _) ->
320 raise (Basic_operation "Float shouldn't be in logical oper.")
321 | (Zero, N i2) -> basic_logic oper (N 0) b2
322 | (Zero, R f2) ->
323 raise (Basic_operation "Float shouldn't be in logical oper.")
324 | (Zero, Vec vec2) ->
325 let vec_zeros = Vec (new vector vec2#size (fun i -> Zero)) in
326 basic_logic oper vec_zeros b2
327 | (Zero, Zero) -> basic_logic oper (N 0) (N 0)
328 | (Zero, Error) -> Error
329 | (Error, Vec vec2) -> raise (Basic_operation "Error logic vec2")
330 | (Error, _) -> Error;;
331
332 let basic_and = basic_logic (&&);;
333 let basic_or = basic_logic (||);;
334 let basic_xor = basic_logic xor;;
335
336 let rec basic_adjust : (float -> float) -> basic -> basic =
337 fun oper -> fun b ->
338 match b with
339 |N i -> R (float_of_int i)
340 |R f -> R (floor f)
341 |Vec vec -> Vec (new vector vec#size
342 (fun_unary (basic_adjust oper) vec#nth))
343 |Zero -> R 0.
344 |Error -> Error;;
345
346 let basic_floor = basic_adjust floor;;
347 let basic_ceil = basic_adjust ceil;;
348 let basic_rint = basic_adjust rint;;
349
350 let rec basic_int : basic -> basic =
351 fun b ->
352 match b with
353 |N i -> b
354 |R f -> N (int_of_float f)
355 |Vec vec -> Vec (new vector vec#size
356 (fun_unary basic_int vec#nth))
357 |Zero -> N 0
358 |Error -> Error;;
359
360 let rec basic_float : basic -> basic =
361 fun b ->
362 match b with
363 | N i -> R (float_of_int i)
364 | R f -> b
365 | Vec vec -> Vec (new vector vec#size
366 (fun_unary basic_float vec#nth))
367 | Zero -> R 0.
368 | Error -> Error;;
369
370 let rec basic_abs : basic -> basic =
371 fun b ->
372 match b with
373 | N i -> N (abs i)
374 | R f -> R (abs_float f)
375 | Vec vec -> Vec (new vector vec#size
376 (fun_unary basic_abs vec#nth))
377 | Zero -> Zero
378 | Error -> Error;;
379
380
381 let rec basic_unary : (float -> float) -> basic -> basic =
382 fun oper ->
383 fun b ->
384 match b with
385 |N i -> R (oper (float_of_int i))
386 |R f -> R (oper f)
387 |Vec vec -> Vec (new vector vec#size
388 (fun_unary (basic_unary oper) vec#nth))
389 |Zero -> R (oper 0.)
390 |Error -> Error;;
391
392 let basic_sin : basic -> basic = basic_unary sin;;
393 let basic_asin : basic -> basic = basic_unary asin;;
394 let basic_cos : basic -> basic = basic_unary cos;;
395 let basic_acos : basic -> basic = basic_unary acos;;
396 let basic_tan : basic -> basic = basic_unary tan;;
397 let basic_atan : basic -> basic = basic_unary atan;;
398 let basic_exp : basic -> basic = basic_unary exp;;
399 let basic_ln : basic -> basic = basic_unary log;;
400 let basic_lg : basic -> basic = basic_unary log10;;
401
402 let rec basic_atan2 : basic -> basic -> basic =
403 fun v1 ->
404 fun v2 ->
405 match (v1, v2) with
406 | (N i1, N i2) -> basic_atan2
407 (R (float_of_int i1)) (R (float_of_int i2))
408 | (N i1, R f2) -> basic_atan2 (R (float_of_int i1)) v2
409 | (N i1, Zero) -> basic_atan2 (R (float_of_int i1)) (R 0.)
410 | (N i1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
411 | (N i1, Error) -> Error
412
413 | (R f1, N i2) -> basic_atan2 v1 (R (float_of_int i2))
414 | (R f1, R f2) -> R (atan2 f1 f2)
415 | (R f1, Zero) -> basic_atan2 v1 (R 0.)
416 | (R f1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
417 | (R f1, Error) -> Error
418
419 | (Vec vec1, Vec vec2) -> Vec (new vector vec1#size
420 (fun_binary basic_atan2 vec1#nth vec2#nth))
421 | (Vec vec1, Zero) -> Vec (new vector vec1#size
422 (fun i -> basic_atan2 (vec1#nth i) Zero))
423 | (Vec vec1, _) -> raise (Basic_operation "atan2 vec sca.")
424
425 | (Zero, N i2) -> basic_atan2 (R 0.) (R (float_of_int i2))
426 | (Zero, R f2) -> basic_atan2 (R 0.) v2
427 | (Zero, Vec vec2) -> Vec (new vector vec2#size
428 (fun_unary (basic_atan2 Zero) vec2#nth))
429 | (Zero, Zero) -> basic_atan2 (R 0.) (R 0.)
430 | (Zero, Error) -> Error
431
432 | (Error, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
433 | (Error, _) -> Error;;
434
435
436 let rec basic_sqrt v = match v with
437 |N i ->
438 if i >= 0 then R (sqrt (float_of_int i))
439 else raise (Basic_operation "sqrt parameter < 0.")
440 |R f ->
441 if f >= 0. then R (sqrt f)
442 else raise (Basic_operation "sqrt parameter < 0.")
443 |Vec vec -> Vec (new vector vec#size (fun_unary basic_sqrt vec#nth))
444 |Zero -> R (sqrt 0.)
445 |Error -> Error;;
446
447
448 let rec basic_mod : basic -> basic -> basic =
449 fun b1 ->
450 fun b2 ->
451 match (b1, b2) with
452 | (N i1, N i2) -> N (i1 mod i2)
453 | (_, R f2) ->
454 raise (Basic_operation "b1 mod b2: b2 cannot be float.")
455 | (R f1, _) ->
456 raise (Basic_operation "b1 mod b2: b1 cannot be float.")
457 | (N i1, Vec vec2) ->
458 raise (Basic_operation "Scalar_Vector: sca mod vec.")
459 | (_, Zero) ->
460 raise (Basic_operation "b1 mod b2: b2 cannot be zero.")
461 | (N i1, Error) -> Error
462 | (Vec vec1, Vec vec2) ->
463 if vec1#size = vec2#size then
464 Vec (new vector vec1#size
465 (fun_binary basic_mod vec1#nth vec2#nth))
466 else raise (Basic_operation "vector size not matched.")
467 | (Vec vec1, _) ->
468 raise (Basic_operation "Vector_Scalar: vec mod sca.")
469 | (Zero, Vec vec2) ->
470 basic_mod (Vec (new vector vec2#size (fun i -> Zero))) b2
471 | (Zero, _) -> basic_mod (N 0) b2
472 | (Error, Vec vec2) ->
473 raise (Basic_operation "Scalar_Vector: sca mod vec.")
474 | (Error, _) -> Error;;
475
476 let rec basic_mod_float :
477 (float -> float -> float) -> basic -> basic -> basic =
478 fun oper -> fun b1 -> fun b2 ->
479 match (b1, b2) with
480 | (R f1, R f2) -> R (oper f1 f2)
481 | (_, N i2) ->
482 raise (Basic_operation "b1 mod_float b2: b2 cannot be int.")
483 | (N i1, _) ->
484 raise (Basic_operation "b1 mod_float b2: b1 cannot be int.")
485 | (R f1, Vec vec2) ->
486 raise (Basic_operation "Scalar_Vector: sca mod_float vec.")
487 | (_, Zero) ->
488 raise (Basic_operation "b1 mod_float b2: b2 cannot be zero.")
489 | (R f1, Error) -> Error
490 | (Vec vec1, Vec vec2) ->
491 if vec1#size = vec2#size then
492 Vec (new vector vec1#size
493 (fun_binary (basic_mod_float oper) vec1#nth vec2#nth))
494 else raise (Basic_operation "vector size not matched.")
495 | (Vec vec1, _) ->
496 raise (Basic_operation "Vector_Scalaire: vec mod_float sca.")
497 | (Zero, Vec vec2) ->
498 basic_mod_float oper (Vec (new vector vec2#size (fun i -> Zero))) b2
499 | (Zero, _) -> basic_mod_float oper (R 0.) b2
500 | (Error, Vec vec2) ->
501 raise (Basic_operation "Scalaire_Vector: int mod_float vec.")
502 | (Error, _) -> Error;;
503
504 let basic_fmod = basic_mod_float mod_float;;
505 let basic_remainder = basic_mod_float remainder_float;;
506
507 let rec basic_compare_zero :
508 ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> basic -> basic =
509 fun oper1 -> fun oper2 -> fun v ->
510 match v with
511 |N i -> if oper1 i 0 then N 1 else N 0
512 |R f -> if oper2 f 0. then N 1 else N 0
513 |Vec vec ->
514 Vec (new vector vec#size
515 (fun_unary (basic_compare_zero oper1 oper2) vec#nth ))
516 |Zero -> basic_compare_zero oper1 oper2 (N 0)
517 |Error -> Error;;
518
519 let basic_gt_zero = basic_compare_zero (>) (>);;
520 let basic_lt_zero = basic_compare_zero (<) (<);;
521 let basic_geq_zero = basic_compare_zero (>=) (>=);;
522 let basic_leq_zero = basic_compare_zero (<=) (<=);;
523 let basic_eq_zero = basic_compare_zero (=) (=);;
524 let basic_neq_zero = basic_compare_zero (<>) (<>);;
525
526 let basic_compare : (basic -> basic) -> basic -> basic -> basic =
527 fun oper -> fun b1 -> fun b2 -> oper (b1 -~ b2);;
528
529 let basic_gt = basic_compare basic_gt_zero;;
530 let basic_lt = basic_compare basic_lt_zero;;
531 let basic_geq = basic_compare basic_geq_zero;;
532 let basic_leq = basic_compare basic_leq_zero;;
533 let basic_eq = basic_compare basic_eq_zero;;
534 let basic_neq = basic_compare basic_neq_zero;;
535
536 let basic_max : basic -> basic -> basic =
537 fun b1 ->
538 fun b2 ->
539 let compare = basic_gt_zero (b1 -~ b2) in
540 match compare with
541 | N i ->
542 if i = 1 then b1
543 else if i = 0 then b2
544 else raise (Basic_operation "compare result not bool.")
545 | Vec vec ->
546 let basics = Array.init vec#size vec#nth in
547 let sum = basic_to_int (Array.fold_left basic_add Zero basics) in
548 if sum = vec#size then b1
549 else if sum = 0 then b2
550 else Error
551 | Error -> Error
552 | _ -> raise (Basic_operation "compare result not bool.");;
553
554
555 let basic_min : basic -> basic -> basic =
556 fun b1 ->
557 fun b2 ->
558 let compare = basic_gt_zero (b1 -~ b2) in
559 match compare with
560 | N i ->
561 if i = 1 then b2
562 else if i = 0 then b1
563 else raise (Basic_operation "compare result not bool.")
564 | Vec vec ->
565 let basics = Array.init vec#size vec#nth in
566 let sum = basic_to_int (Array.fold_left basic_add Zero basics) in
567 if sum = vec#size then b2
568 else if sum = 0 then b1
569 else Error
570 | Error -> Error
571 | _ -> raise (Basic_operation "compare result not bool.");;