| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
|
| 14 |
let maybe_error_many_udecls = function |
| 15 |
| ({CAst.loc;v=id}, Some _) -> |
| 16 |
CErrors.user_err ?loc |
| 17 |
Pp.(strbrk "When declaring multiple symbols in one command, " ++ |
| 18 |
strbrk "only the first is allowed a universe binder " ++ |
| 19 |
strbrk "(which will be shared by the whole block).") |
| 20 |
| (_, None) -> () |
| 21 |
|
| 22 |
let preprocess_symbols l = |
| 23 |
let open Vernacexpr in |
| 24 |
if Global.sections_are_opened () then |
| 25 |
CErrors.user_err Pp.(str "Declaring a symbol is not allowed in sections."); |
| 26 |
let udecl = match l with |
| 27 |
| (coe, ((id, udecl)::rest, c))::rest' -> |
| 28 |
List.iter maybe_error_many_udecls rest; |
| 29 |
List.iter (fun (coe, (idl, c)) -> List.iter maybe_error_many_udecls idl) rest'; |
| 30 |
udecl |
| 31 |
| (_, ([], _))::_ | [] -> assert false |
| 32 |
in |
| 33 |
let no_coercion_msg = Pp.(str "Cannot deal with coercions in symbols") in |
| 34 |
List.iter (function AddCoercion, (({CAst.loc; _}, _) :: _, _) -> CErrors.user_err ?loc no_coercion_msg | AddCoercion, _ -> assert false | _ -> ()) l; |
| 35 |
udecl, List.concat_map (fun (coe, (idl, c)) -> List.map (fun (id, _) -> id, c) idl) l |
| 36 |
|
| 37 |
let do_symbol ~poly ~unfold_fix udecl (id, typ) = |
| 38 |
if Dumpglob.dump () then Dumpglob.dump_definition id false "symb"; |
| 39 |
let loc = id.CAst.loc in |
| 40 |
let id = id.CAst.v in |
| 41 |
let env = Global.env () in |
| 42 |
let evd, udecl = Constrintern.interp_univ_decl_opt env udecl in |
| 43 |
let evd, (typ, impls) = |
| 44 |
Constrintern.(interp_type_evars_impls ~impls:empty_internalization_env) |
| 45 |
env evd typ |
| 46 |
in |
| 47 |
Pretyping.check_evars_are_solved ~program_mode:false env evd; |
| 48 |
let evd = Evd.minimize_universes evd in |
| 49 |
let _qvars, uvars = EConstr.universes_of_constr evd typ in |
| 50 |
let evd = Evd.restrict_universe_context evd uvars in |
| 51 |
let typ = EConstr.to_constr evd typ in |
| 52 |
let univs = Evd.check_univ_decl ~poly evd udecl in |
| 53 |
let entry = Declare.symbol_entry ~univs ~unfold_fix typ in |
| 54 |
let kn = Declare.declare_constant ?loc ~name:id ~kind:Decls.IsSymbol (Declare.SymbolEntry entry) in |
| 55 |
let () = Impargs.maybe_declare_manual_implicits false (GlobRef.ConstRef kn) impls in |
| 56 |
let () = Declare.assumption_message id in |
| 57 |
() |
| 58 |
|
| 59 |
let do_symbols ~poly ~unfold_fix l = |
| 60 |
let env = Global.env () in |
| 61 |
if not @@ Environ.rewrite_rules_allowed env then raise Environ.(RewriteRulesNotAllowed Symb); |
| 62 |
let udecl, l = preprocess_symbols l in |
| 63 |
List.iter (do_symbol ~poly ~unfold_fix udecl) l |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
open Util |
| 68 |
open Constr |
| 69 |
open Declarations |
| 70 |
|
| 71 |
type state = (int * int Evar.Map.t) * (int * int Int.Map.t) * (int * int Int.Map.t) |
| 72 |
|
| 73 |
let rec is_rel_inst k = function |
| 74 |
| SList.Nil -> true |
| 75 |
| SList.Default _ -> false |
| 76 |
| SList.Cons (t, l) -> kind t = Rel k && is_rel_inst (succ k) l |
| 77 |
|
| 78 |
let update_invtbl ~loc env evd evk (curvar, tbl) = |
| 79 |
curvar, (succ curvar, tbl |> Evar.Map.update evk @@ function |
| 80 |
| None -> Some curvar |
| 81 |
| Some k as c when k = curvar -> c |
| 82 |
| Some k -> |
| 83 |
CErrors.user_err ?loc |
| 84 |
Pp.(str "Variable " |
| 85 |
++ Termops.pr_existential_key env evd evk |
| 86 |
++ str" is bound multiple times in the pattern (holes number " |
| 87 |
++ int k ++ str" and " ++ int curvar ++ str").")) |
| 88 |
|
| 89 |
let update_invtblu1 ~loc evd lvlold lvl (curvaru, tbl) = |
| 90 |
succ curvaru, tbl |> Int.Map.update lvl @@ function |
| 91 |
| None -> Some curvaru |
| 92 |
| Some k as c when k = curvaru -> c |
| 93 |
| Some k -> |
| 94 |
CErrors.user_err ?loc |
| 95 |
Pp.(str "Universe variable " |
| 96 |
++ Termops.pr_evd_level evd lvlold |
| 97 |
++ str" is bound multiple times in the pattern (holes number " |
| 98 |
++ int k ++ str" and " ++ int curvaru ++ str").") |
| 99 |
|
| 100 |
let update_invtblq1 ~loc evd qold qvar (curvarq, tbl) = |
| 101 |
succ curvarq, tbl |> Int.Map.update qvar @@ function |
| 102 |
| None -> Some curvarq |
| 103 |
| Some k as c when k = curvarq -> c |
| 104 |
| Some k -> |
| 105 |
CErrors.user_err ?loc |
| 106 |
Pp.(str "Sort variable " |
| 107 |
++ Sorts.Quality.pr (Termops.pr_evd_qvar evd) qold |
| 108 |
++ str" is bound multiple times in the pattern (holes number " |
| 109 |
++ int k ++ str" and " ++ int curvarq ++ str").") |
| 110 |
|
| 111 |
let safe_quality_pattern_of_quality ~loc evd qsubst stateq q = |
| 112 |
match Sorts.Quality.(subst (subst_fn qsubst) q) with |
| 113 |
| QConstant qc -> stateq, PQConstant qc |
| 114 |
| QVar qv -> |
| 115 |
let qio = Sorts.QVar.var_index qv in |
| 116 |
let stateq = Option.fold_right (update_invtblq1 ~loc evd q) qio stateq in |
| 117 |
stateq, PQVar qio |
| 118 |
|
| 119 |
let update_invtblu ~loc evd (qsubst, usubst) (state, stateq, stateu : state) u : state * _ = |
| 120 |
let (q, u) = u |> UVars.Instance.to_array in |
| 121 |
let stateq, maskq = Array.fold_left_map (safe_quality_pattern_of_quality ~loc evd qsubst) stateq q |
| 122 |
in |
| 123 |
let stateu, masku = Array.fold_left_map (fun stateu lvlold -> |
| 124 |
let lvlnew = Univ.Level.var_index @@ UVars.subst_univs_level_level usubst lvlold in |
| 125 |
Option.fold_right (update_invtblu1 ~loc evd lvlold) lvlnew stateu, lvlnew |
| 126 |
) stateu u |
| 127 |
in |
| 128 |
(state, stateq, stateu), (maskq, masku) |
| 129 |
|
| 130 |
let universe_level_subst_var_index usubst u = |
| 131 |
match Univ.Universe.level u with |
| 132 |
| None -> None |
| 133 |
| Some lvlold -> |
| 134 |
let lvl = UVars.subst_univs_level_level usubst lvlold in |
| 135 |
Option.map (fun lvl -> lvlold, lvl) @@ Univ.Level.var_index lvl |
| 136 |
|
| 137 |
let safe_sort_pattern_of_sort ~loc evd (qsubst, usubst) (st, sq, su as state) s = |
| 138 |
let open Sorts in |
| 139 |
match s with |
| 140 |
| Type u -> |
| 141 |
begin match universe_level_subst_var_index usubst u with |
| 142 |
| None -> state, PSType None |
| 143 |
| Some (lvlold, lvl) -> |
| 144 |
(st, sq, update_invtblu1 ~loc evd lvlold lvl su), PSType (Some lvl) |
| 145 |
end |
| 146 |
| SProp -> state, PSSProp |
| 147 |
| Prop -> state, PSProp |
| 148 |
| Set -> state, PSSet |
| 149 |
| QSort (qold, u) -> |
| 150 |
let sq, bq = |
| 151 |
match Sorts.Quality.(var_index @@ subst_fn qsubst qold) with |
| 152 |
| Some q -> update_invtblq1 ~loc evd (QVar qold) q sq, Some q |
| 153 |
| None -> sq, None |
| 154 |
in |
| 155 |
let su, ba = |
| 156 |
match universe_level_subst_var_index usubst u with |
| 157 |
| Some (lvlold, lvl) -> update_invtblu1 ~loc evd lvlold lvl su, Some lvl |
| 158 |
| None -> su, None |
| 159 |
in |
| 160 |
(st, sq, su), PSQSort (bq, ba) |
| 161 |
|
| 162 |
|
| 163 |
let warn_irrelevant_pattern = |
| 164 |
CWarnings.create ~name:"irrelevant-pattern" ~category:CWarnings.CoreCategories.rewrite_rules |
| 165 |
Pp.(fun () -> str "This subpattern is irrelevant and can never be matched against.") |
| 166 |
|
| 167 |
let warn_eta_in_pattern = |
| 168 |
CWarnings.create ~name:"eta-in-pattern" ~category:CWarnings.CoreCategories.rewrite_rules Fun.id |
| 169 |
|
| 170 |
let warn_redex_in_rewrite_rules = |
| 171 |
CWarnings.create ~name:"redex-in-rewrite-rules" ~category:CWarnings.CoreCategories.rewrite_rules |
| 172 |
Pp.(fun redex -> str "This pattern contains a" ++ redex ++ str " which may prevent this rule from being triggered.") |
| 173 |
|
| 174 |
let rec check_may_eta ~loc env evd t = |
| 175 |
match EConstr.kind evd (Reductionops.whd_all env evd t) with |
| 176 |
| Prod _ -> |
| 177 |
warn_eta_in_pattern ?loc |
| 178 |
Pp.(str "This subpattern has a product type, but pattern-matching is not done modulo eta, so this rule may not trigger at required times.") |
| 179 |
| Sort _ -> () |
| 180 |
| Ind (ind, u) -> |
| 181 |
let specif = Inductive.lookup_mind_specif env ind in |
| 182 |
if not @@ Inductive.is_primitive_record specif then () else |
| 183 |
warn_eta_in_pattern ?loc |
| 184 |
Pp.(str "This subpattern has a primitive record type, but pattern-matching is not done modulo eta, so this rule may not trigger at required times.") |
| 185 |
| App (i, _) -> check_may_eta ~loc env evd i |
| 186 |
| _ -> |
| 187 |
warn_eta_in_pattern ?loc |
| 188 |
Pp.(str "This subpattern has a yet unknown type, which may be a product type, but pattern-matching is not done modulo eta, so this rule may not trigger at required times.") |
| 189 |
|
| 190 |
let test_may_eta ~loc env evd constr = |
| 191 |
let t = Retyping.get_type_of env evd constr in |
| 192 |
let () = check_may_eta ~loc env evd t in |
| 193 |
() |
| 194 |
|
| 195 |
|
| 196 |
let rec safe_pattern_of_constr_aux ~loc env evd usubst depth state t = Constr.kind t |> function |
| 197 |
| App (f, args) -> |
| 198 |
let state, (head, elims) = safe_pattern_of_constr_aux ~loc env evd usubst depth state f in |
| 199 |
let state, pargs = Array.fold_left_map (safe_arg_pattern_of_constr ~loc env evd usubst depth) state args in |
| 200 |
state, (head, elims @ [PEApp pargs]) |
| 201 |
| Case (ci, u, params, (ret, _), _, c, brs) -> |
| 202 |
let mib, mip = Inductive.lookup_mind_specif env ci.ci_ind in |
| 203 |
|
| 204 |
let state, (head, elims) = safe_pattern_of_constr_aux ~loc env evd usubst depth state c in |
| 205 |
|
| 206 |
let paramdecl = Vars.subst_instance_context u mib.mind_params_ctxt in |
| 207 |
let paramsubst = Vars.subst_of_rel_context_instance paramdecl params in |
| 208 |
|
| 209 |
let state, pret = |
| 210 |
let (nas, p) = ret in |
| 211 |
let realdecls, _ = List.chop mip.mind_nrealdecls mip.mind_arity_ctxt in |
| 212 |
let self = |
| 213 |
let args = Context.Rel.instance mkRel 0 mip.mind_arity_ctxt in |
| 214 |
let inst = UVars.Instance.(abstract_instance (length u)) in |
| 215 |
mkApp (mkIndU (ci.ci_ind, inst), args) |
| 216 |
in |
| 217 |
let na = Context.make_annot Anonymous mip.mind_relevance in |
| 218 |
let realdecls = Context.Rel.Declaration.LocalAssum (na, self) :: realdecls in |
| 219 |
let realdecls = |
| 220 |
Inductive.instantiate_context u paramsubst nas realdecls |
| 221 |
in |
| 222 |
let p_env = Environ.push_rel_context realdecls env in |
| 223 |
safe_arg_pattern_of_constr ~loc p_env evd usubst (depth + Array.length nas) state p |
| 224 |
in |
| 225 |
let do_one_branch i state (nas, br) = |
| 226 |
let (ctx, cty) = mip.mind_nf_lc.(i) in |
| 227 |
let bctx, _ = List.chop mip.mind_consnrealdecls.(i) ctx in |
| 228 |
let bctx = Inductive.instantiate_context u paramsubst nas bctx in |
| 229 |
let br_env = Environ.push_rel_context bctx env in |
| 230 |
safe_arg_pattern_of_constr ~loc br_env evd usubst (depth + Array.length nas) state br |
| 231 |
in |
| 232 |
let state, pbrs = Array.fold_left_map_i do_one_branch state brs in |
| 233 |
state, (head, elims @ [PECase (ci.ci_ind, pret, pbrs)]) |
| 234 |
| Proj (p, _, c) -> |
| 235 |
let state, (head, elims) = safe_pattern_of_constr_aux ~loc env evd usubst depth state c in |
| 236 |
state, (head, elims @ [PEProj (Projection.repr p)]) |
| 237 |
| _ -> |
| 238 |
let state, head = safe_head_pattern_of_constr ~loc env evd usubst depth state t in |
| 239 |
state, (head, []) |
| 240 |
|
| 241 |
and safe_pattern_of_constr ~loc env evd usubst depth state t = |
| 242 |
begin match EConstr.ERelevance.kind evd @@ Retyping.relevance_of_term env evd (EConstr.of_constr t) with |
| 243 |
| Sorts.Irrelevant -> warn_irrelevant_pattern ?loc () |
| 244 |
| Sorts.RelevanceVar _ -> () |
| 245 |
| Sorts.Relevant -> () |
| 246 |
end; |
| 247 |
safe_pattern_of_constr_aux ~loc env evd usubst depth state t |
| 248 |
|
| 249 |
and safe_head_pattern_of_constr ~loc env evd usubst depth state t = Constr.kind t |> function |
| 250 |
| Const (c, u) when Environ.is_symbol env c -> |
| 251 |
let state, mask = update_invtblu ~loc evd usubst state u in |
| 252 |
state, PHSymbol (c, mask) |
| 253 |
| Rel i -> |
| 254 |
assert (i <= depth); |
| 255 |
state, PHRel i |
| 256 |
| Sort s -> |
| 257 |
let state, ps = safe_sort_pattern_of_sort ~loc evd usubst state s in |
| 258 |
state, PHSort ps |
| 259 |
| Ind (ind, u) -> |
| 260 |
let state, mask = update_invtblu ~loc evd usubst state u in |
| 261 |
state, PHInd (ind, mask) |
| 262 |
| Construct (c, u) -> |
| 263 |
let state, mask = update_invtblu ~loc evd usubst state u in |
| 264 |
state, PHConstr (c, mask) |
| 265 |
| Int i -> state, PHInt i |
| 266 |
| Float f -> state, PHFloat f |
| 267 |
| String s -> state, PHString s |
| 268 |
| Lambda _ -> |
| 269 |
let (ntys, b) = Term.decompose_lambda t in |
| 270 |
let tys = Array.rev_of_list ntys in |
| 271 |
let (state, env), ptys = Array.fold_left_map_i |
| 272 |
(fun i (state, env) (na, ty) -> |
| 273 |
let state, p = safe_arg_pattern_of_constr ~loc env evd usubst (depth+i) state ty in |
| 274 |
(state, Environ.push_rel (LocalAssum (na, ty)) env), p) |
| 275 |
(state, env) tys |
| 276 |
in |
| 277 |
let state, pbod = safe_pattern_of_constr ~loc env evd usubst (depth + Array.length tys) state b in |
| 278 |
state, PHLambda (ptys, pbod) |
| 279 |
| Prod _ -> |
| 280 |
let (ntys, b) = Term.decompose_prod t in |
| 281 |
let tys = Array.rev_of_list ntys in |
| 282 |
let (state, env), ptys = Array.fold_left_map_i |
| 283 |
(fun i (state, env) (na, ty) -> |
| 284 |
let state, p = safe_arg_pattern_of_constr ~loc env evd usubst (depth+i) state ty in |
| 285 |
(state, Environ.push_rel (LocalAssum (na, ty)) env), p) |
| 286 |
(state, env) tys |
| 287 |
in |
| 288 |
let state, pbod = safe_arg_pattern_of_constr ~loc env evd usubst (depth + Array.length tys) state b in |
| 289 |
state, PHProd (ptys, pbod) |
| 290 |
| _ -> |
| 291 |
CErrors.user_err ?loc Pp.(str "Subterm not recognised as pattern: " ++ Printer.safe_pr_lconstr_env env evd t) |
| 292 |
|
| 293 |
and safe_arg_pattern_of_constr ~loc env evd usubst depth (st, stateq, stateu as state) t = Constr.kind t |> function |
| 294 |
| Evar (evk, inst) -> |
| 295 |
let EvarInfo evi = Evd.find evd evk in |
| 296 |
if Evd.is_rewrite_rule_evar evd evk then |
| 297 |
let holei, st = update_invtbl ~loc env evd evk st in |
| 298 |
if not @@ is_rel_inst 1 inst then |
| 299 |
CErrors.user_err ?loc |
| 300 |
Pp.(str "In " ++ Printer.safe_pr_lconstr_env env evd (of_kind (Evar (evk, inst))) |
| 301 |
++ str ", variable " |
| 302 |
++ Termops.pr_existential_key env evd evk |
| 303 |
++ str" appears with a non-trivial instantiation."); |
| 304 |
if Evd.evar_hyps evi |> Environ.named_context_of_val |> Context.Named.length <> SList.length inst then |
| 305 |
CErrors.user_err ?loc Pp.(str "Pattern variable cannot access the whole context: " ++ Printer.safe_pr_lconstr_env env evd t); |
| 306 |
(st, stateq, stateu), EHole holei |
| 307 |
else |
| 308 |
(match snd (Evd.evar_source evi) with |
| 309 |
| Evar_kinds.RewriteRulePattern _ -> assert false |
| 310 |
| Evar_kinds.NamedHole _ -> CErrors.user_err ?loc Pp.(str "Named holes are not supported, you must use regular evars: " ++ Printer.safe_pr_lconstr_env env evd t) |
| 311 |
| _ -> |
| 312 |
if Option.is_empty @@ Evd.evar_ident evk evd then state, EHoleIgnored else |
| 313 |
CErrors.user_err ?loc Pp.(str "Named evar in unsupported context: " ++ Printer.safe_pr_lconstr_env env evd t) |
| 314 |
) |
| 315 |
| _ -> |
| 316 |
test_may_eta ~loc env evd (EConstr.of_constr t); |
| 317 |
let state, p = safe_pattern_of_constr ~loc env evd usubst depth state t in |
| 318 |
state, ERigid p |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
let rec evar_subst evmap evd k t = |
| 323 |
match EConstr.kind evd t with |
| 324 |
| Evar (evk, inst) -> begin |
| 325 |
match Evar.Map.find_opt evk evmap with |
| 326 |
| None -> t |
| 327 |
| Some (n, vars) -> |
| 328 |
let head = EConstr.mkRel (n + k) in |
| 329 |
let Evd.EvarInfo evi = Evd.find evd evk in |
| 330 |
let body = EConstr.mkApp (head, vars) in |
| 331 |
let inst = inst |> SList.Smart.map (evar_subst evmap evd k) in |
| 332 |
Evd.instantiate_evar_array evd evi body inst |
| 333 |
end |
| 334 |
| _ -> EConstr.map_with_binders evd succ (evar_subst evmap evd) k t |
| 335 |
|
| 336 |
let test_projection_apps env evd ~loc ind args = |
| 337 |
let specif = Inductive.lookup_mind_specif env ind in |
| 338 |
if not @@ Inductive.is_primitive_record specif then () else |
| 339 |
if Array.for_all_i (fun i arg -> |
| 340 |
match arg with |
| 341 |
| EHole _ | EHoleIgnored -> true |
| 342 |
| ERigid (_, []) -> false |
| 343 |
| ERigid (_, elims) -> |
| 344 |
match List.last elims with |
| 345 |
| PEProj p -> Environ.QInd.equal env (Projection.Repr.inductive p) ind && Projection.Repr.arg p = i |
| 346 |
| _ -> false |
| 347 |
) 0 args then |
| 348 |
warn_redex_in_rewrite_rules ?loc Pp.(str " subpattern compatible with an eta-long form for " ++ Id.print (snd specif).mind_typename ++ str"," ) |
| 349 |
|
| 350 |
let rec test_pattern_redex env evd ~loc = function |
| 351 |
| PHLambda _, PEApp _ :: _ -> warn_redex_in_rewrite_rules ?loc (Pp.str " beta redex") |
| 352 |
| PHConstr _, (PECase _ | PEProj _) :: _ -> warn_redex_in_rewrite_rules ?loc (Pp.str " iota redex") |
| 353 |
| PHConstr _, PEApp _ :: (PECase _ | PEProj _) :: _ -> warn_redex_in_rewrite_rules ?loc (Pp.str " iota redex") |
| 354 |
| PHLambda _, _ -> warn_redex_in_rewrite_rules ?loc (Pp.str " lambda pattern") |
| 355 |
| PHConstr (c, _) as head, PEApp args :: elims -> test_projection_apps env evd ~loc (fst c) args; Array.iter (test_pattern_redex_aux env evd ~loc) args; test_pattern_redex env evd ~loc (head, elims) |
| 356 |
| head, PEApp args :: elims -> Array.iter (test_pattern_redex_aux env evd ~loc) args; test_pattern_redex env evd ~loc (head, elims) |
| 357 |
| head, PECase (_, ret, brs) :: elims -> test_pattern_redex_aux env evd ~loc ret; Array.iter (test_pattern_redex_aux env evd ~loc) brs; test_pattern_redex env evd ~loc (head, elims) |
| 358 |
| head, PEProj _ :: elims -> test_pattern_redex env evd ~loc (head, elims) |
| 359 |
| PHProd (tys, bod), [] -> Array.iter (test_pattern_redex_aux env evd ~loc) tys; test_pattern_redex_aux env evd ~loc bod |
| 360 |
| (PHRel _ | PHInt _ | PHFloat _ | PHString _ | PHSort _ | PHInd _ | PHConstr _ | PHSymbol _), [] -> () |
| 361 |
and test_pattern_redex_aux env evd ~loc = function |
| 362 |
| EHole _ | EHoleIgnored -> () |
| 363 |
| ERigid p -> test_pattern_redex env evd ~loc p |
| 364 |
|
| 365 |
|
| 366 |
let warn_rewrite_rules_break_SR = |
| 367 |
CWarnings.create ~name:"rewrite-rules-break-SR" ~category:CWarnings.CoreCategories.rewrite_rules |
| 368 |
Pp.(fun reason -> |
| 369 |
str "This rewrite rule breaks subject reduction" ++ spc() ++ reason) |
| 370 |
|
| 371 |
let interp_rule (udecl, lhs, rhs: Constrexpr.universe_decl_expr option * _ * _) = |
| 372 |
let env = Global.env () in |
| 373 |
let evd = Evd.from_env env in |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
let evd, udecl = |
| 378 |
let open CAst in let open UState in |
| 379 |
match udecl with |
| 380 |
| None -> evd, default_univ_decl |
| 381 |
| Some udecl -> |
| 382 |
let evd, qualities = List.fold_left_map (fun evd lid -> |
| 383 |
Evd.new_quality_variable ?loc:lid.loc ~name:lid.v evd) |
| 384 |
evd |
| 385 |
udecl.univdecl_qualities |
| 386 |
in |
| 387 |
let evd, instance = List.fold_left_map (fun evd lid -> |
| 388 |
Evd.new_univ_level_variable ?loc:lid.loc univ_rigid ~name:lid.v evd) |
| 389 |
evd |
| 390 |
udecl.univdecl_instance |
| 391 |
in |
| 392 |
let elim_cstrs = |
| 393 |
udecl.univdecl_elim_constraints |> List.to_seq |
| 394 |
|> Seq.map (Constrintern.interp_elim_constraint evd) |
| 395 |
|> Sorts.ElimConstraints.of_seq |
| 396 |
in |
| 397 |
let univ_cstrs = |
| 398 |
udecl.univdecl_univ_constraints |> List.to_seq |
| 399 |
|> Seq.map (Constrintern.interp_univ_constraint evd) |
| 400 |
|> Univ.UnivConstraints.of_seq |
| 401 |
in |
| 402 |
let decl = { |
| 403 |
univdecl_qualities = qualities; |
| 404 |
univdecl_extensible_qualities = udecl.univdecl_extensible_qualities; |
| 405 |
univdecl_elim_constraints = elim_cstrs; |
| 406 |
univdecl_instance = instance; |
| 407 |
univdecl_extensible_instance = udecl.univdecl_extensible_instance; |
| 408 |
univdecl_univ_constraints = univ_cstrs; |
| 409 |
univdecl_extensible_constraints = udecl.univdecl_extensible_constraints; |
| 410 |
} in |
| 411 |
evd, decl |
| 412 |
in |
| 413 |
let nvarqs = List.length udecl.univdecl_qualities in |
| 414 |
let nvarus = List.length udecl.univdecl_instance in |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
let lhs_loc = lhs.CAst.loc in |
| 421 |
let rhs_loc = rhs.CAst.loc in |
| 422 |
|
| 423 |
let lhs = Constrintern.(intern_gen WithoutTypeConstraint env evd lhs) in |
| 424 |
let poly = PolyFlags.make ~univ_poly:true ~cumulative:false ~collapse_sort_variables:false in |
| 425 |
let flags = { Pretyping.no_classes_no_fail_inference_flags with |
| 426 |
undeclared_evars_rr = true; expand_evars = false; |
| 427 |
solve_unification_constraints = false; poly } in |
| 428 |
let evd, lhs, typ = Pretyping.understand_tcc_ty ~flags env evd lhs in |
| 429 |
let evd = Evd.minimize_universes evd in |
| 430 |
let _qvars, uvars = EConstr.universes_of_constr evd lhs in |
| 431 |
let evd = Evd.restrict_universe_context evd uvars in |
| 432 |
let uctx, uctx' = UState.check_univ_decl_rev (Evd.ustate evd) udecl in |
| 433 |
|
| 434 |
let usubst = |
| 435 |
let inst, auctx = UVars.abstract_universes uctx' in |
| 436 |
UVars.make_instance_subst inst |
| 437 |
in |
| 438 |
|
| 439 |
let ((nvars', invtbl), (nvarqs', invtblq), (nvarus', invtblu)), (head_pat, elims) = |
| 440 |
safe_pattern_of_constr ~loc:lhs_loc env evd usubst 0 ((1, Evar.Map.empty), (0, Int.Map.empty), (0, Int.Map.empty)) (EConstr.Unsafe.to_constr lhs) |
| 441 |
in |
| 442 |
let () = test_pattern_redex env evd ~loc:lhs_loc (head_pat, elims) in |
| 443 |
|
| 444 |
let head_symbol, head_umask = match head_pat with PHSymbol (symb, mask) -> symb, mask | _ -> |
| 445 |
CErrors.user_err ?loc:lhs_loc |
| 446 |
Pp.(str "Head head-pattern is not a symbol.") |
| 447 |
in |
| 448 |
if nvarus <> nvarus' then begin |
| 449 |
assert (nvarus' < nvarus); |
| 450 |
CErrors.user_err ?loc:lhs_loc |
| 451 |
Pp.(str "Not all universe level variables appear in the pattern.") |
| 452 |
end; |
| 453 |
if nvarqs <> nvarqs' then begin |
| 454 |
assert (nvarqs' < nvarqs); |
| 455 |
CErrors.user_err ?loc:lhs_loc |
| 456 |
Pp.(str "Not all sort variables appear in the pattern.") |
| 457 |
end; |
| 458 |
|
| 459 |
let update_invtbl evd evk n = |
| 460 |
let Evd.EvarInfo evi = Evd.find evd evk in |
| 461 |
let vars = Evd.evar_hyps evi |> Environ.named_context_of_val |> Context.Named.instance EConstr.mkVar in |
| 462 |
(n, vars) |
| 463 |
in |
| 464 |
|
| 465 |
let invtbl = Evar.Map.mapi (update_invtbl evd) invtbl in |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
let evd = Evd.set_universe_context evd uctx in |
| 470 |
let rhs = Constrintern.(intern_gen WithoutTypeConstraint env evd rhs) in |
| 471 |
let flags = { Pretyping.no_classes_no_fail_inference_flags with poly } in |
| 472 |
let evd', rhs = |
| 473 |
try Pretyping.understand_tcc ~flags env evd ~expected_type:(OfType typ) rhs |
| 474 |
with Pretype_errors.PretypeError (env', evd', e) -> |
| 475 |
warn_rewrite_rules_break_SR ?loc:rhs_loc |
| 476 |
Pp.(surround (str "the replacement term doesn't have the type of the pattern") ++ str "." ++ fnl () ++ Himsg.explain_pretype_error env' evd' e); |
| 477 |
Pretyping.understand_tcc ~flags env evd rhs |
| 478 |
in |
| 479 |
|
| 480 |
let evd' = Evd.minimize_universes evd' in |
| 481 |
let _qvars', uvars' = EConstr.universes_of_constr evd' rhs in |
| 482 |
let evd' = Evd.restrict_universe_context evd' (Univ.Level.Set.union uvars uvars') in |
| 483 |
let fail pp = warn_rewrite_rules_break_SR ?loc:rhs_loc Pp.(surround (str "universe inconsistency") ++ str"." ++ spc() ++ str "Missing constraints: " ++ pp) in |
| 484 |
let () = UState.check_uctx_impl ~fail (Evd.ustate evd) (Evd.ustate evd') in |
| 485 |
let evd = evd' in |
| 486 |
|
| 487 |
let rhs = |
| 488 |
let rhs' = evar_subst invtbl evd 0 rhs in |
| 489 |
match EConstr.to_constr_opt evd rhs' with |
| 490 |
| Some rhs -> rhs |
| 491 |
| None -> |
| 492 |
let pr_unresolved_evar e = |
| 493 |
Pp.(hov 2 (str"- " ++ Printer.pr_existential_key env evd e ++ str ": " ++ |
| 494 |
Himsg.explain_pretype_error env evd (Pretype_errors.UnsolvableImplicit (e,None)))) |
| 495 |
in |
| 496 |
CErrors.user_err ?loc:rhs_loc Pp.(hov 0 begin |
| 497 |
str "The replacement term contains unresolved implicit arguments:"++ fnl () ++ |
| 498 |
str " " ++ Printer.pr_econstr_env env evd rhs ++ fnl () ++ |
| 499 |
str "More precisely: " ++ fnl () ++ |
| 500 |
v 0 (prlist_with_sep cut pr_unresolved_evar (Evar.Set.elements (Evarutil.undefined_evars_of_term evd rhs'))) |
| 501 |
end) |
| 502 |
in |
| 503 |
|
| 504 |
let rhs = Vars.subst_univs_level_constr usubst rhs in |
| 505 |
|
| 506 |
let test_qvar q = |
| 507 |
match Sorts.QVar.var_index q with |
| 508 |
| Some -1 -> |
| 509 |
CErrors.user_err ?loc:rhs_loc |
| 510 |
Pp.(str "Sort variable " ++ Termops.pr_evd_qvar evd q ++ str " appears in the replacement but does not appear in the pattern.") |
| 511 |
| Some n when n < 0 || n > nvarqs' -> CErrors.anomaly Pp.(str "Unknown sort variable in rewrite rule.") |
| 512 |
| Some _ -> () |
| 513 |
| None -> |
| 514 |
if not @@ Sorts.QVar.Set.mem q (evd |> Evd.sort_context_set |> fst |> fst) then |
| 515 |
CErrors.user_err ?loc:rhs_loc |
| 516 |
Pp.(str "Sort variable " ++ Termops.pr_evd_qvar evd q ++ str " appears in the replacement but does not appear in the pattern.") |
| 517 |
in |
| 518 |
|
| 519 |
let test_level ?(alg_ok=false) lvl = |
| 520 |
match Univ.Level.var_index lvl with |
| 521 |
| Some -1 -> |
| 522 |
CErrors.user_err ?loc:rhs_loc |
| 523 |
Pp.(str "Universe level variable " ++ Termops.pr_evd_level evd lvl ++ str " appears in the replacement but does not appear in the pattern.") |
| 524 |
| Some n when n < 0 || n > nvarus' -> CErrors.anomaly Pp.(str "Unknown universe level variable in rewrite rule") |
| 525 |
| Some _ -> () |
| 526 |
| None -> |
| 527 |
match UGraph.check_declared_universes (Environ.universes env) (Univ.Level.Set.singleton lvl) with |
| 528 |
| Ok () -> () |
| 529 |
| Error l -> |
| 530 |
let l = Univ.Level.Set.elements l in |
| 531 |
CErrors.user_err ?loc:rhs_loc |
| 532 |
Pp.(str "Universe level " ++ |
| 533 |
prlist_with_sep spc (Termops.pr_evd_level evd) l ++ |
| 534 |
str " appears in the replacement but does not appear in the pattern.") |
| 535 |
in |
| 536 |
|
| 537 |
let () = |
| 538 |
let qs, us = Vars.sort_and_universes_of_constr rhs in |
| 539 |
Sorts.QVar.Set.iter test_qvar qs; |
| 540 |
Univ.Level.Set.iter test_level us |
| 541 |
in |
| 542 |
|
| 543 |
head_symbol, { nvars = (nvars' - 1, nvarqs', nvarus'); lhs_pat = head_umask, elims; rhs } |
| 544 |
|
| 545 |
let do_rules id rules = |
| 546 |
let env = Global.env () in |
| 547 |
if not @@ Environ.rewrite_rules_allowed env then raise Environ.(RewriteRulesNotAllowed Rule); |
| 548 |
let body = { rewrules_rules = List.map interp_rule rules } in |
| 549 |
Global.add_rewrite_rules id body |