aboutsummaryrefslogtreecommitdiffhomepage
path: root/toplevel/search.ml
diff options
context:
space:
mode:
authorGravatar ppedrot <ppedrot@85f007b7-540e-0410-9357-904b9bb8a0f7>2013-03-19 15:18:03 +0000
committerGravatar ppedrot <ppedrot@85f007b7-540e-0410-9357-904b9bb8a0f7>2013-03-19 15:18:03 +0000
commit508e8afd8538dd38afa4eaa0dd9c7e349e50e20d (patch)
treea1b2f16de9eb4f35f49f6bf598331a7518588a7f /toplevel/search.ml
parent906b063b798e6c0787fd8f514b9d7f1f8eef9cf8 (diff)
Removing the module Libtypes and unifying the Search[Pattern|Rewrite]?
mechanism with the SearchAbout one. Searches may be slower, but this is unlikely to be noticed. In addition, according to Hugo, the Libtype summary was imposing a noticeable time penalty without any real advantage. Now Search and SearchPattern are the same. The documentation was not really clear about the difference between both, except that Search was restricted to closed terms. This is an artificial restriction by now. Fixing #2815 btw. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/coq/trunk@16320 85f007b7-540e-0410-9357-904b9bb8a0f7
Diffstat (limited to 'toplevel/search.ml')
-rw-r--r--toplevel/search.ml280
1 files changed, 120 insertions, 160 deletions
diff --git a/toplevel/search.ml b/toplevel/search.ml
index c492f077d..23cff9931 100644
--- a/toplevel/search.ml
+++ b/toplevel/search.ml
@@ -15,7 +15,6 @@ open Declarations
open Libobject
open Environ
open Pattern
-open Matching
open Printer
open Libnames
open Globnames
@@ -34,16 +33,17 @@ module SearchBlacklist =
let synchronous = true
end)
-(* The functions print_constructors and crible implement the behavior needed
- for the Coq searching commands.
+(* The functions iter_constructors and iter_declarations implement the behavior
+ needed for the Coq searching commands.
These functions take as first argument the procedure
that will be called to treat each entry. This procedure receives the name
of the object, the assumptions that will make it possible to print its type,
and the constr term that represent its type. *)
-let print_constructors indsp fn env nconstr =
+let iter_constructors indsp fn env nconstr =
for i = 1 to nconstr do
- fn (ConstructRef (indsp,i)) env (Inductiveops.type_of_constructor env (indsp,i))
+ let typ = Inductiveops.type_of_constructor env (indsp, i) in
+ fn (ConstructRef (indsp, i)) env typ
done
let rec head_const c = match kind_of_term c with
@@ -53,52 +53,36 @@ let rec head_const c = match kind_of_term c with
| Cast (d,_,_) -> head_const d
| _ -> c
-(* General search, restricted to head constant if [only_head] *)
-
-let gen_crible refopt (fn : global_reference -> env -> constr -> unit) =
+(* General search over declarations *)
+let iter_declarations (fn : global_reference -> env -> constr -> unit) =
let env = Global.env () in
- let crible_rec (sp,kn) lobj = match object_tag lobj with
- | "VARIABLE" ->
- (try
- let (id,_,typ) = Global.lookup_named (basename sp) in
- begin match refopt with
- | None ->
- fn (VarRef id) env typ
- | Some r when eq_constr (head_const typ) (constr_of_global r) ->
- fn (VarRef id) env typ
- | _ -> ()
- end
- with Not_found -> (* we are in a section *) ())
- | "CONSTANT" ->
- let cst = Global.constant_of_delta_kn kn in
- let typ = Typeops.type_of_constant env cst in
- begin match refopt with
- | None ->
- fn (ConstRef cst) env typ
- | Some r when eq_constr (head_const typ) (constr_of_global r) ->
- fn (ConstRef cst) env typ
- | _ -> ()
- end
- | "INDUCTIVE" ->
- let mind = Global.mind_of_delta_kn kn in
- let mib = Global.lookup_mind mind in
- (match refopt with
- | Some (IndRef ((kn',tyi) as ind)) when eq_mind mind kn' ->
- print_constructors ind fn env
- (Array.length (mib.mind_packets.(tyi).mind_user_lc))
- | Some _ -> ()
- | _ ->
- Array.iteri
- (fun i mip -> print_constructors (mind,i) fn env
- (Array.length mip.mind_user_lc)) mib.mind_packets)
- | _ -> ()
+ let iter_obj (sp, kn) lobj = match object_tag lobj with
+ | "VARIABLE" ->
+ begin try
+ let (id, _, typ) = Global.lookup_named (basename sp) in
+ fn (VarRef id) env typ
+ with Not_found -> (* we are in a section *) () end
+ | "CONSTANT" ->
+ let cst = Global.constant_of_delta_kn kn in
+ let typ = Typeops.type_of_constant env cst in
+ fn (ConstRef cst) env typ
+ | "INDUCTIVE" ->
+ let mind = Global.mind_of_delta_kn kn in
+ let mib = Global.lookup_mind mind in
+ let iter_packet i mip =
+ let ind = (mind, i) in
+ let typ = Inductiveops.type_of_inductive env ind in
+ let () = fn (IndRef ind) env typ in
+ let len = Array.length mip.mind_user_lc in
+ iter_constructors ind fn env len
+ in
+ Array.iteri iter_packet mib.mind_packets
+ | _ -> ()
in
- try
- Declaremods.iter_all_segments crible_rec
- with Not_found ->
- ()
+ try Declaremods.iter_all_segments iter_obj
+ with Not_found -> ()
-let crible ref = gen_crible (Some ref)
+let generic_search = iter_declarations
(* Fine Search. By Yves Bertot. *)
@@ -109,7 +93,9 @@ let rec head c =
| LetIn (_,_,_,c) -> head c
| _ -> c
-let xor a b = (a or b) & (not (a & b))
+let xor a b = (a || b) && (not (a && b))
+
+(** Standard display *)
let plain_display accu ref a c =
let pc = pr_lconstr_env a c in
@@ -118,143 +104,114 @@ let plain_display accu ref a c =
let format_display l = prlist_with_sep fnl (fun x -> x) (List.rev l)
-let filter_by_module (module_list:DirPath.t list) (accept:bool)
- (ref:global_reference) _ _ =
- let sp = path_of_global ref in
- let sl = dirpath sp in
- let rec filter_aux = function
- | m :: tl -> (not (is_dirpath_prefix_of m sl)) && (filter_aux tl)
- | [] -> true
- in
- xor accept (filter_aux module_list)
-
-let gref_eq = Coqlib.glob_eq
-let c_eq = mkInd (Globnames.destIndRef gref_eq)
-
-let mk_rewrite_pattern1 eq pattern =
- PApp (PRef eq, [| PMeta None; pattern; PMeta None |])
-
-let mk_rewrite_pattern2 eq pattern =
- PApp (PRef eq, [| PMeta None; PMeta None; pattern |])
-
-let pattern_filter pat _ a c =
- try
- try
- is_matching pat (head c)
- with e when Errors.noncritical e ->
- is_matching
- pat (head (Typing.type_of (Global.env()) Evd.empty c))
- with UserError _ ->
- false
-
-let filtered_search filter_function display_function ref =
- crible ref
- (fun s a c -> if filter_function s a c then display_function s a c)
-
-let rec id_from_pattern = function
- | PRef gr -> gr
-(* should be appear as a PRef (VarRef sp) !!
- | PVar id -> Nametab.locate (make_qualid [] (Id.to_string id))
- *)
- | PApp (p,_) -> id_from_pattern p
- | _ -> error "The pattern is not simple enough."
-
-let raw_pattern_search extra_filter display_function pat =
- let name = id_from_pattern pat in
- filtered_search
- (fun s a c -> (pattern_filter pat s a c) & extra_filter s a c)
- display_function name
-
-let raw_search_rewrite extra_filter display_function pattern =
- filtered_search
- (fun s a c ->
- ((pattern_filter (mk_rewrite_pattern1 gref_eq pattern) s a c) ||
- (pattern_filter (mk_rewrite_pattern2 gref_eq pattern) s a c))
- && extra_filter s a c)
- display_function gref_eq
-
-let raw_search_by_head extra_filter display_function pattern =
- Errors.todo "raw_search_by_head"
+(** Filters *)
-let name_of_reference ref = Id.to_string (basename_of_global ref)
+(** This function tries to see whether the conclusion matches a pattern. *)
+(** FIXME: this is quite dummy, we may find a more efficient algorithm. *)
+let rec pattern_filter pat ref env typ =
+ let typ = strip_outer_cast typ in
+ if Matching.is_matching pat typ then true
+ else match kind_of_term typ with
+ | Prod (_, _, typ)
+ | LetIn (_, _, _, typ) -> pattern_filter pat ref env typ
+ | _ -> false
let full_name_of_reference ref =
let (dir,id) = repr_path (path_of_global ref) in
DirPath.to_string dir ^ "." ^ Id.to_string id
-(*
- * functions to use the new Libtypes facility
- *)
+(** Whether a reference is blacklisted *)
+let blacklist_filter ref env typ =
+ let name = full_name_of_reference ref in
+ let l = SearchBlacklist.elements () in
+ let is_not_bl str = not (String.string_contains ~where:name ~what:str) in
+ List.for_all is_not_bl l
-let raw_search search_function extra_filter display_function pat =
- let env = Global.env() in
- List.iter
- (fun (gr,_,_) ->
- let typ = Global.type_of_global gr in
- if extra_filter gr env typ then
- display_function gr env typ
- ) (search_function pat)
+let module_filter (mods, outside) ref env typ =
+ let sp = path_of_global ref in
+ let sl = dirpath sp in
+ let is_outside md = not (is_dirpath_prefix_of md sl) in
+ let is_inside md = is_dirpath_prefix_of md sl in
+ if outside then List.for_all is_outside mods
+ else List.is_empty mods || List.exists is_inside mods
-let text_pattern_search extra_filter pat =
- let ans = ref [] in
- raw_search Libtypes.search_concl extra_filter (plain_display ans) pat;
- format_display !ans
+(** SearchPattern *)
-let text_search_rewrite extra_filter pat =
+let search_pattern pat mods =
let ans = ref [] in
- raw_search (Libtypes.search_eq_concl c_eq) extra_filter (plain_display ans) pat;
+ let filter ref env typ =
+ let f_module = module_filter mods ref env typ in
+ let f_blacklist = blacklist_filter ref env typ in
+ let f_pattern () = pattern_filter pat ref env typ in
+ f_module && f_pattern () && f_blacklist
+ in
+ let iter ref env typ =
+ if filter ref env typ then plain_display ans ref env typ
+ in
+ let () = generic_search iter in
format_display !ans
-let text_search_by_head extra_filter pat =
- let ans = ref [] in
- raw_search Libtypes.search_head_concl extra_filter (plain_display ans) pat;
- format_display !ans
+(** SearchRewrite *)
-let filter_by_module_from_list = function
- | [], _ -> (fun _ _ _ -> true)
- | l, outside -> filter_by_module l (not outside)
+let eq = Coqlib.glob_eq
-let filter_blacklist gr _ _ =
- let name = full_name_of_reference gr in
- let l = SearchBlacklist.elements () in
- List.for_all (fun str -> not (String.string_contains ~where:name ~what:str)) l
+let rewrite_pat1 pat =
+ PApp (PRef eq, [| PMeta None; pat; PMeta None |])
-let (&&&&&) f g x y z = f x y z && g x y z
+let rewrite_pat2 pat =
+ PApp (PRef eq, [| PMeta None; PMeta None; pat |])
-let search_by_head pat inout =
- text_search_by_head (filter_by_module_from_list inout &&&&& filter_blacklist) pat
-
-let search_rewrite pat inout =
- text_search_rewrite (filter_by_module_from_list inout &&&&& filter_blacklist) pat
+let search_rewrite pat mods =
+ let pat1 = rewrite_pat1 pat in
+ let pat2 = rewrite_pat2 pat in
+ let ans = ref [] in
+ let filter ref env typ =
+ let f_module = module_filter mods ref env typ in
+ let f_blacklist = blacklist_filter ref env typ in
+ let f_pattern () =
+ pattern_filter pat1 ref env typ ||
+ pattern_filter pat2 ref env typ
+ in
+ f_module && f_pattern () && f_blacklist
+ in
+ let iter ref env typ =
+ if filter ref env typ then plain_display ans ref env typ
+ in
+ let () = generic_search iter in
+ format_display !ans
-let search_pattern pat inout =
- text_pattern_search (filter_by_module_from_list inout &&&&& filter_blacklist) pat
+(** Search *)
-let gen_filtered_search filter_function display_function =
- gen_crible None
- (fun s a c -> if filter_function s a c then display_function s a c)
+let search_by_head = search_pattern
+(** Now search_by_head is the same as search_pattern... *)
(** SearchAbout *)
+let name_of_reference ref = Id.to_string (basename_of_global ref)
+
type glob_search_about_item =
| GlobSearchSubPattern of constr_pattern
| GlobSearchString of string
let search_about_item (itemref,typ) = function
- | GlobSearchSubPattern pat -> is_matching_appsubterm ~closed:false pat typ
- | GlobSearchString s -> String.string_contains ~where:(name_of_reference itemref) ~what:s
-
-let raw_search_about filter_modules display_function l =
- let filter ref' env typ =
- filter_modules ref' env typ &&
- List.for_all (fun (b,i) -> (b : bool) == search_about_item (ref',typ) i) l &&
- filter_blacklist ref' () ()
- in
- gen_filtered_search filter display_function
+ | GlobSearchSubPattern pat ->
+ Matching.is_matching_appsubterm ~closed:false pat typ
+ | GlobSearchString s ->
+ String.string_contains ~where:(name_of_reference itemref) ~what:s
-let search_about reference inout =
+let search_about items mods =
let ans = ref [] in
- raw_search_about (filter_by_module_from_list inout) (plain_display ans) reference;
+ let filter ref env typ =
+ let eqb b1 b2 = if b1 then b2 else not b2 in
+ let f_module = module_filter mods ref env typ in
+ let f_about (b, i) = eqb b (search_about_item (ref, typ) i) in
+ let f_blacklist = blacklist_filter ref env typ in
+ f_module && List.for_all f_about items && f_blacklist
+ in
+ let iter ref env typ =
+ if filter ref env typ then plain_display ans ref env typ
+ in
+ let () = generic_search iter in
format_display !ans
let interface_search flags =
@@ -309,7 +266,7 @@ let interface_search flags =
toggle (Libnames.is_dirpath_prefix_of mdl path) flag
in
let in_blacklist =
- blacklist || (filter_blacklist ref env constr)
+ blacklist || (blacklist_filter ref env constr)
in
List.for_all match_name name &&
List.for_all match_type tpe &&
@@ -340,5 +297,8 @@ let interface_search flags =
} in
ans := answer :: !ans;
in
- let () = gen_filtered_search filter_function print_function in
+ let iter ref env typ =
+ if filter_function ref env typ then print_function ref env typ
+ in
+ let () = generic_search iter in
!ans