| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Names |
| 12 |
open Conversion |
| 13 |
open Util |
| 14 |
open Values |
| 15 |
open Nativevalues |
| 16 |
open Nativecode |
| 17 |
open Environ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type 'a fail = { fail : 'r. 'a -> 'r } |
| 22 |
|
| 23 |
exception NotConvertible |
| 24 |
|
| 25 |
let fail_check state check box = match state with |
| 26 |
| Result.Ok state -> (state, check, box) |
| 27 |
| Result.Error None -> raise NotConvertible |
| 28 |
| Result.Error (Some err) -> box.fail err |
| 29 |
|
| 30 |
let convert_instances ~flex u1 u2 (state, check, box) = |
| 31 |
let state, check = Conversion.convert_instances ~flex u1 u2 (state, check) in |
| 32 |
fail_check state check box |
| 33 |
|
| 34 |
let sort_cmp_universes pb s1 s2 (state, check, box) = |
| 35 |
let state, check = Conversion.sort_cmp_universes pb s1 s2 (state, check) in |
| 36 |
fail_check state check box |
| 37 |
|
| 38 |
let rec conv_val env pb lvl v1 v2 cu = |
| 39 |
if v1 == v2 then cu |
| 40 |
else |
| 41 |
match kind_of_value v1, kind_of_value v2 with |
| 42 |
| Vfun f1, Vfun f2 -> |
| 43 |
let v = mk_rel_accu lvl in |
| 44 |
conv_val env CONV (lvl+1) (f1 v) (f2 v) cu |
| 45 |
| Vfun _f1, _ -> |
| 46 |
conv_val env CONV lvl v1 (eta_expand v2) cu |
| 47 |
| _, Vfun _f2 -> |
| 48 |
conv_val env CONV lvl (eta_expand v1) v2 cu |
| 49 |
| Vaccu k1, Vaccu k2 -> |
| 50 |
conv_accu env pb lvl k1 k2 cu |
| 51 |
| Vprod(_,d1,c1), Vprod(_,d2,c2) -> |
| 52 |
let cu = conv_val env CONV lvl d1 d2 cu in |
| 53 |
let v = mk_rel_accu lvl in |
| 54 |
conv_val env pb (lvl + 1) (apply c1 v) (apply c2 v) cu |
| 55 |
| Vconst i1, Vconst i2 -> |
| 56 |
if Int.equal i1 i2 then cu else raise NotConvertible |
| 57 |
| Vint64 i1, Vint64 i2 -> |
| 58 |
if Int64.equal i1 i2 then cu else raise NotConvertible |
| 59 |
| Vfloat64 f1, Vfloat64 f2 -> |
| 60 |
if Float64.(equal (of_float f1) (of_float f2)) then cu |
| 61 |
else raise NotConvertible |
| 62 |
| Vstring s1, Vstring s2 -> |
| 63 |
if Pstring.equal s1 s2 then cu |
| 64 |
else raise NotConvertible |
| 65 |
| Varray t1, Varray t2 -> |
| 66 |
let len = Parray.length_int t1 in |
| 67 |
if not (Int.equal len (Parray.length_int t2)) then raise NotConvertible; |
| 68 |
Parray.fold_left2 (fun cu v1 v2 -> conv_val env CONV lvl v1 v2 cu) cu t1 t2 |
| 69 |
| Vblock b1, Vblock b2 -> |
| 70 |
let n1 = block_size b1 in |
| 71 |
let n2 = block_size b2 in |
| 72 |
if not (Int.equal (block_tag b1) (block_tag b2)) || not (Int.equal n1 n2) then |
| 73 |
raise NotConvertible; |
| 74 |
let rec aux lvl max b1 b2 i cu = |
| 75 |
if Int.equal i max then |
| 76 |
conv_val env CONV lvl (block_field b1 i) (block_field b2 i) cu |
| 77 |
else |
| 78 |
let cu = conv_val env CONV lvl (block_field b1 i) (block_field b2 i) cu in |
| 79 |
aux lvl max b1 b2 (i+1) cu |
| 80 |
in |
| 81 |
aux lvl (n1-1) b1 b2 0 cu |
| 82 |
| (Vfix e | Vcofix e), _ | _, (Vfix e | Vcofix e) -> Empty.abort e |
| 83 |
| (Vaccu _ | Vprod _ | Vconst _ | Vint64 _ | Vfloat64 _ | Vstring _ | Varray _ | Vblock _), _ -> raise NotConvertible |
| 84 |
|
| 85 |
and conv_accu env pb lvl k1 k2 cu = |
| 86 |
let n1 = accu_nargs k1 in |
| 87 |
let n2 = accu_nargs k2 in |
| 88 |
if not (Int.equal n1 n2) then raise NotConvertible; |
| 89 |
if Int.equal n1 0 then |
| 90 |
conv_atom env pb lvl (atom_of_accu k1) (atom_of_accu k2) cu |
| 91 |
else |
| 92 |
let cu = conv_atom env pb lvl (atom_of_accu k1) (atom_of_accu k2) cu in |
| 93 |
List.fold_right2 (conv_val env CONV lvl) (args_of_accu k1) (args_of_accu k2) cu |
| 94 |
|
| 95 |
and conv_atom env pb lvl a1 a2 cu = |
| 96 |
if a1 == a2 then cu |
| 97 |
else |
| 98 |
match a1, a2 with |
| 99 |
| Aevar (ev1, args1), Aevar (ev2, args2) -> |
| 100 |
if Evar.equal ev1 ev2 then |
| 101 |
Array.fold_right2 (conv_val env CONV lvl) args1 args2 cu |
| 102 |
else raise NotConvertible |
| 103 |
| Arel i1, Arel i2 -> |
| 104 |
if Int.equal i1 i2 then cu else raise NotConvertible |
| 105 |
| Aind (ind1,u1), Aind (ind2,u2) -> |
| 106 |
if Ind.CanOrd.equal ind1 ind2 then convert_instances ~flex:false u1 u2 cu |
| 107 |
else raise NotConvertible |
| 108 |
| Aconstant (c1,u1), Aconstant (c2,u2) -> |
| 109 |
if Constant.CanOrd.equal c1 c2 then convert_instances ~flex:true u1 u2 cu |
| 110 |
else raise NotConvertible |
| 111 |
| Asort s1, Asort s2 -> |
| 112 |
sort_cmp_universes pb s1 s2 cu |
| 113 |
| Avar id1, Avar id2 -> |
| 114 |
if Id.equal id1 id2 then cu else raise NotConvertible |
| 115 |
| Acase(a1,ac1,p1,bs1), Acase(a2,ac2,p2,bs2) -> |
| 116 |
if not (Ind.CanOrd.equal a1.asw_ind a2.asw_ind) then raise NotConvertible; |
| 117 |
let cu = conv_accu env CONV lvl ac1 ac2 cu in |
| 118 |
let tbl = a1.asw_reloc in |
| 119 |
let len = Array.length tbl in |
| 120 |
if Int.equal len 0 then conv_val env CONV lvl p1 p2 cu |
| 121 |
else begin |
| 122 |
let cu = conv_val env CONV lvl p1 p2 cu in |
| 123 |
let max = len - 1 in |
| 124 |
let rec aux i cu = |
| 125 |
let tag,arity = tbl.(i) in |
| 126 |
let ci = |
| 127 |
if Int.equal arity 0 then mk_const tag |
| 128 |
else mk_block tag (mk_rels_accu lvl arity) in |
| 129 |
let bi1 = apply bs1 ci and bi2 = apply bs2 ci in |
| 130 |
if Int.equal i max then conv_val env CONV (lvl + arity) bi1 bi2 cu |
| 131 |
else aux (i+1) (conv_val env CONV (lvl + arity) bi1 bi2 cu) in |
| 132 |
aux 0 cu |
| 133 |
end |
| 134 |
| Afix(t1,f1,rp1,s1), Afix(t2,f2,rp2,s2) -> |
| 135 |
if not (Int.equal s1 s2) || not (Array.equal Int.equal rp1 rp2) then raise NotConvertible; |
| 136 |
if f1 == f2 then cu |
| 137 |
else conv_fix env lvl t1 f1 t2 f2 cu |
| 138 |
| Acofix (t1, f1, s1, args1, _), Acofix (t2, f2, s2, args2, _) -> |
| 139 |
if not (Int.equal s1 s2) then raise NotConvertible; |
| 140 |
if f1 == f2 && args1 == args2 then cu |
| 141 |
else if not (Int.equal (Array.length f1) (Array.length f2) && Int.equal (Array.length args1) (Array.length args2)) then |
| 142 |
raise NotConvertible |
| 143 |
else |
| 144 |
Array.fold_left2 (fun cu v1 v2 -> conv_val env CONV lvl v1 v2 cu) (conv_fix env lvl t1 f1 t2 f2 cu) args1 args2 |
| 145 |
| Aproj((ind1, i1), ac1), Aproj((ind2, i2), ac2) -> |
| 146 |
if not (Ind.CanOrd.equal ind1 ind2 && Int.equal i1 i2) then raise NotConvertible |
| 147 |
else conv_accu env CONV lvl ac1 ac2 cu |
| 148 |
| Arel _, _ | Aind _, _ | Aconstant _, _ | Asort _, _ | Avar _, _ |
| 149 |
| Acase _, _ | Afix _, _ | Acofix _, _ |
| 150 |
| Aproj _, _ | Aevar _, _ -> raise NotConvertible |
| 151 |
|
| 152 |
|
| 153 |
and conv_fix env lvl t1 f1 t2 f2 cu = |
| 154 |
let len = Array.length f1 in |
| 155 |
let max = len - 1 in |
| 156 |
let fargs = mk_rels_accu lvl len in |
| 157 |
let flvl = lvl + len in |
| 158 |
let rec aux i cu = |
| 159 |
let cu = conv_val env CONV lvl t1.(i) t2.(i) cu in |
| 160 |
let fi1 = napply f1.(i) fargs in |
| 161 |
let fi2 = napply f2.(i) fargs in |
| 162 |
if Int.equal i max then conv_val env CONV flvl fi1 fi2 cu |
| 163 |
else aux (i+1) (conv_val env CONV flvl fi1 fi2 cu) in |
| 164 |
aux 0 cu |
| 165 |
|
| 166 |
let w_native_disabled = CWarnings.create_warning |
| 167 |
~from:[CWarnings.CoreCategories.native_compiler] ~name:"native-compiler-disabled" |
| 168 |
() |
| 169 |
|
| 170 |
let warn_no_native_compiler = |
| 171 |
let open Pp in |
| 172 |
CWarnings.create_in w_native_disabled |
| 173 |
(fun () -> strbrk "Native compiler is disabled," ++ |
| 174 |
strbrk " falling back to VM conversion test.") |
| 175 |
|
| 176 |
let native_conv_gen (type err) pb sigma env (state, check) t1 t2 = |
| 177 |
Nativelib.link_libraries (); |
| 178 |
let ml_filename, prefix = Nativelib.get_ml_filename () in |
| 179 |
let code, symbols, upds = mk_conv_code env sigma prefix t1 t2 in |
| 180 |
let fn = Nativelib.compile ml_filename code ~profile:false in |
| 181 |
debug_native_compiler (fun () -> Pp.str "Running test..."); |
| 182 |
let t0 = Sys.time () in |
| 183 |
let (rt1, rt2) = Nativelib.execute_library ~prefix fn symbols upds in |
| 184 |
let rt1 = Option.get rt1 and rt2 = Option.get rt2 in |
| 185 |
let t1 = Sys.time () in |
| 186 |
let time_info = Format.sprintf "Evaluation done in %.5f@." (t1 -. t0) in |
| 187 |
debug_native_compiler (fun () -> Pp.str time_info); |
| 188 |
|
| 189 |
let exception Error of err in |
| 190 |
let box = { fail = fun e -> raise (Error e) } in |
| 191 |
try Result.Ok (pi1 (conv_val env pb 0 rt1 rt2 (state, check, box))) |
| 192 |
with |
| 193 |
| NotConvertible -> Result.Error None |
| 194 |
| Error e -> Result.Error (Some e) |
| 195 |
|
| 196 |
let native_conv_gen pb sigma env univs t1 t2 = |
| 197 |
if not (typing_flags env).Declarations.enable_native_compiler then |
| 198 |
let () = warn_no_native_compiler () in |
| 199 |
Vconv.vm_conv_gen pb sigma env univs t1 t2 |
| 200 |
else native_conv_gen pb sigma env univs t1 t2 |
| 201 |
|
| 202 |
|
| 203 |
let native_conv cv_pb sigma env t1 t2 = |
| 204 |
let univs = Environ.universes env in |
| 205 |
let b = |
| 206 |
if cv_pb = CUMUL then Constr.leq_constr_univs univs t1 t2 |
| 207 |
else Constr.eq_constr_univs univs t1 t2 |
| 208 |
in |
| 209 |
if b then Result.Ok () |
| 210 |
else |
| 211 |
let state = (univs, checked_universes) in |
| 212 |
let t1 = Term.it_mkLambda_or_LetIn t1 (Environ.rel_context env) in |
| 213 |
let t2 = Term.it_mkLambda_or_LetIn t2 (Environ.rel_context env) in |
| 214 |
match native_conv_gen cv_pb sigma env state t1 t2 with |
| 215 |
| Result.Ok (_ : UGraph.t) -> Result.Ok () |
| 216 |
| Result.Error None -> Result.Error () |
| 217 |
| Result.Error (Some _) -> |
| 218 |
|
| 219 |
assert false |