]> matita.cs.unibo.it Git - helm.git/blob - components/whelp/fwdQueries.ml
tagged 0.5.0-rc1
[helm.git] / components / whelp / fwdQueries.ml
1 (* Copyright (C) 2005, 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 (* fwd_simpl ****************************************************************)
29
30 let rec filter_map_n f n = function
31    | []       -> []
32    | hd :: tl -> 
33       match f n hd with
34          | None    -> filter_map_n f (succ n) tl
35          | Some hd -> hd :: filter_map_n f (succ n) tl
36
37 let get_uri t =
38    let aux = function
39       | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
40       | hd                  -> Some (CicUtil.uri_of_term hd, []) 
41    in
42    try aux t with
43       | Invalid_argument "uri_of_term" -> None
44
45 let get_metadata t =
46    let f n t =
47       match get_uri t with
48          | None          -> None 
49          | Some (uri, _) -> Some (n, uri)
50    in
51    match get_uri t with
52       | None             -> None
53       | Some (uri, args) -> Some (uri, filter_map_n f 1 args) 
54
55 let debug_metadata = function
56    | None                 -> ()
57    | Some (outer, inners) -> 
58       let f (n, uri) = Printf.eprintf "%s: %i %s\n" "fwd" n (UriManager.string_of_uri uri) in
59       Printf.eprintf "\n%s: %s\n" "fwd" (UriManager.string_of_uri outer);
60       List.iter f inners; prerr_newline ()
61
62 let fwd_simpl ~dbd t =
63    let map inners row = 
64       match row.(0), row.(1), row.(2) with  
65          | Some source, Some inner, Some index -> 
66             source,
67              List.mem
68               (int_of_string index, (UriManager.uri_of_string inner)) inners
69          | _                                   -> "", false
70    in
71    let rec rank ranks (source, ok) = 
72       match ranks, ok with
73          | [], false                               -> [source, 0]
74          | [], true                                -> [source, 1]
75          | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
76          | (uri, 0) :: tl, true when uri = source  -> (uri, 0) :: tl
77          | (uri, i) :: tl, true when uri = source  -> (uri, succ i) :: tl
78          | hd :: tl, _ -> hd :: rank tl (source, ok)
79    in
80    let compare (_, x) (_, y) = compare x y in
81    let filter n (uri, rank) =
82       if rank > 0 then Some (UriManager.uri_of_string uri) else None
83    in
84    let metadata = get_metadata t in debug_metadata metadata;
85    match metadata with
86       | None                 -> []
87       | Some (outer, inners) ->
88          let select = "source, h_inner, h_index" in
89          let from = "genLemma" in
90          let where =
91           Printf.sprintf "h_outer = \"%s\""
92            (HSql.escape HSql.Library dbd (UriManager.string_of_uri outer)) in
93          let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
94          let result = HSql.exec HSql.Library dbd query in
95          let lemmas = HSql.map ~f:(map inners) result in
96          let ranked = List.fold_left rank [] lemmas in
97          let ordered = List.rev (List.fast_sort compare ranked) in
98          filter_map_n filter 0 ordered
99
100 (* get_decomposables ********************************************************)
101
102 let decomposables ~dbd =
103    let map row = match row.(0) with
104       | None     -> None
105       | Some str ->
106          match CicUtil.term_of_uri (UriManager.uri_of_string str) with
107             | Cic.MutInd (uri, typeno, _) -> Some (uri, Some typeno)
108             | Cic.Const (uri, _)          -> Some (uri, None)
109             | _                           -> 
110                raise (UriManager.IllFormedUri str)
111    in
112    let select, from = "source", "decomposables" in
113    let query = Printf.sprintf "SELECT %s FROM %s" select from in
114    let decomposables = HSql.map ~f:map (HSql.exec HSql.Library dbd query) in
115    filter_map_n (fun _ x -> x) 0 decomposables