]> matita.cs.unibo.it Git - helm.git/blob - components/metadata/metadataConstraints.ml
Set of Set of uri added.
[helm.git] / components / metadata / metadataConstraints.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 open MetadataTypes 
30
31 let critical_value = 7
32 let just_factor = 1
33
34 module UriManagerSet = UriManager.UriSet
35 module SetSet = Set.Make (UriManagerSet)
36
37 type term_signature = (UriManager.uri * UriManager.uri list) option * UriManagerSet.t
38
39 type cardinality_condition =
40   | Eq of int
41   | Gt of int
42   | Lt of int
43
44 type rating_criterion =
45   [ `Hits   (** order by number of hits, most used objects first *)
46   ]
47
48 let default_tables =
49    (library_obj_tbl,library_rel_tbl,library_sort_tbl,library_count_tbl)
50
51 let current_tables () = 
52   (obj_tbl (),rel_tbl (),sort_tbl (), count_tbl ())
53
54 let tbln n = "table" ^ string_of_int n
55
56 (*
57 let add_depth_constr depth_opt cur_tbl where =
58   match depth_opt with
59   | None -> where
60   | Some depth -> (sprintf "%s.h_depth = %d" cur_tbl depth) :: where
61 *)
62
63 let mk_positions positions cur_tbl =
64   "(" ^
65   String.concat " or "
66     (List.map
67       (fun pos ->
68         let pos_str = MetadataPp.pp_position_tag pos in
69         match pos with
70         | `InBody
71         | `InConclusion
72         | `InHypothesis
73         | `MainConclusion None
74         | `MainHypothesis None ->
75             sprintf "%s.h_position = \"%s\"" cur_tbl pos_str
76         | `MainConclusion (Some r)
77         | `MainHypothesis (Some r) ->
78             let depth = MetadataPp.pp_relation r in
79             sprintf "(%s.h_position = \"%s\" and %s.h_depth %s)"
80               cur_tbl pos_str cur_tbl depth)
81       (positions :> MetadataTypes.position list)) ^
82   ")"
83
84 let explode_card_constr = function
85   | Eq card -> "=", card
86   | Gt card -> ">", card
87   | Lt card -> "<", card
88
89 let add_card_constr tbl col where = function
90   | None -> where
91   | Some constr ->
92       let op, card = explode_card_constr constr in
93       (* count(_utente).hypothesis = 3 *)
94       (sprintf "%s.%s %s %d" tbl col op card :: where)
95
96 let add_diff_constr tbl where = function
97   | None -> where
98   | Some constr ->
99       let op, card = explode_card_constr constr in
100       (sprintf "%s.hypothesis - %s.conclusion %s %d" tbl tbl op card :: where)
101       
102 let add_all_constr ?(tbl=library_count_tbl) (n,from,where) concl full diff =
103   match (concl, full, diff) with
104   | None, None, None -> (n,from,where)
105   | _ -> 
106       let cur_tbl = tbln n in
107       let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
108       let where = add_card_constr cur_tbl "conclusion" where concl in
109       let where = add_card_constr cur_tbl "statement" where full in
110       let where = add_diff_constr cur_tbl where diff in
111       (n+2,from, 
112         (if n > 0 then 
113           sprintf "table0.source = %s.source" cur_tbl :: where 
114         else
115           where))
116       
117
118 let add_constraint ?(start=0) ?(tables=default_tables) (n,from,where) metadata =
119   let obj_tbl,rel_tbl,sort_tbl,count_tbl = tables 
120   in
121   let cur_tbl = tbln n in
122   let start_table = tbln start in
123   match metadata with
124   | `Obj (uri, positions) ->
125       let from = (sprintf "%s as %s" obj_tbl cur_tbl) :: from in
126       let where = 
127         (sprintf "(%s.h_occurrence = \"%s\")" cur_tbl (UriManager.string_of_uri uri)) ::
128         mk_positions positions cur_tbl ::
129         (if n=start then []
130         else [sprintf "%s.source = %s.source" start_table cur_tbl]) @ 
131         where
132       in
133       ((n+2), from, where)
134   | `Rel positions ->
135       let from = (sprintf "%s as %s" rel_tbl cur_tbl) :: from in
136       let where =
137         mk_positions positions cur_tbl ::
138         (if n=start then []
139         else [sprintf "%s.source = %s.source" start_table cur_tbl]) @ 
140         where
141       in
142       ((n+2), from, where)
143   | `Sort (sort, positions) ->
144       let sort_str = CicPp.ppsort sort in
145       let from = (sprintf "%s as %s" sort_tbl cur_tbl) :: from in
146       let where =
147         (sprintf "%s.h_sort = \"%s\"" cur_tbl sort_str ) ::
148             mk_positions positions cur_tbl ::
149         (if n=start then 
150           []
151         else 
152           [sprintf "%s.source = %s.source" start_table cur_tbl ]) @ where
153       in
154       ((n+2), from, where)
155
156 let exec ~(dbd:HMysql.dbd) ?rating (n,from,where) =
157   let from = String.concat ", " from in
158   let where = String.concat " and " where in
159   let query =
160     match rating with
161     | None -> sprintf "select distinct table0.source from %s where %s" from where
162     | Some `Hits ->
163         sprintf
164           ("select distinct table0.source from %s, hits where %s
165             and table0.source = hits.source order by hits.no desc")
166           from where 
167   in
168   (* prerr_endline query; *) 
169   let result = HMysql.exec dbd query in
170   HMysql.map result
171     (fun row -> match row.(0) with Some s -> UriManager.uri_of_string s | _ -> assert false)
172
173
174 let at_least ~(dbd:HMysql.dbd) ?concl_card ?full_card ?diff ?rating tables
175   (metadata: MetadataTypes.constr list)
176 =
177   let obj_tbl,rel_tbl,sort_tbl, count_tbl = tables in
178   if (metadata = []) && concl_card = None && full_card = None then
179     begin
180       HLog.warn "MetadataConstraints.at_least: no constraints given";
181       []
182     end
183   else
184     let (n,from,where) =
185       List.fold_left (add_constraint ~tables) (0,[],[]) metadata
186     in
187     let (n,from,where) =
188       add_all_constr ~tbl:count_tbl (n,from,where) concl_card full_card diff
189     in
190     exec ~dbd ?rating (n,from,where)
191 ;;
192     
193 let at_least  
194   ~(dbd:HMysql.dbd) ?concl_card ?full_card ?diff ?rating
195       (metadata: MetadataTypes.constr list)
196 =
197   if are_tables_ownerized () then
198     (at_least 
199        ~dbd ?concl_card ?full_card ?diff ?rating default_tables metadata) @
200     (at_least 
201        ~dbd ?concl_card ?full_card ?diff ?rating (current_tables ()) metadata)
202   else
203     at_least 
204       ~dbd ?concl_card ?full_card ?diff ?rating default_tables metadata 
205   
206     
207   (** Prefix handling *)
208
209 let filter_by_card n =
210   SetSet.filter (fun t -> (UriManagerSet.cardinal t) <= n)
211   
212 let merge n a b = 
213   let init = SetSet.union a b in
214   let merge_single_set s1 b = 
215     SetSet.fold 
216       (fun s2 res -> SetSet.add (UriManagerSet.union s1 s2) res)
217       b SetSet.empty in
218   let res = 
219     SetSet.fold (fun s1 res -> SetSet.union (merge_single_set s1 b) res) a init
220   in
221   filter_by_card n res 
222
223 let rec inspect_children n childs =
224   List.fold_left 
225     (fun res term -> merge n (inspect_conclusion n term) res)
226     SetSet.empty childs 
227
228 and add_root n root childs =
229   let childunion = inspect_children n childs in
230   let addroot = UriManagerSet.add root in
231     SetSet.fold 
232       (fun child newsets -> SetSet.add (addroot child) newsets)
233       childunion 
234       (SetSet.singleton (UriManagerSet.singleton root))
235
236 and inspect_conclusion n t = 
237   if n = 0 then SetSet.empty
238   else match t with
239       Cic.Rel _                    
240     | Cic.Meta _                     
241     | Cic.Sort _ 
242     | Cic.Implicit _ -> SetSet.empty 
243     | Cic.Var (u,exp_named_subst) -> SetSet.empty
244     | Cic.Const (u,exp_named_subst) -> 
245         SetSet.singleton (UriManagerSet.singleton u)
246     | Cic.MutInd (u, t, exp_named_subst) -> 
247         SetSet.singleton (UriManagerSet.singleton
248           (UriManager.uri_of_uriref u t None))
249     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
250         SetSet.singleton (UriManagerSet.singleton
251           (UriManager.uri_of_uriref u t (Some c)))
252     | Cic.Cast (t, _) -> inspect_conclusion n t
253     | Cic.Prod (_, s, t) -> 
254         merge n (inspect_conclusion n s) (inspect_conclusion n t)
255     | Cic.Lambda (_, s, t) ->
256         merge n (inspect_conclusion n s) (inspect_conclusion n t)
257     | Cic.LetIn (_, s, t) ->
258         merge n (inspect_conclusion n s) (inspect_conclusion n t)
259     | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
260         add_root (n-1) u l
261     | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
262         let uri = UriManager.uri_of_uriref u t None in
263         add_root (n-1) uri l
264     | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
265         let suri = UriManager.uri_of_uriref u t (Some c) in
266         add_root (n-1) suri l
267     | Cic.Appl l -> 
268         SetSet.empty
269     | Cic.MutCase (u, t, tt, uu, m) ->
270         SetSet.empty
271     | Cic.Fix (_, m) -> 
272         SetSet.empty
273     | Cic.CoFix (_, m) -> 
274         SetSet.empty
275
276 let rec inspect_term n t = 
277   if n = 0 then
278     assert false
279   else
280     match t with
281       Cic.Rel _                    
282     | Cic.Meta _                     
283     | Cic.Sort _ 
284     | Cic.Implicit _ -> None, SetSet.empty 
285     | Cic.Var (u,exp_named_subst) -> None, SetSet.empty
286     | Cic.Const (u,exp_named_subst) -> 
287         Some u, SetSet.empty
288     | Cic.MutInd (u, t, exp_named_subst) -> 
289         let uri = UriManager.uri_of_uriref u t None in
290         Some uri, SetSet.empty
291     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
292         let uri = UriManager.uri_of_uriref u t (Some c) in
293         Some uri, SetSet.empty
294     | Cic.Cast (t, _) -> inspect_term n t
295     | Cic.Prod (_, _, t) -> inspect_term n t
296     | Cic.LetIn (_, _, t) -> inspect_term n t
297     | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
298         let childunion = inspect_children (n-1) l in
299         Some u, childunion
300     | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
301         let suri = UriManager.uri_of_uriref u t None in
302         if u = HelmLibraryObjects.Logic.eq_URI && n>1 then
303           (* equality is handled in a special way: in particular, 
304              the type, if defined, is always added to the prefix, 
305              and n is not decremented - it should have been n-2 *)
306           match l with
307               Cic.Const (u1,exp_named_subst1)::l1 ->
308                 let inconcl = add_root (n-1) u1 l1 in
309                 Some suri, inconcl
310             | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
311                 let suri1 = UriManager.uri_of_uriref u1 t1 None in
312                 let inconcl = add_root (n-1) suri1 l1 in  
313                 Some suri, inconcl
314             | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
315                 let suri1 = UriManager.uri_of_uriref u1 t1 (Some c1) in
316                 let inconcl = add_root (n-1) suri1 l1 in  
317                 Some suri, inconcl
318             | _ :: _ -> Some suri, SetSet.empty
319             | _ -> assert false (* args number must be > 0 *)
320         else
321           let childunion = inspect_children (n-1) l in
322           Some suri, childunion
323     | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
324         let suri = UriManager.uri_of_uriref u t(Some c) in
325         let childunion = inspect_children (n-1) l in
326         Some suri, childunion
327     | _ -> None, SetSet.empty
328
329 let add_cardinality s =
330   let l = SetSet.elements s in
331   let res = 
332     List.map 
333       (fun set -> 
334          let el = UriManagerSet.elements set in
335          (List.length el, el)) l in
336     (* ordered by descending cardinality *)
337     List.sort (fun (n,_) (m,_) -> m - n) ((0,[])::res)
338
339 let prefixes n t =
340   match inspect_term n t with
341       Some a, set -> Some a, add_cardinality set
342     | None, set when (SetSet.is_empty set) -> None, []
343     | _, _ -> assert false
344
345
346 let rec add children =
347   List.fold_left
348     (fun acc t -> UriManagerSet.union (signature_concl t) acc)
349     (UriManagerSet.empty) children
350   
351 (* this function creates the set of all different constants appearing in 
352    the conclusion of the term *)
353 and signature_concl = 
354   function
355       Cic.Rel _                    
356     | Cic.Meta _                     
357     | Cic.Sort _ 
358     | Cic.Implicit _ -> UriManagerSet.empty 
359     | Cic.Var (u,exp_named_subst) ->
360        (*CSC: TODO if the var has a body it must be processed *)
361        UriManagerSet.empty
362     | Cic.Const (u,exp_named_subst) -> 
363         UriManagerSet.singleton u
364     | Cic.MutInd (u, t, exp_named_subst) -> 
365         let uri = UriManager.uri_of_uriref u t None in
366         UriManagerSet.singleton uri
367     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
368         let uri = UriManager.uri_of_uriref u t (Some c) in
369         UriManagerSet.singleton uri
370     | Cic.Cast (t, _) -> signature_concl t
371     | Cic.Prod (_, s, t) -> 
372         UriManagerSet.union (signature_concl s) (signature_concl t)
373     | Cic.Lambda (_, s, t) ->
374         UriManagerSet.union (signature_concl s) (signature_concl t)
375     | Cic.LetIn (_, s, t) ->
376         UriManagerSet.union (signature_concl s) (signature_concl t)
377     | Cic.Appl l  -> add l
378     | Cic.MutCase _
379     | Cic.Fix _
380     | Cic.CoFix _ ->
381         UriManagerSet.empty
382
383 let rec signature_of = function
384   | Cic.Cast (t, _)      -> signature_of t
385   | Cic.Prod (_, _, t)   -> signature_of t               
386   | Cic.LetIn (_, _, t) -> signature_of t
387   | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
388       Some (u, []), add l
389   | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
390       let suri = UriManager.uri_of_uriref u t None in
391        if LibraryObjects.is_eq_URI u then 
392           (* equality is handled in a special way: in particular, 
393              the type, if defined, is always added to the prefix, 
394              and n is not decremented - it should have been n-2 *)
395       match l with
396           Cic.Const (u1,exp_named_subst1)::l1 ->
397             let inconcl = UriManagerSet.remove u1 (add l1) in
398             Some (suri, [u1]), inconcl
399         | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
400             let suri1 = UriManager.uri_of_uriref u1 t1 None in
401             let inconcl =  UriManagerSet.remove suri1 (add l1) in
402               Some (suri, [suri1]), inconcl
403         | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
404             let suri1 = UriManager.uri_of_uriref u1 t1 (Some c1) in
405             let inconcl =  UriManagerSet.remove suri1 (add l1) in
406             Some (suri, [suri1]), inconcl
407         | _ :: tl -> Some (suri, []), add tl
408         | _ -> assert false (* args number must be > 0 *)
409       else
410         Some (suri, []), add l
411   | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
412       let suri = UriManager.uri_of_uriref u t (Some c) in
413       Some (suri, []), add l
414   | t -> None, signature_concl t
415
416 (* takes a list of lists and returns the list of all elements
417    without repetitions *)
418 let union l = 
419   let rec drop_repetitions = function
420       [] -> []
421     | [a] -> [a]
422     | u1::u2::l when u1 = u2 -> drop_repetitions (u2::l)
423     | u::l -> u::(drop_repetitions l) in
424   drop_repetitions (List.sort Pervasives.compare (List.concat l))
425
426 let must_of_prefix ?(where = `Conclusion) m s =
427   let positions =
428     match where with
429     | `Conclusion -> [`InConclusion]
430     | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
431   in
432   let positions =
433    if m = None then `MainConclusion None :: positions else positions in
434   let s' = List.map (fun (u:UriManager.uri) -> `Obj (u, positions)) s in
435    match m with
436       None -> s'
437     | Some m -> `Obj (m, [`MainConclusion None]) :: s'
438
439 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
440
441 let get_constants (dbd:HMysql.dbd) ~where uri =
442   let uri = escape (UriManager.string_of_uri uri) in
443   let positions =
444     match where with
445     | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
446     | `Statement ->
447         [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
448           MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
449   in
450   let query = 
451     let pos_predicate =
452       String.concat " OR "
453         (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
454     in
455     sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION "^^
456              "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)")
457       (MetadataTypes.obj_tbl ()) uri pos_predicate
458       MetadataTypes.library_obj_tbl uri pos_predicate
459       
460   in
461   let result = HMysql.exec dbd query in
462   let set = ref UriManagerSet.empty in
463   HMysql.iter result
464     (fun col ->
465       match col.(0) with
466       | Some uri -> set := UriManagerSet.add (UriManager.uri_of_string uri) !set
467       | _ -> assert false);
468   !set
469
470 let at_most ~(dbd:HMysql.dbd) ?(where = `Conclusion) only u =
471   let inconcl = get_constants dbd ~where u in
472   UriManagerSet.subset inconcl only
473
474   (* Special handling of equality. The problem is filtering out theorems just
475   * containing variables (e.g. all the theorems in cic:/Coq/Ring/). Really
476   * ad-hoc, no better solution found at the moment *)
477 let myspeciallist_of_facts  =
478   [0,UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
479 let myspeciallist =
480   [0,UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
481    (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
482    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/trans_eq.con";
483    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal.con";
484    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal2.con";
485    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal3.con"]
486
487
488 let compute_exactly ~(dbd:HMysql.dbd) ?(facts=false) ~where main prefixes =
489   List.concat
490     (List.map 
491       (fun (m,s) -> 
492         let is_eq,card =
493          match main with
494             None -> false,m
495           | Some main ->
496              (m = 0 &&
497               UriManager.eq main
498                (UriManager.uri_of_string (HelmLibraryObjects.Logic.eq_XURI))),
499              m+1
500         in
501         if m = 0 && is_eq then
502           (if facts then myspeciallist_of_facts
503            else myspeciallist)
504         else
505           let res =
506            (* this gets rid of the ~750 objects of type Set/Prop/Type *)
507            if card = 0 then []
508            else
509             let must = must_of_prefix ~where main s in
510             match where with
511             | `Conclusion -> at_least ~dbd ~concl_card:(Eq card) must
512             | `Statement -> at_least ~dbd ~full_card:(Eq card) must
513           in
514           List.map (fun uri -> (card, uri)) res)
515       prefixes)
516
517   (* critical value reached, fallback to "only" constraints *)
518
519 let compute_with_only ~(dbd:HMysql.dbd) ?(facts=false) ?(where = `Conclusion) 
520   main prefixes constants
521 =
522   let max_prefix_length = 
523     match prefixes with
524     | [] -> assert false 
525     | (max,_)::_ -> max in
526   let maximal_prefixes = 
527     let rec filter res = function 
528         [] -> res
529       | (n,s)::l when n = max_prefix_length -> filter ((n,s)::res) l
530       | _::_-> res in
531     filter [] prefixes in
532     let greater_than =
533     let all =
534       union
535         (List.map 
536           (fun (m,s) -> 
537             let card = if main = None then m else m + 1 in
538             let must = must_of_prefix ~where main s in
539             (let res = 
540               match where with
541               | `Conclusion -> at_least ~dbd ~concl_card:(Gt card) must
542               | `Statement -> at_least ~dbd ~full_card:(Gt card) must
543             in
544             (* we tag the uri with m+1, for sorting purposes *)
545             List.map (fun uri -> (card, uri)) res))
546           maximal_prefixes)
547     in
548 (*     Printf.fprintf stderr "all: %d\n" (List.length all);flush_all (); *)
549 (*
550     List.filter (function (_,uri) -> 
551       at_most ~dbd ~where constants uri) 
552 *)
553     all 
554     in
555   let equal_to = compute_exactly ~dbd ~facts ~where main prefixes in
556     greater_than @ equal_to
557
558   (* real match query implementation *)
559
560 let cmatch ~(dbd:HMysql.dbd)  ?(facts=false) t =
561   let (main, constants) = signature_of t in
562   match main with
563   | None -> []
564   | Some (main, types) ->
565       (* the type of eq is not counted in constants_no *)
566       let types_no = List.length types in
567       let constants_no = UriManagerSet.cardinal constants in
568       if (constants_no > critical_value) then 
569         let prefixes = prefixes just_factor t in
570         (match prefixes with
571         | Some main, all_concl ->
572             let all_constants = 
573               List.fold_right UriManagerSet.add types (UriManagerSet.add main constants)
574             in
575             compute_with_only ~dbd ~facts (Some main) all_concl all_constants
576          | _, _ -> [])
577       else
578         (* in this case we compute all prefixes, and we do not need
579            to apply the only constraints *)
580         let prefixes =
581           if constants_no = 0 then
582             (if types_no = 0 then
583                Some main, [0, []]
584              else
585                Some main, [0, []; types_no, types])
586           else
587             prefixes (constants_no+types_no+1) t
588         in
589         (match prefixes with
590            Some main, all_concl ->
591             compute_exactly ~dbd ~facts ~where:`Conclusion (Some main) all_concl
592          | _, _ -> [])
593
594 let power_upto upto consts =
595   let l = UriManagerSet.elements consts in
596   List.sort (fun (n,_) (m,_) -> m - n)
597   (List.fold_left 
598     (fun res a ->
599        let res' = 
600          List.filter (function (n,l) -> n <= upto)
601            (List.map (function (n,l) -> (n+1,a::l)) res) in
602          res@res')
603      [(0,[])] l)
604
605 let power consts =
606   let l = UriManagerSet.elements consts in
607   List.sort (fun (n,_) (m,_) -> m - n)
608   (List.fold_left 
609     (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res)) 
610      [(0,[])] l)
611
612 type where = [ `Conclusion | `Statement ]
613
614 let sigmatch ~(dbd:HMysql.dbd) ?(facts=false) ?(where = `Conclusion)
615  (main, constants)
616 =
617  let main,types =
618    match main with
619      None -> None,[]
620    | Some (main, types) -> Some main,types
621  in
622   let constants_no = UriManagerSet.cardinal constants in
623   (* prerr_endline (("constants_no: ")^(string_of_int constants_no)); *)
624   if (constants_no > critical_value) then 
625     let subsets = 
626       let subsets = power_upto just_factor constants in
627       (* let _ = prerr_endline (("subsets: ")^
628          (string_of_int (List.length subsets))) in *)
629       let types_no = List.length types in 
630         if types_no > 0 then  
631           List.map (function (n,l) -> (n+types_no,types@l)) subsets
632         else subsets
633     in
634     prerr_endline ("critical_value exceded..." ^ string_of_int constants_no);
635     let all_constants = 
636      let all = match main with None -> types | Some m -> m::types in
637       List.fold_right UriManagerSet.add all constants
638     in
639      compute_with_only ~dbd ~where main subsets all_constants
640   else
641     (prerr_endline ("all subsets..." ^ string_of_int constants_no);
642     let subsets = 
643       let subsets = power constants in
644       let types_no = List.length types in
645        if types_no > 0 then  
646         (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
647        else subsets
648     in
649        prerr_endline "fine1";
650        compute_exactly ~dbd ~facts ~where main subsets)
651
652   (* match query wrappers *)
653
654 let cmatch'= cmatch 
655
656 let cmatch ~dbd ?(facts=false) term =
657   List.map snd
658     (List.sort
659       (fun x y -> Pervasives.compare (fst y) (fst x))
660       (cmatch' ~dbd ~facts term))
661
662 let constants_of = signature_concl
663