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