Debian Codegraph @

plugins/funind/indfun_common.ml

raw symbols SCIP
1 open Names
2 open Pp
3 open Constr
4 open Libnames
5
6 let mk_prefix pre id = Id.of_string (pre ^ Id.to_string id)
7 let mk_rel_id = mk_prefix "R_"
8 let mk_correct_id id = Nameops.add_suffix (mk_rel_id id) "_correct"
9 let mk_complete_id id = Nameops.add_suffix (mk_rel_id id) "_complete"
10 let mk_equation_id id = Nameops.add_suffix id "_equation"
11
12 let fresh_id avoid s =
13 Namegen.next_ident_away_in_goal (Global.env ()) (Id.of_string s) (Id.Set.of_list avoid)
14
15 let fresh_name avoid s = Name (fresh_id avoid s)
16
17 let get_name avoid ?(default = "H") = function
18 | Anonymous -> fresh_name avoid default
19 | Name n -> Name n
20
21 let array_get_start a = Array.init (Array.length a - 1) (fun i -> a.(i))
22 let locate qid = Nametab.locate qid
23
24 let locate_ind ref =
25 match locate ref with GlobRef.IndRef x -> x | _ -> raise Not_found
26
27 let locate_constant ref =
28 match locate ref with GlobRef.ConstRef x -> x | _ -> raise Not_found
29
30 let locate_with_msg msg f x = try f x with Not_found -> CErrors.user_err msg
31
32 let filter_map filter f =
33 let rec it = function
34 | [] -> []
35 | e :: l -> if filter e then f e :: it l else it l
36 in
37 it
38
39 let chop_rlambda_n =
40 let rec chop_lambda_n acc n rt =
41 if n == 0 then (List.rev acc, rt)
42 else
43 match DAst.get rt with
44 | Glob_term.GLambda (name, _, k, t, b) ->
45 chop_lambda_n ((name, t, None) :: acc) (n - 1) b
46 | Glob_term.GLetIn (name, _, v, t, b) ->
47 chop_lambda_n ((name, v, t) :: acc) (n - 1) b
48 | _ ->
49 CErrors.user_err
50 (str "chop_rlambda_n: Not enough Lambdas")
51 in
52 chop_lambda_n []
53
54 let chop_rprod_n =
55 let rec chop_prod_n acc n rt =
56 if n == 0 then (List.rev acc, rt)
57 else
58 match DAst.get rt with
59 | Glob_term.GProd (name, _, k, t, b) ->
60 chop_prod_n ((name, t) :: acc) (n - 1) b
61 | _ ->
62 CErrors.user_err
63 (str "chop_rprod_n: Not enough products")
64 in
65 chop_prod_n []
66
67 let list_union_eq eq_fun l1 l2 =
68 let rec urec = function
69 | [] -> l2
70 | a :: l -> if List.exists (eq_fun a) l2 then urec l else a :: urec l
71 in
72 urec l1
73
74 let list_add_set_eq eq_fun x l = if List.exists (eq_fun x) l then l else x :: l
75 let rocq_constant s = UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref s
76
77 let find_reference sl s =
78 let dp = Names.DirPath.make (List.rev_map Id.of_string sl) in
79 Nametab.locate (make_qualid dp (Id.of_string s))
80
81 let eq = lazy (EConstr.of_constr (rocq_constant "core.eq.type"))
82 let refl_equal = lazy (EConstr.of_constr (rocq_constant "core.eq.refl"))
83
84 let without_implicit_declarations f () =
85 let old_implicit_args = Impargs.is_implicit_args ()
86 and old_strict_implicit_args = Impargs.is_strict_implicit_args ()
87 and old_contextual_implicit_args = Impargs.is_contextual_implicit_args () in
88 let reset () =
89 Impargs.make_implicit_args old_implicit_args;
90 Impargs.make_strict_implicit_args old_strict_implicit_args;
91 Impargs.make_contextual_implicit_args old_contextual_implicit_args;
92 Dumpglob.pop_output()
93 in
94 Impargs.make_implicit_args false;
95 Impargs.make_strict_implicit_args false;
96 Impargs.make_contextual_implicit_args false;
97 Dumpglob.pause();
98 Util.try_finally f () reset ()
99
100 let full_detype_flags () =
101 let flags = PrintingFlags.Detype.current() in
102 PrintingFlags.Detype.make_raw { flags with universes = true; }
103
104 let full_extern_flags () =
105 let flags = PrintingFlags.Extern.current() in
106 let flags = PrintingFlags.Extern.make_raw flags in
107 { flags with depth = None }
108
109 let extern_env_full_printing () =
110 Constrextern.empty_extern_env ~flags:(full_extern_flags())
111
112 let full_printing_flags () =
113 { PrintingFlags.detype = full_detype_flags(); extern = full_extern_flags() }
114
115 (**********************)
116
117 type function_info =
118 { function_constant : Constant.t
119 ; graph_ind : inductive
120 ; equation_lemma : Constant.t option
121 ; correctness_lemma : Constant.t option
122 ; completeness_lemma : Constant.t option
123 ; rect_lemma : Constant.t option
124 ; rec_lemma : Constant.t option
125 ; prop_lemma : Constant.t option
126 ; sprop_lemma : Constant.t option
127 ; is_general : bool
128 (* Has this function been defined using general recursive definition *)
129 }
130
131 (* type function_db = function_info list *)
132
133 (* let function_table = ref ([] : function_db) *)
134
135 let from_function = Summary.ref Cmap_env.empty ~name:"functions_db_fn"
136 let from_graph = Summary.ref Indmap_env.empty ~name:"functions_db_gr"
137
138 (*
139 let rec do_cache_info finfo = function
140 | [] -> raise Not_found
141 | (finfo'::finfos as l) ->
142 if finfo' == finfo then l
143 else if finfo'.function_constant = finfo.function_constant
144 then finfo::finfos
145 else
146 let res = do_cache_info finfo finfos in
147 if res == finfos then l else finfo'::l
148
149
150 let cache_Function (_,(finfos)) =
151 let new_tbl =
152 try do_cache_info finfos !function_table
153 with Not_found -> finfos::!function_table
154 in
155 if new_tbl != !function_table
156 then function_table := new_tbl
157 *)
158
159 let cache_Function finfos =
160 from_function := Cmap_env.add finfos.function_constant finfos !from_function;
161 from_graph := Indmap_env.add finfos.graph_ind finfos !from_graph
162
163 let subst_Function (subst, finfos) =
164 let do_subst_con c = Mod_subst.subst_constant subst c
165 and do_subst_ind i = Mod_subst.subst_ind subst i in
166 let function_constant' = do_subst_con finfos.function_constant in
167 let graph_ind' = do_subst_ind finfos.graph_ind in
168 let equation_lemma' = Option.Smart.map do_subst_con finfos.equation_lemma in
169 let correctness_lemma' =
170 Option.Smart.map do_subst_con finfos.correctness_lemma
171 in
172 let completeness_lemma' =
173 Option.Smart.map do_subst_con finfos.completeness_lemma
174 in
175 let rect_lemma' = Option.Smart.map do_subst_con finfos.rect_lemma in
176 let rec_lemma' = Option.Smart.map do_subst_con finfos.rec_lemma in
177 let prop_lemma' = Option.Smart.map do_subst_con finfos.prop_lemma in
178 let sprop_lemma' = Option.Smart.map do_subst_con finfos.sprop_lemma in
179 if
180 function_constant' == finfos.function_constant
181 && graph_ind' == finfos.graph_ind
182 && equation_lemma' == finfos.equation_lemma
183 && correctness_lemma' == finfos.correctness_lemma
184 && completeness_lemma' == finfos.completeness_lemma
185 && rect_lemma' == finfos.rect_lemma
186 && rec_lemma' == finfos.rec_lemma
187 && prop_lemma' == finfos.prop_lemma
188 && sprop_lemma' == finfos.sprop_lemma
189 then finfos
190 else
191 { function_constant = function_constant'
192 ; graph_ind = graph_ind'
193 ; equation_lemma = equation_lemma'
194 ; correctness_lemma = correctness_lemma'
195 ; completeness_lemma = completeness_lemma'
196 ; rect_lemma = rect_lemma'
197 ; rec_lemma = rec_lemma'
198 ; prop_lemma = prop_lemma'
199 ; sprop_lemma = sprop_lemma'
200 ; is_general = finfos.is_general }
201
202 let discharge_Function finfos = Some finfos
203
204 let pr_ocst env sigma c =
205 Option.fold_right
206 (fun v acc -> Termops.pr_global_env env (ConstRef v))
207 c (mt ())
208
209 let pr_info env sigma f_info =
210 str "function_constant := "
211 ++ Termops.pr_global_env env (ConstRef f_info.function_constant)
212 ++ fnl ()
213 ++ str "function_constant_type := "
214 ++ ( try
215 Printer.pr_lconstr_env env sigma
216 (fst
217 (Typeops.type_of_global_in_context env
218 (GlobRef.ConstRef f_info.function_constant)))
219 with e when CErrors.noncritical e -> mt () )
220 ++ fnl () ++ str "equation_lemma := "
221 ++ pr_ocst env sigma f_info.equation_lemma
222 ++ fnl ()
223 ++ str "completeness_lemma :="
224 ++ pr_ocst env sigma f_info.completeness_lemma
225 ++ fnl ()
226 ++ str "correctness_lemma := "
227 ++ pr_ocst env sigma f_info.correctness_lemma
228 ++ fnl () ++ str "rect_lemma := "
229 ++ pr_ocst env sigma f_info.rect_lemma
230 ++ fnl () ++ str "rec_lemma := "
231 ++ pr_ocst env sigma f_info.rec_lemma
232 ++ fnl () ++ str "prop_lemma := "
233 ++ pr_ocst env sigma f_info.prop_lemma
234 ++ fnl () ++ str "graph_ind := "
235 ++ Printer.pr_global_env (Termops.vars_of_env env) (IndRef f_info.graph_ind)
236 ++ fnl ()
237
238 let pr_table env sigma tb =
239 let l = Cmap_env.fold (fun k v acc -> v :: acc) tb [] in
240 Pp.prlist_with_sep fnl (pr_info env sigma) l
241
242 let in_Function : function_info -> Libobject.obj =
243 let open Libobject in
244 declare_object
245 @@ superglobal_object "FUNCTIONS_DB" ~cache:cache_Function
246 ~subst:(Some subst_Function) ~discharge:discharge_Function
247
248 let find_or_none id =
249 try
250 Some
251 ( match Nametab.locate (qualid_of_ident id) with
252 | GlobRef.ConstRef c -> c
253 | _ -> CErrors.anomaly (Pp.str "Not a constant.") )
254 with Not_found -> None
255
256 let find_Function_infos f = Cmap_env.find_opt f !from_function
257 let find_Function_of_graph ind = Indmap_env.find_opt ind !from_graph
258
259 let update_Function finfo =
260 (* Pp.msgnl (pr_info finfo); *)
261 Lib.add_leaf (in_Function finfo)
262
263 let add_Function is_general f =
264 let f_id = Constant.label f in
265 let equation_lemma = find_or_none (mk_equation_id f_id)
266 and correctness_lemma = find_or_none (mk_correct_id f_id)
267 and completeness_lemma = find_or_none (mk_complete_id f_id)
268 and rect_lemma = find_or_none (Nameops.add_suffix f_id "_rect")
269 and rec_lemma = find_or_none (Nameops.add_suffix f_id "_rec")
270 and prop_lemma = find_or_none (Nameops.add_suffix f_id "_ind")
271 and sprop_lemma = find_or_none (Nameops.add_suffix f_id "_sind")
272 and graph_ind =
273 match Nametab.locate (qualid_of_ident (mk_rel_id f_id)) with
274 | GlobRef.IndRef ind -> ind
275 | _ -> CErrors.anomaly (Pp.str "Not an inductive.")
276 in
277 let finfos =
278 { function_constant = f
279 ; equation_lemma
280 ; completeness_lemma
281 ; correctness_lemma
282 ; rect_lemma
283 ; rec_lemma
284 ; prop_lemma
285 ; sprop_lemma
286 ; graph_ind
287 ; is_general }
288 in
289 update_Function finfos
290
291 let pr_table env sigma = pr_table env sigma !from_function
292
293 (*********************************)
294 (* Debugging *)
295
296 let { Goptions.get = do_rewrite_dependent } =
297 Goptions.declare_bool_option_and_ref
298 ~key:["Functional"; "Induction"; "Rewrite"; "Dependent"]
299 ~value:true
300 ()
301
302 let observe_flag, observe = CDebug.create_full ~name:"funind" ()
303
304 let do_observe () = CDebug.get_flag observe_flag
305
306 let debug_queue = Stack.create ()
307
308 let print_debug_queue b e =
309 if not (Stack.is_empty debug_queue) then
310 let lmsg, goal = Stack.pop debug_queue in
311 if b then
312 Feedback.msg_debug
313 (hov 1
314 ( lmsg
315 ++ (str " raised exception " ++ CErrors.print e)
316 ++ str " on goal" ++ fnl () ++ goal ))
317 else
318 Feedback.msg_debug
319 (hov 1 (str " from " ++ lmsg ++ str " on goal" ++ fnl () ++ goal))
320
321 (* print_debug_queue false e; *)
322
323 let do_observe_tac ~header s tac =
324 let open Proofview.Notations in
325 let open Proofview in
326 Goal.enter (fun gl ->
327 let goal = Printer.Debug.pr_goal gl in
328 let env, sigma = (Goal.env gl, Goal.sigma gl) in
329 let s = s env sigma in
330 let lmsg = seq [header; str " : " ++ s] in
331 tclLIFT (NonLogical.make (fun () -> Feedback.msg_debug (s ++ fnl ())))
332 >>= fun () ->
333 tclOR
334 ( Stack.push (lmsg, goal) debug_queue;
335 tac
336 >>= fun v ->
337 ignore (Stack.pop debug_queue);
338 Proofview.tclUNIT v )
339 (fun (exn, info) ->
340 if not (Stack.is_empty debug_queue) then print_debug_queue true exn;
341 tclZERO ~info exn))
342
343 let observe_tac ~header s tac =
344 if do_observe () then do_observe_tac ~header s tac else tac
345
346 let { Goptions.get = is_strict_tcc } =
347 Goptions.declare_bool_option_and_ref
348 ~key:["Function_raw_tcc"]
349 ~value:false
350 ()
351
352 exception Building_graph of exn
353 exception Defining_principle of exn
354 exception ToShow of exn
355
356 let jmeq () =
357 try
358 Rocqlib.check_required_library Rocqlib.jmeq_module_name;
359 EConstr.of_constr @@ UnivGen.constr_of_monomorphic_global (Global.env ())
360 @@ Rocqlib.lib_ref "core.JMeq.type"
361 with e when CErrors.noncritical e -> raise (ToShow e)
362
363 let jmeq_refl () =
364 try
365 Rocqlib.check_required_library Rocqlib.jmeq_module_name;
366 EConstr.of_constr @@ UnivGen.constr_of_monomorphic_global (Global.env ())
367 @@ Rocqlib.lib_ref "core.JMeq.refl"
368 with e when CErrors.noncritical e -> raise (ToShow e)
369
370 let h_intros l = Tacticals.tclMAP (fun x -> Tactics.Simple.intro x) l
371 let h_id = Id.of_string "h"
372 let hrec_id = Id.of_string "hrec"
373
374 let well_founded = function
375 | () -> EConstr.of_constr (rocq_constant "core.wf.well_founded")
376
377 let acc_rel = function () -> EConstr.of_constr (rocq_constant "core.wf.acc")
378
379 let acc_inv_id = function
380 | () -> EConstr.of_constr (rocq_constant "core.wf.acc_inv")
381
382 let well_founded_ltof () =
383 EConstr.of_constr (rocq_constant "num.nat.well_founded_ltof")
384
385 let ltof_ref = function () -> find_reference ["Stdlib"; "Arith"; "Wf_nat"] "ltof"
386
387 let make_eq () =
388 try
389 EConstr.of_constr
390 (UnivGen.constr_of_monomorphic_global (Global.env ()) (Rocqlib.lib_ref "core.eq.type"))
391 with e when CErrors.noncritical e -> assert false
392
393 let evaluable_of_global_reference r =
394 (* Tacred.evaluable_of_global_reference (Global.env ()) *)
395 match r with
396 | GlobRef.ConstRef sp -> Evaluable.EvalConstRef sp
397 | GlobRef.VarRef id -> Evaluable.EvalVarRef id
398 | _ -> assert false
399
400 let list_rewrite (rev : bool) (eqs : (EConstr.constr * bool) list) =
401 let open Tacticals in
402 (tclREPEAT
403 (List.fold_right
404 (fun (eq, b) i ->
405 tclORELSE
406 ((if b then Equality.rewriteLR else Equality.rewriteRL) eq)
407 i)
408 (if rev then List.rev eqs else eqs)
409 (tclFAIL (mt ()))))
410
411 let decompose_lambda_n sigma n =
412 if n < 0 then
413 CErrors.user_err
414 Pp.(str "decompose_lambda_n: integer parameter must be positive");
415 let rec lamdec_rec l n c =
416 if Int.equal n 0 then (l, c)
417 else
418 match EConstr.kind sigma c with
419 | Lambda (x, t, c) -> lamdec_rec ((x, t) :: l) (n - 1) c
420 | Cast (c, _, _) -> lamdec_rec l n c
421 | _ ->
422 CErrors.user_err Pp.(str "decompose_lambda_n: not enough abstractions")
423 in
424 lamdec_rec [] n
425
426 let lamn n env b =
427 let open EConstr in
428 let rec lamrec = function
429 | 0, env, b -> b
430 | n, (v, t) :: l, b -> lamrec (n - 1, l, mkLambda (v, t, b))
431 | _ -> assert false
432 in
433 lamrec (n, env, b)
434
435 (* compose_lam [xn:Tn;..;x1:T1] b = [x1:T1]..[xn:Tn]b *)
436 let compose_lam l b = lamn (List.length l) l b
437
438 (* prodn n [xn:Tn;..;x1:T1;Gamma] b = (x1:T1)..(xn:Tn)b *)
439 let prodn n env b =
440 let open EConstr in
441 let rec prodrec = function
442 | 0, env, b -> b
443 | n, (v, t) :: l, b -> prodrec (n - 1, l, mkProd (v, t, b))
444 | _ -> assert false
445 in
446 prodrec (n, env, b)
447
448 (* compose_prod [xn:Tn;..;x1:T1] b = (x1:T1)..(xn:Tn)b *)
449 let compose_prod l b = prodn (List.length l) l b
450
451 type tcc_lemma_value = Undefined | Value of constr | Not_needed
452
453 (* We only "purify" on exceptions. XXX: What is this doing here? *)
454 let funind_purify f x =
455 let st = Vernacstate.freeze_full_state () in
456 try f x
457 with e ->
458 let e = Exninfo.capture e in
459 Vernacstate.unfreeze_full_state st;
460 Exninfo.iraise e