Debian Codegraph @

plugins/funind/indfun.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 Pp
12 open Util
13 open CErrors
14 open Names
15 open Constr
16 open EConstr
17 open Tacticals
18 open Tactics
19 open Induction
20 open Indfun_common
21 module RelDecl = Context.Rel.Declaration
22
23 let is_rec_info sigma scheme_info =
24 let test_branche min acc decl =
25 acc
26 ||
27 let new_branche =
28 it_mkProd_or_LetIn mkProp
29 (fst (decompose_prod_decls sigma (RelDecl.get_type decl)))
30 in
31 let free_rels_in_br = Termops.free_rels sigma new_branche in
32 let max = min + scheme_info.npredicates in
33 Int.Set.exists (fun i -> i >= min && i < max) free_rels_in_br
34 in
35 List.fold_left_i test_branche 1 false (List.rev scheme_info.branches)
36
37 let choose_dest_or_ind scheme_info args =
38 Proofview.tclBIND Proofview.tclEVARMAP (fun sigma ->
39 Induction.induction_destruct (is_rec_info sigma scheme_info) false args)
40
41 let functional_induction with_clean c princl pat =
42 let open Proofview.Notations in
43 Proofview.Goal.enter_one (fun gl ->
44 let env = Proofview.Goal.env gl in
45 let sigma = Proofview.Goal.sigma gl in
46 let concl = Proofview.Goal.concl gl in
47 let f, args = decompose_app_list sigma c in
48 match princl with
49 | None -> (
50 (* No principle is given let's find the good one *)
51 match EConstr.kind sigma f with
52 | Const (c', u) ->
53 let princ_option =
54 let finfo =
55 (* we first try to find out a graph on f *)
56 match find_Function_infos c' with
57 | Some finfo -> finfo
58 | None ->
59 user_err
60 ( str "Cannot find induction information on "
61 ++ Termops.pr_global_env env (ConstRef c') )
62 in
63 match Retyping.get_sort_quality_of env sigma concl with
64 | Qual (QConstant QSProp) -> finfo.sprop_lemma
65 | Qual (QConstant QProp) -> finfo.prop_lemma
66 | Set -> finfo.rec_lemma
67 | Qual (QConstant QType | QVar _) -> finfo.rect_lemma
68 in
69 let sigma, princ =
70 (* then we get the principle *)
71 match princ_option with
72 | Some princ ->
73 Evd.fresh_global env sigma (GlobRef.ConstRef princ)
74 | None ->
75 (*i If there is not default lemma defined then,
76 we cross our finger and try to find a lemma named f_ind
77 (or f_rec, f_rect) i*)
78 let princ_name =
79 Elimschemes.make_elimination_ident
80 (Constant.label c')
81 (Retyping.get_sort_quality_of env sigma concl)
82 in
83 let princ_ref =
84 match
85 Constrintern.locate_reference
86 (Libnames.qualid_of_ident princ_name)
87 with
88 | Some r -> r
89 | None ->
90 user_err
91 ( str "Cannot find induction principle for "
92 ++ Termops.pr_global_env env (ConstRef c') )
93 in
94 Evd.fresh_global env sigma princ_ref
95 in
96 let princt = Retyping.get_type_of env sigma princ in
97 Proofview.Unsafe.tclEVARS sigma
98 <*> Proofview.tclUNIT (princ, Tactypes.NoBindings, princt, args)
99 | _ ->
100 CErrors.user_err
101 (str "functional induction must be used with a function") )
102 | Some (princ, binding) ->
103 let sigma, princt = Typing.type_of env sigma princ in
104 Proofview.Unsafe.tclEVARS sigma
105 <*> Proofview.tclUNIT (princ, binding, princt, args))
106 >>= fun (princ, bindings, princ_type, args) ->
107 Proofview.Goal.enter (fun gl ->
108 let sigma = Proofview.Goal.sigma gl in
109 let princ_infos = compute_elim_sig sigma princ_type in
110 let args_as_induction_constr =
111 let c_list = if princ_infos.farg_in_concl then [c] else [] in
112 if List.length args + List.length c_list = 0 then
113 user_err Pp.(str "Cannot recognize a valid functional scheme");
114 let encoded_pat_as_patlist =
115 List.make (List.length args + List.length c_list - 1) None @ [pat]
116 in
117 List.map2
118 (fun c pat ->
119 ( ( None
120 , ElimOnConstr
121 (fun env sigma -> (sigma, (c, Tactypes.NoBindings))) )
122 , (None, pat)
123 , None ))
124 (args @ c_list) encoded_pat_as_patlist
125 in
126 let princ' = Some (princ, bindings) in
127 let princ_vars =
128 List.fold_right
129 (fun a acc ->
130 try Id.Set.add (destVar sigma a) acc with DestKO -> acc)
131 args Id.Set.empty
132 in
133 let old_idl =
134 List.fold_right Id.Set.add (Tacmach.pf_ids_of_hyps gl) Id.Set.empty
135 in
136 let old_idl = Id.Set.diff old_idl princ_vars in
137 let subst_and_reduce gl =
138 if with_clean then
139 let idl =
140 List.filter
141 (fun id -> not (Id.Set.mem id old_idl))
142 (Tacmach.pf_ids_of_hyps gl)
143 in
144 let flag =
145 Genredexpr.Cbv {Redops.all_flags with Genredexpr.rDelta = false}
146 in
147 tclTHEN
148 (tclMAP
149 (fun id ->
150 tclTRY (Equality.subst_gen (do_rewrite_dependent ()) [id]))
151 idl)
152 (reduce flag Locusops.allHypsAndConcl)
153 else tclIDTAC
154 in
155 tclTHEN
156 (choose_dest_or_ind princ_infos (args_as_induction_constr, princ'))
157 (Proofview.Goal.enter subst_and_reduce))