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