| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
open Constr |
| 15 |
open Declarations |
| 16 |
open Libobject |
| 17 |
open Environ |
| 18 |
open Pattern |
| 19 |
open Libnames |
| 20 |
open Vernacexpr |
| 21 |
|
| 22 |
module NamedDecl = Context.Named.Declaration |
| 23 |
|
| 24 |
type filter_function = |
| 25 |
GlobRef.t -> Decls.logical_kind option -> env -> Evd.evar_map -> constr -> bool |
| 26 |
type display_function = |
| 27 |
GlobRef.t -> Decls.logical_kind option -> env -> Evd.evar_map -> constr -> unit |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type glob_search_item = |
| 36 |
| GlobSearchSubPattern of glob_search_where * bool * constr_pattern |
| 37 |
| GlobSearchString of string |
| 38 |
| GlobSearchKind of Decls.logical_kind |
| 39 |
| GlobSearchFilter of (GlobRef.t -> bool) |
| 40 |
|
| 41 |
type glob_search_request = |
| 42 |
| GlobSearchLiteral of glob_search_item |
| 43 |
| GlobSearchDisjConj of (bool * glob_search_request) list list |
| 44 |
|
| 45 |
module SearchBlacklist = |
| 46 |
Goptions.MakeStringTable |
| 47 |
(struct |
| 48 |
let key = ["Search";"Blacklist"] |
| 49 |
let title = "Current search blacklist : " |
| 50 |
let member_message s b = |
| 51 |
str "Search blacklist does " ++ (if b then mt () else str "not ") ++ str "include " ++ str s |
| 52 |
end) |
| 53 |
|
| 54 |
let { Goptions.get = blacklist_locals } = |
| 55 |
Goptions.declare_bool_option_and_ref ~key:["Search";"Blacklist";"Locals"] ~value:true () |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
let iter_constructors indsp u fn env sigma nconstr = |
| 65 |
for i = 1 to nconstr do |
| 66 |
let typ = Inductive.type_of_constructor ((indsp, i), u) (Inductive.lookup_mind_specif env indsp) in |
| 67 |
fn (GlobRef.ConstructRef (indsp, i)) None env sigma typ |
| 68 |
done |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
module DynHandle = Libobject.Dyn.Map(struct type 'a t = 'a -> unit end) |
| 73 |
|
| 74 |
let handle h (Libobject.Dyn.Dyn (tag, o)) = match DynHandle.find tag h with |
| 75 |
| f -> f o |
| 76 |
| exception Not_found -> () |
| 77 |
|
| 78 |
|
| 79 |
let generic_search env sigma (fn : GlobRef.t -> Decls.logical_kind option -> env -> Evd.evar_map -> constr -> unit) = |
| 80 |
List.iter (fun d -> fn (GlobRef.VarRef (NamedDecl.get_id d)) None env sigma (NamedDecl.get_type d)) |
| 81 |
(Environ.named_context env); |
| 82 |
let iter_obj prefix lobj = match lobj with |
| 83 |
| AtomicObject o -> |
| 84 |
let handler = |
| 85 |
DynHandle.add Declare.Internal.Constant.tag begin fun (id,obj) -> |
| 86 |
let kn = KerName.make prefix.obj_mp id in |
| 87 |
let cst = Global.constant_of_delta_kn kn in |
| 88 |
let gr = GlobRef.ConstRef cst in |
| 89 |
let (typ, _) = Typeops.type_of_global_in_context (Global.env ()) gr in |
| 90 |
let kind = Declare.Internal.Constant.kind obj in |
| 91 |
fn gr (Some kind) env sigma typ |
| 92 |
end @@ |
| 93 |
DynHandle.add DeclareInd.Internal.objInductive begin fun (id,_) -> |
| 94 |
let kn = KerName.make prefix.obj_mp id in |
| 95 |
let mind = Global.mind_of_delta_kn kn in |
| 96 |
let mib = Global.lookup_mind mind in |
| 97 |
let iter_packet i mip = |
| 98 |
let ind = (mind, i) in |
| 99 |
let u = UVars.make_abstract_instance (Declareops.inductive_polymorphic_context mib) in |
| 100 |
let typ = Inductive.type_of_inductive (Inductive.lookup_mind_specif env ind, u) in |
| 101 |
let () = fn (GlobRef.IndRef ind) None env sigma typ in |
| 102 |
let len = Array.length mip.mind_user_lc in |
| 103 |
iter_constructors ind u fn env sigma len |
| 104 |
in |
| 105 |
Array.iteri iter_packet mib.mind_packets |
| 106 |
end @@ |
| 107 |
DynHandle.empty |
| 108 |
in |
| 109 |
handle handler o |
| 110 |
| _ -> () |
| 111 |
in |
| 112 |
try Declaremods.iter_all_interp_segments iter_obj |
| 113 |
with Not_found -> () |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
module ConstrPriority = struct |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
type t = GlobRef.t * Decls.logical_kind option * Environ.env * Evd.evar_map * Constr.t * priority |
| 127 |
and priority = int |
| 128 |
|
| 129 |
|
| 130 |
let rec size t = |
| 131 |
Constr.fold (fun s t -> 1 + s + size t) 0 t |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
let rec symbols acc t = |
| 136 |
let open Constr in |
| 137 |
match kind t with |
| 138 |
| Const _ | Ind _ | Construct _ -> GlobRef.Set_env.add (fst @@ destRef t) acc |
| 139 |
| _ -> Constr.fold symbols acc t |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
let num_symbols t = |
| 144 |
GlobRef.Set_env.(cardinal (symbols empty t)) |
| 145 |
|
| 146 |
let priority gref t : priority = |
| 147 |
-(3*(num_symbols t) + size t) |
| 148 |
|
| 149 |
let compare (_,_,_,_,_,p1) (_,_,_,_,_,p2) = |
| 150 |
Stdlib.compare p1 p2 |
| 151 |
end |
| 152 |
|
| 153 |
module PriorityQueue = Heap.Functional(ConstrPriority) |
| 154 |
|
| 155 |
let rec iter_priority_queue q fn = |
| 156 |
|
| 157 |
match PriorityQueue.maximum q with |
| 158 |
| (gref,kind,env,sigma,t,_) -> |
| 159 |
fn gref kind env sigma t; |
| 160 |
iter_priority_queue (PriorityQueue.remove q) fn |
| 161 |
| exception Heap.EmptyHeap -> () |
| 162 |
|
| 163 |
let prioritize_search seq fn = |
| 164 |
let acc = ref PriorityQueue.empty in |
| 165 |
let iter gref kind env sigma t = |
| 166 |
let p = ConstrPriority.priority gref t in |
| 167 |
acc := PriorityQueue.add (gref,kind,env,sigma,t,p) !acc |
| 168 |
in |
| 169 |
let () = seq iter in |
| 170 |
iter_priority_queue !acc fn |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
let rec pattern_filter pat env sigma typ = |
| 177 |
let typ = Termops.strip_outer_cast sigma typ in |
| 178 |
if Constr_matching.is_matching env sigma pat typ then true |
| 179 |
else match EConstr.kind sigma typ with |
| 180 |
| Prod (_, _, typ) |
| 181 |
| LetIn (_, _, _, typ) -> pattern_filter pat env sigma typ |
| 182 |
| _ -> false |
| 183 |
|
| 184 |
let full_name_of_reference ref = |
| 185 |
let (dir,id) = repr_path (Nametab.path_of_global ref) in |
| 186 |
DirPath.to_string dir ^ "." ^ Id.to_string id |
| 187 |
|
| 188 |
let is_local_ref : GlobRef.t -> bool = function |
| 189 |
| ConstRef c -> Declare.is_local_constant c |
| 190 |
| IndRef _ | ConstructRef _ | VarRef _ -> false |
| 191 |
|
| 192 |
|
| 193 |
let blacklist_filter : filter_function = fun ref kind env sigma typ -> |
| 194 |
if blacklist_locals() && is_local_ref ref then false |
| 195 |
else |
| 196 |
let name = full_name_of_reference ref in |
| 197 |
let is_not_bl str = not (String.string_contains ~where:name ~what:str) in |
| 198 |
CString.Set.for_all is_not_bl (SearchBlacklist.v ()) |
| 199 |
|
| 200 |
let module_filter : _ -> filter_function = fun mods ref kind env sigma typ -> |
| 201 |
let sp = Nametab.path_of_global ref in |
| 202 |
let sl = pop_dirpath @@ dirpath_of_path sp in |
| 203 |
let is_inside md = is_dirpath_prefix_of (dirpath_of_path md) sl in |
| 204 |
match mods with |
| 205 |
| SearchOutside mods -> |
| 206 |
let is_outside md = not (is_inside md) in |
| 207 |
List.for_all is_outside mods |
| 208 |
| SearchInside mods -> |
| 209 |
List.is_empty mods || List.exists is_inside mods |
| 210 |
|
| 211 |
let name_of_reference ref = Id.to_string (Nametab.basename_of_global ref) |
| 212 |
|
| 213 |
let search_filter : _ -> filter_function = fun query gr kind env sigma typ -> match query with |
| 214 |
| GlobSearchSubPattern (where,head,pat) -> |
| 215 |
let open Context.Rel.Declaration in |
| 216 |
let rec collect env hyps typ = |
| 217 |
match Constr.kind typ with |
| 218 |
| LetIn (na,b,t,c) -> collect (push_rel (LocalDef (na,b,t)) env) ((env,b) :: (env,t) :: hyps) c |
| 219 |
| Prod (na,t,c) -> collect (push_rel (LocalAssum (na,t)) env) ((env,t) :: hyps) c |
| 220 |
| _ -> (hyps,(env,typ)) in |
| 221 |
let typl= match where with |
| 222 |
| InHyp -> fst (collect env [] typ) |
| 223 |
| InConcl -> [snd (collect env [] typ)] |
| 224 |
| Anywhere -> if head then let hyps, ccl = collect env [] typ in ccl :: hyps else [env,typ] in |
| 225 |
List.exists (fun (env,typ) -> |
| 226 |
let f = |
| 227 |
if head then Constr_matching.is_matching_head |
| 228 |
else Constr_matching.is_matching_appsubterm ~closed:false in |
| 229 |
f env sigma pat (EConstr.of_constr typ)) typl |
| 230 |
| GlobSearchString s -> |
| 231 |
String.string_contains ~where:(name_of_reference gr) ~what:s |
| 232 |
| GlobSearchKind k -> (match kind with None -> false | Some k' -> k = k') |
| 233 |
| GlobSearchFilter f -> f gr |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
let search_pattern env sigma pat mods pr_search = |
| 238 |
let filter ref kind env sigma typ = |
| 239 |
module_filter mods ref kind env sigma typ && |
| 240 |
pattern_filter pat env sigma (EConstr.of_constr typ) && |
| 241 |
blacklist_filter ref kind env sigma typ |
| 242 |
in |
| 243 |
let iter ref kind env sigma typ = |
| 244 |
if filter ref kind env sigma typ then pr_search ref kind env sigma typ |
| 245 |
in |
| 246 |
generic_search env sigma iter |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
let eq () = Rocqlib.(lib_ref "core.eq.type") |
| 251 |
|
| 252 |
let rewrite_pat1 pat = |
| 253 |
PApp (PRef (eq ()), [| PMeta None; pat; PMeta None |]) |
| 254 |
|
| 255 |
let rewrite_pat2 pat = |
| 256 |
PApp (PRef (eq ()), [| PMeta None; PMeta None; pat |]) |
| 257 |
|
| 258 |
let search_rewrite env sigma pat mods pr_search = |
| 259 |
let pat1 = rewrite_pat1 pat in |
| 260 |
let pat2 = rewrite_pat2 pat in |
| 261 |
let filter ref kind env sigma typ = |
| 262 |
module_filter mods ref kind env sigma typ && |
| 263 |
(pattern_filter pat1 env sigma (EConstr.of_constr typ) || |
| 264 |
pattern_filter pat2 env sigma (EConstr.of_constr typ)) && |
| 265 |
blacklist_filter ref kind env sigma typ |
| 266 |
in |
| 267 |
let iter ref kind env sigma typ = |
| 268 |
if filter ref kind env sigma typ then pr_search ref kind env sigma typ |
| 269 |
in |
| 270 |
generic_search env sigma iter |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
let search env sigma items mods pr_search = |
| 275 |
let filter ref kind env sigma typ = |
| 276 |
let eqb b1 b2 = if b1 then b2 else not b2 in |
| 277 |
module_filter mods ref kind env sigma typ && |
| 278 |
let rec aux = function |
| 279 |
| GlobSearchLiteral i -> search_filter i ref kind env sigma typ |
| 280 |
| GlobSearchDisjConj l -> List.exists (List.for_all aux') l |
| 281 |
and aux' (b,s) = eqb b (aux s) in |
| 282 |
List.for_all aux' items && blacklist_filter ref kind env sigma typ |
| 283 |
in |
| 284 |
let iter ref kind env sigma typ = |
| 285 |
if filter ref kind env sigma typ then pr_search ref kind env sigma typ |
| 286 |
in |
| 287 |
generic_search env sigma iter |
| 288 |
|
| 289 |
type search_constraint = |
| 290 |
| Name_Pattern of Str.regexp |
| 291 |
| Type_Pattern of Pattern.constr_pattern |
| 292 |
| SubType_Pattern of Pattern.constr_pattern |
| 293 |
| In_Module of full_path |
| 294 |
| Include_Blacklist |
| 295 |
|
| 296 |
type 'a coq_object = { |
| 297 |
coq_object_prefix : string list; |
| 298 |
coq_object_qualid : string list; |
| 299 |
coq_object_object : 'a; |
| 300 |
} |
| 301 |
|
| 302 |
let interface_search env sigma = |
| 303 |
let rec extract_flags name tpe subtpe mods blacklist = function |
| 304 |
| [] -> (name, tpe, subtpe, mods, blacklist) |
| 305 |
| (Name_Pattern regexp, b) :: l -> |
| 306 |
extract_flags ((regexp, b) :: name) tpe subtpe mods blacklist l |
| 307 |
| (Type_Pattern pat, b) :: l -> |
| 308 |
extract_flags name ((pat, b) :: tpe) subtpe mods blacklist l |
| 309 |
| (SubType_Pattern pat, b) :: l -> |
| 310 |
extract_flags name tpe ((pat, b) :: subtpe) mods blacklist l |
| 311 |
| (In_Module id, b) :: l -> |
| 312 |
extract_flags name tpe subtpe ((id, b) :: mods) blacklist l |
| 313 |
| (Include_Blacklist, b) :: l -> |
| 314 |
extract_flags name tpe subtpe mods b l |
| 315 |
in |
| 316 |
fun flags -> |
| 317 |
let (name, tpe, subtpe, mods, blacklist) = |
| 318 |
extract_flags [] [] [] [] false flags |
| 319 |
in |
| 320 |
let filter_function ref kind env sigma constr = |
| 321 |
let id = Names.Id.to_string (Nametab.basename_of_global ref) in |
| 322 |
let path = pop_dirpath @@ dirpath_of_path (Nametab.path_of_global ref) in |
| 323 |
let toggle x b = if x then b else not b in |
| 324 |
let match_name (regexp, flag) = |
| 325 |
toggle (Str.string_match regexp id 0) flag |
| 326 |
in |
| 327 |
let match_type (pat, flag) = |
| 328 |
toggle (Constr_matching.is_matching env sigma pat (EConstr.of_constr constr)) flag |
| 329 |
in |
| 330 |
let match_subtype (pat, flag) = |
| 331 |
toggle |
| 332 |
(Constr_matching.is_matching_appsubterm ~closed:false |
| 333 |
env sigma pat (EConstr.of_constr constr)) flag |
| 334 |
in |
| 335 |
let match_module (mdl, flag) = |
| 336 |
toggle (Libnames.is_dirpath_prefix_of (dirpath_of_path mdl) path) flag |
| 337 |
in |
| 338 |
List.for_all match_name name && |
| 339 |
List.for_all match_type tpe && |
| 340 |
List.for_all match_subtype subtpe && |
| 341 |
List.for_all match_module mods && |
| 342 |
(blacklist || blacklist_filter ref kind env sigma constr) |
| 343 |
in |
| 344 |
let ans = ref [] in |
| 345 |
let print_function ref env sigma constr = |
| 346 |
let fullpath = DirPath.repr (Nametab.dirpath_of_global ref) in |
| 347 |
let qualid = Nametab.shortest_qualid_of_global Id.Set.empty ref in |
| 348 |
let (shortpath, basename) = Libnames.repr_qualid qualid in |
| 349 |
let shortpath = DirPath.repr shortpath in |
| 350 |
|
| 351 |
|
| 352 |
let rec prefix full short accu = match full, short with |
| 353 |
| _, [] -> |
| 354 |
let full = List.rev_map Id.to_string full in |
| 355 |
(full, accu) |
| 356 |
| _ :: full, m :: short -> |
| 357 |
prefix full short (Id.to_string m :: accu) |
| 358 |
| _ -> assert false |
| 359 |
in |
| 360 |
let (prefix, qualid) = prefix fullpath shortpath [Id.to_string basename] in |
| 361 |
let answer = { |
| 362 |
coq_object_prefix = prefix; |
| 363 |
coq_object_qualid = qualid; |
| 364 |
coq_object_object = constr; |
| 365 |
} in |
| 366 |
ans := answer :: !ans; |
| 367 |
in |
| 368 |
let iter ref kind env sigma typ = |
| 369 |
if filter_function ref kind env sigma typ then print_function ref env sigma typ |
| 370 |
in |
| 371 |
let () = generic_search env sigma iter in |
| 372 |
!ans |