Debian Codegraph @

kernel/discharge.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 Constr
14 open Term
15 open Declarations
16 open UVars
17 open Cooking
18
19 module NamedDecl = Context.Named.Declaration
20
21 let lift_univs info univ_hyps = function
22 | Monomorphic ->
23 assert (UVars.Instance.is_empty univ_hyps);
24 info, univ_hyps, Monomorphic
25 | Polymorphic auctx ->
26 let info, (qn, un), auctx = lift_poly_univs info auctx in
27 let univ_hyps =
28 let open UVars.Instance in
29 let qs, us = to_array univ_hyps in
30 let qs = Array.sub qs 0 (Array.length qs - qn) in
31 let us = Array.sub us 0 (Array.length us - un) in
32 of_array (qs,us)
33 in
34 info, univ_hyps, Polymorphic auctx
35
36 (********************************)
37 (* Discharging opaque proof terms *)
38
39 let lift_private_univs info = function
40 | Opaqueproof.PrivateMonomorphic () as priv ->
41 let () = lift_private_mono_univs info () in
42 priv
43 | Opaqueproof.PrivatePolymorphic ctx ->
44 let ctx = lift_private_poly_univs info ctx in
45 Opaqueproof.PrivatePolymorphic ctx
46
47 let cook_opaque_proofterm info c =
48 let fold info (c, priv) =
49 let priv = lift_private_univs info priv in
50 let c = abstract_as_body (create_cache info) c in
51 (c, priv)
52 in
53 List.fold_right fold info c
54
55 (********************************)
56 (* Discharging constant *)
57
58 let cook_constant _env info cb =
59 (* Adjust the info so that it is meaningful under the block of quantified universe binders *)
60 let info, univ_hyps, univs = lift_univs info cb.const_univ_hyps cb.const_universes in
61 let cache = create_cache info in
62 let map c = abstract_as_body cache c in
63 let body = match cb.const_body with
64 | Undef _ as x -> x
65 | Def cs -> Def (map cs)
66 | OpaqueDef o ->
67 OpaqueDef (Opaqueproof.discharge_opaque info o)
68 | Primitive _ -> CErrors.anomaly (Pp.str "Primitives cannot be cooked")
69 | Symbol _ -> CErrors.anomaly (Pp.str "Symbols cannot be cooked")
70 in
71 let typ = abstract_as_type cache cb.const_type in
72 let names = names_info info in
73 let hyps = List.filter (fun d -> not (Id.Set.mem (NamedDecl.get_id d) names)) cb.const_hyps in
74 {
75 const_hyps = hyps;
76 const_univ_hyps = univ_hyps;
77 const_body = body;
78 const_type = typ;
79 const_body_code = ();
80 const_universes = univs;
81 const_relevance = lift_relevance info cb.const_relevance;
82 const_inline_code = cb.const_inline_code;
83 const_typing_flags = cb.const_typing_flags;
84 }
85
86 (********************************)
87 (* Discharging mutual inductive *)
88
89 let cook_rel_context cache ctx =
90 (* Dealing with substitutions between contexts is too annoying, so
91 we reify [ctx] into a big [forall] term and work on that. *)
92 let t = it_mkProd_or_LetIn mkProp ctx in
93 let t = abstract_as_type cache t in
94 let ctx, t = decompose_prod_decls t in
95 assert (Constr.equal t mkProp);
96 ctx
97
98 let cook_lc cache ~ntypes t =
99 (* Expand the recursive call to the inductive types *)
100 let diff = Context.Rel.length (rel_context_of_cooking_cache cache) in
101 let subs = List.init ntypes (fun k -> mkApp (mkRel (k+diff+1), instance_of_cooking_cache cache)) in
102 let t = Vars.substl subs t in
103 (* Apply the abstraction *)
104 abstract_as_type cache t
105
106 let cook_projection cache ~params t =
107 let t = mkArrowR mkProp t in (* dummy type standing in for the inductive *)
108 let t = it_mkProd_or_LetIn t params in
109 let t = abstract_as_type cache t in
110 let nrels = Context.Rel.nhyps (rel_context_of_cooking_cache cache) in
111 let _, t = decompose_prod_n_decls (Context.Rel.length params + 1 + nrels) t in
112 t
113
114 let lift_squashed info = let open Declarations in function
115 | AlwaysSquashed -> AlwaysSquashed
116 | SometimesSquashed s ->
117 let s = Sorts.Quality.Set.fold (fun x acc ->
118 let x = lift_quality info x in
119 Sorts.Quality.Set.add x acc)
120 s Sorts.Quality.Set.empty
121 in
122 SometimesSquashed s
123
124 let cook_one_ind info cache ~params ~ntypes mip =
125 let mind_user_arity = abstract_as_type cache mip.mind_user_arity in
126 let mind_sort = abstract_as_sort cache mip.mind_sort in
127 let mind_arity_ctxt = cook_rel_context cache mip.mind_arity_ctxt in
128 let mind_user_lc = Array.map (cook_lc cache ~ntypes) mip.mind_user_lc in
129 let mind_nf_lc = Array.map (fun (ctx,t) ->
130 let lc = it_mkProd_or_LetIn t ctx in
131 let lc = cook_lc cache ~ntypes lc in
132 decompose_prod_decls lc)
133 mip.mind_nf_lc
134 in
135 let mind_record = match mip.mind_record with
136 | NotRecord -> NotRecord
137 | FakeRecord -> FakeRecord
138 | PrimRecord ({ relevances; tys; _ } as pinfo) ->
139 let tys = Array.map (cook_projection cache ~params) tys in
140 let relevances = Array.map (lift_relevance info) relevances in
141 PrimRecord { pinfo with relevances ; tys }
142 in
143 let squashed = Option.map (lift_squashed info) mip.mind_squashed in
144
145 { mind_typename = mip.mind_typename;
146 mind_record;
147 mind_arity_ctxt;
148 mind_user_arity;
149 mind_sort;
150 mind_consnames = mip.mind_consnames;
151 mind_user_lc;
152 mind_nrealargs = mip.mind_nrealargs;
153 mind_nrealdecls = mip.mind_nrealdecls;
154 mind_squashed = squashed;
155 mind_nf_lc;
156 mind_consnrealargs = mip.mind_consnrealargs;
157 mind_consnrealdecls = mip.mind_consnrealdecls;
158 mind_recargs = mip.mind_recargs;
159 mind_relevance = lift_relevance info mip.mind_relevance;
160 mind_nb_constant = mip.mind_nb_constant;
161 mind_nb_args = mip.mind_nb_args;
162 mind_reloc_tbl = mip.mind_reloc_tbl;
163 }
164
165 let cook_inductive info mib =
166 let info, univ_hyps, mind_universes = lift_univs info mib.mind_univ_hyps mib.mind_universes in
167 let cache = create_cache info in
168 let nnewparams = Context.Rel.nhyps (rel_context_of_cooking_cache cache) in
169 let mind_params_ctxt = cook_rel_context cache mib.mind_params_ctxt in
170 let ntypes = Declareops.mind_ntypes mib in
171 let mind_packets = Array.map (cook_one_ind info cache ~params:mib.mind_params_ctxt ~ntypes) mib.mind_packets in
172 let names = names_info info in
173 let mind_hyps =
174 List.filter (fun d -> not (Id.Set.mem (NamedDecl.get_id d) names))
175 mib.mind_hyps
176 in
177 let mind_variance, mind_sec_variance =
178 match mib.mind_variance, mib.mind_sec_variance with
179 | None, None -> None, None
180 | None, Some _ | Some _, None -> assert false
181 | Some variance, Some sec_variance ->
182 (* no variance for qualities *)
183 let ulen = snd (AbstractContext.size (universe_context_of_cooking_info info)) in
184 let sec_variance, newvariance =
185 Array.chop (Array.length sec_variance - ulen) sec_variance
186 in
187 Some (Array.append newvariance variance), Some sec_variance
188 in
189 let mind_template = match mib.mind_template with
190 | None -> None
191 | Some {template_param_arguments=levels; template_context; template_concl; template_defaults;} ->
192 let sec_levels = List.make (Context.Rel.nhyps (rel_context_of_cooking_cache cache)) None in
193 let levels = List.rev_append sec_levels levels in
194 Some {template_param_arguments=levels; template_context; template_concl; template_defaults}
195 in
196 {
197 mind_packets;
198 mind_finite = mib.mind_finite;
199 mind_hyps;
200 mind_univ_hyps = univ_hyps;
201 mind_nparams = mib.mind_nparams + nnewparams;
202 mind_nparams_rec = mib.mind_nparams_rec + nnewparams;
203 mind_params_ctxt;
204 mind_universes;
205 mind_template;
206 mind_variance;
207 mind_sec_variance;
208 mind_private = mib.mind_private;
209 mind_typing_flags = mib.mind_typing_flags;
210 }
211
212 let cook_rel_context info ctx =
213 cook_rel_context (create_cache info) ctx