Debian Codegraph @

plugins/funind/invfun.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 EConstr
15 open Tactics
16 open Tacticals
17 open Indfun_common
18
19 (* funind doesn't support univ poly *)
20 open UnsafeMonomorphic
21
22 (***********************************************)
23
24 (* [revert_graph kn post_tac hid] transforme an hypothesis [hid] having type Ind(kn,num) t1 ... tn res
25 when [kn] denotes a graph block into
26 f_num t1... tn = res (by applying [f_complete] to the first type) before apply post_tac on the result
27
28 if the type of hypothesis has not this form or if we cannot find the completeness lemma then we do nothing
29 *)
30 let revert_graph kn post_tac hid =
31 Proofview.Goal.enter (fun gl ->
32 let env = Proofview.Goal.env gl in
33 let sigma = Proofview.Goal.sigma gl in
34 let typ = Tacmach.pf_get_hyp_typ hid gl in
35 match EConstr.kind sigma typ with
36 | App (i, args) when isInd sigma i ->
37 let ((kn', num) as ind'), u = destInd sigma i in
38 if Environ.QMutInd.equal env kn kn' then
39 (* We have generated a graph hypothesis so that we must change it if we can *)
40 let info =
41 match find_Function_of_graph ind' with
42 | Some info -> info
43 | None ->
44 (* The graphs are mutually recursive but we cannot find one of them !*)
45 CErrors.anomaly
46 (Pp.str "Cannot retrieve infos about a mutual block.")
47 in
48 (* if we can find a completeness lemma for this function
49 then we can come back to the functional form. If not, we do nothing
50 *)
51 match info.completeness_lemma with
52 | None -> tclIDTAC
53 | Some f_complete ->
54 let f_args, res = Array.chop (Array.length args - 1) args in
55 tclTHENLIST
56 [ Generalize.generalize
57 [ applist
58 ( mkConst f_complete
59 , Array.to_list f_args @ [res.(0); mkVar hid] ) ]
60 ; clear [hid]
61 ; Simple.intro hid
62 ; post_tac hid ]
63 else tclIDTAC
64 | _ -> tclIDTAC)
65
66 (*
67 [functional_inversion hid fconst f_correct ] is the functional version of [inversion]
68
69 [hid] is the hypothesis to invert, [fconst] is the function to invert and [f_correct]
70 is the correctness lemma for [fconst].
71
72 The sketch is the following~:
73 \begin{enumerate}
74 \item Transforms the hypothesis [hid] such that its type is now $res\ =\ f\ t_1 \ldots t_n$
75 (fails if it is not possible)
76 \item replace [hid] with $R\_f t_1 \ldots t_n res$ using [f_correct]
77 \item apply [inversion] on [hid]
78 \item finally in each branch, replace each hypothesis [R\_f ..] by [f ...] using [f_complete] (whenever
79 such a lemma exists)
80 \end{enumerate}
81 *)
82
83 let functional_inversion kn hid fconst f_correct =
84 Proofview.Goal.enter (fun gl ->
85 let old_ids =
86 List.fold_right Id.Set.add (Tacmach.pf_ids_of_hyps gl) Id.Set.empty
87 in
88 let sigma = Proofview.Goal.sigma gl in
89 let type_of_h = Tacmach.pf_get_hyp_typ hid gl in
90 match EConstr.kind sigma type_of_h with
91 | App (eq, args) when EConstr.eq_constr sigma eq (make_eq ()) ->
92 let pre_tac, f_args, res =
93 match (EConstr.kind sigma args.(1), EConstr.kind sigma args.(2)) with
94 | App (f, f_args), _ when EConstr.eq_constr sigma f fconst ->
95 ((fun hid -> intros_symmetry (Locusops.onHyp hid)), f_args, args.(2))
96 | _, App (f, f_args) when EConstr.eq_constr sigma f fconst ->
97 ((fun hid -> tclIDTAC), f_args, args.(1))
98 | _ -> ((fun hid -> tclFAILn 1 Pp.(mt ())), [||], args.(2))
99 in
100 tclTHENLIST
101 [ pre_tac hid
102 ; Generalize.generalize
103 [applist (f_correct, Array.to_list f_args @ [res; mkVar hid])]
104 ; clear [hid]
105 ; Simple.intro hid
106 ; Inv.inv Inv.FullInversion None (Tactypes.NamedHyp (CAst.make hid))
107 ; Proofview.Goal.enter (fun gl ->
108 let new_ids =
109 List.filter
110 (fun id -> not (Id.Set.mem id old_ids))
111 (Tacmach.pf_ids_of_hyps gl)
112 in
113 tclMAP (revert_graph kn pre_tac) (hid :: new_ids)) ]
114 | _ -> tclFAILn 1 Pp.(mt ()))
115
116 let invfun qhyp f =
117 let f =
118 match f with
119 | GlobRef.ConstRef f -> f
120 | _ -> CErrors.user_err Pp.(str "Not a function")
121 in
122 match find_Function_infos f with
123 | None -> CErrors.user_err (Pp.str "No graph found")
124 | Some finfos -> (
125 match finfos.correctness_lemma with
126 | None -> CErrors.user_err (Pp.str "Cannot use equivalence with graph!")
127 | Some f_correct ->
128 let f_correct = mkConst f_correct and kn = fst finfos.graph_ind in
129 Tactics.try_intros_until
130 (fun hid -> functional_inversion kn hid (mkConst f) f_correct)
131 qhyp )
132
133 let invfun qhyp f =
134 let exception NoFunction in
135 match f with
136 | Some f -> invfun qhyp f
137 | None ->
138 let tac_action hid =
139 Proofview.Goal.enter begin fun gl ->
140 let sigma = Proofview.Goal.sigma gl in
141 let hyp_typ = Tacmach.pf_get_hyp_typ hid gl in
142 match EConstr.kind sigma hyp_typ with
143 | App (eq, args) when EConstr.eq_constr sigma eq (make_eq ()) -> (
144 let f1, _ = decompose_app sigma args.(1) in
145 let get_opt = function
146 | None -> raise NoFunction
147 | Some v -> v
148 in
149 try
150 if not (isConst sigma f1) then raise NoFunction;
151 let finfos =
152 get_opt (find_Function_infos (fst (destConst sigma f1)))
153 in
154 let f_correct = mkConst (get_opt finfos.correctness_lemma) in
155 let kn = fst finfos.graph_ind in
156 functional_inversion kn hid f1 f_correct
157 with NoFunction ->
158 let f2, _ = decompose_app sigma args.(2) in
159 if isConst sigma f2 then
160 match find_Function_infos (fst (destConst sigma f2)) with
161 | None ->
162 if do_observe () then
163 CErrors.user_err
164 (Pp.str "No graph found for any side of equality")
165 else
166 CErrors.user_err
167 Pp.(
168 str "Cannot find inversion information for hypothesis "
169 ++ Ppconstr.pr_id hid)
170 | Some finfos -> (
171 match finfos.correctness_lemma with
172 | None ->
173 if do_observe () then
174 CErrors.user_err
175 (Pp.str
176 "Cannot use equivalence with graph for any side of the \
177 equality")
178 else
179 CErrors.user_err
180 Pp.(
181 str "Cannot find inversion information for hypothesis "
182 ++ Ppconstr.pr_id hid)
183 | Some f_correct ->
184 let f_correct = mkConst f_correct
185 and kn = fst finfos.graph_ind in
186 functional_inversion kn hid f2 f_correct )
187 else
188 (* NoFunction *)
189 CErrors.user_err
190 Pp.(
191 str "Hypothesis " ++ Ppconstr.pr_id hid
192 ++ str " must contain at least one Function") )
193 | _ ->
194 CErrors.user_err Pp.(Ppconstr.pr_id hid ++ str " must be an equality ")
195 end
196 in
197 try_intros_until tac_action qhyp