| 1 |
open Util |
| 2 |
open Environ |
| 3 |
open Conversion |
| 4 |
open Vm |
| 5 |
open Values |
| 6 |
open Vmvalues |
| 7 |
open Vmsymtable |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type 'a fail = { fail : 'r. 'a -> 'r } |
| 12 |
|
| 13 |
exception NotConvertible |
| 14 |
|
| 15 |
let fail_check state check box = match state with |
| 16 |
| Result.Ok state -> (state, check, box) |
| 17 |
| Result.Error None -> raise NotConvertible |
| 18 |
| Result.Error (Some err) -> box.fail err |
| 19 |
|
| 20 |
let convert_instances ~flex u1 u2 (state, check, box) = |
| 21 |
let state, check = Conversion.convert_instances ~flex u1 u2 (state, check) in |
| 22 |
fail_check state check box |
| 23 |
|
| 24 |
let sort_cmp_universes pb s1 s2 (state, check, box) = |
| 25 |
let state, check = Conversion.sort_cmp_universes pb s1 s2 (state, check) in |
| 26 |
fail_check state check box |
| 27 |
|
| 28 |
let table_key_instance env = function |
| 29 |
| ConstKey cst -> Environ.constant_context env cst |
| 30 |
| RelKey _ | VarKey _ | EvarKey _ -> UVars.AbstractContext.empty |
| 31 |
|
| 32 |
let compare_zipper z1 z2 = |
| 33 |
match z1, z2 with |
| 34 |
| Zapp args1, Zapp args2 -> Int.equal (nargs args1) (nargs args2) |
| 35 |
| Zfix(_f1,args1), Zfix(_f2,args2) -> Int.equal (nargs args1) (nargs args2) |
| 36 |
| Zswitch _, Zswitch _ | Zproj _, Zproj _ -> true |
| 37 |
| Zapp _ , _ | Zfix _, _ | Zswitch _, _ | Zproj _, _ -> false |
| 38 |
|
| 39 |
let rec compare_stack stk1 stk2 = |
| 40 |
match stk1, stk2 with |
| 41 |
| [], [] -> true |
| 42 |
| z1::stk1, z2::stk2 -> |
| 43 |
if compare_zipper z1 z2 then compare_stack stk1 stk2 |
| 44 |
else false |
| 45 |
| _, _ -> false |
| 46 |
|
| 47 |
|
| 48 |
let conv_vect fconv vect1 vect2 cu = |
| 49 |
let n = Array.length vect1 in |
| 50 |
if Int.equal n (Array.length vect2) then |
| 51 |
let rcu = ref cu in |
| 52 |
for i = 0 to n - 1 do |
| 53 |
rcu := fconv vect1.(i) vect2.(i) !rcu |
| 54 |
done; |
| 55 |
!rcu |
| 56 |
else raise NotConvertible |
| 57 |
|
| 58 |
let rec conv_val env pb k v1 v2 cu = |
| 59 |
if v1 == v2 then cu |
| 60 |
else conv_whd env pb k (whd_val v1) (whd_val v2) cu |
| 61 |
|
| 62 |
and conv_whd env pb k whd1 whd2 cu = |
| 63 |
|
| 64 |
match whd1, whd2 with |
| 65 |
| Vprod p1, Vprod p2 -> |
| 66 |
let cu = conv_val env CONV k (dom p1) (dom p2) cu in |
| 67 |
conv_fun env pb k (codom p1) (codom p2) cu |
| 68 |
| Vfun f1, Vfun f2 -> conv_fun env CONV k f1 f2 cu |
| 69 |
| Vfix (f1,None), Vfix (f2,None) -> conv_fix env k f1 f2 cu |
| 70 |
| Vfix (f1,Some args1), Vfix(f2,Some args2) -> |
| 71 |
if nargs args1 <> nargs args2 then raise NotConvertible |
| 72 |
else conv_arguments env k args1 args2 (conv_fix env k f1 f2 cu) |
| 73 |
| Vcofix (cf1,_,None), Vcofix (cf2,_,None) -> conv_cofix env k cf1 cf2 cu |
| 74 |
| Vcofix (cf1,_,Some args1), Vcofix (cf2,_,Some args2) -> |
| 75 |
if nargs args1 <> nargs args2 then raise NotConvertible |
| 76 |
else conv_arguments env k args1 args2 (conv_cofix env k cf1 cf2 cu) |
| 77 |
| Vconst i1, Vconst i2 -> |
| 78 |
if Int.equal i1 i2 then cu else raise NotConvertible |
| 79 |
| Vblock b1, Vblock b2 -> |
| 80 |
let tag1 = btag b1 and tag2 = btag b2 in |
| 81 |
let sz = bsize b1 in |
| 82 |
if Int.equal tag1 tag2 && Int.equal sz (bsize b2) then |
| 83 |
let rcu = ref cu in |
| 84 |
for i = 0 to sz - 1 do |
| 85 |
rcu := conv_val env CONV k (bfield b1 i) (bfield b2 i) !rcu |
| 86 |
done; |
| 87 |
!rcu |
| 88 |
else raise NotConvertible |
| 89 |
| Vint64 i1, Vint64 i2 -> |
| 90 |
if Int64.equal i1 i2 then cu else raise NotConvertible |
| 91 |
| Vfloat64 f1, Vfloat64 f2 -> |
| 92 |
if Float64.(equal (of_float f1) (of_float f2)) then cu |
| 93 |
else raise NotConvertible |
| 94 |
| Vstring s1, Vstring s2 -> |
| 95 |
if Pstring.equal s1 s2 then cu |
| 96 |
else raise NotConvertible |
| 97 |
| Varray t1, Varray t2 -> |
| 98 |
if t1 == t2 then cu else |
| 99 |
let n = Parray.length_int t1 in |
| 100 |
if not (Int.equal n (Parray.length_int t2)) then raise NotConvertible; |
| 101 |
Parray.fold_left2 (fun cu v1 v2 -> conv_val env CONV k v1 v2 cu) cu t1 t2 |
| 102 |
| Vaccu (a1, stk1), Vaccu (a2, stk2) -> |
| 103 |
conv_atom env pb k a1 stk1 a2 stk2 cu |
| 104 |
| Vfun _, _ | _, Vfun _ -> |
| 105 |
|
| 106 |
conv_val env CONV (k+1) (apply_whd k whd1) (apply_whd k whd2) cu |
| 107 |
|
| 108 |
| Vprod _, _ | Vfix _, _ | Vcofix _, _ | Vconst _, _ | Vint64 _, _ |
| 109 |
| Vfloat64 _, _ | Vstring _, _ | Varray _, _ | Vblock _, _ | Vaccu _, _ -> raise NotConvertible |
| 110 |
|
| 111 |
|
| 112 |
and conv_atom env pb k a1 stk1 a2 stk2 cu = |
| 113 |
|
| 114 |
match a1, a2 with |
| 115 |
| Aind ((mi,_i) as ind1) , Aind ind2 -> |
| 116 |
if Names.Ind.CanOrd.equal ind1 ind2 && compare_stack stk1 stk2 then |
| 117 |
if UVars.AbstractContext.is_constant (Environ.mind_context env mi) then |
| 118 |
conv_stack env k stk1 stk2 cu |
| 119 |
else begin |
| 120 |
match stk1 , stk2 with |
| 121 |
| Zapp args1 :: stk1' , Zapp args2 :: stk2' -> |
| 122 |
assert (0 < nargs args1); |
| 123 |
assert (0 < nargs args2); |
| 124 |
let u1 = uni_instance (arg args1 0) in |
| 125 |
let u2 = uni_instance (arg args2 0) in |
| 126 |
let cu = convert_instances ~flex:false u1 u2 cu in |
| 127 |
conv_arguments env ~from:1 k args1 args2 |
| 128 |
(conv_stack env k stk1' stk2' cu) |
| 129 |
| _, _ -> assert false |
| 130 |
end |
| 131 |
else raise NotConvertible |
| 132 |
| Aid ik1, Aid ik2 -> |
| 133 |
if Vmvalues.eq_id_key ik1 ik2 && compare_stack stk1 stk2 then |
| 134 |
if UVars.AbstractContext.is_constant (table_key_instance env ik1) then |
| 135 |
conv_stack env k stk1 stk2 cu |
| 136 |
else |
| 137 |
match stk1 , stk2 with |
| 138 |
| Zapp args1 :: stk1' , Zapp args2 :: stk2' -> |
| 139 |
assert (0 < nargs args1); |
| 140 |
assert (0 < nargs args2); |
| 141 |
let u1 = uni_instance (arg args1 0) in |
| 142 |
let u2 = uni_instance (arg args2 0) in |
| 143 |
let cu = convert_instances ~flex:false u1 u2 cu in |
| 144 |
conv_arguments env ~from:1 k args1 args2 |
| 145 |
(conv_stack env k stk1' stk2' cu) |
| 146 |
| _, _ -> assert false |
| 147 |
else raise NotConvertible |
| 148 |
| Asort s1, Asort s2 -> |
| 149 |
sort_cmp_universes pb s1 s2 cu |
| 150 |
| Asort _ , _ | Aind _, _ | Aid _, _ -> raise NotConvertible |
| 151 |
|
| 152 |
and conv_stack env k stk1 stk2 cu = |
| 153 |
match stk1, stk2 with |
| 154 |
| [], [] -> cu |
| 155 |
| Zapp args1 :: stk1, Zapp args2 :: stk2 -> |
| 156 |
conv_stack env k stk1 stk2 (conv_arguments env k args1 args2 cu) |
| 157 |
| Zfix(f1,args1) :: stk1, Zfix(f2,args2) :: stk2 -> |
| 158 |
conv_stack env k stk1 stk2 |
| 159 |
(conv_arguments env k args1 args2 (conv_fix env k f1 f2 cu)) |
| 160 |
| Zswitch sw1 :: stk1, Zswitch sw2 :: stk2 -> |
| 161 |
if check_switch sw1 sw2 then |
| 162 |
let vt1,vt2 = type_of_switch sw1, type_of_switch sw2 in |
| 163 |
let rcu = ref (conv_val env CONV k vt1 vt2 cu) in |
| 164 |
let b1, b2 = branch_of_switch k sw1, branch_of_switch k sw2 in |
| 165 |
for i = 0 to Array.length b1 - 1 do |
| 166 |
rcu := |
| 167 |
conv_val env CONV (k + fst b1.(i)) (snd b1.(i)) (snd b2.(i)) !rcu |
| 168 |
done; |
| 169 |
conv_stack env k stk1 stk2 !rcu |
| 170 |
else raise NotConvertible |
| 171 |
| Zproj p1 :: stk1, Zproj p2 :: stk2 -> |
| 172 |
if Int.equal p1 p2 then conv_stack env k stk1 stk2 cu |
| 173 |
else raise NotConvertible |
| 174 |
| [], _ | Zapp _ :: _, _ | Zfix _ :: _, _ | Zswitch _ :: _, _ |
| 175 |
| Zproj _ :: _, _ -> raise NotConvertible |
| 176 |
|
| 177 |
and conv_fun env pb k f1 f2 cu = |
| 178 |
if f1 == f2 then cu |
| 179 |
else |
| 180 |
let arity,b1,b2 = decompose_vfun2 k f1 f2 in |
| 181 |
conv_val env pb (k+arity) b1 b2 cu |
| 182 |
|
| 183 |
and conv_fix env k f1 f2 cu = |
| 184 |
if f1 == f2 then cu |
| 185 |
else |
| 186 |
if check_fix f1 f2 then |
| 187 |
let bf1, tf1 = reduce_fix k f1 in |
| 188 |
let bf2, tf2 = reduce_fix k f2 in |
| 189 |
let cu = conv_vect (conv_val env CONV k) tf1 tf2 cu in |
| 190 |
conv_vect (conv_fun env CONV (k + Array.length tf1)) bf1 bf2 cu |
| 191 |
else raise NotConvertible |
| 192 |
|
| 193 |
and conv_cofix env k cf1 cf2 cu = |
| 194 |
if cf1 == cf2 then cu |
| 195 |
else |
| 196 |
if check_cofix cf1 cf2 then |
| 197 |
let bcf1, tcf1 = reduce_cofix k cf1 in |
| 198 |
let bcf2, tcf2 = reduce_cofix k cf2 in |
| 199 |
let cu = conv_vect (conv_val env CONV k) tcf1 tcf2 cu in |
| 200 |
conv_vect (conv_val env CONV (k + Array.length tcf1)) bcf1 bcf2 cu |
| 201 |
else raise NotConvertible |
| 202 |
|
| 203 |
and conv_arguments env ?from:(from=0) k args1 args2 cu = |
| 204 |
if args1 == args2 then cu |
| 205 |
else |
| 206 |
let n = nargs args1 in |
| 207 |
if Int.equal n (nargs args2) then |
| 208 |
let rcu = ref cu in |
| 209 |
for i = from to n - 1 do |
| 210 |
rcu := conv_val env CONV k (arg args1 i) (arg args2 i) !rcu |
| 211 |
done; |
| 212 |
!rcu |
| 213 |
else raise NotConvertible |
| 214 |
|
| 215 |
let warn_bytecode_compiler_failed = |
| 216 |
let open Pp in |
| 217 |
CWarnings.create ~name:"bytecode-compiler-failed" ~category:CWarnings.CoreCategories.bytecode_compiler |
| 218 |
(fun () -> strbrk "Bytecode compiler failed, " ++ |
| 219 |
strbrk "falling back to standard conversion") |
| 220 |
|
| 221 |
let vm_conv_gen (type err) cv_pb sigma env univs t1 t2 = |
| 222 |
if not (typing_flags env).Declarations.enable_VM then |
| 223 |
Conversion.generic_conv cv_pb ~l2r:false ~evars:sigma.Genlambda.evars_val |
| 224 |
TransparentState.full env univs t1 t2 |
| 225 |
else |
| 226 |
let exception Error of err in |
| 227 |
let box = { fail = fun e -> raise (Error e) } in |
| 228 |
try |
| 229 |
let (state, check) = univs in |
| 230 |
let v1 = val_of_constr env sigma t1 in |
| 231 |
let v2 = val_of_constr env sigma t2 in |
| 232 |
Result.Ok (pi1 (conv_val env cv_pb (nb_rel env) v1 v2 (state, check, box))) |
| 233 |
with |
| 234 |
| NotConvertible -> Result.Error None |
| 235 |
| Error e -> Result.Error (Some e) |
| 236 |
| Not_found | Invalid_argument _ | Vmerrors.CompileError _ -> |
| 237 |
warn_bytecode_compiler_failed (); |
| 238 |
Conversion.generic_conv cv_pb ~l2r:false ~evars:sigma.Genlambda.evars_val |
| 239 |
TransparentState.full env univs t1 t2 |
| 240 |
|
| 241 |
let vm_conv cv_pb env t1 t2 = |
| 242 |
let univs = Environ.universes env in |
| 243 |
let b = |
| 244 |
if cv_pb = CUMUL then Constr.leq_constr_univs univs t1 t2 |
| 245 |
else Constr.eq_constr_univs univs t1 t2 |
| 246 |
in |
| 247 |
if b then Result.Ok () |
| 248 |
else |
| 249 |
let state = (univs, checked_universes) in |
| 250 |
let ans : (UGraph.t, 'a option) result = |
| 251 |
NewProfile.profile "vm_conv" (fun () -> |
| 252 |
vm_conv_gen cv_pb (Genlambda.empty_evars env) env state t1 t2) |
| 253 |
() |
| 254 |
in |
| 255 |
match ans with |
| 256 |
| Result.Ok (_ : UGraph.t)-> Result.Ok () |
| 257 |
| Result.Error None -> Result.Error () |
| 258 |
| Result.Error (Some e) -> Empty.abort e |