]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tactics/metadataQuery.ml
Signature_of has been closed with respect to constructors.
[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.oblivion_ugraph 
55               in
56               let sort,_ = 
57                 CicTypeChecker.type_of_aux' 
58                   metasenv current_ctx ty CicUniv.oblivion_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, _subst, _,_, _ = 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.oblivion_ugraph 
117   in
118   let ty_sort2,_ = 
119     CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.oblivion_ugraph 
120   in
121   let prop1 =
122     let b,_ = 
123       CicReduction.are_convertible 
124         ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.oblivion_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.oblivion_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.oblivion_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.oblivion_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                                     prerr_endline ("adding " ^ (UriManager.string_of_uri curi));
175                                     j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
176                              (i+1,s)) (0,bag) tl)
177                | _ -> assert false)
178         | _ -> bag)
179     s s
180
181 (* Profiling code
182 let apply_tac_verbose =
183  let profiler = CicUtil.profile "apply_tac_verbose" in
184   fun ~term status -> profiler.profile (PrimitiveTactics.apply_tac_verbose ~term) status
185
186 let sigmatch =
187  let profiler = CicUtil.profile "sigmatch" in
188  fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler.profile (Constr.sigmatch ~dbd ~facts ~where) signature
189
190 let cmatch' =
191  let profiler = CicUtil.profile "cmatch'" in
192  fun ~dbd ~facts signature -> profiler.profile (Constr.cmatch' ~dbd ~facts) signature
193 *)
194 let apply_tac_verbose = PrimitiveTactics.apply_tac_verbose
195 let cmatch' = Constr.cmatch'
196
197 (* used only by te old auto *)
198 let signature_of_goal ~(dbd:HSql.dbd) ((proof, goal) as _status) =
199  let (_, metasenv, _subst, _, _, _) = proof in
200  let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
201  let main, sig_constants = Constr.signature_of ty in
202  let set = signature_of_hypothesis context metasenv in
203  let set =
204   match main with
205      None -> set
206    | Some (main,l) ->
207       List.fold_right Constr.UriManagerSet.add (main::l) set in
208  let set = Constr.UriManagerSet.union set sig_constants in
209  let all_constants_closed = close_with_types set metasenv context in
210  let uris =
211   sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
212  let uris = List.filter nonvar (List.map snd uris) in
213  let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
214   uris
215
216 let is_predicate u = 
217     let ty, _ = 
218       try CicTypeChecker.type_of_aux' [] []
219         (CicUtil.term_of_uri u) CicUniv.oblivion_ugraph
220       with CicTypeChecker.TypeCheckerFailure _ -> assert false
221     in
222     let rec check_last_pi = function
223       | Cic.Prod (_,_,tgt) -> check_last_pi tgt
224       | Cic.Sort Cic.Prop -> true
225       | _ -> false
226     in
227     check_last_pi ty
228 ;;
229
230 let only constants uri =
231   prerr_endline (UriManager.string_of_uri uri);
232   let t = CicUtil.term_of_uri uri in (* FIXME: write ty_of_term *)
233   let ty,_ = CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph in
234   let consts = Constr.constants_of ty in
235 (*
236   prerr_endline ("XXX " ^ UriManager.string_of_uri uri);
237   Constr.UriManagerSet.iter (fun u -> prerr_endline (" - " ^
238  UriManager.string_of_uri u)) consts;
239   Constr.UriManagerSet.iter (fun u -> prerr_endline (" + " ^
240   UriManager.string_of_uri u)) constants;*)
241   Constr.UriManagerSet.subset consts constants 
242 ;;
243
244 let rec types_of_equality = function
245   | Cic.Appl [Cic.MutInd (uri, _, _); ty; _; _] 
246     when (LibraryObjects.is_eq_URI uri) -> 
247       let uri_set = Constr.constants_of ty in
248       if Constr.UriManagerSet.equal uri_set Constr.UriManagerSet.empty then
249         Constr.SetSet.empty
250       else Constr.SetSet.singleton uri_set
251   | Cic.Prod (_, s, t) -> 
252       Constr.SetSet.union (types_of_equality s) (types_of_equality t)
253   | _ -> Constr.SetSet.empty
254 ;;
255
256 let types_for_equality metasenv goal =
257   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
258   let all = types_of_equality ty in
259   let _, all = 
260     List.fold_left
261       (fun (i,acc) _ ->          
262          let ty, _ = 
263            CicTypeChecker.type_of_aux' 
264              metasenv context (Cic.Rel i) CicUniv.oblivion_ugraph in
265          let newty = types_of_equality ty in
266            (i+1,Constr.SetSet.union newty acc)) 
267       (1,all) context
268   in all
269 ;;
270            
271 let signature_of metasenv goal = 
272   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
273   let ty_set = Constr.constants_of ty in
274   let hyp_set = signature_of_hypothesis context metasenv in
275   let set = Constr.UriManagerSet.union ty_set hyp_set in
276     close_with_constructors (close_with_types set metasenv context)
277     metasenv context
278
279
280 let universe_of_goal ~(dbd:HSql.dbd) apply_only metasenv goal =
281   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
282   let ty_set = Constr.constants_of ty in
283   let hyp_set = signature_of_hypothesis context metasenv in
284   let set = Constr.UriManagerSet.union ty_set hyp_set in
285   let all_constants_closed = close_with_types set metasenv context in
286   (* we split predicates from the rest *)
287   let predicates, rest = 
288     Constr.UriManagerSet.partition is_predicate all_constants_closed
289   in
290   let uris =
291     Constr.UriManagerSet.fold
292       (fun u acc -> 
293          debug_print (lazy ("processing "^(UriManager.string_of_uri u)));
294          let set_for_sigmatch = 
295            Constr.UriManagerSet.remove u all_constants_closed in
296          if LibraryObjects.is_eq_URI (UriManager.strip_xpointer u) then
297            (* equality has a special treatment *)
298            (debug_print (lazy "special treatment");
299            let tfe =
300              Constr.SetSet.elements (types_for_equality metasenv goal) 
301            in
302              List.fold_left 
303                (fun acc l ->
304                   let tyl = Constr.UriManagerSet.elements l in
305                   debug_print (lazy ("tyl: "^(String.concat "\n" 
306                         (List.map UriManager.string_of_uri tyl))));
307                   let set_for_sigmatch =
308                     Constr.UriManagerSet.diff set_for_sigmatch l in
309                   let uris =
310                     sigmatch ~dbd ~facts:false ~where:`Statement 
311                       (Some (u,tyl),set_for_sigmatch) in
312                     acc @ uris) 
313                acc tfe)
314          else
315            (debug_print (lazy "normal treatment");
316            let uris =
317              sigmatch ~dbd ~facts:false ~where:`Statement 
318                (Some (u,[]),set_for_sigmatch)
319            in
320              acc @ uris))
321       predicates []
322   in
323 (*
324   let uris =
325     sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) 
326   in
327 *)
328   let uris = List.filter nonvar (List.map snd uris) in
329   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
330   if apply_only then 
331     List.filter (only all_constants_closed) uris 
332   else uris
333 ;;
334
335 let filter_out_predicate set ctx menv =
336   Constr.UriManagerSet.filter (fun u -> not (is_predicate u)) set  
337 ;;
338
339 let equations_for_goal ~(dbd:HSql.dbd) ?signature ((proof, goal) as _status) =
340 (*
341   let to_string set =
342     "{\n" ^
343       (String.concat "\n"
344          (Constr.UriManagerSet.fold
345             (fun u l -> ("  "^UriManager.string_of_uri u)::l) set []))
346     ^ "\n}"
347   in
348 *)
349  let (_, metasenv, _subst, _, _, _) = proof in
350  let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
351  let main, sig_constants = 
352    match signature with 
353    | None -> Constr.signature_of ty 
354    | Some s -> s
355  in
356 (*  Printf.printf "\nsig_constants: %s\n\n" (to_string sig_constants); *)
357 (*  match main with *)
358 (*      None -> raise Goal_is_not_an_equation *)
359 (*    | Some (m,l) -> *)
360  let l =
361    let eq_URI =
362     match LibraryObjects.eq_URI () with
363        None -> None
364      | Some s ->
365         Some
366          (UriManager.uri_of_string
367           (UriManager.string_of_uri s ^ "#xpointer(1/1)"))
368    in
369    match eq_URI,main with
370    | Some eq_URI, Some (m, l) when UriManager.eq m eq_URI -> m::l
371    | _ -> []
372  in
373  (*Printf.printf "\nSome (m, l): %s, [%s]\n\n"
374    (UriManager.string_of_uri (List.hd l))
375    (String.concat "; " (List.map UriManager.string_of_uri (List.tl l)));
376  *)
377  (*        if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then ( *)
378  let set = signature_of_hypothesis context metasenv in
379  (*          Printf.printf "\nsignature_of_hypothesis: %s\n\n" (to_string set); *)
380  let set = Constr.UriManagerSet.union set sig_constants in
381  let set = filter_out_predicate set context metasenv in
382  let set = close_with_types set metasenv context in
383  (*          Printf.printf "\ndopo close_with_types: %s\n\n" (to_string set); *)
384  let set = close_with_constructors set metasenv context in
385  (*          Printf.printf "\ndopo close_with_constructors: %s\n\n" (to_string set); *)
386  let set_for_sigmatch = List.fold_right Constr.UriManagerSet.remove l set in
387  let uris =
388    sigmatch ~dbd ~facts:false ~where:`Statement (main,set_for_sigmatch) in
389  let uris = List.filter nonvar (List.map snd uris) in
390  let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
391  let set = List.fold_right Constr.UriManagerSet.add l set in
392  let uris = List.filter (only set) uris in
393  uris
394    (*        ) *)
395    (*        else raise Goal_is_not_an_equation *)
396
397 let experimental_hint 
398   ~(dbd:HSql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
399   let (_, metasenv, _subst, _, _, _) = proof in
400   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
401   let (uris, (main, sig_constants)) =
402     match signature with
403     | Some signature -> 
404         (sigmatch ~dbd ~facts signature, signature)
405     | None -> 
406         (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
407   in 
408   let uris = List.filter nonvar (List.map snd uris) in
409   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
410   let types_constants =
411     match main with
412     | None -> Constr.UriManagerSet.empty
413     | Some (main, types) ->
414         List.fold_right Constr.UriManagerSet.add (main :: types)
415           Constr.UriManagerSet.empty
416   in
417   let all_constants =
418     let hyp_and_sug =
419       Constr.UriManagerSet.union
420         (signature_of_hypothesis context metasenv) 
421         sig_constants
422     in
423     let main = 
424       match main with
425       | None -> Constr.UriManagerSet.empty
426       | Some (main,_) -> 
427           let ty, _ = 
428             CicTypeChecker.type_of_aux' 
429               metasenv context (CicUtil.term_of_uri main)
430               CicUniv.oblivion_ugraph
431           in
432           Constr.constants_of ty
433     in
434     Constr.UriManagerSet.union main hyp_and_sug
435   in
436 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
437   let all_constants_closed = close_with_types all_constants metasenv context in
438   let other_constants = 
439     Constr.UriManagerSet.diff all_constants_closed types_constants
440   in
441   debug_print (lazy "all_constants_closed");
442   if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) all_constants_closed;
443   debug_print (lazy "other_constants");
444   if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) other_constants;
445   let uris = 
446     let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
447     if ((List.length uris < pow) or (pow <= 0))
448     then begin
449       debug_print (lazy "MetadataQuery: large sig, falling back to old method");
450       filter_uris_forward ~dbd (main, other_constants) uris
451     end else
452       filter_uris_backward ~dbd ~facts (main, other_constants) uris
453   in 
454   let rec aux = function
455     | [] -> []
456     | uri :: tl ->
457         (let status' =
458             try
459               let (subst,(proof, goal_list)) =
460                   (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
461                   apply_tac_verbose 
462                     ~term:(CicUtil.term_of_uri uri)
463                   status
464               in
465               let goal_list =
466                 List.stable_sort (compare_goal_list proof) goal_list
467               in
468               Some (uri, (subst,(proof, goal_list)))
469             with ProofEngineTypes.Fail _ -> None
470           in
471           match status' with
472           | None -> aux tl
473           | Some status' -> status' :: aux tl)
474   in
475   List.stable_sort
476     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
477       Pervasives.compare (List.length goals1) (List.length goals2))
478     (aux uris)
479
480 let new_experimental_hint 
481   ~(dbd:HSql.dbd) ?(facts=false) ?signature ~universe
482   ((proof, goal) as status)
483 =
484   let (_, metasenv,  _subst, _, _, _) = proof in
485   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
486   let (uris, (main, sig_constants)) =
487     match signature with
488     | Some signature -> 
489         (sigmatch ~dbd ~facts signature, signature)
490     | None -> 
491         (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in 
492   let universe =
493    List.fold_left
494     (fun res u -> Constr.UriManagerSet.add u res)
495     Constr.UriManagerSet.empty universe in
496   let uris =
497    List.fold_left
498     (fun res (_,u) -> Constr.UriManagerSet.add u res)
499     Constr.UriManagerSet.empty uris in
500   let uris = Constr.UriManagerSet.inter uris universe in
501   let uris = Constr.UriManagerSet.elements uris in
502   let rec aux = function
503     | [] -> []
504     | uri :: tl ->
505         (let status' =
506             try
507               let (subst,(proof, goal_list)) =
508                   (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
509                   apply_tac_verbose 
510                     ~term:(CicUtil.term_of_uri uri)
511                   status
512               in
513               let goal_list =
514                 List.stable_sort (compare_goal_list proof) goal_list
515               in
516               Some (uri, (subst,(proof, goal_list)))
517             with ProofEngineTypes.Fail _ -> None
518           in
519           match status' with
520           | None -> aux tl
521           | Some status' -> status' :: aux tl)
522   in
523   List.stable_sort
524     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
525       Pervasives.compare (List.length goals1) (List.length goals2))
526     (aux uris)
527