package p4spectec
Source file ints.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 56open Lang open Xl open Il module Value = Runtime.Value open Util.Source (* Conversion between meta-numerics and OCaml numerics *) let bigint_of_value (value : value) : Bigint.t = value |> Value.Get.num |> Num.to_int let value_of_bigint (add : value -> unit) (i : Bigint.t) : value = let value = Value.Make.int i in add value; value (* dec $sum_int(nat* ) : nat *) let sum_int (add : value -> unit) (at : region) (targs : targ list) (values_input : value list) : value = Extract.zero at targs; let values = Extract.one at values_input |> Value.Get.list |> List.map bigint_of_value in let sum = List.fold_left Bigint.( + ) Bigint.zero values in value_of_bigint add sum (* dec $max_int(int* ) : int *) let max_int (add : value -> unit) (at : region) (targs : targ list) (values_input : value list) : value = Extract.zero at targs; let values = Extract.one at values_input |> Value.Get.list |> List.map bigint_of_value in let max = match values with | [] -> Bigint.zero | value_h :: values_t -> List.fold_left Bigint.max value_h values_t in value_of_bigint add max (* dec $min_int(int* ) : int *) let min_int (add : value -> unit) (at : region) (targs : targ list) (values_input : value list) : value = Extract.zero at targs; let values = Extract.one at values_input |> Value.Get.list |> List.map bigint_of_value in let min = match values with | [] -> Bigint.zero | value_h :: values_t -> List.fold_left Bigint.min value_h values_t in value_of_bigint add min