Skip to content

Commit 1cde045

Browse files
committed
Add a use_coercions flag to pretype_flags
1 parent d0a4cad commit 1cde045

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

pretyping/coercion.ml

+21-19
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,12 @@ let reapply_coercions_body sigma trace body =
519519
start_app_body sigma body
520520

521521
(* Try to coerce to a funclass; raise NoCoercion if not possible *)
522-
let inh_app_fun_core ~program_mode env sigma body typ =
522+
let inh_app_fun_core ~program_mode ?(use_coercions=true) env sigma body typ =
523523
match unify_product env sigma typ with
524524
| Inl sigma -> sigma, body, typ, IdCoe
525525
| Inr t ->
526526
try
527+
if not use_coercions then raise NoCoercion;
527528
let p = lookup_path_to_fun_from env sigma typ in
528529
let body = force_app_body body in
529530
let sigma, body, typ, trace = apply_coercion env sigma p body typ in
@@ -541,13 +542,13 @@ let inh_app_fun_core ~program_mode env sigma body typ =
541542
else Exninfo.iraise (NoCoercion,info)
542543

543544
(* Try to coerce to a funclass; returns [j] if no coercion is applicable *)
544-
let inh_app_fun ~program_mode ~resolve_tc env sigma body typ =
545-
try inh_app_fun_core ~program_mode env sigma body typ
545+
let inh_app_fun ~program_mode ~resolve_tc ?use_coercions env sigma body typ =
546+
try inh_app_fun_core ~program_mode ?use_coercions env sigma body typ
546547
with
547548
| NoCoercion when not resolve_tc
548549
|| not (get_use_typeclasses_for_conversion ()) -> (sigma, body, typ, IdCoe)
549550
| NoCoercion ->
550-
try inh_app_fun_core ~program_mode env (saturate_evd env sigma) body typ
551+
try inh_app_fun_core ~program_mode ?use_coercions env (saturate_evd env sigma) body typ
551552
with NoCoercion -> (sigma, body, typ, IdCoe)
552553

553554
let type_judgment env sigma j =
@@ -564,15 +565,16 @@ let inh_tosort_force ?loc env sigma ({ uj_val; uj_type } as j) =
564565
with Not_found | NoCoercion ->
565566
error_not_a_type ?loc env sigma j
566567

567-
let inh_coerce_to_sort ?loc env sigma j =
568+
let inh_coerce_to_sort ?loc ?(use_coercions=true) env sigma j =
568569
let typ = whd_all env sigma j.uj_type in
569570
match EConstr.kind sigma typ with
570571
| Sort s -> (sigma,{ utj_val = j.uj_val; utj_type = ESorts.kind sigma s })
571572
| Evar ev ->
572573
let (sigma,s) = Evardefine.define_evar_as_sort env sigma ev in
573574
(sigma,{ utj_val = j.uj_val; utj_type = s })
574575
| _ ->
575-
inh_tosort_force ?loc env sigma j
576+
if use_coercions then inh_tosort_force ?loc env sigma j
577+
else error_not_a_type ?loc env sigma j
576578

577579
let inh_coerce_to_base ?loc ~program_mode env sigma j =
578580
if program_mode then
@@ -582,8 +584,8 @@ let inh_coerce_to_base ?loc ~program_mode env sigma j =
582584
sigma, res
583585
else (sigma, j)
584586

585-
let inh_coerce_to_fail flags env sigma rigidonly v v_ty target_type =
586-
if rigidonly && not (Heads.is_rigid env (EConstr.Unsafe.to_constr target_type) && Heads.is_rigid env (EConstr.Unsafe.to_constr v_ty))
587+
let inh_coerce_to_fail ?(use_coercions=true) flags env sigma rigidonly v v_ty target_type =
588+
if not use_coercions || (rigidonly && not (Heads.is_rigid env (EConstr.Unsafe.to_constr target_type) && Heads.is_rigid env (EConstr.Unsafe.to_constr v_ty)))
587589
then
588590
raise NoCoercion
589591
else
@@ -601,10 +603,10 @@ let inh_coerce_to_fail flags env sigma rigidonly v v_ty target_type =
601603
let default_flags_of env =
602604
default_flags_of TransparentState.full
603605

604-
let rec inh_conv_coerce_to_fail ?loc env sigma ?(flags=default_flags_of env) rigidonly v t c1 =
606+
let rec inh_conv_coerce_to_fail ?loc ?use_coercions env sigma ?(flags=default_flags_of env) rigidonly v t c1 =
605607
try (unify_leq_delay ~flags env sigma t c1, v, IdCoe)
606608
with UnableToUnify (best_failed_sigma,e) ->
607-
try inh_coerce_to_fail flags env sigma rigidonly v t c1
609+
try inh_coerce_to_fail ?use_coercions flags env sigma rigidonly v t c1
608610
with NoCoercion as exn ->
609611
let _, info = Exninfo.capture exn in
610612
match
@@ -624,21 +626,21 @@ let rec inh_conv_coerce_to_fail ?loc env sigma ?(flags=default_flags_of env) rig
624626
let open Context.Rel.Declaration in
625627
let env1 = push_rel (LocalAssum (name,u1)) env in
626628
let (sigma, v1, trace1) =
627-
inh_conv_coerce_to_fail ?loc env1 sigma rigidonly
629+
inh_conv_coerce_to_fail ?loc ?use_coercions env1 sigma rigidonly
628630
(mkRel 1) (lift 1 u1) (lift 1 t1) in
629631
let v2 = beta_applist sigma (lift 1 v,[v1]) in
630632
let t2 = Retyping.get_type_of env1 sigma v2 in
631-
let (sigma,v2',trace2) = inh_conv_coerce_to_fail ?loc env1 sigma rigidonly v2 t2 u2 in
633+
let (sigma,v2',trace2) = inh_conv_coerce_to_fail ?loc ?use_coercions env1 sigma rigidonly v2 t2 u2 in
632634
let trace = ProdCoe { na=name; ty=u1; dom=trace1; body=trace2 } in
633635
(sigma, mkLambda (name, u1, v2'), trace)
634636
| _ ->
635637
Exninfo.iraise (NoCoercionNoUnifier (best_failed_sigma,e), info)
636638

637639
(* Look for cj' obtained from cj by inserting coercions, s.t. cj'.typ = t *)
638-
let inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc rigidonly flags env sigma cj t =
640+
let inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc ?use_coercions rigidonly flags env sigma cj t =
639641
let (sigma, val', otrace) =
640642
try
641-
let (sigma, val', trace) = inh_conv_coerce_to_fail ?loc env sigma ~flags rigidonly cj.uj_val cj.uj_type t in
643+
let (sigma, val', trace) = inh_conv_coerce_to_fail ?loc ?use_coercions env sigma ~flags rigidonly cj.uj_val cj.uj_type t in
642644
(sigma, val', Some trace)
643645
with NoCoercionNoUnifier (best_failed_sigma,e) as exn ->
644646
let _, info = Exninfo.capture exn in
@@ -659,15 +661,15 @@ let inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc rigidonly flags env si
659661
error_actual_type ?loc ~info env best_failed_sigma cj t e
660662
else
661663
let sigma = sigma' in
662-
let (sigma, val', trace) = inh_conv_coerce_to_fail ?loc env sigma rigidonly cj.uj_val cj.uj_type t in
664+
let (sigma, val', trace) = inh_conv_coerce_to_fail ?loc ?use_coercions env sigma rigidonly cj.uj_val cj.uj_type t in
663665
(sigma, val', Some trace)
664666
with NoCoercionNoUnifier (_sigma,_error) as exn ->
665667
let _, info = Exninfo.capture exn in
666668
error_actual_type ?loc ~info env best_failed_sigma cj t e
667669
in
668670
(sigma,{ uj_val = val'; uj_type = t },otrace)
669671

670-
let inh_conv_coerce_to ?loc ~program_mode ~resolve_tc env sigma ?(flags=default_flags_of env) =
671-
inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc false flags env sigma
672-
let inh_conv_coerce_rigid_to ?loc ~program_mode ~resolve_tc env sigma ?(flags=default_flags_of env) =
673-
inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc true flags env sigma
672+
let inh_conv_coerce_to ?loc ~program_mode ~resolve_tc ?use_coercions env sigma ?(flags=default_flags_of env) =
673+
inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc ?use_coercions false flags env sigma
674+
let inh_conv_coerce_rigid_to ?loc ~program_mode ~resolve_tc ?use_coercions env sigma ?(flags=default_flags_of env) =
675+
inh_conv_coerce_to_gen ?loc ~program_mode ~resolve_tc ?use_coercions true flags env sigma

pretyping/coercion.mli

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ val reapply_coercions : evar_map -> coercion_trace -> EConstr.t -> EConstr.t
2525
(** [inh_coerce_to_sort env isevars j] coerces [j] to a type; i.e. it
2626
inserts a coercion into [j], if needed, in such a way it gets as
2727
type a sort; it fails if no coercion is applicable *)
28-
val inh_coerce_to_sort : ?loc:Loc.t ->
28+
val inh_coerce_to_sort : ?loc:Loc.t -> ?use_coercions:bool ->
2929
env -> evar_map -> unsafe_judgment -> evar_map * unsafe_type_judgment
3030

3131
(** [inh_coerce_to_base env isevars j] coerces [j] to its base type; i.e. it
@@ -45,11 +45,11 @@ val remove_subset : env -> evar_map -> types -> types
4545
resort before failing) *)
4646

4747
val inh_conv_coerce_to : ?loc:Loc.t -> program_mode:bool -> resolve_tc:bool ->
48-
env -> evar_map -> ?flags:Evarconv.unify_flags ->
48+
?use_coercions:bool -> env -> evar_map -> ?flags:Evarconv.unify_flags ->
4949
unsafe_judgment -> types -> evar_map * unsafe_judgment * coercion_trace option
5050

5151
val inh_conv_coerce_rigid_to : ?loc:Loc.t -> program_mode:bool -> resolve_tc:bool ->
52-
env -> evar_map -> ?flags:Evarconv.unify_flags ->
52+
?use_coercions:bool -> env -> evar_map -> ?flags:Evarconv.unify_flags ->
5353
unsafe_judgment -> types -> evar_map * unsafe_judgment * coercion_trace option
5454

5555
(** [inh_pattern_coerce_to loc env isevars pat ind1 ind2] coerces the Cases
@@ -74,5 +74,5 @@ val reapply_coercions_body : evar_map -> coercion_trace -> delayed_app_body -> d
7474
type a product; it returns [j] if no coercion is applicable.
7575
resolve_tc=false disables resolving type classes (as the last
7676
resort before failing) *)
77-
val inh_app_fun : program_mode:bool -> resolve_tc:bool ->
77+
val inh_app_fun : program_mode:bool -> resolve_tc:bool -> ?use_coercions:bool ->
7878
env -> evar_map -> delayed_app_body -> types -> evar_map * delayed_app_body * types * coercion_trace

pretyping/pretyping.ml

+18-14
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ type pretype_flags = {
195195
poly : bool;
196196
resolve_tc : bool;
197197
program_mode : bool;
198+
use_coercions : bool;
198199
}
199200

200201
(* Compute the set of still-undefined initial evars up to restriction
@@ -355,10 +356,10 @@ let adjust_evar_source sigma na c =
355356
| _, _ -> sigma, c
356357

357358
(* coerce to tycon if any *)
358-
let inh_conv_coerce_to_tycon ?loc ~flags:{ program_mode; resolve_tc } env sigma j = function
359+
let inh_conv_coerce_to_tycon ?loc ~flags:{ program_mode; resolve_tc; use_coercions } env sigma j = function
359360
| None -> sigma, j, Some Coercion.empty_coercion_trace
360361
| Some t ->
361-
Coercion.inh_conv_coerce_to ?loc ~program_mode ~resolve_tc !!env sigma j t
362+
Coercion.inh_conv_coerce_to ?loc ~program_mode ~resolve_tc ~use_coercions !!env sigma j t
362363

363364
let check_instance subst = function
364365
| [] -> ()
@@ -840,7 +841,7 @@ struct
840841
sigma, body, na, c1, subs, c2, Coercion.empty_coercion_trace
841842
| _ ->
842843
let typ = Vars.esubst Vars.lift_substituend subs typ in
843-
let sigma, body, typ, trace = Coercion.inh_app_fun ~program_mode:flags.program_mode ~resolve_tc:flags.resolve_tc !!env sigma body typ in
844+
let sigma, body, typ, trace = Coercion.inh_app_fun ~program_mode:flags.program_mode ~resolve_tc:flags.resolve_tc ~use_coercions:flags.use_coercions !!env sigma body typ in
844845
let resty = whd_all !!env sigma typ in
845846
let na, c1, c2 = match EConstr.kind sigma resty with
846847
| Prod (na, c1, c2) -> (na, c1, c2)
@@ -923,7 +924,7 @@ struct
923924
let pretype_lambda self (name, bk, c1, c2) =
924925
fun ?loc ~flags tycon env sigma ->
925926
let open Context.Rel.Declaration in
926-
let tycon' = if flags.program_mode
927+
let tycon' = if flags.program_mode && flags.use_coercions
927928
then Option.map (Coercion.remove_subset !!env sigma) tycon
928929
else tycon
929930
in
@@ -1256,16 +1257,18 @@ let pretype_type self c ?loc ~flags valcon (env : GlobEnv.t) sigma = match DAst.
12561257
| _ ->
12571258
let sigma, j = eval_pretyper self ~flags empty_tycon env sigma c in
12581259
let loc = loc_of_glob_constr c in
1259-
let sigma, tj = Coercion.inh_coerce_to_sort ?loc !!env sigma j in
1260-
match valcon with
1261-
| None -> sigma, tj
1262-
| Some v ->
1263-
begin match Evarconv.unify_leq_delay !!env sigma v tj.utj_val with
1264-
| sigma -> sigma, tj
1265-
| exception Evarconv.UnableToUnify (sigma,e) ->
1266-
error_unexpected_type
1267-
?loc:(loc_of_glob_constr c) !!env sigma tj.utj_val v e
1268-
end
1260+
let sigma, tj =
1261+
let use_coercions = flags.use_coercions in
1262+
Coercion.inh_coerce_to_sort ?loc ~use_coercions !!env sigma j in
1263+
match valcon with
1264+
| None -> sigma, tj
1265+
| Some v ->
1266+
begin match Evarconv.unify_leq_delay !!env sigma v tj.utj_val with
1267+
| sigma -> sigma, tj
1268+
| exception Evarconv.UnableToUnify (sigma,e) ->
1269+
error_unexpected_type
1270+
?loc:(loc_of_glob_constr c) !!env sigma tj.utj_val v e
1271+
end
12691272

12701273
let pretype_int self i =
12711274
fun ?loc ~flags tycon env sigma ->
@@ -1358,6 +1361,7 @@ let ise_pretype_gen (flags : inference_flags) env sigma lvar kind c =
13581361
let pretype_flags = {
13591362
program_mode = flags.program_mode;
13601363
poly = flags.polymorphic;
1364+
use_coercions = true;
13611365
resolve_tc = match flags.use_typeclasses with
13621366
| NoUseTC -> false
13631367
| UseTC | UseTCForConv -> true

pretyping/pretyping.mli

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ type pretype_flags = {
144144
poly : bool;
145145
resolve_tc : bool;
146146
program_mode : bool;
147+
use_coercions : bool;
147148
}
148149

149150
type 'a pretype_fun = ?loc:Loc.t -> flags:pretype_flags -> Evardefine.type_constraint -> GlobEnv.t -> evar_map -> evar_map * 'a

0 commit comments

Comments
 (0)