Debian Codegraph @

vernac/comSearch.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 (* Interpretation of search commands *)
12
13 open CErrors
14 open Names
15 open Util
16 open Pp
17 open Printer
18 open Search
19 open Vernacexpr
20 open Goptions
21
22 let open_or_global_module qid =
23 try Nametab.full_name_open_mod qid
24 with Not_found ->
25 try Nametab.full_name_module qid
26 with Not_found ->
27 user_err ?loc:qid.CAst.loc
28 (str "Module/Section " ++ Ppconstr.pr_qualid qid ++ str " not found.")
29
30 (*
31 let global_module qid =
32 user_err ?loc:qid.CAst.loc
33 (str "Module/Section " ++ Ppconstr.pr_qualid qid ++ str " not found.")
34 *)
35 let interp_search_restriction = function
36 | SearchOutside l ->
37 SearchOutside (List.map open_or_global_module l)
38 | SearchInside l ->
39 SearchInside (List.map open_or_global_module l)
40
41 let kind_searcher env = Decls.(function
42 (* Kinds referring to the keyword introducing the object *)
43 | IsAssumption _
44 | IsDefinition (Definition | Example | Fixpoint | CoFixpoint | Method | StructureComponent | Let | LetContext)
45 | IsProof _
46 | IsPrimitive
47 | IsSymbol as k -> Inl k
48 (* Kinds referring to the status of the object *)
49 | IsDefinition (Coercion | SubClass | IdentityCoercion as k') ->
50 let coercions = Coercionops.coercions () in
51 Inr (fun gr -> List.exists (fun c -> Environ.QGlobRef.equal env c.Coercionops.coe_value gr &&
52 (k' <> SubClass && k' <> IdentityCoercion || c.Coercionops.coe_is_identity)) coercions)
53 | IsDefinition CanonicalStructure ->
54 let canonproj = Structures.CSTable.entries () in
55 Inr (fun gr -> List.exists (fun c -> Environ.QGlobRef.equal env c.Structures.CSTable.solution gr) canonproj)
56 | IsDefinition Scheme ->
57 let schemes = DeclareScheme.all_schemes () in
58 let schemes = lazy begin
59 Indmap_env.fold (fun _ schemes acc ->
60 CString.Map.fold (fun _ c acc ->
61 GlobRef.Set_env.add c acc) schemes acc)
62 schemes GlobRef.Set_env.empty
63 end
64 in
65 Inr (fun x -> GlobRef.Set_env.mem x (Lazy.force schemes))
66 | IsDefinition Instance ->
67 let instances = Typeclasses.all_instances () in
68 Inr (fun gr -> List.exists (fun c -> Environ.QGlobRef.equal env c.Typeclasses.is_impl gr) instances))
69
70 let interp_constr_pattern env sigma ?(expected_type=Pretyping.WithoutTypeConstraint) c =
71 let c = Constrintern.intern_gen expected_type ~pattern_mode:true env sigma c in
72 let flags = { Pretyping.no_classes_no_fail_inference_flags with expand_evars = false } in
73 let sigma, c = Pretyping.understand_tcc ~flags env sigma ~expected_type c in
74 (* FIXME: it is necessary to be unsafe here because of the way we handle
75 evars in the pretyper. Sometimes they get solved eagerly. *)
76 Patternops.legacy_bad_pattern_of_constr env sigma c
77
78 let interp_search_item env sigma =
79 function
80 | SearchSubPattern ((where,head),pat) ->
81 let expected_type = Pretyping.(if head then IsType else WithoutTypeConstraint) in
82 let pat =
83 try interp_constr_pattern env sigma ~expected_type pat
84 with e when CErrors.noncritical e ->
85 (* We cannot ensure (yet?) that a typable pattern will
86 actually be typed, consider e.g. (forall A, A -> A /\ A)
87 which fails, not seeing that A can be Prop; so we use an
88 untyped pattern as a fallback (i.e w/o no insertion of
89 coercions, no compilation of pattern-matching) *)
90 snd (Constrintern.interp_constr_pattern env sigma ~as_type:head pat) in
91 GlobSearchSubPattern (where,head,pat)
92 | SearchString ((Anywhere,false),s,None)
93 when Id.is_valid_ident_part s && String.equal (String.drop_simple_quotes s) s ->
94 GlobSearchString s
95 | SearchString ((where,head),s,sc) ->
96 let sc = Option.map snd sc in
97 let ref =
98 Notation.interp_notation_as_global_reference
99 ~head:false (fun _ -> true) s sc in
100 GlobSearchSubPattern (where,head,Pattern.PRef ref)
101 | SearchKind k ->
102 match kind_searcher env k with
103 | Inl k -> GlobSearchKind k
104 | Inr f -> GlobSearchFilter f
105
106 let rec interp_search_request env sigma = function
107 | b, SearchLiteral i -> b, GlobSearchLiteral (interp_search_item env sigma i)
108 | b, SearchDisjConj l -> b, GlobSearchDisjConj (List.map (List.map (interp_search_request env sigma)) l)
109
110 (* 05f22a5d6d5b8e3e80f1a37321708ce401834430 introduced the
111 `search_output_name_only` option to avoid excessive printing when
112 searching.
113
114 The motivation was to make search usable for IDE completion,
115 however, it is still too slow due to the non-indexed nature of the
116 underlying search mechanism.
117
118 In the future we should deprecate the option and provide a fast,
119 indexed name-searching interface.
120 *)
121 let search_output_name_only = ref false
122
123 let () =
124 declare_bool_option
125 { optstage = Summary.Stage.Interp;
126 optdepr = None;
127 optkey = ["Search";"Output";"Name";"Only"];
128 optread = (fun () -> !search_output_name_only);
129 optwrite = (:=) search_output_name_only }
130
131 let interp_search env sigma s r =
132 let r = interp_search_restriction r in
133 let get_pattern c = snd (Constrintern.interp_constr_pattern env sigma c) in
134 let warnlist = ref [] in
135 let pr_search ref kind env sigma c =
136 let pr = pr_global ref in
137 let pp = if !search_output_name_only
138 then pr
139 else begin
140 let open Impargs in
141 let impls = implicits_of_global ref in
142 let impargs = select_stronger_impargs impls in
143 let impargs = List.map binding_kind_of_status impargs in
144 if List.length impls > 1 ||
145 List.exists Glob_term.(function Explicit -> false | MaxImplicit | NonMaxImplicit -> true)
146 (List.skipn_at_best (Termops.nb_prod_modulo_zeta sigma (EConstr.of_constr c)) impargs)
147 then warnlist := pr :: !warnlist;
148 let pc = pr_ltype_env env sigma ~impargs c in
149 hov 2 (pr ++ str":" ++ spc () ++ pc)
150 end
151 in Feedback.msg_notice pp
152 in
153 (match s with
154 | SearchPattern c ->
155 (Search.search_pattern env sigma (get_pattern c) r |> Search.prioritize_search) pr_search
156 | SearchRewrite c ->
157 (Search.search_rewrite env sigma (get_pattern c) r |> Search.prioritize_search) pr_search
158 | Search sl ->
159 (Search.search env sigma (List.map (interp_search_request env Evd.(from_env env)) sl) r |>
160 Search.prioritize_search) pr_search);
161 if !warnlist <> [] then
162 Feedback.msg_notice (str "(" ++
163 hov 0 (strbrk "use \"About\" for full details on the implicit arguments of " ++
164 pr_enum (fun x -> x) !warnlist ++ str ")"))