Debian Codegraph @

kernel/constant_typing.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 (* Created by Jacek Chrzaszcz, Aug 2002 as part of the implementation of
12 the Coq module system *)
13
14 (* This module provides the main entry points for type-checking basic
15 declarations *)
16
17 open Util
18 open Names
19 open Constr
20 open Declarations
21 open Environ
22 open Entries
23 open UVars
24
25 module NamedDecl = Context.Named.Declaration
26
27 (* Checks the section variables for the body.
28 Returns the closure of the union with the variables in the type.
29 *)
30 let check_section_variables env declared_vars body typ =
31 let env_ids = ids_of_named_context_val (named_context_val env) in
32 Id.Set.iter (fun id -> if not (Id.Set.mem id env_ids) then Type_errors.error_unbound_var env id) declared_vars;
33 if List.is_empty (named_context env) then begin
34 assert (Id.Set.is_empty declared_vars);
35 declared_vars
36 end
37 else
38 let tyvars = global_vars_set env typ in
39 let declared_vars = Environ.really_needed env (Id.Set.union declared_vars tyvars) in
40 let () = match body with
41 | None -> ()
42 | Some body ->
43 let ids_def = global_vars_set env body in
44 let inferred_vars = Environ.really_needed env (Id.Set.union declared_vars ids_def) in
45 if not (Id.Set.subset inferred_vars declared_vars) then
46 Type_errors.error_undeclared_used_variables env ~declared_vars ~inferred_vars
47 in
48 declared_vars
49
50 let compute_section_variables env body typ =
51 if List.is_empty (named_context env) then
52 (* Empty section context: optimization *)
53 Id.Set.empty
54 else
55 let ids =
56 Option.fold_right
57 (fun c -> Id.Set.union (global_vars_set env c))
58 body (global_vars_set env typ) in
59 Environ.really_needed env ids
60
61 let used_section_variables env declared_hyps body typ =
62 let hyps =
63 match declared_hyps with
64 | None -> compute_section_variables env body typ
65 | Some declared -> check_section_variables env declared body typ
66 in
67 (* Order the variables *)
68 List.filter (fun d -> Id.Set.mem (NamedDecl.get_id d) hyps) (Environ.named_context env)
69
70 (* Insertion of constants and parameters in environment. *)
71
72 type 'a effect_handler =
73 env -> Constr.t -> 'a -> (Constr.t * Univ.ContextSet.t * int)
74
75 let skip_trusted_seff sl b e =
76 let rec aux sl b e =
77 let open Context.Rel.Declaration in
78 if Int.equal sl 0 then b, e
79 else match HConstr.kind b with
80 | LetIn (n,c,ty,bo) ->
81 let c = HConstr.self c in
82 let ty = HConstr.self ty in
83 aux (sl - 1) bo (Environ.push_rel (LocalDef (n,c,ty)) e)
84 | App (hd, args) ->
85 let () = assert (Int.equal (Array.length args) 1) in
86 begin match HConstr.kind hd with
87 | Lambda (n,ty,bo) ->
88 let ty = HConstr.self ty in
89 aux (sl - 1) bo (Environ.push_rel (LocalAssum (n,ty)) e)
90 | _ -> assert false
91 end
92 | _ -> assert false
93 in
94 aux sl b e
95
96 type typing_context =
97 TyCtx of Environ.env * unsafe_type_judgment * Id.Set.t * UVars.sort_level_subst * universes
98
99 let process_universes env = function
100 | Entries.Monomorphic_entry ->
101 env, UVars.empty_sort_subst, UVars.Instance.empty, Monomorphic
102 | Entries.Polymorphic_entry uctx ->
103 (** [ctx] must contain local universes, such that it has no impact
104 on the rest of the graph (up to transitivity). *)
105 let inst, auctx = UVars.abstract_universes uctx in
106 let ctx = AbstractContext.repr auctx in
107 let () = check_ucontext ctx env in
108 let env = Environ.push_context ~strict:false ctx env in
109 let usubst = UVars.make_instance_subst inst in
110 env, usubst, UVars.make_abstract_instance auctx, Polymorphic auctx
111
112 let check_primitive_type env op_t u t =
113 let inft = Typeops.type_of_prim_or_type env u op_t in
114 match Conversion.default_conv Conversion.CONV env inft t with
115 | Result.Ok () -> ()
116 | Result.Error () ->
117 Type_errors.error_incorrect_primitive env (make_judge op_t inft) t
118
119 let adjust_primitive_univ_entry p auctx = function
120 | Monomorphic_entry ->
121 assert (AbstractContext.is_empty auctx); (* ensured by ComPrimitive *)
122 Monomorphic_entry
123 | Polymorphic_entry uctx ->
124 assert (not (AbstractContext.is_empty auctx)); (* ensured by ComPrimitive *)
125 (* [push_context] will check that the universes aren't repeated in
126 the instance so comparing the sizes works. No polymorphic
127 primitive uses constraints currently. *)
128 if not (AbstractContext.size auctx = UContext.size uctx
129 && PConstraints.is_empty (UContext.constraints uctx))
130 then CErrors.user_err Pp.(str "Incorrect universes for primitive " ++
131 str (CPrimitives.op_or_type_to_string p));
132 Polymorphic_entry (UContext.refine_names (AbstractContext.names auctx) uctx)
133
134 let infer_primitive env { prim_entry_type = utyp; prim_entry_content = p; } =
135 let open CPrimitives in
136 let auctx = CPrimitives.op_or_type_univs p in
137 let univs, typ =
138 match utyp with
139 | None ->
140 let u = UContext.instance (AbstractContext.repr auctx) in
141 let typ = Typeops.type_of_prim_or_type env u p in
142 let univs = if AbstractContext.is_empty auctx then Monomorphic
143 else Polymorphic auctx
144 in
145 univs, typ
146
147 | Some (typ, univ_entry) ->
148 let univ_entry = adjust_primitive_univ_entry p auctx univ_entry in
149 let env, usubst, u, univs = process_universes env univ_entry in
150 let typ = Vars.subst_univs_level_constr usubst typ in
151 let typ = (Typeops.infer_type env typ).utj_val in
152 let () = check_primitive_type env p u typ in
153 univs, typ
154 in
155 let body = match p with
156 | OT_op op -> Declarations.Primitive op
157 | OT_type _ -> Undef None
158 | OT_const c -> Def (CPrimitives.body_of_prim_const c)
159 in
160 (* Primitives not allowed in sections (checked in safe_typing) *)
161 assert (List.is_empty (named_context env));
162 {
163 const_hyps = [];
164 const_univ_hyps = Instance.empty;
165 const_body = body;
166 const_type = typ;
167 const_body_code = ();
168 const_universes = univs;
169 const_relevance = Sorts.Relevant;
170 const_inline_code = false;
171 const_typing_flags = Environ.typing_flags env;
172 }
173
174 let infer_symbol env { symb_entry_universes; symb_entry_unfold_fix; symb_entry_type } =
175 let env, usubst, _, univs = process_universes env symb_entry_universes in
176 let symb_entry_type = Vars.subst_univs_level_constr usubst symb_entry_type in
177 let j = Typeops.infer env symb_entry_type in
178 let r = Typeops.assumption_of_judgment env j in
179 {
180 const_hyps = [];
181 const_univ_hyps = Instance.empty;
182 const_body = Symbol symb_entry_unfold_fix;
183 const_type = j.uj_val;
184 const_body_code = ();
185 const_universes = univs;
186 const_relevance = r;
187 const_inline_code = false;
188 const_typing_flags = Environ.typing_flags env;
189 }
190
191
192 let make_univ_hyps = function
193 | None -> Instance.empty
194 | Some us -> us
195
196 let infer_parameter ~sec_univs env entry =
197 let env, usubst, _, univs = process_universes env entry.parameter_entry_universes in
198 let typ = Vars.subst_univs_level_constr usubst entry.parameter_entry_type in
199 let j = Typeops.infer env typ in
200 let r = Typeops.assumption_of_judgment env j in
201 let typ = j.uj_val in
202 let undef = Undef entry.parameter_entry_inline_code in
203 let hyps = used_section_variables env entry.parameter_entry_secctx None typ in
204 {
205 const_hyps = hyps;
206 const_univ_hyps = make_univ_hyps sec_univs;
207 const_body = undef;
208 const_type = typ;
209 const_body_code = ();
210 const_universes = univs;
211 const_relevance = r;
212 const_inline_code = false;
213 const_typing_flags = Environ.typing_flags env;
214 }
215
216 let infer_definition ~sec_univs env entry =
217 let env, usubst, _, univs = process_universes env entry.definition_entry_universes in
218 let body = Vars.subst_univs_level_constr usubst entry.definition_entry_body in
219 let hbody = HConstr.of_constr env body in
220 let j = Typeops.infer_hconstr env hbody in
221 let typ = match entry.definition_entry_type with
222 | None ->
223 j.uj_type
224 | Some t ->
225 let t = Vars.subst_univs_level_constr usubst t in
226 let tj = Typeops.infer_type env t in
227 let () = Typeops.check_cast env j DEFAULTcast tj in
228 tj.utj_val
229 in
230 let body = j.uj_val in
231 let hbody = Some hbody in
232 let def = Def body in
233 let hyps = used_section_variables env entry.definition_entry_secctx (Some body) typ in
234 hbody, {
235 const_hyps = hyps;
236 const_univ_hyps = make_univ_hyps sec_univs;
237 const_body = def;
238 const_type = typ;
239 const_body_code = ();
240 const_universes = univs;
241 const_relevance = Relevanceops.relevance_of_term env body;
242 const_inline_code = entry.definition_entry_inline_code;
243 const_typing_flags = Environ.typing_flags env;
244 }
245
246 (** Definition is opaque (Qed), so we delay the typing of its body. *)
247 let infer_opaque ~sec_univs env entry =
248 let env, usubst, _, univs = process_universes env entry.opaque_entry_universes in
249 let typ = Vars.subst_univs_level_constr usubst entry.opaque_entry_type in
250 let typj = Typeops.infer_type env typ in
251 let context = TyCtx (env, typj, entry.opaque_entry_secctx, usubst, univs) in
252 let def = OpaqueDef () in
253 let typ = typj.utj_val in
254 let hyps = used_section_variables env (Some entry.opaque_entry_secctx) None typ in
255 {
256 const_hyps = hyps;
257 const_univ_hyps = make_univ_hyps sec_univs;
258 const_body = def;
259 const_type = typ;
260 const_body_code = ();
261 const_universes = univs;
262 const_relevance = Sorts.relevance_of_sort typj.utj_type;
263 const_inline_code = false;
264 const_typing_flags = Environ.typing_flags env;
265 }, context
266
267 let check_delayed (type a) (handle : a effect_handler) tyenv (body : a proof_output) =
268 let TyCtx (env, tyj, declared, usubst, univs) = tyenv in
269 let ((body, uctx), side_eff) = body in
270 let (body, uctx', valid_signatures) = handle env body side_eff in
271 let uctx = Univ.ContextSet.union uctx uctx' in
272 let env, univs = match univs with
273 | Monomorphic ->
274 assert (UVars.is_empty_sort_subst usubst);
275 push_context_set uctx env, Opaqueproof.PrivateMonomorphic uctx
276 | Polymorphic _ ->
277 let () = assert (Int.equal valid_signatures 0) in
278 let uctx = on_snd (fun cst -> subst_univs_constraints (snd usubst) cst) uctx in
279 push_subgraph uctx env, Opaqueproof.PrivatePolymorphic uctx
280 in
281 (* Note: non-trivial usubst only in polymorphic case *)
282 let body = Vars.subst_univs_level_constr usubst body in
283 let hbody = HConstr.of_constr env body in
284 (* Note: non-trivial trusted side-effects only in monomorphic case *)
285 let () =
286 let eff_body, eff_env = skip_trusted_seff valid_signatures hbody env in
287 let j = Typeops.infer_hconstr eff_env eff_body in
288 let () = assert (HConstr.self eff_body == j.uj_val) in
289 let j = { uj_val = HConstr.self hbody; uj_type = j.uj_type } in
290 Typeops.check_cast eff_env j DEFAULTcast tyj
291 in
292 let declared =
293 if List.is_empty (named_context env) then declared
294 else Environ.really_needed env (Id.Set.union declared (global_vars_set env tyj.utj_val))
295 in
296 let declared' = check_section_variables env declared (Some body) tyj.utj_val in
297 let () = assert (Id.Set.equal declared declared') in
298 let def = HConstr.self hbody in
299 let hbody = Some hbody in
300 hbody, def, univs
301
302 (*s Global and local constant declaration. *)
303
304 let infer_local_assum env t =
305 let j = Typeops.infer env t in
306 let t = Typeops.assumption_of_judgment env j in
307 j.uj_val, t
308
309 let infer_local_def env _id { secdef_body; secdef_type; } =
310 let j = Typeops.infer env secdef_body in
311 let typ = match secdef_type with
312 | None -> j.uj_type
313 | Some typ ->
314 let tj = Typeops.infer_type env typ in
315 let () = Typeops.check_cast env j DEFAULTcast tj in
316 tj.utj_val
317 in
318 let c = j.uj_val in
319 let r = Relevanceops.relevance_of_term env c in
320 c, r, typ