Debian Codegraph @

plugins/funind/g_indfun.mlg

raw
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 {
12
13 open Ltac_plugin
14 open Util
15 open Pp
16 open Constrexpr
17 open Indfun_common
18 open Indfun
19 open Stdarg
20 open Tacarg
21 open Extraargs
22 open Tactypes
23 open Procq.Prim
24 open Procq.Constr
25 open Pltac
26
27 }
28
29 DECLARE PLUGIN "rocq-runtime.plugins.funind"
30
31 {
32
33 let pr_fun_ind_using env sigma prc prlc _ opt_c =
34 match opt_c with
35 | None -> mt ()
36 | Some b -> spc () ++ hov 2 (str "using" ++ spc () ++ Miscprint.pr_with_bindings (prc env sigma) (prlc env sigma) b)
37
38 (* Duplication of printing functions because "'a with_bindings" is
39 (internally) not uniform in 'a: indeed constr_with_bindings at the
40 "typed" level has type "open_constr with_bindings" instead of
41 "constr with_bindings"; hence, its printer cannot be polymorphic in
42 (prc,prlc)... *)
43
44 let pr_fun_ind_using_typed prc prlc _ opt_c =
45 match opt_c with
46 | None -> mt ()
47 | Some b ->
48 let env = Global.env () in
49 let evd = Evd.from_env env in
50 let (_, b) = b env evd in
51 spc () ++ hov 2 (str "using" ++ spc () ++ Miscprint.pr_with_bindings (prc env evd) (prlc env evd) b)
52
53 }
54
55 ARGUMENT EXTEND fun_ind_using
56 TYPED AS constr_with_bindings option
57 PRINTED BY { pr_fun_ind_using_typed }
58 RAW_PRINTED BY { pr_fun_ind_using env sigma }
59 GLOB_PRINTED BY { pr_fun_ind_using env sigma }
60 | [ "using" constr_with_bindings(c) ] -> { Some c }
61 | [ ] -> { None }
62 END
63
64
65 TACTIC EXTEND newfuninv
66 | [ "functional" "inversion" quantified_hypothesis(hyp) reference_opt(fname) ] ->
67 {
68 Invfun.invfun hyp fname
69 }
70 END
71
72 {
73
74 let pr_intro_as_pat prc pat =
75 match pat with
76 | Some pat ->
77 str "as" ++ spc () ++ Miscprint.pr_intro_pattern prc pat
78 | None -> mt ()
79
80 let out_disjunctive = CAst.map (function
81 | IntroAction (IntroOrAndPattern l) -> l
82 | _ -> CErrors.user_err Pp.(str "Disjunctive or conjunctive intro pattern expected."))
83
84 }
85
86 ARGUMENT EXTEND with_names TYPED AS intropattern option
87 PRINTED BY { fun prc _ _ -> pr_intro_as_pat (fun c -> prc env sigma @@ snd @@ c env sigma) }
88 RAW_PRINTED BY { fun prc _ _ -> pr_intro_as_pat (prc env sigma) }
89 GLOB_PRINTED BY { fun prc _ _ -> pr_intro_as_pat (prc env sigma) }
90 | [ "as" simple_intropattern(ipat) ] -> { Some ipat }
91 | [] -> { None }
92 END
93
94 {
95
96 let functional_induction b c x pat =
97 functional_induction true c x (Option.map out_disjunctive pat)
98
99 }
100
101 TACTIC EXTEND newfunind
102 | ["functional" "induction" lconstr(c) fun_ind_using(princl) with_names(pat)] ->
103 {
104 (Ltac_plugin.Internals.onSomeWithHoles
105 (fun x -> functional_induction true c x pat) princl)
106 }
107
108 END
109 (***** debug only ***)
110 TACTIC EXTEND snewfunind
111 | ["soft" "functional" "induction" ne_constr_list(cl) fun_ind_using(princl) with_names(pat)] ->
112 {
113 let c = match cl with
114 | [] -> assert false
115 | [c] -> c
116 | c::cl -> EConstr.applist(c,cl)
117 in
118 Ltac_plugin.Internals.onSomeWithHoles (fun x -> functional_induction false c x pat) princl }
119 END
120
121 {
122
123 module Vernac = Pvernac.Vernac_
124
125 let (wit_function_fix_definition : Vernacexpr.fixpoint_expr Loc.located Genarg.vernac_genarg_type) =
126 Genarg.create_arg "function_fix_definition"
127
128 let function_fix_definition =
129 Procq.create_generic_entry2 "function_fix_definition" (Genarg.rawwit wit_function_fix_definition)
130
131 }
132
133 GRAMMAR EXTEND Gram
134 GLOBAL: function_fix_definition ;
135
136 function_fix_definition:
137 [ [ g = Vernac.fix_definition -> { Loc.tag ~loc g } ]]
138 ;
139
140 END
141
142 {
143
144 let () =
145 let raw_printer (_loc,body) = Genprint.PrinterBasic (fun env sigma -> Ppvernac.pr_rec_definition body) in
146 Genprint.register_vernac_print0 wit_function_fix_definition raw_printer
147
148 let is_proof_termination_interactively_checked recsl =
149 List.exists (function
150 | _, ( Some { CAst.v = (CMeasureRec _ | CWfRec _) }, _ ) -> true
151 | _, ( ( Some { CAst.v = CStructRec _ } | None), _) -> false) recsl
152
153 let classify_as_Fixpoint recsl =
154 Vernac_classifier.classify_vernac
155 (Vernacexpr.(CAst.make @@ { control = []; attrs = []; expr = VernacSynPure (VernacFixpoint(NoDischarge, List.split (List.map snd recsl)))}))
156
157 let classify_funind recsl =
158 match classify_as_Fixpoint recsl with
159 | Vernacextend.VtSideff (ids, _)
160 when is_proof_termination_interactively_checked recsl ->
161 Vernacextend.(VtStartProof (GuaranteesOpacity, ids))
162 | x -> x
163
164 let is_interactive recsl =
165 match classify_funind recsl with
166 | Vernacextend.VtStartProof _ -> true
167 | _ -> false
168
169 }
170
171 (* For usability we temporarily switch off some flags during the call
172 to Function. However this is not satisfactory:
173
174 1- Function should not warn "non-recursive" and call the Definition
175 mechanism instead of Fixpoint when needed
176
177 2- Only for automatically generated names should
178 unused-pattern-matching-variable be ignored. *)
179
180 VERNAC COMMAND EXTEND Function STATE CUSTOM
181 | ["Function" ne_function_fix_definition_list_sep(recsl,"with")]
182 => { classify_funind recsl }
183 -> {
184 let warn = "-unused-pattern-matching-variable,-non-recursive" in
185 if is_interactive recsl then
186 Vernactypes.vtopenproof (fun () ->
187 CWarnings.with_warn warn
188 Gen_principle.do_generate_principle_interactive (List.split (List.map snd recsl)))
189 else
190 Vernactypes.vtdefault (fun () ->
191 CWarnings.with_warn warn
192 Gen_principle.do_generate_principle (List.split (List.map snd recsl)))
193 }
194 END
195
196 {
197
198 let pr_fun_scheme_arg (princ_name,fun_name,s) =
199 Names.Id.print princ_name.CAst.v ++ str " :=" ++ spc() ++ str "Induction for " ++
200 Libnames.pr_qualid fun_name ++ spc() ++ str "Sort " ++
201 UnivGen.QualityOrSet.pr Sorts.QVar.raw_pr s
202
203 }
204
205 VERNAC ARGUMENT EXTEND fun_scheme_arg
206 PRINTED BY { pr_fun_scheme_arg }
207 | [ identref(princ_name) ":=" "Induction" "for" reference(fun_name) "Sort" sort_quality_or_set(s) ] -> { (princ_name,fun_name,s) }
208 END
209
210 {
211
212 let warning_error names e =
213 match e with
214 | Building_graph e ->
215 let names = pr_enum Libnames.pr_qualid names in
216 let error = if do_observe () then (spc () ++ CErrors.print e) else mt () in
217 Gen_principle.warn_cannot_define_graph (names,error)
218 | Defining_principle e ->
219 let names = pr_enum Libnames.pr_qualid names in
220 let error = if do_observe () then CErrors.print e else mt () in
221 Gen_principle.warn_cannot_define_principle (names,error)
222 | _ -> assert false
223
224 }
225
226 VERNAC COMMAND EXTEND NewFunctionalScheme
227 | ["Functional" "Scheme" ne_fun_scheme_arg_list_sep(fas,"with") ]
228 => { Vernacextend.(VtSideff(List.map (fun x -> (pi1 x).CAst.v) fas, VtLater)) }
229 ->
230 { begin
231 try
232 Gen_principle.build_scheme fas
233 with
234 | Gen_principle.No_graph_found ->
235 begin
236 match fas with
237 | (_,fun_name,_)::_ ->
238 begin
239 Gen_principle.make_graph (Smartlocate.global_with_alias fun_name);
240 try Gen_principle.build_scheme fas
241 with
242 | Gen_principle.No_graph_found ->
243 CErrors.user_err Pp.(str "Cannot generate induction principle(s)")
244 | Building_graph _ | Defining_principle _ as e ->
245 let names = List.map (fun (_,na,_) -> na) fas in
246 warning_error names e
247 end
248 | _ -> assert false (* we can only have non empty list *)
249 end
250 | Building_graph _ | Defining_principle _ as e ->
251 let names = List.map (fun (_,na,_) -> na) fas in
252 warning_error names e
253 end
254 }
255 END
256 (***** debug only ***)
257
258 VERNAC COMMAND EXTEND NewFunctionalCase
259 | ["Functional" "Case" fun_scheme_arg(fas) ]
260 => { Vernacextend.(VtSideff([(pi1 fas).CAst.v], VtLater)) }
261 -> { Gen_principle.build_case_scheme fas }
262 END
263
264 (***** debug only ***)
265 VERNAC COMMAND EXTEND GenerateGraph CLASSIFIED AS QUERY
266 | ["Generate" "graph" "for" reference(c)] ->
267 { Gen_principle.make_graph (Smartlocate.global_with_alias c) }
268 END