]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tactics/metadataQuery.ml
auto snapshot
[helm.git] / helm / software / components / 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 (* $Id$ *)
27
28 open Printf
29
30 let nonvar uri = not (UriManager.uri_is_var uri)
31
32 module Constr = MetadataConstraints
33
34 exception Goal_is_not_an_equation
35
36 let debug = false
37 let debug_print s = if debug then prerr_endline (Lazy.force s)
38
39 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
40
41 let signature_of_hypothesis context metasenv =
42   let set, _ =
43     List.fold_right
44       (fun hyp (set,current_ctx) ->
45         match hyp with
46         | None -> set, hyp::current_ctx
47         | Some (_, Cic.Decl t) -> 
48             Constr.UriManagerSet.union set (Constr.constants_of t),
49             hyp::current_ctx
50         | Some (_, Cic.Def (t, _)) ->
51             try 
52               let ty,_ = 
53                 CicTypeChecker.type_of_aux' 
54                   metasenv current_ctx t CicUniv.empty_ugraph 
55               in
56               let sort,_ = 
57                 CicTypeChecker.type_of_aux' 
58                   metasenv current_ctx ty CicUniv.empty_ugraph 
59               in
60               let set = Constr.UriManagerSet.union set(Constr.constants_of ty)in
61               match sort with
62               | Cic.Sort Cic.Prop -> set, hyp::current_ctx
63               | _ -> Constr.UriManagerSet.union set (Constr.constants_of t),
64                      hyp::current_ctx
65             with
66             | CicTypeChecker.TypeCheckerFailure _ -> set, hyp::current_ctx)
67       context (Constr.UriManagerSet.empty,[]) 
68   in
69   set
70 ;;
71
72 let intersect uris siguris =
73   let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
74   let set2 =
75     List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
76   in
77   let inter = Constr.UriManagerSet.inter set1 set2 in
78   List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
79
80 (* Profiling code
81 let at_most =
82  let profiler = CicUtil.profile "at_most" in
83  fun ~dbd ~where uri -> profiler.profile (Constr.at_most ~dbd ~where) uri
84
85 let sigmatch =
86  let profiler = CicUtil.profile "sigmatch" in
87  fun ~dbd ~facts ~where signature ->
88   profiler.profile (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
89 *)
90 let at_most = Constr.at_most
91 let sigmatch = MetadataConstraints.sigmatch
92
93 let filter_uris_forward ~dbd (main, constants) uris =
94   let main_uris =
95     match main with
96     | None -> []
97     | Some (main, types) -> main :: types
98   in
99   let full_signature =
100     List.fold_right Constr.UriManagerSet.add main_uris constants
101   in
102   List.filter (at_most ~dbd ~where:`Statement full_signature) uris
103
104 let filter_uris_backward ~dbd ~facts signature uris =
105   let siguris =
106     List.map snd 
107       (sigmatch ~dbd ~facts ~where:`Statement signature)
108   in
109     intersect uris siguris 
110
111 let compare_goal_list proof goal1 goal2 =
112   let _,metasenv,_,_ = proof in
113   let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
114   let (_, ey2, ty2) =  CicUtil.lookup_meta goal2 metasenv in
115   let ty_sort1,_ = 
116     CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph 
117   in
118   let ty_sort2,_ = 
119     CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph 
120   in
121   let prop1 =
122     let b,_ = 
123       CicReduction.are_convertible 
124         ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph 
125     in
126       if b then 0
127       else 1
128   in
129   let prop2 =
130     let b,_ = 
131       CicReduction.are_convertible 
132         ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph 
133     in 
134       if b then 0
135       else 1
136   in
137   prop1 - prop2
138
139 (* experimental_hint is a version of hint for experimental 
140     purposes. It uses auto_tac_verbose instead of auto tac.
141     Auto_tac verbose also returns a substitution - for the moment 
142     as a function from cic to cic, to be changed into an association
143     list in the future -. This substitution is used to build a
144     hash table of the inspected goals with their associated proofs.
145     The cose is a cut and paste of the previous one: at the end 
146     of the experimentation we shall make a choice. *)
147
148 let close_with_types s metasenv context =
149   Constr.UriManagerSet.fold 
150     (fun e bag -> 
151       let t = CicUtil.term_of_uri e in
152       let ty, _ = 
153         CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph  
154       in
155       Constr.UriManagerSet.union bag (Constr.constants_of ty)) 
156     s s
157
158 let close_with_constructors s metasenv context =
159   Constr.UriManagerSet.fold 
160     (fun e bag -> 
161       let t = CicUtil.term_of_uri e in
162       match t with
163           Cic.MutInd (uri,_,_)  
164         | Cic.MutConstruct (uri,_,_,_) ->  
165             (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
166                  Cic.InductiveDefinition(tl,_,_,_) ->
167                    snd
168                      (List.fold_left
169                         (fun (i,s) (_,_,_,cl) ->
170                            let _,s =
171                              List.fold_left 
172                                (fun (j,s) _ -> 
173                                   let curi = UriManager.uri_of_uriref uri i (Some j) in
174                                     j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
175                              (i+1,s)) (0,bag) tl)
176                | _ -> assert false)
177         | _ -> bag)
178     s s
179
180 (* Profiling code
181 let apply_tac_verbose =
182  let profiler = CicUtil.profile "apply_tac_verbose" in
183   fun ~term status -> profiler.profile (PrimitiveTactics.apply_tac_verbose ~term) status
184
185 let sigmatch =
186  let profiler = CicUtil.profile "sigmatch" in
187  fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler.profile (Constr.sigmatch ~dbd ~facts ~where) signature
188
189 let cmatch' =
190  let profiler = CicUtil.profile "cmatch'" in
191  fun ~dbd ~facts signature -> profiler.profile (Constr.cmatch' ~dbd ~facts) signature
192 *)
193 let apply_tac_verbose = PrimitiveTactics.apply_tac_verbose
194 let cmatch' = Constr.cmatch'
195   
196 let is_predicate u = 
197     let ty, _ = 
198       try CicTypeChecker.type_of_aux' [] []
199         (CicUtil.term_of_uri u) CicUniv.empty_ugraph
200       with CicTypeChecker.TypeCheckerFailure _ -> assert false
201     in
202     let rec check_last_pi = function
203       | Cic.Prod (_,_,tgt) -> check_last_pi tgt
204       | Cic.Sort Cic.Prop -> true
205       | _ -> false
206     in
207     check_last_pi ty
208 ;;
209
210 let only constants uri =
211   prerr_endline (UriManager.string_of_uri uri);
212   let t = CicUtil.term_of_uri uri in (* FIXME: write ty_of_term *)
213   let ty,_ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
214   let consts = Constr.constants_of ty in
215   Constr.UriManagerSet.subset consts constants 
216 ;;
217
218 let universe_of_goals ~(dbd:HMysql.dbd) proof goals =
219   let (_, metasenv, _, _) = proof in
220   let add_uris_for acc goal =
221    let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
222    let main, sig_constants = Constr.signature_of ty in
223    let set = signature_of_hypothesis context metasenv in
224    let set =
225     match main with
226        None -> set
227      | Some (main,l) ->
228         List.fold_right Constr.UriManagerSet.add (main::l) set in
229    let set = Constr.UriManagerSet.union set sig_constants in
230    let all_constants_closed = close_with_types set metasenv context in
231    Constr.UriManagerSet.union all_constants_closed acc
232   in
233   let all_constants_closed = 
234     List.fold_left add_uris_for Constr.UriManagerSet.empty goals in
235   (* we split predicates from the rest *)
236   let predicates, rest = 
237     Constr.UriManagerSet.partition is_predicate all_constants_closed
238   in
239   let uris =
240     Constr.UriManagerSet.fold
241       (fun u acc -> 
242         let uris =
243           sigmatch ~dbd ~facts:false ~where:`Statement 
244             (Some (u,[]),all_constants_closed)
245         in
246         acc @ uris)
247       predicates []
248   in
249 (*
250   let uris =
251     sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) 
252   in
253 *)
254   let uris = List.filter nonvar (List.map snd uris) in
255   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
256   let uris = List.filter (only all_constants_closed) uris in
257    uris
258 ;;
259
260 let filter_out_predicate set ctx menv =
261   Constr.UriManagerSet.filter (fun u -> not (is_predicate u)) set  
262 ;;
263
264 let equations_for_goal ~(dbd:HMysql.dbd) ?signature ((proof, goal) as _status) =
265 (*   let to_string set =
266     "{ " ^
267       (String.concat ", "
268          (Constr.UriManagerSet.fold
269             (fun u l -> (UriManager.string_of_uri u)::l) set []))
270     ^ " }"
271   in *)
272  let (_, metasenv, _, _) = proof in
273  let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
274  let main, sig_constants = 
275    match signature with 
276    | None -> Constr.signature_of ty 
277    | Some s -> s
278  in
279 (*  Printf.printf "\nsig_constants: %s\n\n" (to_string sig_constants); *)
280 (*  match main with *)
281 (*      None -> raise Goal_is_not_an_equation *)
282 (*    | Some (m,l) -> *)
283  let l =
284    let eq_URI =
285     match LibraryObjects.eq_URI () with
286        None -> None
287      | Some s ->
288         Some
289          (UriManager.uri_of_string
290           (UriManager.string_of_uri s ^ "#xpointer(1/1)"))
291    in
292    match eq_URI,main with
293    | Some eq_URI, Some (m, l) when UriManager.eq m eq_URI -> m::l
294    | _ -> []
295  in
296  (*Printf.printf "\nSome (m, l): %s, [%s]\n\n"
297    (UriManager.string_of_uri (List.hd l))
298    (String.concat "; " (List.map UriManager.string_of_uri (List.tl l)));
299  *)
300  (*        if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then ( *)
301  let set = signature_of_hypothesis context metasenv in
302  (*          Printf.printf "\nsignature_of_hypothesis: %s\n\n" (to_string set); *)
303  let set = Constr.UriManagerSet.union set sig_constants in
304  let set = filter_out_predicate set context metasenv in
305  let set = close_with_types set metasenv context in
306  (*          Printf.printf "\ndopo close_with_types: %s\n\n" (to_string set); *)
307  let set = close_with_constructors set metasenv context in
308  (*          Printf.printf "\ndopo close_with_constructors: %s\n\n" (to_string set); *)
309  let set = List.fold_right Constr.UriManagerSet.remove l set in
310  let uris =
311    sigmatch ~dbd ~facts:false ~where:`Statement (main,set) in
312  let uris = List.filter nonvar (List.map snd uris) in
313  let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
314  uris
315    (*        ) *)
316    (*        else raise Goal_is_not_an_equation *)
317
318 let experimental_hint 
319   ~(dbd:HMysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
320   let (_, metasenv, _, _) = proof in
321   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
322   let (uris, (main, sig_constants)) =
323     match signature with
324     | Some signature -> 
325         (sigmatch ~dbd ~facts signature, signature)
326     | None -> 
327         (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
328   in 
329   let uris = List.filter nonvar (List.map snd uris) in
330   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
331   let types_constants =
332     match main with
333     | None -> Constr.UriManagerSet.empty
334     | Some (main, types) ->
335         List.fold_right Constr.UriManagerSet.add (main :: types)
336           Constr.UriManagerSet.empty
337   in
338   let all_constants =
339     let hyp_and_sug =
340       Constr.UriManagerSet.union
341         (signature_of_hypothesis context metasenv) 
342         sig_constants
343     in
344     let main = 
345       match main with
346       | None -> Constr.UriManagerSet.empty
347       | Some (main,_) -> 
348           let ty, _ = 
349             CicTypeChecker.type_of_aux' 
350               metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
351           in
352           Constr.constants_of ty
353     in
354     Constr.UriManagerSet.union main hyp_and_sug
355   in
356 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
357   let all_constants_closed = close_with_types all_constants metasenv context in
358   let other_constants = 
359     Constr.UriManagerSet.diff all_constants_closed types_constants
360   in
361   debug_print (lazy "all_constants_closed");
362   if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) all_constants_closed;
363   debug_print (lazy "other_constants");
364   if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) other_constants;
365   let uris = 
366     let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
367     if ((List.length uris < pow) or (pow <= 0))
368     then begin
369       debug_print (lazy "MetadataQuery: large sig, falling back to old method");
370       filter_uris_forward ~dbd (main, other_constants) uris
371     end else
372       filter_uris_backward ~dbd ~facts (main, other_constants) uris
373   in 
374   let rec aux = function
375     | [] -> []
376     | uri :: tl ->
377         (let status' =
378             try
379               let (subst,(proof, goal_list)) =
380                   (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
381                   apply_tac_verbose 
382                     ~term:(CicUtil.term_of_uri uri)
383                   status
384               in
385               let goal_list =
386                 List.stable_sort (compare_goal_list proof) goal_list
387               in
388               Some (uri, (subst,(proof, goal_list)))
389             with ProofEngineTypes.Fail _ -> None
390           in
391           match status' with
392           | None -> aux tl
393           | Some status' -> status' :: aux tl)
394   in
395   List.stable_sort
396     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
397       Pervasives.compare (List.length goals1) (List.length goals2))
398     (aux uris)
399
400 let new_experimental_hint 
401   ~(dbd:HMysql.dbd) ?(facts=false) ?signature ~universe
402   ((proof, goal) as status)
403 =
404   let (_, metasenv, _, _) = proof in
405   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
406   let (uris, (main, sig_constants)) =
407     match signature with
408     | Some signature -> 
409         (sigmatch ~dbd ~facts signature, signature)
410     | None -> 
411         (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in 
412   let universe =
413    List.fold_left
414     (fun res u -> Constr.UriManagerSet.add u res)
415     Constr.UriManagerSet.empty universe in
416   let uris =
417    List.fold_left
418     (fun res (_,u) -> Constr.UriManagerSet.add u res)
419     Constr.UriManagerSet.empty uris in
420   let uris = Constr.UriManagerSet.inter uris universe in
421   let uris = Constr.UriManagerSet.elements uris in
422   let rec aux = function
423     | [] -> []
424     | uri :: tl ->
425         (let status' =
426             try
427               let (subst,(proof, goal_list)) =
428                   (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
429                   apply_tac_verbose 
430                     ~term:(CicUtil.term_of_uri uri)
431                   status
432               in
433               let goal_list =
434                 List.stable_sort (compare_goal_list proof) goal_list
435               in
436               Some (uri, (subst,(proof, goal_list)))
437             with ProofEngineTypes.Fail _ -> None
438           in
439           match status' with
440           | None -> aux tl
441           | Some status' -> status' :: aux tl)
442   in
443   List.stable_sort
444     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
445       Pervasives.compare (List.length goals1) (List.length goals2))
446     (aux uris)
447