| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Names |
| 12 |
open EConstr |
| 13 |
open Inductiveops |
| 14 |
|
| 15 |
|
| 16 |
type term_extraction = |
| 17 |
| Id |
| 18 |
|
| 19 |
| Extraction of ((constructor * EInstance.t) * EConstr.t * int * term_extraction) |
| 20 |
|
| 21 |
|
| 22 |
type term_composition = |
| 23 |
|
| 24 |
| FromEnv of EConstr.t |
| 25 |
|
| 26 |
| FromParameter of EConstr.t * EConstr.t * int * term_extraction |
| 27 |
|
| 28 |
| FromIndex of EConstr.t * int * term_extraction |
| 29 |
|
| 30 |
| Composition of term_composition * term_composition array |
| 31 |
|
| 32 |
type projection_type = |
| 33 |
|
| 34 |
| Simple |
| 35 |
|
| 36 |
| Dependent_Extractable of term_composition |
| 37 |
|
| 38 |
| NotProjectable |
| 39 |
|
| 40 |
|
| 41 |
let get_ith_arg sigma i term = |
| 42 |
let (rels_to_arg, rest) = decompose_prod_n sigma (i-1) term in |
| 43 |
let (arg_name, arg_type,rest) = destProd sigma rest in |
| 44 |
(rels_to_arg, (arg_name, arg_type), rest) |
| 45 |
|
| 46 |
|
| 47 |
let is_field_i_dependent env sigma cnstr i = |
| 48 |
let constructor_type = e_type_of_constructor env sigma cnstr in |
| 49 |
let ind_n_params = inductive_nparams env (fst (fst cnstr)) in |
| 50 |
let (_, (_, field_type), _) = get_ith_arg sigma (i + ind_n_params) constructor_type in |
| 51 |
not (Vars.noccur_between sigma 1 (i - 1) field_type) |
| 52 |
|
| 53 |
|
| 54 |
let build_simple_projection env sigma intype cnstr special default = |
| 55 |
let open Context in |
| 56 |
let ci = (snd (fst cnstr)) in |
| 57 |
let body = Combinators.make_selector env sigma ~pos:ci ~special ~default (mkRel 1) intype in |
| 58 |
let id = Id.of_string "t" in |
| 59 |
mkLambda (make_annot (Name id) ERelevance.relevant, intype, body) |
| 60 |
|
| 61 |
let (let*) m f = match m with |
| 62 |
| None -> None |
| 63 |
| Some x -> f x |
| 64 |
|
| 65 |
|
| 66 |
let find_term_composition env sigma cnstr argindex env_field_type ind env_ind_params env_ind_args = |
| 67 |
|
| 68 |
let env_ind_n_params = Array.length env_ind_params in |
| 69 |
let rel_type_of_constructor = type_of_constructor env cnstr in |
| 70 |
let (_, (_, rel_field_type), rel_field_rest) = get_ith_arg sigma (argindex+env_ind_n_params) rel_type_of_constructor in |
| 71 |
let (rel_target_context, rel_target_type) = decompose_prod sigma rel_field_rest in |
| 72 |
let rel_field_type_lifted = Vars.lift (List.length rel_target_context + 1) rel_field_type in |
| 73 |
let (_, rel_args) = decompose_app sigma rel_target_type in |
| 74 |
let (rel_ind_params, rel_ind_args) = CArray.chop env_ind_n_params rel_args in |
| 75 |
|
| 76 |
let rec find_term_composition_rec env sigma rel_term_to_compose env_term_to_compose = |
| 77 |
if isRef sigma rel_term_to_compose then |
| 78 |
Some (FromEnv rel_term_to_compose) |
| 79 |
else match find_arg env sigma rel_term_to_compose rel_ind_params env_ind_params with |
| 80 |
| Some (i, extraction) -> |
| 81 |
Some (FromParameter (env_term_to_compose, env_ind_params.(i-1), i, extraction)) |
| 82 |
| None -> |
| 83 |
begin match find_arg env sigma rel_term_to_compose rel_ind_args env_ind_args with |
| 84 |
| Some (i, extraction) -> |
| 85 |
Some (FromIndex (env_term_to_compose, i, extraction)) |
| 86 |
| None -> |
| 87 |
begin match EConstr.kind sigma rel_term_to_compose with |
| 88 |
| App (rel_f,rel_args) -> |
| 89 |
let (env_f,env_args) = decompose_app sigma env_term_to_compose in |
| 90 |
let* f_composition = find_term_composition_rec env sigma rel_f env_f in |
| 91 |
if Array.length env_args != Array.length rel_args then |
| 92 |
None |
| 93 |
else |
| 94 |
let* args_compositions = |
| 95 |
let exception ArgNotComposable in |
| 96 |
let map i rel_arg = |
| 97 |
let arg_composition = find_term_composition_rec env sigma rel_arg env_args.(i) in |
| 98 |
match arg_composition with |
| 99 |
| Some arg_composition -> arg_composition |
| 100 |
| None -> raise ArgNotComposable |
| 101 |
in |
| 102 |
try Some (CArray.mapi map rel_args) with ArgNotComposable -> None |
| 103 |
in |
| 104 |
Some (Composition (f_composition, args_compositions)) |
| 105 |
| _ -> None |
| 106 |
end |
| 107 |
end |
| 108 |
|
| 109 |
|
| 110 |
and find_arg env sigma term_to_extract terms_to_extract_from env_types_of_fields = |
| 111 |
let find (i, term_to_extract_from) = |
| 112 |
let* r = find_term_extraction env sigma term_to_extract term_to_extract_from env_types_of_fields.(i) in |
| 113 |
Some (i + 1, r) |
| 114 |
in |
| 115 |
let rec seq_find_map xs = match xs() with |
| 116 |
| Seq.Nil -> None |
| 117 |
| Seq.Cons(x,xs) -> |
| 118 |
match find x with |
| 119 |
| None -> seq_find_map xs |
| 120 |
| Some _ as result -> result |
| 121 |
in |
| 122 |
seq_find_map (Array.to_seqi terms_to_extract_from) |
| 123 |
|
| 124 |
|
| 125 |
and find_term_extraction env sigma term_to_extract term_to_extract_from type_of_term_to_extract_from = |
| 126 |
if eq_constr_nounivs sigma term_to_extract term_to_extract_from then |
| 127 |
Some Id |
| 128 |
else match EConstr.kind sigma term_to_extract_from with |
| 129 |
| App (f, args) -> |
| 130 |
begin match EConstr.kind sigma f with |
| 131 |
| Construct c -> |
| 132 |
let (_, env_args) = decompose_app sigma type_of_term_to_extract_from in |
| 133 |
let* (i, extraction_result) = find_arg env sigma term_to_extract args env_args in |
| 134 |
Some (Extraction (c, type_of_term_to_extract_from, i, extraction_result)) |
| 135 |
| _ -> None |
| 136 |
end |
| 137 |
| _ -> None |
| 138 |
in |
| 139 |
find_term_composition_rec env sigma rel_field_type_lifted env_field_type |
| 140 |
|
| 141 |
|
| 142 |
let projectability_test env sigma cnstr argindex field_type ind ind_params ind_args = |
| 143 |
let dependent = is_field_i_dependent env sigma cnstr argindex in |
| 144 |
if dependent then |
| 145 |
let composition = find_term_composition env sigma cnstr argindex field_type ind ind_params ind_args in |
| 146 |
match composition with |
| 147 |
| Some composition -> Dependent_Extractable composition |
| 148 |
| None -> NotProjectable |
| 149 |
else Simple |
| 150 |
|
| 151 |
|
| 152 |
let rec build_term_extraction env sigma default rel_term_to_extract_from env_term_to_extract_from extraction = |
| 153 |
match extraction with |
| 154 |
| Id -> rel_term_to_extract_from |
| 155 |
|
| 156 |
| Extraction ((cnstr, _), env_next_term_to_extract_from, index, next_extraction) -> |
| 157 |
let cnstr_n_args = constructor_nrealargs env cnstr in |
| 158 |
let special = build_term_extraction env sigma default (mkRel (cnstr_n_args-(index-1))) env_next_term_to_extract_from next_extraction in |
| 159 |
let pos = snd cnstr in |
| 160 |
let (_, match_type) = Typing.type_of env sigma env_term_to_extract_from in |
| 161 |
Combinators.make_selector env sigma ~pos ~special ~default rel_term_to_extract_from match_type |
| 162 |
|
| 163 |
|
| 164 |
let rec build_term_composition env sigma term_composition ind_params n_ind_params ind_args n_ind_args = |
| 165 |
match term_composition with |
| 166 |
|
| 167 |
| FromEnv env_type_to_compose -> env_type_to_compose |
| 168 |
|
| 169 |
| FromParameter (env_type_to_compose, env_parameter_to_extract_from, parameter_index, extraction) -> |
| 170 |
let type_to_extract_from = ind_params.(parameter_index-1) in |
| 171 |
build_term_extraction env sigma env_type_to_compose env_parameter_to_extract_from type_to_extract_from extraction |
| 172 |
|
| 173 |
| FromIndex (env_type_to_compose, index_index, extraction) -> |
| 174 |
let type_to_extract_from = ind_args.(index_index-1) in |
| 175 |
build_term_extraction env sigma env_type_to_compose (mkRel (n_ind_args-(index_index-1))) type_to_extract_from extraction |
| 176 |
|
| 177 |
| Composition (f_composition, arg_compositions) -> |
| 178 |
let f_extraction_term = build_term_composition env sigma f_composition ind_params n_ind_params ind_args n_ind_args in |
| 179 |
let map arg_composition = build_term_composition env sigma arg_composition ind_params n_ind_params ind_args n_ind_args in |
| 180 |
let arg_extraction_terms = Array.map map arg_compositions in |
| 181 |
mkApp (f_extraction_term, arg_extraction_terms) |
| 182 |
|
| 183 |
|
| 184 |
let match_indices env sigma cnst_summary term_to_match n_ind_args max_free_rel = |
| 185 |
let rec replace_rec n t = match EConstr.kind sigma t with |
| 186 |
| Rel i -> |
| 187 |
if i <= n_ind_args + n && i > n then |
| 188 |
cnst_summary.cs_concl_realargs.(n_ind_args-i) |
| 189 |
else t |
| 190 |
| _ -> EConstr.map_with_binders sigma (fun n -> n+1) replace_rec n t |
| 191 |
in |
| 192 |
EConstr.map_with_binders sigma (fun n -> n+1) replace_rec 0 term_to_match |
| 193 |
|
| 194 |
let make_annot_numbered s i r = |
| 195 |
Context.make_annot (Name.mk_name (Nameops.make_ident s i)) r |
| 196 |
|
| 197 |
|
| 198 |
let make_selector_match_indices env sigma ~pos ~special c (ind_fam, ind_args) return_type composition_type_template = |
| 199 |
let n_ind_args = List.length ind_args in |
| 200 |
let indt = IndType (ind_fam, ind_args) in |
| 201 |
let (ind, _),_ = dest_ind_family ind_fam in |
| 202 |
let () = Tacred.check_privacy env ind in |
| 203 |
let (_, mip) = Inductive.lookup_mind_specif env ind in |
| 204 |
let deparsign = make_arity_signature env sigma true ind_fam in |
| 205 |
let p = it_mkLambda_or_LetIn return_type deparsign in |
| 206 |
let cstrs = get_constructors env ind_fam in |
| 207 |
let build_branch i = |
| 208 |
let max_free_rel = Option.default 0 (Int.Set.max_elt_opt (Termops.free_rels sigma composition_type_template)) in |
| 209 |
let matched_template = match_indices env sigma cstrs.(i-1) composition_type_template n_ind_args max_free_rel in |
| 210 |
let endpt = if Int.equal i pos then |
| 211 |
mkLambda (Context.make_annot Name.Anonymous ERelevance.relevant, matched_template, Vars.lift 1 special) |
| 212 |
else |
| 213 |
mkLambda (make_annot_numbered "t" None ERelevance.relevant, matched_template, mkRel 1) |
| 214 |
in |
| 215 |
let args = cstrs.(i-1).cs_args in |
| 216 |
it_mkLambda_or_LetIn endpt args |
| 217 |
in |
| 218 |
let brl = |
| 219 |
List.map build_branch(CList.interval 1 (Array.length mip.mind_consnames)) in |
| 220 |
let rci = ERelevance.relevant in |
| 221 |
let ci = make_case_info env ind RegularStyle in |
| 222 |
make_case_or_project env sigma indt ci (p, rci) c (Array.of_list brl) |
| 223 |
|
| 224 |
|
| 225 |
let build_dependent_projection_with_term_composition env sigma cnstr default special argty term_composition ((ind, ind_params) as ind_fam) ind_args = |
| 226 |
let n_ind_params = List.length ind_params in |
| 227 |
let n_ind_args = List.length ind_args in |
| 228 |
let composition_type_template = build_term_composition env sigma term_composition (Array.of_list ind_params) n_ind_params (Array.of_list ind_args) n_ind_args in |
| 229 |
let return_type = Vars.lift 1 ( |
| 230 |
mkProd (Context.make_annot Name.Anonymous ERelevance.relevant, composition_type_template, Vars.lift 1 composition_type_template) |
| 231 |
) in |
| 232 |
let e_match = make_selector_match_indices env sigma ~pos:(snd (fst cnstr)) ~special (mkRel 1) (make_ind_family ind_fam, ind_args) return_type composition_type_template in |
| 233 |
let match_default = mkApp (e_match, [|default|]) in |
| 234 |
let proj = mkLambda (make_annot_numbered "e" None ERelevance.relevant, argty, match_default) in |
| 235 |
proj |
| 236 |
|
| 237 |
|
| 238 |
let build_projection env sigma |
| 239 |
cnstr |
| 240 |
argindex |
| 241 |
field_type |
| 242 |
default |
| 243 |
special |
| 244 |
argty = |
| 245 |
let IndType (ind_family,ind_args) = |
| 246 |
try find_rectype env sigma argty |
| 247 |
with Not_found -> |
| 248 |
CErrors.user_err |
| 249 |
Pp.(str "Cannot discriminate on inductive constructors with \ |
| 250 |
dependent types.") in |
| 251 |
let (ind, ind_params) = dest_ind_family ind_family in |
| 252 |
let cnstr = (fst cnstr, EInstance.make (snd cnstr)) in |
| 253 |
let proj_result = projectability_test env sigma cnstr argindex field_type ind (Array.of_list ind_params) (Array.of_list ind_args) in |
| 254 |
match proj_result with |
| 255 |
| Simple | NotProjectable -> |
| 256 |
let p = build_simple_projection env sigma argty cnstr special default in |
| 257 |
sigma, p |
| 258 |
| Dependent_Extractable type_composition -> |
| 259 |
let p = build_dependent_projection_with_term_composition env sigma cnstr default special argty type_composition (ind, ind_params) ind_args in |
| 260 |
sigma, p |