Skip to content

Commit c727292

Browse files
committed
refactor: Change option array to list option
1 parent d04f191 commit c727292

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

kernel/inductive.ml

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,31 +1667,32 @@ let inductive_of_mutfix ?evars env ((nvect, bodynum), (names, types, bodies as r
16671667
else anomaly ~label:"check_one_fix" (Pp.str "Bad occurrence of recursive call.")
16681668
| _ -> raise_err env i (NotEnoughAbstractionInFixBody k)
16691669
in
1670-
let ((ind, inst), bs) = check_occur fixenv 1 def in
1671-
(* recursive sprop means non record with projections -> squashed *)
1672-
let sorts_opt =
1673-
if Environ.is_type_in_type env (GlobRef.IndRef ind) then None
1674-
else
1670+
check_occur fixenv 1 def
1671+
in
1672+
(* Do it on every fixpoint *)
1673+
let rv = Array.map2_i find_ind nvect bodies in
1674+
(Array.map fst rv, Array.map snd rv)
1675+
1676+
(* Returns the pairs of (inductive sort * output sort) or None if Type in Type *)
1677+
let sorts_of_mutfix env minds names =
1678+
let check_type_in_type (ind, _) = Environ.is_type_in_type env (GlobRef.IndRef ind) in
1679+
(* recursive sprop means non record with projections -> squashed *)
1680+
if Array.exists check_type_in_type minds then None
1681+
else
1682+
Some (Array.fold_left_i (fun i sorts (ind, inst) ->
16751683
let _, mip = lookup_mind_specif env ind in
1676-
let sind = UVars.subst_instance_sort inst mip.mind_sort in
1677-
let u = Sorts.univ_of_sort sind in
1684+
let ind_sort = UVars.subst_instance_sort inst mip.mind_sort in
1685+
let u = Sorts.univ_of_sort ind_sort in
16781686
(* This is an approximation: a [Relevant] variable might be of sort [Prop]
16791687
or [Type]. As we only care about the quality, we have to be conservative
16801688
here, i.e., every relevant sort (so, [Prop] or above) can be eliminated
16811689
into any other relevant sort. *)
1682-
let bsort = match names.(i).Context.binder_relevance with
1690+
let out_sort = match names.(i).Context.binder_relevance with
16831691
| Irrelevant -> Sorts.sprop
16841692
| Relevant -> Sorts.prop
16851693
| RelevanceVar q -> Sorts.qsort q u in
1686-
Some (sind, bsort)
1687-
in
1688-
ind, bs, sorts_opt
1689-
in
1690-
(* Do it on every fixpoint *)
1691-
let rv = Array.map2_i find_ind nvect bodies in
1692-
(Array.map (fun (minds, _, _) -> minds) rv,
1693-
Array.map (fun (_, rdef, _) -> rdef) rv,
1694-
Array.map (fun (_, _, sorts_opt) -> sorts_opt) rv)
1694+
(ind_sort, out_sort) :: sorts
1695+
) [] minds)
16951696

16961697

16971698
let check_fix_pre_sorts ?evars env ((nvect, _), (names, _, bodies as recdef) as fix) =
@@ -1700,38 +1701,35 @@ let check_fix_pre_sorts ?evars env ((nvect, _), (names, _, bodies as recdef) as
17001701
the possibly new constraints (see e.g. [esearch_guard] (Pretyping)). We expose this
17011702
function to be used for this purpose, while check_fix performs the normal check,
17021703
failing when elimination constraints are not satisfied. *)
1703-
let (minds, rdef, sorts_opt) = inductive_of_mutfix ?evars env fix in
1704+
let minds, rdef = inductive_of_mutfix ?evars env fix in
1705+
let sorts_opt = sorts_of_mutfix env minds names in
1706+
let inds = Array.map fst minds in
17041707
let flags = Environ.typing_flags env in
17051708
let raise_err = raise_fix_guard_err_fn env recdef names in
1706-
if flags.check_guarded then
1707-
let get_tree (kn,i) =
1708-
let mib = Environ.lookup_mind kn env in
1709-
mib.mind_packets.(i).mind_recargs
1710-
in
1711-
let trees = Array.map get_tree minds in
1712-
let () =
1709+
let () =
1710+
if flags.check_guarded then
1711+
let get_tree (kn,i) =
1712+
let mib = Environ.lookup_mind kn env in
1713+
mib.mind_packets.(i).mind_recargs
1714+
in
1715+
let trees = Array.map get_tree inds in
17131716
for i = 0 to Array.length bodies - 1 do
17141717
let (fenv, body) = rdef.(i) in
17151718
let renv = make_renv fenv nvect.(i) trees.(i) in
17161719
try check_one_fix cache ?evars renv nvect trees body
17171720
with FixGuardError (err_env, err) -> raise_err err_env i err
17181721
done
1719-
in
1720-
sorts_opt
1721-
else
1722-
sorts_opt
1722+
in
1723+
sorts_opt
17231724

17241725
let check_fix ?evars env (_, (names, _, _ as recdef) as fix) =
17251726
let sorts_opts = check_fix_pre_sorts ?evars env fix in
17261727
let raise_err = raise_fix_guard_err_fn env recdef names in
17271728
let elim_to = eliminates_to (Environ.qualities env) in
1728-
Array.iteri (fun i sort_opt ->
1729-
match sort_opt with
1730-
| None -> ()
1731-
| Some (ind_sort, binder_sort) ->
1732-
if not (is_allowed_fixpoint elim_to ind_sort binder_sort) then
1733-
raise_err env i @@ FixpointOnNonEliminable (ind_sort, binder_sort)
1734-
) sorts_opts
1729+
Option.iter (List.iteri (fun i (ind_sort, out_sort) ->
1730+
if not (is_allowed_fixpoint elim_to ind_sort out_sort) then
1731+
raise_err env i @@ FixpointOnNonEliminable (ind_sort, out_sort)
1732+
)) sorts_opts
17351733

17361734
(************************************************************************)
17371735
(* Co-fixpoints. *)

kernel/inductive.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ val check_case_info : env -> pinductive -> case_info -> unit
193193
in these containers. *)
194194
val is_primitive_positive_container : env -> Constant.t -> bool
195195

196-
val check_fix_pre_sorts : ?evars:evar_handler -> env -> fixpoint -> (Sorts.t * Sorts.t) option array
196+
val check_fix_pre_sorts : ?evars:evar_handler -> env -> fixpoint -> (Sorts.t * Sorts.t) list option
197197
(** Checks fixpoint without checking sort elimination constraints.
198198
Returns an array of possible sorts (None if Type in Type) *)
199199

pretyping/typing.ml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,10 @@ let judge_of_cast env sigma cj k tj =
330330

331331
let check_fix_with_elims env sigma fix =
332332
let evars = Evd.evar_handler sigma in
333-
let sorts_opts = check_fix_pre_sorts ~evars env fix in
334-
Array.fold_left_i
335-
(fun i sigma sort_opt ->
336-
match sort_opt with
337-
| None -> sigma
338-
| Some (ind_sort, binder_sort) ->
339-
let elim_to = Inductive.eliminates_to @@ Evd.elim_graph sigma in
340-
if not (is_allowed_fixpoint elim_to ind_sort binder_sort) then
341-
Evd.set_elim_to sigma (Sorts.quality ind_sort) (Sorts.quality binder_sort)
342-
else
343-
sigma
344-
) sigma sorts_opts
333+
let sorts_opt = check_fix_pre_sorts ~evars env fix in
334+
Option.fold_left (List.fold_left (fun sigma (ind_sort, out_sort) ->
335+
Evd.set_elim_to sigma (Sorts.quality ind_sort) (Sorts.quality out_sort)
336+
)) sigma sorts_opt
345337

346338
let check_fix env sigma pfix =
347339
let inj c = Unsafe.to_constr c in

0 commit comments

Comments
 (0)