]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/metadataQuery.ml
the decompose tactic is now working
[helm.git] / helm / ocaml / tactics / metadataQuery.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 module Constr = MetadataConstraints
29 module PET = ProofEngineTypes 
30
31 exception Goal_is_not_an_equation
32
33 let debug_print = fun _ -> ()
34
35   (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
36   * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
37 let sqlpat_of_shellglob =
38   let star_RE, qmark_RE, percent_RE, uscore_RE =
39     Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
40   in
41   fun shellglob ->
42     Pcre.replace ~rex:star_RE ~templ:"%"
43       (Pcre.replace ~rex:qmark_RE ~templ:"_"
44         (Pcre.replace ~rex:percent_RE ~templ:"\\%"
45           (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
46             shellglob)))
47
48 let nonvar uri = not (UriManager.uri_is_var uri)
49
50 let locate ~(dbd:Mysql.dbd) ?(vars = false) pat =
51   let sql_pat = sqlpat_of_shellglob pat in
52   let query =
53         sprintf ("SELECT source FROM %s WHERE value LIKE \"%s\" UNION "^^
54                  "SELECT source FROM %s WHERE value LIKE \"%s\"")
55           (MetadataTypes.name_tbl ()) sql_pat
56            MetadataTypes.library_name_tbl sql_pat
57   in
58   let result = Mysql.exec dbd query in
59   List.filter nonvar
60     (Mysql.map result
61       (fun cols -> match cols.(0) with Some s -> UriManager.uri_of_string s | _ -> assert false))
62
63 let match_term ~(dbd:Mysql.dbd) ty =
64 (*   debug_print (CicPp.ppterm ty); *)
65   let metadata = MetadataExtractor.compute ~body:None ~ty in
66   let constants_no =
67     MetadataConstraints.UriManagerSet.cardinal (MetadataConstraints.constants_of ty)
68   in
69   let full_card, diff =
70     if CicUtil.is_meta_closed ty then
71       Some (MetadataConstraints.Eq constants_no), None
72     else
73       let diff_no =
74         let (hyp_constants, concl_constants) =
75           (* collect different constants in hypotheses and conclusions *)
76           List.fold_left
77             (fun ((hyp, concl) as acc) metadata ->
78                match (metadata: MetadataTypes.metadata) with
79                | `Sort _ | `Rel _ -> acc
80                | `Obj (uri, `InConclusion) | `Obj (uri, `MainConclusion _)
81                  when not (List.mem uri concl) -> (hyp, uri :: concl)
82                | `Obj (uri, `InHypothesis) | `Obj (uri, `MainHypothesis _)
83                  when not (List.mem uri hyp) -> (uri :: hyp, concl)
84                | `Obj _ -> acc)
85             ([], [])
86             metadata
87         in
88         List.length hyp_constants - List.length concl_constants
89       in
90       let (concl_metas, hyp_metas) = MetadataExtractor.compute_metas ty in
91       let diff =
92         if MetadataExtractor.IntSet.equal concl_metas hyp_metas then
93           Some (MetadataConstraints.Eq diff_no)
94         else if MetadataExtractor.IntSet.subset concl_metas hyp_metas then
95           Some (MetadataConstraints.Gt (diff_no - 1))
96         else if MetadataExtractor.IntSet.subset hyp_metas concl_metas then
97           Some (MetadataConstraints.Lt (diff_no + 1))
98         else
99           None
100       in
101       None, diff
102   in
103   let constraints = List.map MetadataTypes.constr_of_metadata metadata in
104     Constr.at_least ~dbd ?full_card ?diff constraints
105
106 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
107
108 let signature_of_hypothesis context =
109   List.fold_left
110     (fun set hyp ->
111       match hyp with
112       | None -> set
113       | Some (_, Cic.Decl t)
114       | Some (_, Cic.Def (t, _)) ->
115           Constr.UriManagerSet.union set (Constr.constants_of t))
116     Constr.UriManagerSet.empty context
117
118 let intersect uris siguris =
119   let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
120   let set2 =
121     List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
122   in
123   let inter = Constr.UriManagerSet.inter set1 set2 in
124   List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
125
126 let at_most =
127  let profiler = CicUtil.profile "at_most" in
128  fun ~dbd ~where uri -> profiler (Constr.at_most ~dbd ~where) uri
129
130 let sigmatch =
131  let profiler = CicUtil.profile "sigmatch" in
132  fun ~dbd ~facts ~where signature ->
133   profiler (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
134
135 let filter_uris_forward ~dbd (main, constants) uris =
136   let main_uris =
137     match main with
138     | None -> []
139     | Some (main, types) -> main :: types
140   in
141   let full_signature =
142     List.fold_right Constr.UriManagerSet.add main_uris constants
143   in
144   List.filter (at_most ~dbd ~where:`Statement full_signature) uris
145
146 let filter_uris_backward ~dbd ~facts signature uris =
147   let siguris =
148     List.map snd 
149       (sigmatch ~dbd ~facts ~where:`Statement signature)
150   in
151     intersect uris siguris 
152
153 let compare_goal_list proof goal1 goal2 =
154   let _,metasenv,_,_ = proof in
155   let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
156   let (_, ey2, ty2) =  CicUtil.lookup_meta goal2 metasenv in
157   let ty_sort1,_ = 
158     CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph 
159   in
160   let ty_sort2,_ = 
161     CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph 
162   in
163   let prop1 =
164     let b,_ = 
165       CicReduction.are_convertible 
166         ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph 
167     in
168       if b then 0
169       else 1
170   in
171   let prop2 =
172     let b,_ = 
173       CicReduction.are_convertible 
174         ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph 
175     in 
176       if b then 0
177       else 1
178   in
179   prop1 - prop2
180
181 (* experimental_hint is a version of hint for experimental 
182     purposes. It uses auto_tac_verbose instead of auto tac.
183     Auto_tac verbose also returns a substitution - for the moment 
184     as a function from cic to cic, to be changed into an association
185     list in the future -. This substitution is used to build a
186     hash table of the inspected goals with their associated proofs.
187     The cose is a cut and paste of the previous one: at the end 
188     of the experimentation we shall make a choice. *)
189
190 let close_with_types s metasenv context =
191   Constr.UriManagerSet.fold 
192     (fun e bag -> 
193       let t = CicUtil.term_of_uri e in
194       let ty, _ = 
195         CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph  
196       in
197       Constr.UriManagerSet.union bag (Constr.constants_of ty)) 
198     s s
199
200 let close_with_constructors s metasenv context =
201   Constr.UriManagerSet.fold 
202     (fun e bag -> 
203       let t = CicUtil.term_of_uri e in
204       match t with
205           Cic.MutInd (uri,_,_)  
206         | Cic.MutConstruct (uri,_,_,_) ->  
207             (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
208                  Cic.InductiveDefinition(tl,_,_,_) ->
209                    snd
210                      (List.fold_left
211                         (fun (i,s) (_,_,_,cl) ->
212                            let _,s =
213                              List.fold_left 
214                                (fun (j,s) _ -> 
215                                   let curi = UriManager.uri_of_uriref uri i (Some j) in
216                                     j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
217                              (i+1,s)) (0,bag) tl)
218                | _ -> assert false)
219         | _ -> bag)
220     s s
221
222 let apply_tac_verbose =
223  let profiler = CicUtil.profile "apply_tac_verbose" in
224   fun ~term status -> profiler (PrimitiveTactics.apply_tac_verbose ~term) status
225
226 let sigmatch =
227  let profiler = CicUtil.profile "sigmatch" in
228  fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler (Constr.sigmatch ~dbd ~facts ~where) signature
229
230 let cmatch' =
231  let profiler = CicUtil.profile "cmatch'" in
232  fun ~dbd ~facts signature -> profiler (Constr.cmatch' ~dbd ~facts) signature
233
234 let signature_of_goal ~(dbd:Mysql.dbd) ((proof, goal) as status) =
235  let (_, metasenv, _, _) = proof in
236  let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
237  let main, sig_constants = Constr.signature_of ty in
238  let set = signature_of_hypothesis context in
239  let set =
240   match main with
241      None -> set
242    | Some (main,l) ->
243       List.fold_right Constr.UriManagerSet.add (main::l) set in
244  let set = Constr.UriManagerSet.union set sig_constants in
245  let all_constants_closed = close_with_types set metasenv context in
246  let uris =
247   sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
248  let uris = List.filter nonvar (List.map snd uris) in
249  let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
250   uris
251
252 let equations_for_goal ~(dbd:Mysql.dbd) ((proof, goal) as status) =
253  let (_, metasenv, _, _) = proof in
254  let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
255  let main, sig_constants = Constr.signature_of ty in
256  match main with
257      None -> raise Goal_is_not_an_equation
258    | Some (m,l) ->
259        if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then
260          let set = signature_of_hypothesis context in
261          let set = Constr.UriManagerSet.union set sig_constants in
262          let set = close_with_types set metasenv context in
263          let set = close_with_constructors set metasenv context in
264          let set = List.fold_right Constr.UriManagerSet.remove (m::l) set in
265          let uris =
266            sigmatch ~dbd ~facts:false ~where:`Statement (main,set) in
267          let uris = List.filter nonvar (List.map snd uris) in
268          let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
269            uris
270        else raise Goal_is_not_an_equation
271
272 let experimental_hint 
273   ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
274   let (_, metasenv, _, _) = proof in
275   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
276   let (uris, (main, sig_constants)) =
277     match signature with
278     | Some signature -> 
279         (sigmatch ~dbd ~facts signature, signature)
280     | None -> 
281         (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
282   in 
283   let uris = List.filter nonvar (List.map snd uris) in
284   let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
285   let types_constants =
286     match main with
287     | None -> Constr.UriManagerSet.empty
288     | Some (main, types) ->
289         List.fold_right Constr.UriManagerSet.add (main :: types)
290           Constr.UriManagerSet.empty
291   in
292   let all_constants =
293     let hyp_and_sug =
294       Constr.UriManagerSet.union
295         (signature_of_hypothesis context) 
296         sig_constants
297     in
298     let main = 
299       match main with
300       | None -> Constr.UriManagerSet.empty
301       | Some (main,_) -> 
302           let ty, _ = 
303             CicTypeChecker.type_of_aux' 
304               metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
305           in
306           Constr.constants_of ty
307     in
308     Constr.UriManagerSet.union main hyp_and_sug
309   in
310 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
311   let all_constants_closed = close_with_types all_constants metasenv context in
312   let other_constants = 
313     Constr.UriManagerSet.diff all_constants_closed types_constants
314   in
315   debug_print "all_constants_closed";
316   Constr.UriManagerSet.iter debug_print all_constants_closed;
317   debug_print "other_constants";
318   Constr.UriManagerSet.iter debug_print other_constants;
319   let uris = 
320     let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
321     if ((List.length uris < pow) or (pow <= 0))
322     then begin
323       debug_print "MetadataQuery: large sig, falling back to old method";
324       filter_uris_forward ~dbd (main, other_constants) uris
325     end else
326       filter_uris_backward ~dbd ~facts (main, other_constants) uris
327   in 
328   let rec aux = function
329     | [] -> []
330     | uri :: tl ->
331         (let status' =
332             try
333               let (subst,(proof, goal_list)) =
334                   (* debug_print ("STO APPLICANDO" ^ uri); *)
335                   apply_tac_verbose 
336                     ~term:(CicUtil.term_of_uri uri)
337                   status
338               in
339               let goal_list =
340                 List.stable_sort (compare_goal_list proof) goal_list
341               in
342               Some (uri, (subst,(proof, goal_list)))
343             with ProofEngineTypes.Fail _ -> None
344           in
345           match status' with
346           | None -> aux tl
347           | Some status' -> status' :: aux tl)
348   in
349   List.stable_sort
350     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
351       Pervasives.compare (List.length goals1) (List.length goals2))
352     (aux uris)
353
354 let new_experimental_hint 
355   ~(dbd:Mysql.dbd) ?(facts=false) ?signature ~universe
356   ((proof, goal) as status)
357 =
358   let (_, metasenv, _, _) = proof in
359   let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
360   let (uris, (main, sig_constants)) =
361     match signature with
362     | Some signature -> 
363         (sigmatch ~dbd ~facts signature, signature)
364     | None -> 
365         (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in 
366   let universe =
367    List.fold_left
368     (fun res u -> Constr.UriManagerSet.add u res)
369     Constr.UriManagerSet.empty universe in
370   let uris =
371    List.fold_left
372     (fun res (_,u) -> Constr.UriManagerSet.add u res)
373     Constr.UriManagerSet.empty uris in
374   let uris = Constr.UriManagerSet.inter uris universe in
375   let uris = Constr.UriManagerSet.elements uris in
376   let rec aux = function
377     | [] -> []
378     | uri :: tl ->
379         (let status' =
380             try
381               let (subst,(proof, goal_list)) =
382                   (* debug_print ("STO APPLICANDO" ^ uri); *)
383                   apply_tac_verbose 
384                     ~term:(CicUtil.term_of_uri uri)
385                   status
386               in
387               let goal_list =
388                 List.stable_sort (compare_goal_list proof) goal_list
389               in
390               Some (uri, (subst,(proof, goal_list)))
391             with ProofEngineTypes.Fail _ -> None
392           in
393           match status' with
394           | None -> aux tl
395           | Some status' -> status' :: aux tl)
396   in
397   List.stable_sort
398     (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
399       Pervasives.compare (List.length goals1) (List.length goals2))
400     (aux uris)
401
402 let elim ~dbd uri =
403   let constraints =
404     [`Rel [`MainConclusion None];
405      `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
406      `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
407      `Obj (uri,[`InHypothesis]);
408     ]
409   in
410   MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
411
412
413 let fill_with_dummy_constants t =
414   let rec aux i types =
415     function
416         Cic.Lambda (n,s,t) -> 
417           let dummy_uri = 
418             UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)) in
419             (aux (i+1) (s::types)
420                (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
421       | t -> t,types
422   in 
423   let t,types = aux 0 [] t in
424   t, List.rev types
425       
426 let instance ~dbd t =
427   let t',types = fill_with_dummy_constants t in 
428   let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
429 (*   List.iter 
430     (fun x -> 
431        debug_print 
432          (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x))) 
433     metadata; *)
434   let no_concl = MetadataDb.count_distinct `Conclusion metadata in
435   let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
436   let no_full = MetadataDb.count_distinct `Statement metadata in
437   let is_dummy = function
438     | `Obj(s, _) -> (String.sub (UriManager.string_of_uri s) 0 10) <> "cic:/dummy" 
439           | _ -> true 
440   in
441   let rec look_for_dummy_main = function
442           | [] -> None
443     | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_ 
444       when (String.sub (UriManager.string_of_uri s) 0 10 = "cic:/dummy") -> 
445       let s = UriManager.string_of_uri s in
446       let len = String.length s in
447             let dummy_index = int_of_string (String.sub s 11 (len-11)) in
448       let dummy_type = List.nth types dummy_index in
449       Some (d,dummy_type)
450     | _::l -> look_for_dummy_main l 
451   in
452   match (look_for_dummy_main metadata) with
453     | None->
454 (*         debug_print "Caso None"; *)
455         (* no dummy in main position *)
456         let metadata = List.filter is_dummy metadata in
457         let constraints = List.map MetadataTypes.constr_of_metadata metadata in
458         let concl_card = Some (MetadataConstraints.Eq no_concl) in
459         let full_card = Some (MetadataConstraints.Eq no_full) in
460         let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
461           Constr.at_least ~dbd ?concl_card ?full_card ?diff constraints
462     | Some (depth, dummy_type) ->
463 (*         debug_print 
464           (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type)); *)
465         (* a dummy in main position *)
466         let metadata_for_dummy_type = 
467           MetadataExtractor.compute ~body:None ~ty:dummy_type in
468         (* Let us skip this for the moment 
469            let main_of_dummy_type = 
470            look_for_dummy_main metadata_for_dummy_type in *)
471         let metadata = List.filter is_dummy metadata in
472         let constraints = List.map MetadataTypes.constr_of_metadata metadata in
473         let metadata_for_dummy_type = 
474           List.filter is_dummy metadata_for_dummy_type in
475         let metadata_for_dummy_type, depth' = 
476           (* depth' = the depth of the A -> A -> Prop *)
477           List.fold_left (fun (acc,dep) c ->
478             match c with
479             | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) -> 
480                 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
481             | `Obj  (s,`MainConclusion (Some (MetadataTypes.Eq i))) -> 
482                 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
483             | `Rel  (`MainConclusion (Some (MetadataTypes.Eq i))) -> 
484                 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
485             | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
486         in
487         let constraints_for_dummy_type =
488            List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
489         (* start with the dummy constant in main conlusion *)
490         let from = ["refObj as table0"] in
491         let where = 
492           [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
493                  sprintf "table0.h_depth >= %d" depth] in
494         let (n,from,where) =
495           List.fold_left 
496             (MetadataConstraints.add_constraint ~start:2)
497             (2,from,where) constraints in
498         let concl_card = Some (MetadataConstraints.Eq no_concl) in
499         let full_card = Some (MetadataConstraints.Eq no_full) in
500         let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
501         let (n,from,where) = 
502           MetadataConstraints.add_all_constr 
503             (n,from,where) concl_card full_card diff in
504               (* join with the constraints over the type of the constant *)
505         let where = 
506           (sprintf "table0.h_occurrence = table%d.source" n)::where in
507         let where = 
508           sprintf "table0.h_depth - table%d.h_depth = %d" 
509             n (depth - depth')::where
510         in
511         let (m,from,where) =
512           List.fold_left 
513             (MetadataConstraints.add_constraint ~start:n)
514             (n,from,where) constraints_for_dummy_type in
515         Constr.exec ~dbd (m,from,where)
516
517 (* fwd_simpl ****************************************************************)
518
519 let rec map_filter f n = function
520    | []       -> []
521    | hd :: tl -> 
522       match f n hd with
523          | None    -> map_filter f (succ n) tl
524          | Some hd -> hd :: map_filter f (succ n) tl
525
526 let get_uri t =
527    let aux = function
528       | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
529       | hd                  -> Some (CicUtil.uri_of_term hd, []) 
530    in
531    try aux t with
532       | Invalid_argument "uri_of_term" -> None
533
534 let get_metadata t =
535    let f n t =
536       match get_uri t with
537          | None          -> None 
538          | Some (uri, _) -> Some (n, uri)
539    in
540    match get_uri t with
541       | None             -> None
542       | Some (uri, args) -> Some (uri, map_filter f 1 args) 
543
544 let debug_metadata = function
545    | None                 -> ()
546    | Some (outer, inners) -> 
547       let f (n, uri) = Printf.eprintf "%s: %i %s\n" "fwd" n (UriManager.string_of_uri uri) in
548       Printf.eprintf "\n%s: %s\n" "fwd" (UriManager.string_of_uri outer);
549       List.iter f inners; prerr_newline ()
550
551 let fwd_simpl ~dbd t =
552    let map inners row = 
553       match row.(0), row.(1), row.(2) with  
554          | Some source, Some inner, Some index -> 
555             source,
556              List.mem
557               (int_of_string index, (UriManager.uri_of_string inner)) inners
558          | _                                   -> "", false
559    in
560    let rec rank ranks (source, ok) = 
561       match ranks, ok with
562          | [], false                               -> [source, 0]
563          | [], true                                -> [source, 1]
564          | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
565          | (uri, 0) :: tl, true when uri = source  -> (uri, 0) :: tl
566          | (uri, i) :: tl, true when uri = source  -> (uri, succ i) :: tl
567          | hd :: tl, _ -> hd :: rank tl (source, ok)
568    in
569    let compare (_, x) (_, y) = compare x y in
570    let filter n (uri, rank) =
571       if rank > 0 then Some (UriManager.uri_of_string uri) else None
572    in
573    let metadata = get_metadata t in debug_metadata metadata;
574    match metadata with
575       | None                 -> []
576       | Some (outer, inners) ->
577          let select = "source, h_inner, h_index" in
578          let from = "genLemma" in
579          let where =
580           Printf.sprintf "h_outer = \"%s\""
581            (Mysql.escape (UriManager.string_of_uri outer)) in
582          let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
583          let result = Mysql.exec dbd query in
584          let lemmas = Mysql.map ~f:(map inners) result in
585          let ranked = List.fold_left rank [] lemmas in
586          let ordered = List.rev (List.fast_sort compare ranked) in
587          map_filter filter 0 ordered
588
589 (* get_decomposables ********************************************************)
590
591 let decomposables ~dbd =
592    let map row = match row.(0) with
593       | None     -> None
594       | Some str ->
595          match CicUtil.term_of_uri (UriManager.uri_of_string str) with
596             | Cic.MutInd (uri, typeno, _) -> Some (uri, typeno)
597             | _                           -> 
598                raise (UriManager.IllFormedUri str)
599    in
600    let select, from = "source", "decomposables" in
601    let query = Printf.sprintf "SELECT %s FROM %s" select from in
602    let decomposables = Mysql.map ~f:map (Mysql.exec dbd query) in
603    map_filter (fun _ x -> x) 0 decomposables