]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/metadataQuery.ml
added owner support to the disambiguator (now locate finds only objects
[helm.git] / helm / ocaml / tactics / metadataQuery.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 module Constr = MetadataConstraints
29 module PET = ProofEngineTypes 
30
31   (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
32   * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
33 let sqlpat_of_shellglob =
34   let star_RE, qmark_RE, percent_RE, uscore_RE =
35     Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
36   in
37   fun shellglob ->
38     Pcre.replace ~rex:star_RE ~templ:"%"
39       (Pcre.replace ~rex:qmark_RE ~templ:"_"
40         (Pcre.replace ~rex:percent_RE ~templ:"\\%"
41           (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
42             shellglob)))
43
44 let nonvar s =
45   let len = String.length s in
46   let suffix = String.sub s (len-4) 4 in
47   not (suffix  = ".var")
48
49 let locate ~(dbd:Mysql.dbd) ?(vars = false) ?owner pat =
50   let sql_pat = sqlpat_of_shellglob pat in
51   let query =
52     match owner with
53     | None -> 
54         sprintf "SELECT source FROM %s WHERE value LIKE \"%s\""
55           MetadataTypes.name_tbl sql_pat
56     | Some owner -> 
57         sprintf "SELECT %s.source FROM %s,%s WHERE 
58                   (%s.source LIKE CONCAT(%s.source,\"#%%\") OR 
59                    %s.source = %s.source) AND
60                   value LIKE \"%s\" AND owner = \"%s\""
61           MetadataTypes.name_tbl
62           MetadataTypes.name_tbl MetadataTypes.owners_tbl
63           MetadataTypes.name_tbl MetadataTypes.owners_tbl
64           MetadataTypes.name_tbl MetadataTypes.owners_tbl
65           sql_pat owner
66   in
67   let result = Mysql.exec dbd query in
68   List.filter nonvar
69     (Mysql.map result
70       (fun cols -> match cols.(0) with Some s -> s | _ -> assert false))
71
72 let match_term ~(dbd:Mysql.dbd) ty =
73   let metadata = MetadataExtractor.compute ~body:None ~ty in
74   let constants_no =
75     MetadataConstraints.StringSet.cardinal (MetadataConstraints.constants_of ty)
76   in
77   let constraints = List.map MetadataTypes.constr_of_metadata metadata in
78   Constr.at_least ~dbd ~full_card:(MetadataConstraints.Eq constants_no)
79     constraints
80
81 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
82
83 let signature_of_hypothesis context =
84   List.fold_left
85     (fun set hyp ->
86       match hyp with
87       | None -> set
88       | Some (_, Cic.Decl t)
89       | Some (_, Cic.Def (t, _)) ->
90           Constr.StringSet.union set (Constr.constants_of t))
91     Constr.StringSet.empty context
92
93 let intersect uris siguris =
94   let set1 = List.fold_right Constr.StringSet.add uris Constr.StringSet.empty in
95   let set2 =
96     List.fold_right Constr.StringSet.add siguris Constr.StringSet.empty
97   in
98   let inter = Constr.StringSet.inter set1 set2 in
99   List.filter (fun s -> Constr.StringSet.mem s inter) uris
100
101 let filter_uris_forward ~dbd (main, constants) uris =
102   let main_uris =
103     match main with
104     | None -> []
105     | Some (main, types) -> main :: types
106   in
107   let full_signature =
108     List.fold_right Constr.StringSet.add main_uris constants
109   in
110   List.filter (Constr.at_most ~dbd ~where:`Statement full_signature) uris
111
112 let filter_uris_backward ~dbd ~facts signature uris =
113   let siguris =
114     List.map snd 
115       (MetadataConstraints.sigmatch ~dbd ~facts ~where:`Statement signature)
116   in
117     intersect uris siguris 
118
119 let compare_goal_list proof goal1 goal2 =
120   let _,metasenv,_,_ = proof in
121   let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
122   let (_, ey2, ty2) =  CicUtil.lookup_meta goal2 metasenv in
123   let ty_sort1,_ = 
124     CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph 
125   in
126   let ty_sort2,_ = 
127     CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph 
128   in
129   let prop1 =
130     let b,_ = 
131       CicReduction.are_convertible 
132         ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph 
133     in
134       if b then 0
135       else 1
136   in
137   let prop2 =
138     let b,_ = 
139       CicReduction.are_convertible 
140         ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph 
141     in 
142       if b then 0
143       else 1
144   in
145   prop1 - prop2
146
147 let hint ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
148   let (_, metasenv, _, _) = proof in
149   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
150   let (uris, (main, sig_constants)) =
151     match signature with
152     | Some signature -> (Constr.sigmatch ~dbd ~facts signature, signature)
153     | None -> (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
154   in
155   let uris = List.filter nonvar (List.map snd uris) in
156   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
157   let types_constants =
158     match main with
159     | None -> Constr.StringSet.empty
160     | Some (main, types) ->
161         List.fold_right Constr.StringSet.add (main :: types)
162           Constr.StringSet.empty
163   in
164   let hyp_constants =
165     Constr.StringSet.diff (signature_of_hypothesis context) types_constants
166   in
167 Constr.StringSet.iter prerr_endline hyp_constants;
168   let other_constants = Constr.StringSet.union sig_constants hyp_constants in
169   let uris = 
170     let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
171     if ((List.length uris < pow) or (pow <= 0))
172     then begin
173       prerr_endline "MetadataQuery: large sig, falling back to old method";
174       filter_uris_forward ~dbd (main, other_constants) uris
175     end else
176       filter_uris_backward ~dbd ~facts (main, other_constants) uris
177   in
178   let rec aux = function
179     | [] -> []
180     | uri :: tl ->
181         (let status' =
182             try
183               let (proof, goal_list) =
184 (*                prerr_endline ("STO APPLICANDO " ^ uri); *)
185                 PET.apply_tactic
186                   (PrimitiveTactics.apply_tac
187                     ~term:(CicUtil.term_of_uri uri))
188                   status
189               in
190               let goal_list =
191                 List.stable_sort (compare_goal_list proof) goal_list
192               in
193               Some (uri, (proof, goal_list))
194             with ProofEngineTypes.Fail _ -> None
195           in
196           match status' with
197           | None -> aux tl
198           | Some status' ->
199               status' :: aux tl)
200   in
201   List.stable_sort
202     (fun (_, (_, goals1)) (_, (_, goals2)) ->
203       Pervasives.compare (List.length goals1) (List.length goals2))
204     (aux uris)
205
206 (* experimental_hint is a version of hint for experimental 
207     purposes. It uses auto_tac_verbose instead of auto tac.
208     Auto_tac verbose also returns a substitution - for the moment 
209     as a function from cic to cic, to be changed into an association
210     list in the future -. This substitution is used to build a
211     hash table of the inspected goals with their associated proofs.
212     The cose is a cut and paste of the previous one: at the end 
213     of the experimentation we shall make a choice. *)
214
215 let experimental_hint 
216   ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
217   let (_, metasenv, _, _) = proof in
218   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
219   let (uris, (main, sig_constants)) =
220     match signature with
221     | Some signature -> 
222         (Constr.sigmatch ~dbd ~facts signature, signature)
223     | None -> 
224         (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
225   in
226   let uris = List.filter nonvar (List.map snd uris) in
227   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
228   let types_constants =
229     match main with
230     | None -> Constr.StringSet.empty
231     | Some (main, types) ->
232         List.fold_right Constr.StringSet.add (main :: types)
233           Constr.StringSet.empty
234   in
235   let hyp_constants =
236     Constr.StringSet.diff (signature_of_hypothesis context) types_constants
237   in
238 Constr.StringSet.iter prerr_endline hyp_constants;
239   let other_constants = Constr.StringSet.union sig_constants hyp_constants in
240   let uris = 
241     let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
242     if ((List.length uris < pow) or (pow <= 0))
243     then begin
244       prerr_endline "MetadataQuery: large sig, falling back to old method";
245       filter_uris_forward ~dbd (main, other_constants) uris
246     end else
247       filter_uris_backward ~dbd ~facts (main, other_constants) uris
248   in
249   let rec aux = function
250     | [] -> []
251     | uri :: tl ->
252         (let status' =
253             try
254               let (subst,(proof, goal_list)) =
255                   (* prerr_endline ("STO APPLICANDO" ^ uri); *)
256                   PrimitiveTactics.apply_tac_verbose 
257                     ~term:(CicUtil.term_of_uri uri)
258                   status
259               in
260               let goal_list =
261                 List.stable_sort (compare_goal_list proof) goal_list
262               in
263               Some (uri, (subst,(proof, goal_list)))
264             with ProofEngineTypes.Fail _ -> None
265           in
266           match status' with
267           | None -> aux tl
268           | Some status' -> status' :: aux tl)
269   in
270   List.stable_sort
271     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
272       Pervasives.compare (List.length goals1) (List.length goals2))
273     (aux uris)
274
275 let elim ~dbd uri =
276   let constraints =
277     [`Rel [`MainConclusion None];
278      `Sort (Cic.Prop,[`MainHypothesis (Some 1)]);
279      `Obj (uri,[`MainHypothesis (Some 0)]);
280      `Obj (uri,[`InHypothesis]);
281     ]
282   in
283   MetadataConstraints.at_least ~dbd constraints
284