Debian Codegraph @

kernel/section.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 Util
12 open Names
13 open Univ
14 open UVars
15 open Cooking
16
17 module NamedDecl = Context.Named.Declaration
18
19 type section_entry =
20 | SecDefinition of Constant.t
21 | SecInductive of MutInd.t
22
23 type 'a t = {
24 prev : 'a t option;
25 (** Section surrounding the current one *)
26 entries : section_entry list;
27 (** Global declarations introduced in the section *)
28 context : Constr.named_context;
29 (** Declarations local to the section, intended to be interleaved
30 with global declarations *)
31 mono_universes : Univ.ContextSet.t;
32 (** Global universes introduced in the section *)
33 poly_universes : UContext.t;
34 (** Universes local to the section *)
35 all_poly_univs : Instance.t;
36 (** All polymorphic universes, including from previous sections. *)
37 has_poly_univs : bool;
38 (** Are there polymorphic universes or constraints, including in previous sections. *)
39 expand_info_map : expand_info;
40 (** Tells how to re-instantiate global declarations when they are
41 generalized *)
42 cooking_info_map : cooking_info entry_map;
43 custom : 'a;
44 }
45
46 let rec depth sec = 1 + match sec.prev with None -> 0 | Some prev -> depth prev
47
48 let has_poly_univs sec = sec.has_poly_univs
49
50 let all_poly_univs sec = sec.all_poly_univs
51
52 let map_custom f sec = {sec with custom = f sec.custom}
53
54 let add_emap e v (cmap, imap) = match e with
55 | SecDefinition con -> (Cmap_env.add con v cmap, imap)
56 | SecInductive ind -> (cmap, Mindmap_env.add ind v imap)
57
58 let push_local_universe_context ctx sec =
59 if UContext.is_empty ctx then sec
60 else
61 let sctx = sec.poly_universes in
62 let poly_universes = UContext.union sctx ctx in
63 let all_poly_univs = Instance.append sec.all_poly_univs (UContext.instance ctx) in
64 { sec with poly_universes; all_poly_univs; has_poly_univs = true }
65
66 let is_polymorphic_univ u sec =
67 let _, us = Instance.to_array sec.all_poly_univs in
68 Array.exists (fun u' -> Level.equal u u') us
69
70 let push_level_constraints uctx sec =
71 if sec.has_poly_univs &&
72 UnivConstraints.exists
73 (fun (l,_,r) -> is_polymorphic_univ l sec || is_polymorphic_univ r sec)
74 (snd uctx)
75 then CErrors.user_err
76 Pp.(str "Cannot add monomorphic constraints which refer to section polymorphic universes.");
77 let uctx' = sec.mono_universes in
78 let mono_universes = Univ.ContextSet.union uctx uctx' in
79 { sec with mono_universes }
80
81 let open_section ~custom prev =
82 {
83 prev;
84 context = [];
85 mono_universes = Univ.ContextSet.empty;
86 poly_universes = UContext.empty;
87 all_poly_univs = Option.cata (fun sec -> sec.all_poly_univs) Instance.empty prev;
88 has_poly_univs = Option.cata has_poly_univs false prev;
89 entries = [];
90 expand_info_map = (Cmap_env.empty, Mindmap_env.empty);
91 cooking_info_map = (Cmap_env.empty, Mindmap_env.empty);
92 custom = custom;
93 }
94
95 let close_section sec =
96 sec.prev, sec.entries, sec.mono_universes, sec.custom
97
98 let push_local d sec =
99 { sec with context = d :: sec.context }
100
101 let extract_hyps vars used =
102 (* Only keep the part that is used by the declaration *)
103 List.filter (fun d -> Id.Set.mem (NamedDecl.get_id d) used) vars
104
105 let segment_of_entry env e uctx sec =
106 let hyps = match e with
107 | SecDefinition con -> (Environ.lookup_constant con env).Declarations.const_hyps
108 | SecInductive mind -> (Environ.lookup_mind mind env).Declarations.mind_hyps
109 in
110 let hyps = Context.Named.to_vars hyps in
111 (* [sec.context] are the named hypotheses, [hyps] the subset that is
112 declared by the global *)
113 let ctx = extract_hyps sec.context hyps in
114 (* Add recursive calls: projections are recursively referring to the
115 mind they belong to *)
116 let recursive = match e with
117 | SecDefinition _ -> None
118 | SecInductive ind -> Some ind
119 in
120 Cooking.make_cooking_info ~recursive sec.expand_info_map ctx uctx
121
122 let push_global env ~poly e sec =
123 if has_poly_univs sec && not poly
124 then CErrors.user_err
125 Pp.(str "Cannot add a universe monomorphic declaration when \
126 section polymorphic universes are present.")
127 else
128 let cooking_info, abstr_inst_info = segment_of_entry env e sec.poly_universes sec in
129 let cooking_info_map = add_emap e cooking_info sec.cooking_info_map in
130 let expand_info_map = add_emap e abstr_inst_info sec.expand_info_map in
131 { sec with entries = e :: sec.entries; expand_info_map; cooking_info_map }
132
133 let segment_of_constant con sec = Cmap_env.find con (fst sec.cooking_info_map)
134 let segment_of_inductive con sec = Mindmap_env.find con (snd sec.cooking_info_map)
135
136 let is_in_section _env gr sec =
137 let open GlobRef in
138 match gr with
139 | VarRef id ->
140 let vars = sec.context in
141 List.exists (fun decl -> Id.equal id (NamedDecl.get_id decl)) vars
142 | ConstRef con ->
143 Cmap_env.mem con (fst sec.expand_info_map)
144 | IndRef (ind, _) | ConstructRef ((ind, _), _) ->
145 Mindmap_env.mem ind (snd sec.expand_info_map)