]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/metadataQuery.ml
- handle metavariables: if at least one of them is present, then the
[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) pat =
50   let sql_pat = sqlpat_of_shellglob pat in
51   let query =
52         sprintf ("SELECT source FROM %s WHERE value LIKE \"%s\" UNION "^^
53                  "SELECT source FROM %s WHERE value LIKE \"%s\"")
54           (MetadataTypes.name_tbl ()) sql_pat
55            MetadataTypes.library_name_tbl sql_pat
56   in
57   let result = Mysql.exec dbd query in
58   List.filter nonvar
59     (Mysql.map result
60       (fun cols -> match cols.(0) with Some s -> s | _ -> assert false))
61
62 let match_term ~(dbd:Mysql.dbd) ty =
63   let metadata = MetadataExtractor.compute ~body:None ~ty in
64   let constants_no =
65     MetadataConstraints.StringSet.cardinal (MetadataConstraints.constants_of ty)
66   in
67   let full_card =
68     if CicUtil.is_meta_closed ty then
69       Some (MetadataConstraints.Eq constants_no)
70     else
71       None  (* we already require a set of constants to appear, this additional
72              constraints is useless *)
73 (*       MetadataConstraints.Gt (constants_no - 1) *)
74   in
75   let constraints = List.map MetadataTypes.constr_of_metadata metadata in
76   Constr.at_least ~dbd ?full_card constraints
77
78 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
79
80 let signature_of_hypothesis context =
81   List.fold_left
82     (fun set hyp ->
83       match hyp with
84       | None -> set
85       | Some (_, Cic.Decl t)
86       | Some (_, Cic.Def (t, _)) ->
87           Constr.StringSet.union set (Constr.constants_of t))
88     Constr.StringSet.empty context
89
90 let intersect uris siguris =
91   let set1 = List.fold_right Constr.StringSet.add uris Constr.StringSet.empty in
92   let set2 =
93     List.fold_right Constr.StringSet.add siguris Constr.StringSet.empty
94   in
95   let inter = Constr.StringSet.inter set1 set2 in
96   List.filter (fun s -> Constr.StringSet.mem s inter) uris
97
98 let filter_uris_forward ~dbd (main, constants) uris =
99   let main_uris =
100     match main with
101     | None -> []
102     | Some (main, types) -> main :: types
103   in
104   let full_signature =
105     List.fold_right Constr.StringSet.add main_uris constants
106   in
107   List.filter (Constr.at_most ~dbd ~where:`Statement full_signature) uris
108
109 let filter_uris_backward ~dbd ~facts signature uris =
110   let siguris =
111     List.map snd 
112       (MetadataConstraints.sigmatch ~dbd ~facts ~where:`Statement signature)
113   in
114     intersect uris siguris 
115
116 let compare_goal_list proof goal1 goal2 =
117   let _,metasenv,_,_ = proof in
118   let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
119   let (_, ey2, ty2) =  CicUtil.lookup_meta goal2 metasenv in
120   let ty_sort1,_ = 
121     CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph 
122   in
123   let ty_sort2,_ = 
124     CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph 
125   in
126   let prop1 =
127     let b,_ = 
128       CicReduction.are_convertible 
129         ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph 
130     in
131       if b then 0
132       else 1
133   in
134   let prop2 =
135     let b,_ = 
136       CicReduction.are_convertible 
137         ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph 
138     in 
139       if b then 0
140       else 1
141   in
142   prop1 - prop2
143
144 let hint ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
145   let (_, metasenv, _, _) = proof in
146   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
147   let (uris, (main, sig_constants)) =
148     match signature with
149     | Some signature -> (Constr.sigmatch ~dbd ~facts signature, signature)
150     | None -> (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
151   in
152   let uris = List.filter nonvar (List.map snd uris) in
153   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
154   let types_constants =
155     match main with
156     | None -> Constr.StringSet.empty
157     | Some (main, types) ->
158         List.fold_right Constr.StringSet.add (main :: types)
159           Constr.StringSet.empty
160   in
161   let hyp_constants =
162     Constr.StringSet.diff (signature_of_hypothesis context) types_constants
163   in
164 (* Constr.StringSet.iter prerr_endline hyp_constants; *)
165   let other_constants = Constr.StringSet.union sig_constants hyp_constants in
166   let uris = 
167     let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
168     if ((List.length uris < pow) or (pow <= 0))
169     then begin
170       prerr_endline "MetadataQuery: large sig, falling back to old method";
171       filter_uris_forward ~dbd (main, other_constants) uris
172     end else
173       filter_uris_backward ~dbd ~facts (main, other_constants) uris
174   in
175   let rec aux = function
176     | [] -> []
177     | uri :: tl ->
178         (let status' =
179             try
180               let (proof, goal_list) =
181 (*                prerr_endline ("STO APPLICANDO " ^ uri); *)
182                 PET.apply_tactic
183                   (PrimitiveTactics.apply_tac
184                     ~term:(CicUtil.term_of_uri uri))
185                   status
186               in
187               let goal_list =
188                 List.stable_sort (compare_goal_list proof) goal_list
189               in
190               Some (uri, (proof, goal_list))
191             with ProofEngineTypes.Fail _ -> None
192           in
193           match status' with
194           | None -> aux tl
195           | Some status' ->
196               status' :: aux tl)
197   in
198   List.stable_sort
199     (fun (_, (_, goals1)) (_, (_, goals2)) ->
200       Pervasives.compare (List.length goals1) (List.length goals2))
201     (aux uris)
202
203 (* experimental_hint is a version of hint for experimental 
204     purposes. It uses auto_tac_verbose instead of auto tac.
205     Auto_tac verbose also returns a substitution - for the moment 
206     as a function from cic to cic, to be changed into an association
207     list in the future -. This substitution is used to build a
208     hash table of the inspected goals with their associated proofs.
209     The cose is a cut and paste of the previous one: at the end 
210     of the experimentation we shall make a choice. *)
211
212 let experimental_hint 
213   ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
214   let (_, metasenv, _, _) = proof in
215   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
216   let (uris, (main, sig_constants)) =
217     match signature with
218     | Some signature -> 
219         (Constr.sigmatch ~dbd ~facts signature, signature)
220     | None -> 
221         (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
222   in
223   let uris = List.filter nonvar (List.map snd uris) in
224   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
225   let types_constants =
226     match main with
227     | None -> Constr.StringSet.empty
228     | Some (main, types) ->
229         List.fold_right Constr.StringSet.add (main :: types)
230           Constr.StringSet.empty
231   in
232   let hyp_constants =
233     Constr.StringSet.diff (signature_of_hypothesis context) types_constants
234   in
235 (* Constr.StringSet.iter prerr_endline hyp_constants; *)
236   let other_constants = Constr.StringSet.union sig_constants hyp_constants in
237   let uris = 
238     let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
239     if ((List.length uris < pow) or (pow <= 0))
240     then begin
241       prerr_endline "MetadataQuery: large sig, falling back to old method";
242       filter_uris_forward ~dbd (main, other_constants) uris
243     end else
244       filter_uris_backward ~dbd ~facts (main, other_constants) uris
245   in
246   let rec aux = function
247     | [] -> []
248     | uri :: tl ->
249         (let status' =
250             try
251               let (subst,(proof, goal_list)) =
252                   (* prerr_endline ("STO APPLICANDO" ^ uri); *)
253                   PrimitiveTactics.apply_tac_verbose 
254                     ~term:(CicUtil.term_of_uri uri)
255                   status
256               in
257               let goal_list =
258                 List.stable_sort (compare_goal_list proof) goal_list
259               in
260               Some (uri, (subst,(proof, goal_list)))
261             with ProofEngineTypes.Fail _ -> None
262           in
263           match status' with
264           | None -> aux tl
265           | Some status' -> status' :: aux tl)
266   in
267   List.stable_sort
268     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
269       Pervasives.compare (List.length goals1) (List.length goals2))
270     (aux uris)
271
272 let elim ~dbd uri =
273   let constraints =
274     [`Rel [`MainConclusion None];
275      `Sort (Cic.Prop,[`MainHypothesis (Some 1)]);
276      `Obj (uri,[`MainHypothesis (Some 0)]);
277      `Obj (uri,[`InHypothesis]);
278     ]
279   in
280   MetadataConstraints.at_least ~dbd constraints
281