| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Printer |
| 12 |
open CErrors |
| 13 |
open Term |
| 14 |
open Util |
| 15 |
open Constr |
| 16 |
open Context |
| 17 |
open Vars |
| 18 |
open Names |
| 19 |
open Pp |
| 20 |
open Induction |
| 21 |
open Context.Rel.Declaration |
| 22 |
open Indfun_common |
| 23 |
module RelDecl = Context.Rel.Declaration |
| 24 |
|
| 25 |
exception Toberemoved_with_rel of int * constr |
| 26 |
exception Toberemoved |
| 27 |
|
| 28 |
let observe s = if do_observe () then Feedback.msg_debug s |
| 29 |
let pop t = Vars.lift (-1) t |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
let compute_new_princ_type_from_rel env rel_to_fun sorts princ_type = |
| 36 |
let princ_type = EConstr.of_constr princ_type in |
| 37 |
let princ_type_info = compute_elim_sig Evd.empty princ_type in |
| 38 |
let env_with_params = EConstr.push_rel_context princ_type_info.params env in |
| 39 |
let tbl = Hashtbl.create 792 in |
| 40 |
let rec change_predicates_names (avoid : Id.t list) |
| 41 |
(predicates : EConstr.rel_context) : EConstr.rel_context = |
| 42 |
match predicates with |
| 43 |
| [] -> [] |
| 44 |
| decl :: predicates -> ( |
| 45 |
match Context.Rel.Declaration.get_name decl with |
| 46 |
| Name x -> |
| 47 |
let id = Namegen.next_ident_away x (Id.Set.of_list avoid) in |
| 48 |
Hashtbl.add tbl id x; |
| 49 |
RelDecl.set_name (Name id) decl |
| 50 |
:: change_predicates_names (id :: avoid) predicates |
| 51 |
| Anonymous -> anomaly (Pp.str "Anonymous property binder.") ) |
| 52 |
in |
| 53 |
let avoid = Termops.ids_of_context env_with_params in |
| 54 |
let princ_type_info = |
| 55 |
{ princ_type_info with |
| 56 |
predicates = change_predicates_names avoid princ_type_info.predicates } |
| 57 |
in |
| 58 |
|
| 59 |
|
| 60 |
let change_predicate_sort i decl = |
| 61 |
let new_sort = sorts.(i) in |
| 62 |
let args, _ = |
| 63 |
decompose_prod_decls (EConstr.Unsafe.to_constr (RelDecl.get_type decl)) |
| 64 |
in |
| 65 |
let real_args = |
| 66 |
if princ_type_info.indarg_in_concl then List.tl args else args |
| 67 |
in |
| 68 |
let na = map_annot Nameops.Name.get_id (Context.Rel.Declaration.get_annot decl) in |
| 69 |
let na = EConstr.Unsafe.to_binder_annot na in |
| 70 |
Context.Named.Declaration.LocalAssum (na, Term.it_mkProd_or_LetIn (mkSort new_sort) real_args) |
| 71 |
in |
| 72 |
let new_predicates = |
| 73 |
List.map_i change_predicate_sort 0 princ_type_info.predicates |
| 74 |
in |
| 75 |
let env_with_params_and_predicates = |
| 76 |
List.fold_right Environ.push_named new_predicates env_with_params |
| 77 |
in |
| 78 |
let rel_as_kn = |
| 79 |
fst |
| 80 |
( match princ_type_info.indref with |
| 81 |
| Some (GlobRef.IndRef ind) -> ind |
| 82 |
| _ -> user_err Pp.(str "Not a valid predicate") ) |
| 83 |
in |
| 84 |
let ptes_vars = List.map Context.Named.Declaration.get_id new_predicates in |
| 85 |
let is_pte = |
| 86 |
let set = List.fold_right Id.Set.add ptes_vars Id.Set.empty in |
| 87 |
fun t -> match Constr.kind t with Var id -> Id.Set.mem id set | _ -> false |
| 88 |
in |
| 89 |
let pre_princ = |
| 90 |
let open EConstr in |
| 91 |
it_mkProd_or_LetIn |
| 92 |
(it_mkProd_or_LetIn |
| 93 |
(Option.fold_right mkProd_or_LetIn princ_type_info.indarg |
| 94 |
princ_type_info.concl) |
| 95 |
princ_type_info.args) |
| 96 |
princ_type_info.branches |
| 97 |
in |
| 98 |
let pre_princ = EConstr.Unsafe.to_constr pre_princ in |
| 99 |
let pre_princ = substl (List.map mkVar ptes_vars) pre_princ in |
| 100 |
let is_dom c = |
| 101 |
match Constr.kind c with |
| 102 |
| Ind ((u, _), _) -> Environ.QMutInd.equal env u rel_as_kn |
| 103 |
| Construct (((u, _), _), _) -> Environ.QMutInd.equal env u rel_as_kn |
| 104 |
| _ -> false |
| 105 |
in |
| 106 |
let get_fun_num c = |
| 107 |
match Constr.kind c with |
| 108 |
| Ind ((_, num), _) -> num |
| 109 |
| Construct (((_, num), _), _) -> num |
| 110 |
| _ -> assert false |
| 111 |
in |
| 112 |
let dummy_var = mkVar (Id.of_string "________") in |
| 113 |
let mk_replacement c i args = |
| 114 |
let res = mkApp (rel_to_fun.(i), Array.map pop (array_get_start args)) in |
| 115 |
observe |
| 116 |
( str "replacing " |
| 117 |
++ pr_lconstr_env env Evd.empty c |
| 118 |
++ str " by " |
| 119 |
++ pr_lconstr_env env Evd.empty res ); |
| 120 |
res |
| 121 |
in |
| 122 |
let rec compute_new_princ_type remove env pre_princ : types * constr list = |
| 123 |
let ((new_princ_type, _) as res) = |
| 124 |
match Constr.kind pre_princ with |
| 125 |
| Rel n -> ( |
| 126 |
try |
| 127 |
match Environ.lookup_rel n env with |
| 128 |
| (LocalAssum (_, t) | LocalDef (_, _, t)) when is_dom t -> |
| 129 |
raise Toberemoved |
| 130 |
| _ -> (pre_princ, []) |
| 131 |
with Not_found -> assert false ) |
| 132 |
| Prod (x, t, b) -> |
| 133 |
compute_new_princ_type_for_binder remove mkProd env x t b |
| 134 |
| Lambda (x, t, b) -> |
| 135 |
compute_new_princ_type_for_binder remove mkLambda env x t b |
| 136 |
| (Ind _ | Construct _) when is_dom pre_princ -> raise Toberemoved |
| 137 |
| App (f, args) when is_dom f -> |
| 138 |
let var_to_be_removed = destRel (Array.last args) in |
| 139 |
let num = get_fun_num f in |
| 140 |
raise |
| 141 |
(Toberemoved_with_rel |
| 142 |
(var_to_be_removed, mk_replacement pre_princ num args)) |
| 143 |
| App (f, args) -> |
| 144 |
let args = if is_pte f && remove then array_get_start args else args in |
| 145 |
let new_args, binders_to_remove = |
| 146 |
Array.fold_right |
| 147 |
(compute_new_princ_type_with_acc remove env) |
| 148 |
args ([], []) |
| 149 |
in |
| 150 |
let new_f, binders_to_remove_from_f = |
| 151 |
compute_new_princ_type remove env f |
| 152 |
in |
| 153 |
( applistc new_f new_args |
| 154 |
, list_union_eq Constr.equal binders_to_remove_from_f binders_to_remove |
| 155 |
) |
| 156 |
| LetIn (x, v, t, b) -> |
| 157 |
compute_new_princ_type_for_letin remove env x v t b |
| 158 |
| _ -> (pre_princ, []) |
| 159 |
in |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
res |
| 168 |
and compute_new_princ_type_for_binder remove bind_fun env x t b = |
| 169 |
try |
| 170 |
let new_t, binders_to_remove_from_t = |
| 171 |
compute_new_princ_type remove env t |
| 172 |
in |
| 173 |
let new_x = map_annot (get_name (Termops.ids_of_context env)) x in |
| 174 |
let new_env = Environ.push_rel (LocalAssum (x, t)) env in |
| 175 |
let new_b, binders_to_remove_from_b = |
| 176 |
compute_new_princ_type remove new_env b |
| 177 |
in |
| 178 |
if List.exists (Constr.equal (mkRel 1)) binders_to_remove_from_b then |
| 179 |
( pop new_b |
| 180 |
, filter_map (Constr.equal (mkRel 1)) pop binders_to_remove_from_b ) |
| 181 |
else |
| 182 |
( bind_fun (new_x, new_t, new_b) |
| 183 |
, list_union_eq Constr.equal binders_to_remove_from_t |
| 184 |
(List.map pop binders_to_remove_from_b) ) |
| 185 |
with |
| 186 |
| Toberemoved -> |
| 187 |
|
| 188 |
let new_b, binders_to_remove_from_b = |
| 189 |
compute_new_princ_type remove env (substnl [dummy_var] 1 b) |
| 190 |
in |
| 191 |
(new_b, List.map pop binders_to_remove_from_b) |
| 192 |
| Toberemoved_with_rel (n, c) -> |
| 193 |
|
| 194 |
let new_b, binders_to_remove_from_b = |
| 195 |
compute_new_princ_type remove env (substnl [c] n b) |
| 196 |
in |
| 197 |
( new_b |
| 198 |
, list_add_set_eq Constr.equal (mkRel n) |
| 199 |
(List.map pop binders_to_remove_from_b) ) |
| 200 |
and compute_new_princ_type_for_letin remove env x v t b = |
| 201 |
try |
| 202 |
let new_t, binders_to_remove_from_t = |
| 203 |
compute_new_princ_type remove env t |
| 204 |
in |
| 205 |
let new_v, binders_to_remove_from_v = |
| 206 |
compute_new_princ_type remove env v |
| 207 |
in |
| 208 |
let new_x = map_annot (get_name (Termops.ids_of_context env)) x in |
| 209 |
let new_env = Environ.push_rel (LocalDef (x, v, t)) env in |
| 210 |
let new_b, binders_to_remove_from_b = |
| 211 |
compute_new_princ_type remove new_env b |
| 212 |
in |
| 213 |
if List.exists (Constr.equal (mkRel 1)) binders_to_remove_from_b then |
| 214 |
( pop new_b |
| 215 |
, filter_map (Constr.equal (mkRel 1)) pop binders_to_remove_from_b ) |
| 216 |
else |
| 217 |
( mkLetIn (new_x, new_v, new_t, new_b) |
| 218 |
, list_union_eq Constr.equal |
| 219 |
(list_union_eq Constr.equal binders_to_remove_from_t |
| 220 |
binders_to_remove_from_v) |
| 221 |
(List.map pop binders_to_remove_from_b) ) |
| 222 |
with |
| 223 |
| Toberemoved -> |
| 224 |
|
| 225 |
let new_b, binders_to_remove_from_b = |
| 226 |
compute_new_princ_type remove env (substnl [dummy_var] 1 b) |
| 227 |
in |
| 228 |
(new_b, List.map pop binders_to_remove_from_b) |
| 229 |
| Toberemoved_with_rel (n, c) -> |
| 230 |
|
| 231 |
let new_b, binders_to_remove_from_b = |
| 232 |
compute_new_princ_type remove env (substnl [c] n b) |
| 233 |
in |
| 234 |
( new_b |
| 235 |
, list_add_set_eq Constr.equal (mkRel n) |
| 236 |
(List.map pop binders_to_remove_from_b) ) |
| 237 |
and compute_new_princ_type_with_acc remove env e (c_acc, to_remove_acc) = |
| 238 |
let new_e, to_remove_from_e = compute_new_princ_type remove env e in |
| 239 |
(new_e :: c_acc, list_union_eq Constr.equal to_remove_from_e to_remove_acc) |
| 240 |
in |
| 241 |
|
| 242 |
let pre_res, _ = |
| 243 |
compute_new_princ_type princ_type_info.indarg_in_concl |
| 244 |
env_with_params_and_predicates pre_princ |
| 245 |
in |
| 246 |
let pre_res = |
| 247 |
replace_vars |
| 248 |
(List.map_i (fun i id -> (id, mkRel i)) 1 ptes_vars) |
| 249 |
(lift (List.length ptes_vars) pre_res) |
| 250 |
in |
| 251 |
it_mkProd_or_LetIn |
| 252 |
(it_mkProd_or_LetIn pre_res |
| 253 |
(List.map |
| 254 |
(function |
| 255 |
| Context.Named.Declaration.LocalAssum (id, b) -> |
| 256 |
LocalAssum |
| 257 |
(map_annot (fun id -> Name.mk_name (Hashtbl.find tbl id)) id, b) |
| 258 |
| Context.Named.Declaration.LocalDef (id, t, b) -> |
| 259 |
LocalDef |
| 260 |
( map_annot (fun id -> Name.mk_name (Hashtbl.find tbl id)) id |
| 261 |
, t |
| 262 |
, b )) |
| 263 |
new_predicates)) |
| 264 |
(EConstr.Unsafe.to_rel_context princ_type_info.params) |