Debian Codegraph @

checker/mod_checking.ml

raw symbols SCIP
1 open Pp
2 open Util
3 open Names
4 open Conversion
5 open Declarations
6 open Mod_declarations
7 open Environ
8
9 (** {6 Checking constants } *)
10
11 let indirect_accessor : (Opaqueproof.opaque -> Opaqueproof.opaque_proofterm) ref =
12 ref (fun _ -> assert false)
13
14 let set_indirect_accessor f = indirect_accessor := f
15
16 let register_opacified_constant env opac kn cb =
17 let rec gather_consts s c =
18 match Constr.kind c with
19 | Constr.Const (c, _) -> Cset.add c s
20 | _ -> Constr.fold gather_consts s c
21 in
22 let wo_body =
23 Cset.fold
24 (fun kn s ->
25 if Declareops.constant_has_body (lookup_constant kn env) then s else
26 match Cmap.find_opt kn opac with
27 | None -> Cset.add kn s
28 | Some s' -> Cset.union s' s)
29 (gather_consts Cset.empty cb)
30 Cset.empty
31 in
32 Cmap.add kn wo_body opac
33
34 exception BadConstant of Constant.t * Pp.t
35
36 let check_constant_declaration env opac kn cb opacify =
37 Flags.if_verbose Feedback.msg_notice (str " checking cst:" ++ Constant.print kn);
38 let env = CheckFlags.set_local_flags cb.const_typing_flags env in
39 let poly, env =
40 match cb.const_universes with
41 | Monomorphic ->
42 (* Monomorphic universes are stored at the library level, the
43 ones in const_universes should not be needed *)
44 false, env
45 | Polymorphic auctx ->
46 let ctx = UVars.AbstractContext.repr auctx in
47 (* [env] contains De Bruijn universe variables *)
48 let () = check_ucontext ctx env in
49 let env = push_context ~strict:false ctx env in
50 true, env
51 in
52 let ty = cb.const_type in
53 let jty = Typeops.infer_type env ty in
54 if not (Sorts.relevance_equal cb.const_relevance (Sorts.relevance_of_sort jty.utj_type))
55 then raise Pp.(BadConstant (kn, str "incorrect const_relevance"));
56 let body, env = match cb.const_body with
57 | Undef _ | Primitive _ | Symbol _ -> None, env
58 | Def c -> Some c, env
59 | OpaqueDef o ->
60 let c, u = !indirect_accessor o in
61 let env = match u, cb.const_universes with
62 | Opaqueproof.PrivateMonomorphic (), Monomorphic -> env
63 | Opaqueproof.PrivatePolymorphic local, Polymorphic _ ->
64 push_subgraph local env
65 | _ -> assert false
66 in
67 Some c, env
68 in
69 let () =
70 match body with
71 | Some bd ->
72 let j = Typeops.infer env bd in
73 begin match conv_leq env j.uj_type ty with
74 | Result.Ok () -> ()
75 | Result.Error () -> Type_errors.error_actual_type env j ty
76 end
77 | None -> ()
78 in
79 match body with
80 | Some body when opacify -> register_opacified_constant env opac kn body
81 | Some _ | None -> opac
82
83 let check_constant_declaration env opac kn cb opacify =
84 let opac = NewProfile.profile "check_constant" ~args:(fun () ->
85 [("name", `String (Constant.to_string kn))])
86 (fun () -> check_constant_declaration env opac kn cb opacify)
87 ()
88 in
89 Environ.add_constant kn cb env, opac
90
91 let check_quality_mask env qmask lincheck =
92 let open Sorts.Quality in
93 match qmask with
94 | PQConstant QSProp -> if Environ.sprop_allowed env then lincheck else Type_errors.error_not_allowed_sprop env
95 | PQConstant (QProp | QType) -> lincheck
96 | PQVar qio -> Partial_subst.maybe_add_quality qio () lincheck
97
98 let check_instance_mask env udecl umask lincheck =
99 match udecl, umask with
100 | Monomorphic, ([||], [||]) -> lincheck
101 | Polymorphic uctx, (qmask, umask) ->
102 let lincheck = Array.fold_left_i (fun i lincheck mask -> check_quality_mask env mask lincheck) lincheck qmask in
103 let lincheck = Array.fold_left_i (fun i lincheck mask -> Partial_subst.maybe_add_univ mask () lincheck) lincheck umask in
104 if (Array.length qmask, Array.length umask) <> UVars.AbstractContext.size uctx then CErrors.anomaly Pp.(str "Bad univ mask length.");
105 lincheck
106 | _ -> CErrors.anomaly Pp.(str "Bad univ mask length.")
107
108 let rec get_holes_profiles env nargs ndecls lincheck el =
109 List.fold_left (get_holes_profiles_elim env nargs ndecls) lincheck el
110
111 and get_holes_profiles_elim env nargs ndecls lincheck = function
112 | PEApp args -> Array.fold_left (get_holes_profiles_parg env nargs ndecls) lincheck args
113 | PECase (ind, ret, brs) ->
114 let mib, mip = Inductive.lookup_mind_specif env ind in
115 let lincheck = get_holes_profiles_parg env (nargs + mip.mind_nrealargs + 1) (ndecls + mip.mind_nrealdecls + 1) lincheck ret in
116 Array.fold_left3 (fun lincheck nargs_b ndecls_b -> get_holes_profiles_parg env (nargs + nargs_b) (ndecls + ndecls_b) lincheck) lincheck mip.mind_consnrealargs mip.mind_consnrealdecls brs
117 | PEProj proj ->
118 let () = lookup_projection (Projection.make proj false) env |> ignore in
119 lincheck
120
121 and get_holes_profiles_headelim env nargs ndecls lincheck (h, el) =
122 let lincheck = get_holes_profiles_head env nargs ndecls lincheck h in
123 get_holes_profiles env nargs ndecls lincheck el
124
125 and get_holes_profiles_parg env nargs ndecls lincheck = function
126 | EHoleIgnored -> lincheck
127 | EHole i -> Partial_subst.add_term i nargs lincheck
128 | ERigid hel ->
129 get_holes_profiles_headelim env nargs ndecls lincheck hel
130
131 and get_holes_profiles_head env nargs ndecls lincheck = function
132 | PHRel n -> if n <= ndecls then lincheck else Type_errors.error_unbound_rel env n
133 | PHSymbol (c, u) ->
134 let cb = lookup_constant c env in
135 check_instance_mask env cb.const_universes u lincheck
136 | PHConstr (c, u) ->
137 let (mib, _) = Inductive.lookup_mind_specif env (inductive_of_constructor c) in
138 check_instance_mask env mib.mind_universes u lincheck
139 | PHInd (ind, u) ->
140 let (mib, _) = Inductive.lookup_mind_specif env ind in
141 check_instance_mask env mib.mind_universes u lincheck
142 | PHInt _ | PHFloat _ | PHString _ -> lincheck
143 | PHSort PSSProp -> if Environ.sprop_allowed env then lincheck else Type_errors.error_not_allowed_sprop env
144 | PHSort PSType io -> Partial_subst.maybe_add_univ io () lincheck
145 | PHSort PSQSort (qio, uio) ->
146 lincheck
147 |> Partial_subst.maybe_add_quality qio ()
148 |> Partial_subst.maybe_add_univ uio ()
149 | PHSort _ -> lincheck
150 | PHLambda (tys, bod) ->
151 let lincheck = Array.fold_left_i (fun i -> get_holes_profiles_parg env (nargs + i) (ndecls + i)) lincheck tys in
152 let lincheck = get_holes_profiles_headelim env (nargs + Array.length tys) (ndecls + Array.length tys) lincheck bod in
153 lincheck
154 | PHProd (tys, bod) ->
155 let lincheck = Array.fold_left_i (fun i -> get_holes_profiles_parg env (nargs + i) (ndecls + i)) lincheck tys in
156 let lincheck = get_holes_profiles_parg env (nargs + Array.length tys) (ndecls + Array.length tys) lincheck bod in
157 lincheck
158
159 let check_rhs env holes_profile rhs =
160 let rec check i c = match Constr.kind c with
161 | App (f, args) when Constr.isRel f ->
162 let n = Constr.destRel f in
163 if n <= i then () else
164 if n - i > Array.length holes_profile then CErrors.anomaly Pp.(str "Malformed right-hand-side substitution site");
165 let d = holes_profile.(n-i-1) in
166 if Array.length args >= d then () else CErrors.anomaly Pp.(str "Malformed right-hand-side substitution site")
167 | Rel n when n > i ->
168 if n - i > Array.length holes_profile then CErrors.anomaly Pp.(str "Malformed right-hand-side substitution site");
169 let d = holes_profile.(n-i-1) in
170 if d = 0 then () else CErrors.anomaly Pp.(str "Malformed right-hand-side substitution site")
171 | _ -> Constr.iter_with_binders succ check i c
172 in
173 check 0 rhs
174
175 let check_rewrite_rule env lab i (symb, rule) =
176 Flags.if_verbose Feedback.msg_notice (str " checking rule:" ++ Id.print lab ++ str"#" ++ Pp.int i);
177 let { nvars; lhs_pat; rhs } = rule in
178 let symb_cb = Environ.lookup_constant symb env in
179 let () =
180 match symb_cb.const_body with Symbol _ -> ()
181 | _ -> ignore @@ invalid_arg "Rule defined on non-symbol"
182 in
183 let lincheck = Partial_subst.make nvars in
184 let lincheck = check_instance_mask env symb_cb.const_universes (fst lhs_pat) lincheck in
185 let lincheck = get_holes_profiles env 0 0 lincheck (snd lhs_pat) in
186 let holes_profile, _, _ = Partial_subst.to_arrays lincheck in
187 let () = check_rhs env holes_profile rhs in
188 ()
189
190 let check_rewrite_rules_body env lab rrb =
191 List.iteri (check_rewrite_rule env lab) rrb.rewrules_rules
192
193 (** {6 Checking modules } *)
194
195 (** We currently ignore the [mod_type_alg] and [typ_expr_alg] fields.
196 The only delicate part is when [mod_expr] is an algebraic expression :
197 we need to expand it before checking it is indeed a subtype of [mod_type].
198 Fortunately, [mod_expr] cannot contain any [MEwith]. *)
199
200 let lookup_module mp env =
201 try Environ.lookup_module mp env
202 with Not_found ->
203 failwith ("Unknown module: "^ModPath.to_string mp)
204
205 let mk_mtb sign delta = Mod_declarations.make_module_type sign delta
206
207 let rec collect_constants_without_body sign mp accu =
208 let collect_field s lab = function
209 | SFBconst cb ->
210 let c = Constant.make2 mp lab in
211 if Declareops.constant_has_body cb then s else Cset.add c s
212 | SFBmodule msb -> collect_constants_without_body (mod_type msb) (MPdot(mp,lab)) s
213 | SFBmind _ | SFBrules _ | SFBmodtype _ -> s in
214 match sign with
215 | MoreFunctor _ -> Cset.empty (* currently ignored *)
216 | NoFunctor struc ->
217 List.fold_left (fun s (lab,mb) -> collect_field s lab mb) accu struc
218
219 let rec check_mexpr env opac mse mp_mse res = match mse with
220 | MEident mp ->
221 let mb = lookup_module mp env in
222 let mb = Modops.strengthen_and_subst_module_body mp mb mp_mse false in
223 mod_type mb, mod_delta mb
224 | MEapply (f,mp) ->
225 let sign, delta = check_mexpr env opac f mp_mse res in
226 let farg_id, farg_b, fbody_b = Modops.destr_functor sign in
227 let state = (Environ.universes env, Conversion.checked_universes) in
228 let _ : UGraph.t = Subtyping.check_subtypes state env mp (MPbound farg_id) farg_b in
229 let mp_delta =
230 let mb = lookup_module mp env in
231 match mod_type mb with
232 | NoFunctor _ -> mod_delta mb
233 | MoreFunctor _ -> Mod_subst.empty_delta_resolver mp
234 in
235 let subst = Mod_subst.map_mbid farg_id mp mp_delta in
236 Modops.subst_signature subst mp_mse fbody_b, Mod_subst.subst_codom_delta_resolver subst delta
237 | MEwith _ -> CErrors.user_err Pp.(str "Unsupported 'with' constraint in module implementation")
238
239 let rec check_mexpression env opac sign mbtyp mp_mse res = match sign with
240 | MEMoreFunctor body ->
241 let arg_id, mtb, mbtyp = Modops.destr_functor mbtyp in
242 let env' = Modops.add_module_parameter arg_id mtb env in
243 let body, delta = check_mexpression env' opac body mbtyp mp_mse res in
244 MoreFunctor(arg_id,mtb,body), delta
245 | MENoFunctor me -> check_mexpr env opac me mp_mse res
246
247 let rec check_module env opac mp mb opacify =
248 Flags.if_verbose Feedback.msg_notice (str " checking module: " ++ str (ModPath.to_string mp));
249 let env = Modops.add_retroknowledge (mod_retroknowledge mb) env in
250 let delta_mb = mod_delta mb in
251 let opac =
252 check_signature env opac (mod_type mb) mp delta_mb opacify
253 in
254 let optsign, opac = match Mod_declarations.mod_expr mb with
255 | Struct (reso, sign_struct) ->
256 let opacify = collect_constants_without_body (mod_type mb) mp opacify in
257 (* TODO: a bit wasteful, we recheck the types of parameters twice *)
258 let sign_struct = Modops.annotate_struct_body sign_struct (mod_type mb) in
259 let opac = check_signature env opac sign_struct mp reso opacify in
260 Some (sign_struct, reso), opac
261 | Algebraic me -> Some (check_mexpression env opac me (mod_type mb) mp delta_mb), opac
262 | Abstract|FullStruct -> None, opac
263 in
264 let () = match optsign with
265 | None -> ()
266 | Some (sign,delta) ->
267 let mtb1 = mk_mtb sign delta
268 and mtb2 = mk_mtb (mod_type mb) delta_mb in
269 let state = (Environ.universes env, Conversion.checked_universes) in
270 let env = Modops.add_module mp (module_body_of_type mtb1) env in
271 let _ : UGraph.t = Subtyping.check_subtypes state env mp mp mtb2 in
272 ()
273 in
274 opac
275
276 and check_module_type env mp mty =
277 Flags.if_verbose Feedback.msg_notice (str " checking module type: " ++ str (ModPath.to_string @@ mp));
278 let _ : _ Cmap.t =
279 check_signature env Cmap.empty (mod_type mty) mp (mod_delta mty) Cset.empty in
280 ()
281
282 and check_structure_field env opac mp lab res opacify = function
283 | SFBconst cb ->
284 let kn = KerName.make mp lab in
285 let kn = Mod_subst.constant_of_delta_kn res kn in
286 check_constant_declaration env opac kn cb (Cset.mem kn opacify)
287 | SFBmind mib ->
288 let kn = KerName.make mp lab in
289 let kn = Mod_subst.mind_of_delta_kn res kn in
290 CheckInductive.check_inductive env kn mib, opac
291 | SFBmodule msb ->
292 let mp = MPdot(mp, lab) in
293 let opac = check_module env opac mp msb opacify in
294 Modops.add_module mp msb env, opac
295 | SFBmodtype mty ->
296 let mp = MPdot (mp, lab) in
297 let () = check_module_type env mp mty in
298 add_modtype mp mty env, opac
299 | SFBrules rrb ->
300 check_rewrite_rules_body env lab rrb;
301 Environ.add_rewrite_rules rrb.rewrules_rules env, opac
302
303 and check_signature env opac sign mp_mse res opacify = match sign with
304 | MoreFunctor (arg_id, mtb, body) ->
305 let () = check_module_type env (MPbound arg_id) mtb in
306 let env' = Modops.add_module_parameter arg_id mtb env in
307 let opac = check_signature env' opac body mp_mse res Cset.empty in
308 opac
309 | NoFunctor struc ->
310 let (_:env), opac = List.fold_left (fun (env, opac) (lab,mb) ->
311 check_structure_field env opac mp_mse lab res opacify mb) (env, opac) struc
312 in
313 opac
314
315 let check_module env opac mp mb =
316 NewProfile.profile "check_module"
317 ~args:(fun () -> [("name", `String (ModPath.to_string mp))])
318 (fun () -> check_module env opac mp mb Cset.empty)
319 ()