]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/metadata/metadataConstraints.ml
test branch
[helm.git] / helm / ocaml / 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 = 3
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 
178   in
179   if (metadata = []) && concl_card = None && full_card = None then
180     failwith "MetadataQuery.at_least: no constraints given";
181   let (n,from,where) =
182     List.fold_left (add_constraint ~tables) (0,[],[]) metadata
183   in
184   let (n,from,where) =
185     add_all_constr ~tbl:count_tbl (n,from,where) concl_card full_card diff
186   in
187   exec ~dbd ?rating (n,from,where)
188     
189 let at_least  
190   ~(dbd:HMysql.dbd) ?concl_card ?full_card ?diff ?rating
191       (metadata: MetadataTypes.constr list)
192 =
193   if are_tables_ownerized () then
194     (at_least 
195        ~dbd ?concl_card ?full_card ?diff ?rating default_tables metadata) @
196     (at_least 
197        ~dbd ?concl_card ?full_card ?diff ?rating (current_tables ()) metadata)
198   else
199     at_least 
200       ~dbd ?concl_card ?full_card ?diff ?rating default_tables metadata 
201   
202     
203   (** Prefix handling *)
204
205 let filter_by_card n =
206   SetSet.filter (fun t -> (UriManagerSet.cardinal t) <= n)
207   
208 let merge n a b = 
209   let init = SetSet.union a b in
210   let merge_single_set s1 b = 
211     SetSet.fold 
212       (fun s2 res -> SetSet.add (UriManagerSet.union s1 s2) res)
213       b SetSet.empty in
214   let res = 
215     SetSet.fold (fun s1 res -> SetSet.union (merge_single_set s1 b) res) a init
216   in
217   filter_by_card n res 
218
219 let rec inspect_children n childs =
220   List.fold_left 
221     (fun res term -> merge n (inspect_conclusion n term) res)
222     SetSet.empty childs 
223
224 and add_root n root childs =
225   let childunion = inspect_children n childs in
226   let addroot = UriManagerSet.add root in
227     SetSet.fold 
228       (fun child newsets -> SetSet.add (addroot child) newsets)
229       childunion 
230       (SetSet.singleton (UriManagerSet.singleton root))
231
232 and inspect_conclusion n t = 
233   if n = 0 then SetSet.empty
234   else match t with
235       Cic.Rel _                    
236     | Cic.Meta _                     
237     | Cic.Sort _ 
238     | Cic.Implicit _ -> SetSet.empty 
239     | Cic.Var (u,exp_named_subst) -> SetSet.empty
240     | Cic.Const (u,exp_named_subst) -> 
241         SetSet.singleton (UriManagerSet.singleton u)
242     | Cic.MutInd (u, t, exp_named_subst) -> 
243         SetSet.singleton (UriManagerSet.singleton
244           (UriManager.uri_of_uriref u t None))
245     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
246         SetSet.singleton (UriManagerSet.singleton
247           (UriManager.uri_of_uriref u t (Some c)))
248     | Cic.Cast (t, _) -> inspect_conclusion n t
249     | Cic.Prod (_, s, t) -> 
250         merge n (inspect_conclusion n s) (inspect_conclusion n t)
251     | Cic.Lambda (_, s, t) ->
252         merge n (inspect_conclusion n s) (inspect_conclusion n t)
253     | Cic.LetIn (_, s, t) ->
254         merge n (inspect_conclusion n s) (inspect_conclusion n t)
255     | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
256         add_root (n-1) u l
257     | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
258         let uri = UriManager.uri_of_uriref u t None in
259         add_root (n-1) uri l
260     | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
261         let suri = UriManager.uri_of_uriref u t (Some c) in
262         add_root (n-1) suri l
263     | Cic.Appl l -> 
264         SetSet.empty
265     | Cic.MutCase (u, t, tt, uu, m) ->
266         SetSet.empty
267     | Cic.Fix (_, m) -> 
268         SetSet.empty
269     | Cic.CoFix (_, m) -> 
270         SetSet.empty
271
272 let rec inspect_term n t = 
273   if n = 0 then
274     assert false
275   else
276     match t with
277       Cic.Rel _                    
278     | Cic.Meta _                     
279     | Cic.Sort _ 
280     | Cic.Implicit _ -> None, SetSet.empty 
281     | Cic.Var (u,exp_named_subst) -> None, SetSet.empty
282     | Cic.Const (u,exp_named_subst) -> 
283         Some u, SetSet.empty
284     | Cic.MutInd (u, t, exp_named_subst) -> 
285         let uri = UriManager.uri_of_uriref u t None in
286         Some uri, SetSet.empty
287     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
288         let uri = UriManager.uri_of_uriref u t (Some c) in
289         Some uri, SetSet.empty
290     | Cic.Cast (t, _) -> inspect_term n t
291     | Cic.Prod (_, _, t) -> inspect_term n t
292     | Cic.LetIn (_, _, t) -> inspect_term n t
293     | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
294         let childunion = inspect_children (n-1) l in
295         Some u, childunion
296     | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
297         let suri = UriManager.uri_of_uriref u t None in
298         if u = HelmLibraryObjects.Logic.eq_URI && n>1 then
299           (* equality is handled in a special way: in particular, 
300              the type, if defined, is always added to the prefix, 
301              and n is not decremented - it should have been n-2 *)
302           match l with
303               Cic.Const (u1,exp_named_subst1)::l1 ->
304                 let inconcl = add_root (n-1) u1 l1 in
305                 Some suri, inconcl
306             | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
307                 let suri1 = UriManager.uri_of_uriref u1 t1 None in
308                 let inconcl = add_root (n-1) suri1 l1 in  
309                 Some suri, inconcl
310             | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
311                 let suri1 = UriManager.uri_of_uriref u1 t1 (Some c1) in
312                 let inconcl = add_root (n-1) suri1 l1 in  
313                 Some suri, inconcl
314             | _ :: _ -> Some suri, SetSet.empty
315             | _ -> assert false (* args number must be > 0 *)
316         else
317           let childunion = inspect_children (n-1) l in
318           Some suri, childunion
319     | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
320         let suri = UriManager.uri_of_uriref u t(Some c) in
321         let childunion = inspect_children (n-1) l in
322         Some suri, childunion
323     | _ -> None, SetSet.empty
324
325 let add_cardinality s =
326   let l = SetSet.elements s in
327   let res = 
328     List.map 
329       (fun set -> 
330          let el = UriManagerSet.elements set in
331          (List.length el, el)) l in
332     (* ordered by descending cardinality *)
333     List.sort (fun (n,_) (m,_) -> m - n) ((0,[])::res)
334
335 let prefixes n t =
336   match inspect_term n t with
337       Some a, set -> Some a, add_cardinality set
338     | None, set when (SetSet.is_empty set) -> None, []
339     | _, _ -> assert false
340
341
342 let rec add children =
343   List.fold_left
344     (fun acc t -> UriManagerSet.union (signature_concl t) acc)
345     (UriManagerSet.empty) children
346   
347 (* this function creates the set of all different constants appearing in 
348    the conclusion of the term *)
349 and signature_concl = 
350   function
351       Cic.Rel _                    
352     | Cic.Meta _                     
353     | Cic.Sort _ 
354     | Cic.Implicit _ -> UriManagerSet.empty 
355     | Cic.Var (u,exp_named_subst) ->
356        (*CSC: TODO if the var has a body it must be processed *)
357        UriManagerSet.empty
358     | Cic.Const (u,exp_named_subst) -> 
359         UriManagerSet.singleton u
360     | Cic.MutInd (u, t, exp_named_subst) -> 
361         let uri = UriManager.uri_of_uriref u t None in
362         UriManagerSet.singleton uri
363     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
364         let uri = UriManager.uri_of_uriref u t (Some c) in
365         UriManagerSet.singleton uri
366     | Cic.Cast (t, _) -> signature_concl t
367     | Cic.Prod (_, s, t) -> 
368         UriManagerSet.union (signature_concl s) (signature_concl t)
369     | Cic.Lambda (_, s, t) ->
370         UriManagerSet.union (signature_concl s) (signature_concl t)
371     | Cic.LetIn (_, s, t) ->
372         UriManagerSet.union (signature_concl s) (signature_concl t)
373     | Cic.Appl l  -> add l
374     | Cic.MutCase _
375     | Cic.Fix _
376     | Cic.CoFix _ ->
377         UriManagerSet.empty
378
379 let rec signature_of = function
380   | Cic.Cast (t, _)      -> signature_of t
381   | Cic.Prod (_, _, t)   -> signature_of t               
382   | Cic.LetIn (_, _, t) -> signature_of t
383   | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
384       Some (u, []), add l
385   | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
386       let suri = UriManager.uri_of_uriref u t None in
387       if u = HelmLibraryObjects.Logic.eq_URI then
388           (* equality is handled in a special way: in particular, 
389              the type, if defined, is always added to the prefix, 
390              and n is not decremented - it should have been n-2 *)
391       match l with
392           Cic.Const (u1,exp_named_subst1)::l1 ->
393             let inconcl = UriManagerSet.remove u1 (add l1) in
394             Some (suri, [u1]), inconcl
395         | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
396             let suri1 = UriManager.uri_of_uriref u1 t1 None in
397             let inconcl =  UriManagerSet.remove suri1 (add l1) in
398               Some (suri, [suri1]), inconcl
399         | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
400             let suri1 = UriManager.uri_of_uriref u1 t1 (Some c1) in
401             let inconcl =  UriManagerSet.remove suri1 (add l1) in
402             Some (suri, [suri1]), inconcl
403         | _ :: _ -> Some (suri, []), UriManagerSet.empty
404         | _ -> assert false (* args number must be > 0 *)
405       else
406         Some (suri, []), add l
407   | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
408       let suri = UriManager.uri_of_uriref u t (Some c) in
409       Some (suri, []), add l
410   | t -> None, signature_concl t
411
412 (* takes a list of lists and returns the list of all elements
413    without repetitions *)
414 let union l = 
415   let rec drop_repetitions = function
416       [] -> []
417     | [a] -> [a]
418     | u1::u2::l when u1 = u2 -> drop_repetitions (u2::l)
419     | u::l -> u::(drop_repetitions l) in
420   drop_repetitions (List.sort Pervasives.compare (List.concat l))
421
422 let must_of_prefix ?(where = `Conclusion) m s =
423   let positions =
424     match where with
425     | `Conclusion -> [`InConclusion]
426     | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
427   in
428   let positions =
429    if m = None then `MainConclusion None :: positions else positions in
430   let s' = List.map (fun (u:UriManager.uri) -> `Obj (u, positions)) s in
431    match m with
432       None -> s'
433     | Some m -> `Obj (m, [`MainConclusion None]) :: s'
434
435 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
436
437 let get_constants (dbd:HMysql.dbd) ~where uri =
438   let uri = escape (UriManager.string_of_uri uri) in
439   let positions =
440     match where with
441     | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
442     | `Statement ->
443         [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
444           MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
445   in
446   let query = 
447     let pos_predicate =
448       String.concat " OR "
449         (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
450     in
451     sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION "^^
452              "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)")
453       (MetadataTypes.obj_tbl ()) uri pos_predicate
454       MetadataTypes.library_obj_tbl uri pos_predicate
455       
456   in
457   let result = HMysql.exec dbd query in
458   let set = ref UriManagerSet.empty in
459   HMysql.iter result
460     (fun col ->
461       match col.(0) with
462       | Some uri -> set := UriManagerSet.add (UriManager.uri_of_string uri) !set
463       | _ -> assert false);
464   !set
465
466 let at_most ~(dbd:HMysql.dbd) ?(where = `Conclusion) only u =
467   let inconcl = get_constants dbd ~where u in
468   UriManagerSet.subset inconcl only
469
470   (* Special handling of equality. The problem is filtering out theorems just
471   * containing variables (e.g. all the theorems in cic:/Coq/Ring/). Really
472   * ad-hoc, no better solution found at the moment *)
473 let myspeciallist_of_facts  =
474   [0,UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
475 let myspeciallist =
476   [0,UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
477    (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
478    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/trans_eq.con";
479    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal.con";
480    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal2.con";
481    0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal3.con"]
482
483
484 let compute_exactly ~(dbd:HMysql.dbd) ?(facts=false) ~where main prefixes =
485   List.concat
486     (List.map 
487       (fun (m,s) -> 
488         let is_eq,card =
489          match main with
490             None -> false,m
491           | Some main ->
492              (m = 0 &&
493               UriManager.eq main
494                (UriManager.uri_of_string (HelmLibraryObjects.Logic.eq_XURI))),
495              m+1
496         in
497         if m = 0 && is_eq then
498           (if facts then myspeciallist_of_facts
499            else myspeciallist)
500         else
501           let res =
502            (* this gets rid of the ~750 objects of type Set/Prop/Type *)
503            if card = 0 then []
504            else
505             let must = must_of_prefix ~where main s in
506             match where with
507             | `Conclusion -> at_least ~dbd ~concl_card:(Eq card) must
508             | `Statement -> at_least ~dbd ~full_card:(Eq card) must
509           in
510           List.map (fun uri -> (card, uri)) res)
511       prefixes)
512
513   (* critical value reached, fallback to "only" constraints *)
514
515 let compute_with_only ~(dbd:HMysql.dbd) ?(facts=false) ?(where = `Conclusion) 
516   main prefixes constants
517 =
518   let max_prefix_length = 
519     match prefixes with
520     | [] -> assert false 
521     | (max,_)::_ -> max in
522   let maximal_prefixes = 
523     let rec filter res = function 
524         [] -> res
525       | (n,s)::l when n = max_prefix_length -> filter ((n,s)::res) l
526       | _::_-> res in
527     filter [] prefixes in
528     let greater_than =
529     let all =
530       union
531         (List.map 
532           (fun (m,s) -> 
533             let card = if main = None then m else m + 1 in
534             let must = must_of_prefix ~where main s in
535             (let res = 
536               match where with
537               | `Conclusion -> at_least ~dbd ~concl_card:(Gt card) must
538               | `Statement -> at_least ~dbd ~full_card:(Gt card) must
539             in
540             (* we tag the uri with m+1, for sorting purposes *)
541             List.map (fun uri -> (card, uri)) res))
542           maximal_prefixes)
543     in
544     Printf.fprintf stderr "all: %d\n" (List.length all);flush_all ();
545     List.filter (function (_,uri) -> at_most ~dbd ~where constants uri) all in
546   let equal_to = compute_exactly ~dbd ~facts ~where main prefixes in
547     greater_than @ equal_to
548
549   (* real match query implementation *)
550
551 let cmatch ~(dbd:HMysql.dbd)  ?(facts=false) t =
552   let (main, constants) = signature_of t in
553   match main with
554   | None -> []
555   | Some (main, types) ->
556       (* the type of eq is not counted in constants_no *)
557       let types_no = List.length types in
558       let constants_no = UriManagerSet.cardinal constants in
559       if (constants_no > critical_value) then 
560         let prefixes = prefixes just_factor t in
561         (match prefixes with
562         | Some main, all_concl ->
563             let all_constants = 
564               List.fold_right UriManagerSet.add types (UriManagerSet.add main constants)
565             in
566             compute_with_only ~dbd ~facts (Some main) all_concl all_constants
567          | _, _ -> [])
568       else
569         (* in this case we compute all prefixes, and we do not need
570            to apply the only constraints *)
571         let prefixes =
572           if constants_no = 0 then
573             (if types_no = 0 then
574                Some main, [0, []]
575              else
576                Some main, [0, []; types_no, types])
577           else
578             prefixes (constants_no+types_no+1) t
579         in
580         (match prefixes with
581            Some main, all_concl ->
582             compute_exactly ~dbd ~facts ~where:`Conclusion (Some main) all_concl
583          | _, _ -> [])
584
585 let power_upto upto consts =
586   let l = UriManagerSet.elements consts in
587   List.sort (fun (n,_) (m,_) -> m - n)
588   (List.fold_left 
589     (fun res a ->
590        let res' = 
591          List.filter (function (n,l) -> n <= upto)
592            (List.map (function (n,l) -> (n+1,a::l)) res) in
593          res@res')
594      [(0,[])] l)
595
596 let power consts =
597   let l = UriManagerSet.elements consts in
598   List.sort (fun (n,_) (m,_) -> m - n)
599   (List.fold_left 
600     (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res)) 
601      [(0,[])] l)
602
603 type where = [ `Conclusion | `Statement ]
604
605 let sigmatch ~(dbd:HMysql.dbd) ?(facts=false) ?(where = `Conclusion)
606  (main, constants)
607 =
608  let main,types =
609    match main with
610      None -> None,[]
611    | Some (main, types) -> Some main,types
612  in
613   let constants_no = UriManagerSet.cardinal constants in
614   (* prerr_endline (("constants_no: ")^(string_of_int constants_no)); *)
615   if (constants_no > critical_value) then 
616     let subsets = 
617       let subsets = power_upto just_factor constants in
618       (* let _ = prerr_endline (("subsets: ")^
619          (string_of_int (List.length subsets))) in *)
620       let types_no = List.length types in
621       List.map (function (n,l) -> (n+types_no,types@l)) subsets
622     in
623     let all_constants = 
624      let all = match main with None -> types | Some m -> m::types in
625       List.fold_right UriManagerSet.add all constants
626     in
627      compute_with_only ~dbd ~where main subsets all_constants
628   else
629     let subsets = 
630       let subsets = power constants in
631       let types_no = List.length types in
632        if types_no > 0 then  
633         (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
634        else subsets
635       in
636        compute_exactly ~dbd ~facts ~where main subsets
637
638   (* match query wrappers *)
639
640 let cmatch'= cmatch 
641
642 let cmatch ~dbd ?(facts=false) term =
643   List.map snd
644     (List.sort
645       (fun x y -> Pervasives.compare (fst y) (fst x))
646       (cmatch' ~dbd ~facts term))
647
648 let constants_of = signature_concl
649