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