Skip to content

Commit 2abd8ed

Browse files
committed
Allow subtyping for rewrite rules
1 parent 8079fe5 commit 2abd8ed

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

kernel/modops.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type signature_mismatch_error =
5252
| IncompatiblePolymorphism of env * types * types
5353
| IncompatibleUnivConstraints of { env : env; got : UVars.AbstractContext.t; expect : UVars.AbstractContext.t }
5454
| IncompatibleVariance
55-
| NoRewriteRulesSubtyping
55+
| NotConvertibleRewriteRule of env * rewrite_rule
5656

5757
type with_constraint_error =
5858
| WithSignatureMismatch of signature_mismatch_error

kernel/modops.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type signature_mismatch_error =
106106
| IncompatiblePolymorphism of env * types * types
107107
| IncompatibleUnivConstraints of { env : env; got : UVars.AbstractContext.t; expect : UVars.AbstractContext.t }
108108
| IncompatibleVariance
109-
| NoRewriteRulesSubtyping
109+
| NotConvertibleRewriteRule of env * rewrite_rule
110110

111111
type with_constraint_error =
112112
| WithSignatureMismatch of signature_mismatch_error

kernel/subtyping.ml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ let check_constant (cst, ustate) trace env l info1 cb2 subst1 subst2 =
278278
anything of the right type can implement it, even if bodies differ.
279279
*)
280280
(match cb2.const_body with
281-
| Undef _ | OpaqueDef _ -> cst
282-
| Primitive _ | Symbol _ -> error (NotConvertibleBodyField None)
281+
| Undef _ | OpaqueDef _ | Symbol _ -> cst
282+
| Primitive _ -> error (NotConvertibleBodyField None)
283283
| Def c2 ->
284284
(match cb1.const_body with
285285
| Primitive _ | Undef _ | OpaqueDef _ | Symbol _ -> error (NotConvertibleBodyField None)
@@ -288,6 +288,28 @@ let check_constant (cst, ustate) trace env l info1 cb2 subst1 subst2 =
288288
Anyway [check_conv] will handle that afterwards. *)
289289
check_conv (NotConvertibleBodyField (Some (env, c1, c2))) cst poly CONV env c1 c2))
290290

291+
let check_rewrite_rules (cst, ustate) trace env l rrb subst2 =
292+
let rrb = Declareops.subst_rewrite_rules subst2 rrb in
293+
let evar_handler =
294+
let evar_expand (ev, inst) =
295+
CClosure.EvarUndefined (ev, inst |> SList.to_list |> List.map Option.get)
296+
in
297+
let evar_repack (ev, args) = mkEvar (ev, SList.of_full_list args) in
298+
let evar_irrelevant _ = false in
299+
{ (CClosure.default_evar_handler env) with evar_expand; evar_repack; evar_irrelevant }
300+
in
301+
let error why = error_signature_mismatch trace l why in
302+
let check_conv_rr cst rrb =
303+
let t1 = rrb.pat_term and t2 = rrb.replacement in
304+
match Conversion.generic_conv CONV ~l2r:false ~evars:evar_handler TransparentState.full env (cst, ustate) t1 t2 with
305+
| Ok cst -> cst
306+
| Error None -> error (NotConvertibleRewriteRule (env, rrb))
307+
| Error (Some (Univ err)) -> error (IncompatibleUniverses { env; err; t1; t2 })
308+
| Error (Some (Qual err)) -> error (IncompatibleQualities { env; err; t1; t2 })
309+
in
310+
List.fold_left check_conv_rr cst rrb.rewrules_rules
311+
312+
291313
let rec check_modules state trace env mp1 msb1 mp2 msb2 subst1 subst2 =
292314
let mty1 = module_type_of_module msb1 in
293315
let mty2 = module_type_of_module msb2 in
@@ -303,8 +325,8 @@ and check_signatures (cst, ustate) trace env mp1 sig1 mp2 sig2 subst1 subst2 res
303325
| SFBmind mib2 ->
304326
check_inductive (cst, ustate) trace env mp1 l (get_obj mp1 map1 l)
305327
mp2 mib2 subst1 subst2 reso1 reso2
306-
| SFBrules _ ->
307-
error_signature_mismatch trace l NoRewriteRulesSubtyping
328+
| SFBrules rrb ->
329+
check_rewrite_rules (cst, ustate) trace env l rrb subst2
308330
| SFBmodule msb2 ->
309331
let mp1' = MPdot (mp1, l) in
310332
let mp2' = MPdot (mp2, l) in

vernac/himsg.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,14 @@ let explain_not_match_error = function
13191319
fnl() ++ str "(incompatible constraints)")
13201320
| IncompatibleVariance ->
13211321
str "incompatible variance information"
1322-
| NoRewriteRulesSubtyping ->
1323-
strbrk "subtyping for rewrite rule blocks is not supported"
1322+
| NotConvertibleRewriteRule (env, rrb) ->
1323+
let evd = Evd.from_auctx env (UVars.AbstractContext.make rrb.rr_uctx PConstraints.empty) in
1324+
(* XXX: Use evar names *)
1325+
let t1, t2 = pr_explicit env evd (EConstr.of_constr rrb.pat_term) (EConstr.of_constr rrb.replacement) in
1326+
str "rewrite rule not satisfied: " ++
1327+
t1 ++ spc () ++
1328+
str "is not convertible to " ++ spc () ++
1329+
t2
13241330

13251331
let rec get_submodules acc = function
13261332
| [] -> acc, []

0 commit comments

Comments
 (0)