package dolmen_type
Source file float.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 261module Id = Dolmen.Std.Id module Ast = Dolmen.Std.Term (* Smtlib Floating Point *) (* ************************************************************************ *) module Smtlib2 = struct module Tff (Type : Tff_intf.S) (Ty : Dolmen.Intf.Ty.Smtlib_Float with type t := Type.Ty.t) (T : Dolmen.Intf.Term.Smtlib_Float with type t := Type.T.t and type ty := Type.Ty.t and type cst := Type.T.Const.t) = struct module B = T.Bitv module R = T.Real module F = T.Float type _ Type.err += | Invalid_bin_char : char -> Ast.t Type.err | Invalid_hex_char : char -> Ast.t Type.err | Invalid_dec_char : char -> Ast.t Type.err let parse_int env ast s = match int_of_string s with | i when i >= 0 -> i | _ -> Type._error env (Ast ast) (Type.Expected ("a positive integer", None)) | exception Failure _ -> Type._error env (Ast ast) (Type.Expected ("a positive integer", None)) let parse_binary env s ast = match Misc.Bitv.parse_binary s with | s -> B.mk s | exception Misc.Bitv.Invalid_char c -> Type._error env (Ast ast) (Invalid_bin_char c) let parse_hexa env s ast = match Misc.Bitv.parse_hexa s with | s -> B.mk s | exception Misc.Bitv.Invalid_char c -> Type._error env (Ast ast) (Invalid_hex_char c) let meta_to_bv mk = fun _ params ret_ty -> let err () = mk 0 (0,0) in match params with | [_; x] -> begin match Ty.view (Type.T.Var.ty x) with | `Float (e, s) -> begin match Ty.view ret_ty with | `Bitv n -> mk n (e, s) | _ -> err () end | _ -> err () end | _ -> err () let indexed1 env mk i_s ast = let i = parse_int env ast i_s in mk i let indexed2 env mk i_s j_s ast = let i = parse_int env ast i_s in let j = parse_int env ast j_s in mk i j let to_fp env symbol e s = Type.builtin_term (fun ast args -> let e = parse_int env ast e in let s = parse_int env ast s in let args = List.map (Type.parse_term env) args in match args with | [ a ] -> F.ieee_format_to_fp e s a | [ rm; b ] -> begin match Ty.view @@ T.ty b with | `Real -> F.real_to_fp e s rm b | `Bitv _ -> F.sbv_to_fp e s rm b | `Float (_,_) -> F.to_fp e s rm b | _ -> Type._error env (Ast ast) ( Type.Expected ("a real, bitvector or float", Some (Term b))) end | _ -> Type._error env (Ast ast) (Type.Bad_op_arity (symbol, [1; 2], List.length args)) ) let parse version env s = match s with (* sorts *) | Type.Id { ns = Sort; name = Simple "Float16"; } -> Type.builtin_ty (Base.app0 (module Type) env s (Ty.float 5 11)) | Type.Id { ns = Sort; name = Simple "Float32"; } -> Type.builtin_ty (Base.app0 (module Type) env s (Ty.float 8 24)) | Type.Id { ns = Sort; name = Simple "Float64"; } -> Type.builtin_ty (Base.app0 (module Type) env s (Ty.float 11 53)) | Type.Id { ns = Sort; name = Simple "Float128"; } -> Type.builtin_ty (Base.app0 (module Type) env s (Ty.float 15 113)) | Type.Id { ns = Sort; name = Simple "RoundingMode"; } -> Type.builtin_ty (Base.app0 (module Type) env s Ty.roundingMode) (* indexed sorts *) | Type.Id { ns = Sort; name = Indexed { basename; indexes; } } -> Base.parse_indexed basename indexes (function | "BitVec" -> `Unary (function n_s -> Type.builtin_ty (Base.app0_ast (module Type) env s (indexed1 env Ty.bitv n_s))) | "FloatingPoint" -> `Binary (fun n_e n_s -> Type.builtin_ty (Base.app0_ast (module Type) env s (indexed2 env Ty.float n_e n_s))) | _ -> `Not_indexed) ~err:(Base.bad_ty_index_arity (module Type) env) ~k:(function () -> `Not_found) (* Bitvector litterals *) | Type.Id { ns = Value Binary; name = Simple name; } -> Type.builtin_term (Base.app0_ast (module Type) env s (parse_binary env name)) | Type.Id { ns = Value Hexadecimal; name = Simple name; } -> Type.builtin_term (Base.app0_ast (module Type) env s (parse_hexa env name)) (* terms *) | Type.Id { ns = Term; name = Simple name; } -> begin match name with | "fp" -> Type.builtin_term (Base.term_app3 (module Type) env s F.fp) | "RNE" | "roundNearestTiesToEven" -> Type.builtin_term (Base.app0 (module Type) env s F.roundNearestTiesToEven) | "RNA" | "roundNearestTiesToAway" -> Type.builtin_term (Base.app0 (module Type) env s F.roundNearestTiesToAway) | "RTP" | "roundTowardPositive" -> Type.builtin_term (Base.app0 (module Type) env s F.roundTowardPositive) | "RTN" | "roundTowardNegative" -> Type.builtin_term (Base.app0 (module Type) env s F.roundTowardNegative) | "RTZ" | "roundTowardZero" -> Type.builtin_term (Base.app0 (module Type) env s F.roundTowardZero) | "fp.abs" -> Type.builtin_term (Base.term_app1 (module Type) env s F.abs) | "fp.neg" -> Type.builtin_term (Base.term_app1 (module Type) env s F.neg) | "fp.add" -> Type.builtin_term (Base.term_app3 (module Type) env s F.add) | "fp.sub" -> Type.builtin_term (Base.term_app3 (module Type) env s F.sub) | "fp.mul" -> Type.builtin_term (Base.term_app3 (module Type) env s F.mul) | "fp.div" -> Type.builtin_term (Base.term_app3 (module Type) env s F.div) | "fp.fma" -> Type.builtin_term (Base.term_app4 (module Type) env s F.fma) | "fp.sqrt" -> Type.builtin_term (Base.term_app2 (module Type) env s F.sqrt) | "fp.rem" -> Type.builtin_term (Base.term_app2 (module Type) env s F.rem) | "fp.roundToIntegral" -> Type.builtin_term (Base.term_app2 (module Type) env s F.roundToIntegral) | "fp.min" -> Type.builtin_term (Base.term_app2 (module Type) env s F.min) ~meta:(`Partial (fun _ _ ret_ty -> match Ty.view ret_ty with | `Float es -> F.min' es | _ -> F.min' (1, 1))) | "fp.max" -> Type.builtin_term (Base.term_app2 (module Type) env s F.max) ~meta:(`Partial (fun _ _ ret_ty -> match Ty.view ret_ty with | `Float es -> F.max' es | _ -> F.max' (1, 1))) | "fp.leq" -> Type.builtin_term (Base.term_app_chain (module Type) env s F.leq) | "fp.lt" -> Type.builtin_term (Base.term_app_chain (module Type) env s F.lt) | "fp.geq" -> Type.builtin_term (Base.term_app_chain (module Type) env s F.geq) | "fp.gt" -> Type.builtin_term (Base.term_app_chain (module Type) env s F.gt) | "fp.eq" -> Type.builtin_term (Base.term_app_chain (module Type) env s F.eq) | "fp.isNormal" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isNormal) | "fp.isSubnormal" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isSubnormal) | "fp.isZero" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isZero) | "fp.isInfinite" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isInfinite) | "fp.isNaN" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isNaN) | "fp.isNegative" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isNegative) | "fp.isPositive" -> Type.builtin_term (Base.term_app1 (module Type) env s F.isPositive) | "fp.to_real" -> Type.builtin_term (Base.term_app1 (module Type) env s F.to_real) | "fp.to_ubv" -> begin match version with | `Response _ -> `Reserved (`Model ( "completing interpretation of fp.to_bv in models", `Partial (meta_to_bv F.to_ubv'))) | `Script _ -> (* the regular case is handled later, because fp.to_ubv is an indexed identifier. *) `Not_found end | "fp.to_sbv" -> begin match version with | `Response _ -> `Reserved (`Model ( "completing interpretation of fp.to_sbv in models", `Partial (meta_to_bv F.to_sbv'))) | `Script _ -> (* the regular case is handled later, because fp.to_sbv is an indexed identifier. *) `Not_found end | _ -> `Not_found end | Type.Id { ns = Term; name = Indexed { basename; indexes; } } as symbol -> Base.parse_indexed basename indexes (function | "+oo" -> `Binary (fun e s -> Type.builtin_term (Base.app0_ast (module Type) env symbol (indexed2 env F.plus_infinity e s))) | "-oo" -> `Binary (fun e s -> Type.builtin_term (Base.app0_ast (module Type) env symbol (indexed2 env F.minus_infinity e s))) | "+zero" -> `Binary (fun e s -> Type.builtin_term (Base.app0_ast (module Type) env symbol (indexed2 env F.plus_zero e s))) | "-zero" -> `Binary (fun e s -> Type.builtin_term (Base.app0_ast (module Type) env symbol (indexed2 env F.minus_zero e s))) | "NaN" -> `Binary (fun e s -> Type.builtin_term (Base.app0_ast (module Type) env symbol (indexed2 env F.nan e s))) | "to_fp" -> `Binary (to_fp env symbol) | "to_fp_unsigned" -> `Binary (fun e s -> Type.builtin_term (Base.term_app2_ast (module Type) env symbol (indexed2 env F.ubv_to_fp e s))) | "fp.to_ubv" -> `Unary (fun n -> Type.builtin_term (Base.term_app2_ast (module Type) env symbol (indexed1 env F.to_ubv n)) ~meta:(`Partial (meta_to_bv F.to_ubv'))) | "fp.to_sbv" -> `Unary (fun n -> Type.builtin_term (Base.term_app2_ast (module Type) env symbol (indexed1 env F.to_sbv n)) ~meta:(`Partial (meta_to_bv F.to_sbv'))) | _ -> `Not_indexed) ~err:(Base.bad_term_index_arity (module Type) env) ~k:(function () -> `Not_found) | _ -> `Not_found end end