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