]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/metadata/metadataConstraints.ml
cosmetic changes
[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 = 4
31
32 module StringSet = Set.Make (String)
33 module SetSet = Set.Make (StringSet)
34
35 type term_signature = (string * string list) option * StringSet.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 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:Mysql.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 table0.source from %s where %s" from where
160     | Some `Hits ->
161         sprintf
162           ("select 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 = Mysql.exec dbd query in
168   Mysql.map result
169     (fun row -> match row.(0) with Some s -> s | _ -> assert false)
170
171
172 let at_least ~(dbd:Mysql.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:Mysql.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 -> (StringSet.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 (StringSet.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 = StringSet.add root in
225     SetSet.fold 
226       (fun child newsets -> SetSet.add (addroot child) newsets)
227       childunion 
228       (SetSet.singleton (StringSet.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 (StringSet.singleton (UriManager.string_of_uri u))
240     | Cic.MutInd (u, t, exp_named_subst) -> 
241         SetSet.singleton (StringSet.singleton
242           (UriManager.string_of_uriref (u, [t])))
243     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
244         SetSet.singleton (StringSet.singleton
245           (UriManager.string_of_uriref (u, [t; 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         let suri = UriManager.string_of_uri u in
255         add_root (n-1) suri l
256     | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
257         let suri = UriManager.string_of_uriref (u, [t]) in
258         add_root (n-1) suri l
259     | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
260         let suri = UriManager.string_of_uriref (u, [t; c]) in
261         add_root (n-1) suri l
262     | Cic.Appl l -> 
263         SetSet.empty
264     | Cic.MutCase (u, t, tt, uu, m) ->
265         SetSet.empty
266     | Cic.Fix (_, m) -> 
267         SetSet.empty
268     | Cic.CoFix (_, m) -> 
269         SetSet.empty
270
271 let rec inspect_term n t = 
272   if n = 0 then
273     assert false
274   else
275     match t with
276       Cic.Rel _                    
277     | Cic.Meta _                     
278     | Cic.Sort _ 
279     | Cic.Implicit _ -> None, SetSet.empty 
280     | Cic.Var (u,exp_named_subst) -> None, SetSet.empty
281     | Cic.Const (u,exp_named_subst) -> 
282         Some (UriManager.string_of_uri u), SetSet.empty
283     | Cic.MutInd (u, t, exp_named_subst) -> 
284         let uri = UriManager.string_of_uriref (u, [t]) in
285         Some uri, SetSet.empty
286     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
287         let uri = UriManager.string_of_uriref (u, [t; c]) in
288         Some uri, SetSet.empty
289     | Cic.Cast (t, _) -> inspect_term n t
290     | Cic.Prod (_, _, t) -> inspect_term n t
291     | Cic.LetIn (_, _, t) -> inspect_term n t
292     | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
293         let suri = UriManager.string_of_uri u in
294         let childunion = inspect_children (n-1) l in
295         Some suri, childunion
296     | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
297         let suri = UriManager.string_of_uriref (u, [t]) 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 suri1 = UriManager.string_of_uri u1 in
305                 let inconcl = add_root (n-1) suri1 l1 in
306                 Some suri, inconcl
307             | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
308                 let suri1 = UriManager.string_of_uriref (u1, [t1]) in
309                 let inconcl = add_root (n-1) suri1 l1 in  
310                 Some suri, inconcl
311             | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
312                 let suri1 = UriManager.string_of_uriref (u1, [t1; c1]) in
313                 let inconcl = add_root (n-1) suri1 l1 in  
314                 Some suri, inconcl
315             | _ :: _ -> Some suri, SetSet.empty
316             | _ -> assert false (* args number must be > 0 *)
317         else
318           let childunion = inspect_children (n-1) l in
319           Some suri, childunion
320     | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
321         let suri = UriManager.string_of_uriref (u, [t; c]) in
322         let childunion = inspect_children (n-1) l in
323         Some suri, childunion
324     | _ -> None, SetSet.empty
325
326 let add_cardinality s =
327   let l = SetSet.elements s in
328   let res = 
329     List.map 
330       (fun set -> 
331          let el = StringSet.elements set in
332          (List.length el, el)) l in
333     (* ordered by descending cardinality *)
334     List.sort (fun (n,_) (m,_) -> m - n) ((0,[])::res)
335
336 let prefixes n t =
337   match inspect_term n t with
338       Some a, set -> Some a, add_cardinality set
339     | None, set when (SetSet.is_empty set) -> None, []
340     | _, _ -> assert false
341
342
343 let rec add children =
344   List.fold_left
345     (fun acc t -> StringSet.union (signature_concl t) acc)
346     (StringSet.empty) children
347   
348 (* this function creates the set of all different constants appearing in 
349    the conclusion of the term *)
350 and signature_concl = 
351   function
352       Cic.Rel _                    
353     | Cic.Meta _                     
354     | Cic.Sort _ 
355     | Cic.Implicit _ -> StringSet.empty 
356     | Cic.Var (u,exp_named_subst) -> StringSet.empty
357     | Cic.Const (u,exp_named_subst) -> 
358         StringSet.singleton (UriManager.string_of_uri u)
359     | Cic.MutInd (u, t, exp_named_subst) -> 
360         let uri = UriManager.string_of_uriref (u, [t]) in
361         StringSet.singleton uri
362     | Cic.MutConstruct (u, t, c, exp_named_subst) -> 
363         let uri = UriManager.string_of_uriref (u, [t;c]) in
364         StringSet.singleton uri
365     | Cic.Cast (t, _) -> signature_concl t
366     | Cic.Prod (_, s, t) -> 
367         StringSet.union (signature_concl s) (signature_concl t)
368     | Cic.Lambda (_, s, t) ->
369         StringSet.union (signature_concl s) (signature_concl t)
370     | Cic.LetIn (_, s, t) ->
371         StringSet.union (signature_concl s) (signature_concl t)
372     | Cic.Appl l  -> add l
373     | Cic.MutCase _
374     | Cic.Fix _
375     | Cic.CoFix _ ->
376         StringSet.empty
377
378 let rec signature_of = function
379   | Cic.Cast (t, _)      -> signature_of t
380   | Cic.Prod (_, _, t)   -> signature_of t               
381   | Cic.LetIn (_, _, t) -> signature_of t
382   | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
383       let suri = UriManager.string_of_uri u in
384       Some (suri, []), add l
385   | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
386       let suri = UriManager.string_of_uriref (u, [t]) 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 suri1 = UriManager.string_of_uri u1 in
394             let inconcl = StringSet.remove suri1 (add l1) in
395             Some (suri, [suri1]), inconcl
396         | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
397             let suri1 = UriManager.string_of_uriref (u1, [t1]) in
398             let inconcl =  StringSet.remove suri1 (add l1) in
399               Some (suri, [suri1]), inconcl
400         | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
401             let suri1 = UriManager.string_of_uriref (u1, [t1;c1]) in
402             let inconcl =  StringSet.remove suri1 (add l1) in
403             Some (suri, [suri1]), inconcl
404         | _ :: _ -> Some (suri, []), StringSet.empty
405         | _ -> assert false (* args number must be > 0 *)
406       else
407         Some (suri, []), add l
408   | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l)  ->
409       let suri = UriManager.string_of_uriref (u, [t;c]) in
410       Some (suri, []), add l
411   | t -> None, signature_concl t
412
413 (* takes a list of lists and returns the list of all elements
414    without repetitions *)
415 let union l = 
416   let rec drop_repetitions = function
417       [] -> []
418     | [a] -> [a]
419     | u1::u2::l when u1 = u2 -> drop_repetitions (u2::l)
420     | u::l -> u::(drop_repetitions l) in
421   drop_repetitions (List.sort Pervasives.compare (List.concat l))
422
423 let must_of_prefix ?(where = `Conclusion) m s =
424   let positions =
425     match where with
426     | `Conclusion -> [`InConclusion]
427     | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
428   in
429   let s' = List.map (fun u -> `Obj (u, positions)) s in
430   `Obj (m, [`MainConclusion None]) :: s'
431
432 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
433
434 let get_constants (dbd:Mysql.dbd) ~where uri =
435   let uri = escape uri in
436   let positions =
437     match where with
438     | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
439     | `Statement ->
440         [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
441           MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
442   in
443   let query = 
444     let pos_predicate =
445       String.concat " OR "
446         (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
447     in
448     sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION "^^
449              "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)")
450       (MetadataTypes.obj_tbl ()) uri pos_predicate
451       MetadataTypes.library_obj_tbl uri pos_predicate
452       
453   in
454   let result = Mysql.exec dbd query in
455   let set = ref StringSet.empty in
456   Mysql.iter result
457     (fun col ->
458       match col.(0) with
459       | Some uri -> set := StringSet.add uri !set
460       | _ -> assert false);
461   !set
462
463 let at_most ~(dbd:Mysql.dbd) ?(where = `Conclusion) only u =
464   let inconcl = get_constants dbd ~where u in
465   StringSet.subset inconcl only
466
467   (* Special handling of equality. The problem is filtering out theorems just
468   * containing variables (e.g. all the theorems in cic:/Coq/Ring/). Really
469   * ad-hoc, no better solution found at the moment *)
470 let myspeciallist_of_facts  =
471   [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
472 let myspeciallist =
473   [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
474    (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
475    0,"cic:/Coq/Init/Logic/trans_eq.con";
476    0,"cic:/Coq/Init/Logic/f_equal.con";
477    0,"cic:/Coq/Init/Logic/f_equal2.con";
478    0,"cic:/Coq/Init/Logic/f_equal3.con"]
479
480
481 let compute_exactly ~(dbd:Mysql.dbd) ?(facts=false) ~where main prefixes =
482   List.concat
483     (List.map 
484       (fun (m,s) -> 
485         if ((m = 0) && (main = HelmLibraryObjects.Logic.eq_XURI)) then
486           (if facts then myspeciallist_of_facts
487            else myspeciallist)
488         else
489           let res =
490             let must = must_of_prefix ~where main s in
491             match where with
492             | `Conclusion -> at_least ~dbd ~concl_card:(Eq (m+1)) must
493             | `Statement -> at_least ~dbd ~full_card:(Eq (m+1)) must
494           in
495           List.map (fun uri -> (m, uri)) res)
496       prefixes)
497
498   (* critical value reached, fallback to "only" constraints *)
499
500 let compute_with_only ~(dbd:Mysql.dbd) ?(facts=false) ?(where = `Conclusion) 
501   main prefixes constants
502 =
503   let max_prefix_length = 
504     match prefixes with
505     | [] -> assert false 
506     | (max,_)::_ -> max in
507   let maximal_prefixes = 
508     let rec filter res = function 
509         [] -> res
510       | (n,s)::l when n = max_prefix_length -> filter ((n,s)::res) l
511       | _::_-> res in
512     filter [] prefixes in
513   let greater_than =
514     let all =
515       union
516         (List.map 
517           (fun (m,s) -> 
518             let must = must_of_prefix ~where main s in
519             (let res = 
520               match where with
521               | `Conclusion -> at_least ~dbd ~concl_card:(Gt (m+1)) must
522               | `Statement -> at_least ~dbd ~full_card:(Gt (m+1)) must
523             in
524             (* we tag the uri with m+1, for sorting purposes *)
525             List.map (fun uri -> (m+1, uri)) res))
526           maximal_prefixes)
527     in
528     List.filter (function (_,uri) -> at_most ~dbd ~where constants uri) all in
529     let equal_to = compute_exactly ~dbd ~facts ~where main prefixes in
530     greater_than @ equal_to
531
532   (* real match query implementation *)
533
534 let cmatch ~(dbd:Mysql.dbd)  ?(facts=false) t =
535   let (main, constants) = signature_of t in
536   match main with
537   | None -> []
538   | Some (main, types) ->
539       (* the type of eq is not counted in constants_no *)
540       let types_no = List.length types in
541       let constants_no = StringSet.cardinal constants in
542       if (constants_no > critical_value) then 
543         let prefixes = prefixes just_factor t in
544         (match prefixes with
545         | Some main, all_concl ->
546             let all_constants = 
547               List.fold_right StringSet.add types (StringSet.add main constants)
548             in
549             compute_with_only ~dbd ~facts main all_concl all_constants
550          | _, _ -> [])
551       else
552         (* in this case we compute all prefixes, and we do not need
553            to apply the only constraints *)
554         let prefixes =
555           if constants_no = 0 then
556             (if types_no = 0 then
557                Some main, [0, []]
558              else
559                Some main, [0, []; types_no, types])
560           else
561             prefixes (constants_no+types_no+1) t
562         in
563         (match prefixes with
564            Some main, all_concl ->
565              compute_exactly ~dbd ~facts ~where:`Conclusion main all_concl
566 (*
567              List.concat
568                (List.map 
569                   (fun (m,s) -> 
570                     let must = must_of_prefix ~where:`Conclusion main s in
571                     let res = at_least ~dbd ~concl_card:(Eq (m+1)) must in
572                     List.map (fun uri -> (m, uri)) res)
573                   all_concl) *)
574          | _, _ -> [])
575
576 let power_upto upto consts =
577   let l = StringSet.elements consts in
578   List.sort (fun (n,_) (m,_) -> m - n)
579   (List.fold_left 
580     (fun res a ->
581        List.filter (function (n,l) -> n <= upto)
582        res@(List.map (function (n,l) -> (n+1,a::l)) res)) 
583      [(0,[])] l)
584
585 let power consts =
586   let l = StringSet.elements consts in
587   List.sort (fun (n,_) (m,_) -> m - n)
588   (List.fold_left 
589     (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res)) 
590      [(0,[])] l)
591
592 type where = [ `Conclusion | `Statement ]
593
594 let sigmatch ~(dbd:Mysql.dbd) 
595   ?(facts=false) ?(where = `Conclusion) (main, constants) =
596   match main with
597     None -> []
598   | Some (main, types) ->
599       let constants_no = StringSet.cardinal constants in
600       if (constants_no > critical_value) then 
601         let subsets = 
602           let subsets = power_upto just_factor constants in
603           let types_no = List.length types in
604           List.map (function (n,l) -> (n+types_no,types@l)) subsets
605         in
606         let all_constants = 
607           List.fold_right StringSet.add types (StringSet.add main constants)
608         in
609         compute_with_only ~dbd ~where main subsets all_constants
610       else
611         let subsets = 
612           let subsets = power constants in
613           let types_no = List.length types in
614           if types_no > 0 then  
615           (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
616           else subsets
617         in
618         compute_exactly ~dbd ~facts ~where main subsets
619
620   (* match query wrappers *)
621
622 let cmatch'= cmatch 
623
624 let cmatch ~dbd ?(facts=false) term =
625   List.map snd
626     (List.sort
627       (fun x y -> Pervasives.compare (fst y) (fst x))
628       (cmatch' ~dbd ~facts term))
629
630 let constants_of = signature_concl
631