Debian Codegraph @

checker/checkInductive.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Declarations
12 open Environ
13 open Names
14 open UVars
15 open Util
16
17 [@@@ocaml.warning "+9+27"]
18
19 exception InductiveMismatch of MutInd.t * string
20
21 let check mind field b = if not b then raise (InductiveMismatch (mind,field))
22
23 let template_univ_entry {template_context; template_defaults=default_univs; _} =
24 Entries.Template_ind_entry {uctx = AbstractContext.repr template_context; default_univs}
25
26 let to_entry mind (mb:mutual_inductive_body) : Entries.mutual_inductive_entry =
27 let open Entries in
28 let nparams = List.length mb.mind_params_ctxt in (* include letins *)
29 let mind_entry_record =
30 (* NB declarations support blocks with some records and some
31 non-records, but not yet entries *)
32 assert (mb.mind_packets |> Array.for_all @@ fun p ->
33 match mb.mind_packets.(0).mind_record, p.mind_record with
34 | NotRecord, NotRecord | FakeRecord, FakeRecord | PrimRecord _, PrimRecord _ -> true
35 | (NotRecord | FakeRecord | PrimRecord _), _ -> false);
36 match mb.mind_packets.(0).mind_record with
37 | NotRecord -> None | FakeRecord -> Some None
38 | PrimRecord _ ->
39 let get_id p =
40 match p.mind_record with
41 | NotRecord | FakeRecord -> assert false
42 | PrimRecord {id; _ } -> id
43 in
44 Some (Some (Array.map get_id mb.mind_packets))
45 in
46 let template = Option.map template_univ_entry mb.mind_template in
47 let mind_entry_universes = match mb.mind_universes with
48 | Monomorphic ->
49 begin match template with
50 | None -> Monomorphic_ind_entry
51 | Some template -> template
52 end
53 | Polymorphic auctx -> Polymorphic_ind_entry (AbstractContext.repr auctx)
54 in
55 let ntyps = Array.length mb.mind_packets in
56 let mind_entry_params = match mb.mind_template with
57 | None -> mb.mind_params_ctxt
58 | Some template ->
59 let open Context.Rel.Declaration in
60 let rec fix_params acc params template = match params, template with
61 | [], [] -> acc
62 | (LocalDef _ as d) :: params , _ ->
63 fix_params (d::acc) params template
64 | (LocalAssum _ as d) :: params, None :: template ->
65 fix_params (d :: acc) params template
66 | LocalAssum (na, t) :: params, Some s :: template ->
67 let ctx, _ = Term.destArity t in
68 let d = LocalAssum (na, Term.mkArity (ctx, s)) in
69 fix_params (d :: acc) params template
70 | _ :: _, [] | [], _ :: _ -> assert false
71 in
72 fix_params [] (List.rev mb.mind_params_ctxt) template.template_param_arguments
73 in
74 let mind_entry_inds = Array.map_to_list (fun ind ->
75 let mind_entry_arity =
76 match mb.mind_template with
77 | None ->
78 let ctx, arity = Term.decompose_prod_n_decls nparams ind.mind_user_arity in
79 ignore ctx; (* we will check that the produced user_arity is equal to the input *)
80 arity
81 | Some template ->
82 let ctx = ind.mind_arity_ctxt in
83 let ctx = List.firstn (List.length ctx - nparams) ctx in
84 Term.mkArity (ctx, template.template_concl)
85 in
86 {
87 mind_entry_typename = ind.mind_typename;
88 mind_entry_arity;
89 mind_entry_consnames = Array.to_list ind.mind_consnames;
90 mind_entry_lc = Array.map_to_list (fun c ->
91 let c = Inductive.abstract_constructor_type_relatively_to_inductive_types_context ntyps mind c in
92 let ctx, c = Term.decompose_prod_n_decls nparams c in
93 ignore ctx; (* we will check that the produced user_lc is equal to the input *)
94 c
95 ) ind.mind_user_lc;
96 })
97 mb.mind_packets
98 in
99 let mind_entry_variance = Option.map (Array.map (fun v -> Some v)) mb.mind_variance in
100 {
101 mind_entry_record;
102 mind_entry_finite = mb.mind_finite;
103 mind_entry_params;
104 mind_entry_inds;
105 mind_entry_universes;
106 mind_entry_variance;
107 mind_entry_private = mb.mind_private;
108 }
109
110 let check_abstract_uctx a b =
111 eq_sizes (AbstractContext.size a) (AbstractContext.size b)
112 && PConstraints.equal (UContext.constraints @@ AbstractContext.repr a)
113 (UContext.constraints @@ AbstractContext.repr b)
114
115 let check_template ar1 ar2 = match ar1, ar2 with
116 | None, None -> true
117 | Some ar, Some {template_context; template_param_arguments; template_concl; template_defaults} ->
118 List.equal (Option.equal Sorts.equal) ar.template_param_arguments template_param_arguments &&
119 check_abstract_uctx template_context ar.template_context &&
120 Sorts.equal ar.template_concl template_concl &&
121 Instance.equal ar.template_defaults template_defaults
122 | None, Some _ | Some _, None -> false
123
124 (* if the generated inductive is squashed the original one must be squashed *)
125 let check_squashed orig generated = match orig, generated with
126 | None, None -> true
127 | Some _, None ->
128 (* the inductive is from functor instantiation which removed the need for squash *)
129 true
130 | None, Some _ ->
131 (* missing squash *)
132 false
133 | Some s1, Some s2 ->
134 (* functor instantiation can change sort qualities
135 (from Type -> Prop)
136 Condition: every quality which can make the generated inductive
137 squashed must also make the original inductive squashed *)
138 match s1, s2 with
139 | AlwaysSquashed, AlwaysSquashed -> true
140 | AlwaysSquashed, SometimesSquashed _ -> true
141 | SometimesSquashed _, AlwaysSquashed -> false
142 | SometimesSquashed s1, SometimesSquashed s2 ->
143 Sorts.Quality.Set.subset s2 s1
144
145 (* Use [UserOrd] because when we rebuild the recargs we have lost
146 the knowledge of who is the canonical version.
147 Try with to see test-suite/coqchk/include.v *)
148 let eq_recarg_type ty1 ty2 = match ty1, ty2 with
149 | RecArgInd ind1, RecArgInd ind2 -> Names.Ind.UserOrd.equal ind1 ind2
150 | RecArgPrim c1, RecArgPrim c2 -> Names.Constant.UserOrd.equal c1 c2
151 | (RecArgInd _ | RecArgPrim _), _ -> false
152
153 let eq_recarg r1 r2 = match r1, r2 with
154 | Norec, Norec -> true
155 | Mrec ty1, Mrec ty2 -> eq_recarg_type ty1 ty2
156 | (Norec | Mrec _), _ -> false
157
158 let eq_reloc_tbl = Array.equal (fun x y -> Int.equal (fst x) (fst y) && Int.equal (snd x) (snd y))
159
160 let eq_in_context (ctx1, t1) (ctx2, t2) =
161 Context.Rel.equal Sorts.relevance_equal Constr.equal ctx1 ctx2 && Constr.equal t1 t2
162
163 let check_same_record r1 r2 = match r1, r2 with
164 | NotRecord, NotRecord | FakeRecord, FakeRecord -> true
165 | PrimRecord { relevances = r1; tys = tys1 ; _ }, PrimRecord { relevances = r2; tys = tys2 ; _ } ->
166 (* The kernel doesn't care about the names, we just need to check
167 that the saved types are correct. *)
168 Array.equal Sorts.relevance_equal r1 r2 &&
169 Array.equal Constr.equal tys1 tys2
170 | (NotRecord | FakeRecord | PrimRecord _), _ -> false
171
172 let check_packet mind ind
173 { mind_typename; mind_arity_ctxt; mind_user_arity; mind_record; mind_sort; mind_consnames; mind_user_lc;
174 mind_nrealargs; mind_nrealdecls; mind_squashed; mind_nf_lc;
175 mind_consnrealargs; mind_consnrealdecls; mind_recargs; mind_relevance;
176 mind_nb_constant; mind_nb_args; mind_reloc_tbl } =
177 let check = check mind in
178
179 ignore mind_typename; (* passed through *)
180 check "mind_arity_ctxt" (Context.Rel.equal Sorts.relevance_equal Constr.equal ind.mind_arity_ctxt mind_arity_ctxt);
181 check "mind_arity" (Constr.equal ind.mind_user_arity mind_user_arity);
182 check "mind_sort" (Sorts.equal ind.mind_sort mind_sort);
183 ignore mind_consnames; (* passed through *)
184 check "mind_record" (check_same_record ind.mind_record mind_record);
185 check "mind_user_lc" (Array.equal Constr.equal ind.mind_user_lc mind_user_lc);
186 check "mind_nrealargs" Int.(equal ind.mind_nrealargs mind_nrealargs);
187 check "mind_nrealdecls" Int.(equal ind.mind_nrealdecls mind_nrealdecls);
188 check "mind_squashed" (check_squashed ind.mind_squashed mind_squashed);
189
190 check "mind_nf_lc" (Array.equal eq_in_context ind.mind_nf_lc mind_nf_lc);
191 (* NB: here syntactic equality is not just an optimisation, we also
192 care about the shape of the terms *)
193
194 check "mind_consnrealargs" (Array.equal Int.equal ind.mind_consnrealargs mind_consnrealargs);
195 check "mind_consnrealdecls" (Array.equal Int.equal ind.mind_consnrealdecls mind_consnrealdecls);
196
197 check "mind_recargs" (Rtree.equal eq_recarg ind.mind_recargs mind_recargs);
198
199 check "mind_relevant" (Sorts.relevance_equal ind.mind_relevance mind_relevance);
200
201 check "mind_nb_args" Int.(equal ind.mind_nb_args mind_nb_args);
202 check "mind_nb_constant" Int.(equal ind.mind_nb_constant mind_nb_constant);
203 check "mind_reloc_tbl" (eq_reloc_tbl ind.mind_reloc_tbl mind_reloc_tbl);
204
205 ()
206
207 let check_inductive env mind mb =
208 let entry = to_entry mind mb in
209 let { mind_packets; mind_finite; mind_hyps; mind_univ_hyps;
210 mind_nparams; mind_nparams_rec; mind_params_ctxt;
211 mind_universes; mind_template; mind_variance; mind_sec_variance;
212 mind_private; mind_typing_flags; }
213 =
214 (* Locally set typing flags for further typechecking *)
215 let env = CheckFlags.set_local_flags mb.mind_typing_flags env in
216 let mib, not_prim_record = Indtypes.check_inductive env ~sec_univs:None mind entry in
217 assert (Option.is_empty not_prim_record);
218 mib
219 in
220 let check = check mind in
221
222 Array.iter2 (check_packet mind) mb.mind_packets mind_packets;
223 check "mind_finite" (mb.mind_finite == mind_finite);
224 check "mind_hyps" (List.is_empty mind_hyps);
225 check "mind_univ_hyps" (UVars.Instance.is_empty mind_univ_hyps);
226 check "mind_nparams" Int.(equal mb.mind_nparams mind_nparams);
227
228 check "mind_nparams_rec" (mb.mind_nparams_rec <= mind_nparams_rec);
229 (* module substitution can increase the real number of recursively
230 uniform parameters, so be tolerant and use [<=]. *)
231
232 check "mind_params_ctxt" (Context.Rel.equal Sorts.relevance_equal Constr.equal mb.mind_params_ctxt mind_params_ctxt);
233 ignore mind_universes; (* Indtypes did the necessary checking *)
234 check "mind_template" (check_template mb.mind_template mind_template);
235 check "mind_variance" (Option.equal (Array.equal UVars.Variance.equal)
236 mb.mind_variance mind_variance);
237 check "mind_sec_variance" (Option.is_empty mind_sec_variance);
238 ignore mind_private; (* passed through Indtypes *)
239
240 ignore mind_typing_flags;
241 (* TODO non oracle flags *)
242
243 add_mind mind mb env
244
245 let check_inductive env mind mb : Environ.env =
246 NewProfile.profile "check_inductive"
247 ~args:(fun () -> [("name", `String (MutInd.to_string mind))])
248 (fun () -> check_inductive env mind mb)
249 ()