Skip to content

Commit 7a83470

Browse files
committed
Lazy conversion add flag to disable first order approximation, use for VM fallback
1 parent 8f5c9cd commit 7a83470

File tree

8 files changed

+39
-18
lines changed

8 files changed

+39
-18
lines changed

kernel/conversion.ml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,19 @@ let pure_stack lfts stk =
119119
(* Conversion *)
120120
(********************************************************************)
121121

122+
type firstorder_mode =
123+
| Eager
124+
| L2R
125+
| R2L
126+
122127
(* Conversion utility functions *)
123128

124129
(* functions of this type are called from the kernel *)
125130
type 'a kernel_conversion_function = env -> 'a -> 'a -> (unit, unit) result
126131

127132
(* functions of this type can be called from outside the kernel *)
128133
type 'a extended_conversion_function =
129-
?l2r:bool -> ?reds:TransparentState.t -> env ->
134+
?l2r:firstorder_mode -> ?reds:TransparentState.t -> env ->
130135
?evars:evar_handler ->
131136
'a -> 'a -> (unit, unit) result
132137

@@ -385,7 +390,12 @@ let rec ccnv cv_pb l2r infos lft1 lft2 term1 term2 cuniv =
385390
and eqappr cv_pb l2r infos (lft1,st1) (lft2,st2) cuniv =
386391
Control.check_for_interrupt ();
387392
(* First head reduce both terms *)
388-
let ninfos = infos_with_reds infos.cnv_inf RedFlags.betaiotazeta in
393+
let ninfos = match l2r with
394+
| Eager ->
395+
let all = RedFlags.(red_add_transparent all (red_transparent (info_flags infos.cnv_inf))) in
396+
infos_with_reds infos.cnv_inf all
397+
| L2R | R2L -> infos_with_reds infos.cnv_inf RedFlags.betaiotazeta
398+
in
389399
let appr1 = whd_stack ninfos infos.lft_tab (fst st1) (snd st1) in
390400
let appr2 = whd_stack ninfos infos.rgt_tab (fst st2) (snd st2) in
391401
eqwhnf cv_pb l2r infos (lft1, appr1) (lft2, appr2) cuniv
@@ -467,6 +477,11 @@ and eqwhnf cv_pb l2r infos (lft1, (hd1, v1) as appr1) (lft2, (hd2, v2) as appr2)
467477
let () = Control.check_for_interrupt () in
468478
(* Determine which constant to unfold first *)
469479
let unfold_left =
480+
let l2r = match l2r with
481+
| L2R -> true
482+
| R2L -> false
483+
| Eager -> assert false
484+
in
470485
let order = Conv_oracle.oracle_compare oracle (to_er fl1) (to_er fl2) in
471486
match order with
472487
| Conv_oracle.Left -> true
@@ -1010,7 +1025,7 @@ let () =
10101025
let state = info_univs infos in
10111026
let qual_equal q1 q2 = CClosure.eq_quality infos q1 q2 in
10121027
let infos = { cnv_inf = infos; cnv_typ = true; lft_tab = tab; rgt_tab = tab; err_ret = box; } in
1013-
let state', _ = ccnv CONV false infos el_id el_id a b (state, checked_universes_gen qual_equal) in
1028+
let state', _ = ccnv CONV R2L infos el_id el_id a b (state, checked_universes_gen qual_equal) in
10141029
assert (state==state');
10151030
true
10161031
with
@@ -1019,7 +1034,7 @@ let () =
10191034
in
10201035
CClosure.set_conv conv
10211036

1022-
let gen_conv ~typed cv_pb ?(l2r=false) ?(reds=TransparentState.full) env ?(evars=default_evar_handler env) t1 t2 =
1037+
let gen_conv ~typed cv_pb ?(l2r=R2L) ?(reds=TransparentState.full) env ?(evars=default_evar_handler env) t1 t2 =
10231038
let univs = Environ.universes env in
10241039
let state = univs in
10251040
let b =

kernel/conversion.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ open Environ
1414
(***********************************************************************
1515
s conversion functions *)
1616

17+
(** Which side to unfold first (default: R2L ie unfold right side first) *)
18+
type firstorder_mode =
19+
| Eager
20+
| L2R
21+
| R2L
22+
1723
type 'a kernel_conversion_function = env -> 'a -> 'a -> (unit, unit) result
1824
type 'a extended_conversion_function =
19-
?l2r:bool -> ?reds:TransparentState.t -> env ->
25+
?l2r:firstorder_mode -> ?reds:TransparentState.t -> env ->
2026
?evars:CClosure.evar_handler ->
2127
'a -> 'a -> (unit, unit) result
2228

@@ -57,7 +63,7 @@ val conv_leq : types extended_conversion_function
5763

5864
(** The failure values depend on the universe state functions
5965
(for better error messages). *)
60-
val generic_conv : conv_pb -> l2r:bool
66+
val generic_conv : conv_pb -> l2r:firstorder_mode
6167
-> TransparentState.t -> env -> ?evars:CClosure.evar_handler
6268
-> ('a, 'err) generic_conversion_function
6369

kernel/mod_typing.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ let rec rebuild_mp mp l =
5353
| i::r -> rebuild_mp (MPdot(mp,i)) r
5454

5555
let infer_gen_conv state env c1 c2 =
56-
Conversion.generic_conv Conversion.CONV ~l2r:false TransparentState.full env state c1 c2
56+
Conversion.generic_conv Conversion.CONV ~l2r:R2L TransparentState.full env state c1 c2
5757

5858
let infer_gen_conv_leq state env c1 c2 =
59-
Conversion.generic_conv Conversion.CUMUL ~l2r:false TransparentState.full env state c1 c2
59+
Conversion.generic_conv Conversion.CUMUL ~l2r:R2L TransparentState.full env state c1 c2
6060

6161
type with_body = {
6262
w_def : Constr.t;

kernel/subtyping.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ let check_conv_error error why state poly pb env a1 a2 =
8989
if poly then match Conversion.default_conv pb env a1 a2 with
9090
| Result.Ok () -> fst state
9191
| Result.Error () -> error (IncompatiblePolymorphism (env, a1, a2))
92-
else match Conversion.generic_conv pb ~l2r:false TransparentState.full env state a1 a2 with
92+
else match Conversion.generic_conv pb ~l2r:R2L TransparentState.full env state a1 a2 with
9393
| Result.Ok state -> state
9494
| Result.Error None -> error why
9595
| Result.Error (Some (Univ e)) -> error (IncompatibleUniverses { err = e; env; t1 = a1; t2 = a2 })

kernel/typeops.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ let should_invert_case env r ci =
528528
let type_case_scrutinee env (mib, _mip) (u', largs) u pms (pctx, p) c =
529529
let (params, realargs) = List.chop mib.mind_nparams largs in
530530
(* Check that the type of the scrutinee is <= the expected argument type *)
531-
let iter p1 p2 = match Conversion.conv ~l2r:true env p1 p2 with
531+
let iter p1 p2 = match Conversion.conv ~l2r:L2R env p1 p2 with
532532
| Result.Ok () -> ()
533533
| Result.Error () -> raise NotConvertible
534534
in

kernel/vconv.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ let warn_bytecode_compiler_failed =
220220

221221
let vm_conv_gen (type err) cv_pb sigma env univs t1 t2 =
222222
if not (typing_flags env).Declarations.enable_VM then
223-
Conversion.generic_conv cv_pb ~l2r:false ~evars:sigma.Genlambda.evars_val
223+
Conversion.generic_conv cv_pb ~l2r:Eager ~evars:sigma.Genlambda.evars_val
224224
TransparentState.full env univs t1 t2
225225
else
226226
let exception Error of err in
@@ -235,7 +235,7 @@ let vm_conv_gen (type err) cv_pb sigma env univs t1 t2 =
235235
| Error e -> Result.Error (Some e)
236236
| Not_found | Invalid_argument _ | Vmerrors.CompileError _ ->
237237
warn_bytecode_compiler_failed ();
238-
Conversion.generic_conv cv_pb ~l2r:false ~evars:sigma.Genlambda.evars_val
238+
Conversion.generic_conv cv_pb ~l2r:Eager ~evars:sigma.Genlambda.evars_val
239239
TransparentState.full env univs t1 t2
240240

241241
let vm_conv cv_pb env t1 t2 =

pretyping/reductionops.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ let is_fconv ?(reds=TransparentState.full) pb env sigma t1 t2 =
12511251
let evars = Evd.evar_handler sigma in
12521252
try
12531253
let env = Environ.set_universes (Evd.universes sigma) env in
1254-
begin match Conversion.generic_conv ~l2r:false pb ~evars reds env (sigma, CheckUnivs.checked_universes) t1 t2 with
1254+
begin match Conversion.generic_conv ~l2r:R2L pb ~evars reds env (sigma, CheckUnivs.checked_universes) t1 t2 with
12551255
| Result.Ok (_ : Evd.evar_map) -> true
12561256
| Result.Error None -> false
12571257
| Result.Error (Some e) -> Empty.abort e
@@ -1315,7 +1315,7 @@ let univproblem_univ_state =
13151315
compare_cumul_instances = univproblem_check_inductive_instances; }
13161316

13171317
type genconv = {
1318-
genconv : 'a 'err. conv_pb -> l2r:bool -> Evd.evar_map -> TransparentState.t ->
1318+
genconv : 'a 'err. conv_pb -> l2r:Conversion.firstorder_mode -> Evd.evar_map -> TransparentState.t ->
13191319
Environ.env -> ('a, 'err) Conversion.generic_conversion_function
13201320
}
13211321

@@ -1345,7 +1345,7 @@ let infer_conv_gen conv_fun ?(catch_incon=true) ?(pb=Conversion.CUMUL)
13451345
approximation. This may result in unsatisfiable constraints even if
13461346
some unfoldings of the arguments could have been unified, but this
13471347
should be exceedingly rare. *)
1348-
let ans = match conv_fun.genconv pb ~l2r:false sigma ts env (UnivProblem.Set.empty, univproblem_univ_state) x y with
1348+
let ans = match conv_fun.genconv pb ~l2r:R2L sigma ts env (UnivProblem.Set.empty, univproblem_univ_state) x y with
13491349
| Result.Ok cstr -> Some cstr
13501350
| Result.Error None ->
13511351
None (* no universe unification can make these terms convertible *)
@@ -1358,7 +1358,7 @@ let infer_conv_gen conv_fun ?(catch_incon=true) ?(pb=Conversion.CUMUL)
13581358
| sigma -> Some sigma
13591359
| exception UGraph.UniverseInconsistency _ | exception Evd.UniversesDiffer ->
13601360
(* Retry with local universe checking, which may imply constant unfolding *)
1361-
match conv_fun.genconv pb ~l2r:false sigma ts env (sigma, sigma_univ_state) x y with
1361+
match conv_fun.genconv pb ~l2r:R2L sigma ts env (sigma, sigma_univ_state) x y with
13621362
| Result.Ok sigma -> Some sigma
13631363
| Result.Error None -> None
13641364
| Result.Error (Some e) -> raise (UGraph.UniverseInconsistency e)
@@ -1388,7 +1388,7 @@ let infer_conv_ustate ?(catch_incon=true) ?(pb=Conversion.CUMUL)
13881388
let y = EConstr.Unsafe.to_constr y in
13891389
let env = Environ.set_universes (Evd.universes sigma) env in
13901390
match
1391-
Conversion.generic_conv pb ~l2r:false ~evars:(Evd.evar_handler sigma) ts
1391+
Conversion.generic_conv pb ~l2r:R2L ~evars:(Evd.evar_handler sigma) ts
13921392
env (UnivProblem.Set.empty, univproblem_univ_state) x y
13931393
with
13941394
| Result.Ok cstr -> Some cstr

pretyping/reductionops.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ val native_infer_conv : ?pb:conv_pb -> env -> evar_map -> constr -> constr ->
274274
evar_map option
275275

276276
type genconv = {
277-
genconv : 'a 'err. conv_pb -> l2r:bool -> Evd.evar_map -> TransparentState.t ->
277+
genconv : 'a 'err. conv_pb -> l2r:Conversion.firstorder_mode -> Evd.evar_map -> TransparentState.t ->
278278
Environ.env -> ('a, 'err) Conversion.generic_conversion_function
279279
}
280280

0 commit comments

Comments
 (0)