1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 (* fwd_simpl ****************************************************************)
30 let rec filter_map_n f n = function
34 | None -> filter_map_n f (succ n) tl
35 | Some hd -> hd :: filter_map_n f (succ n) tl
39 | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
40 | hd -> Some (CicUtil.uri_of_term hd, [])
43 | Invalid_argument "uri_of_term" -> None
49 | Some (uri, _) -> Some (n, uri)
53 | Some (uri, args) -> Some (uri, filter_map_n f 1 args)
55 let debug_metadata = function
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 ()
62 let fwd_simpl ~dbd t =
64 match row.(0), row.(1), row.(2) with
65 | Some source, Some inner, Some index ->
68 (int_of_string index, (UriManager.uri_of_string inner)) inners
71 let rec rank ranks (source, ok) =
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)
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
84 let metadata = get_metadata t in debug_metadata metadata;
87 | Some (outer, inners) ->
88 let select = "source, h_inner, h_index" in
89 let from = "genLemma" in
91 Printf.sprintf "h_outer = \"%s\""
92 (HMysql.escape (UriManager.string_of_uri outer)) in
93 let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
94 let result = HMysql.exec dbd query in
95 let lemmas = HMysql.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
100 (* get_decomposables ********************************************************)
102 let decomposables ~dbd =
103 let map row = match row.(0) with
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)
110 raise (UriManager.IllFormedUri str)
112 let select, from = "source", "decomposables" in
113 let query = Printf.sprintf "SELECT %s FROM %s" select from in
114 let decomposables = HMysql.map ~f:map (HMysql.exec dbd query) in
115 filter_map_n (fun _ x -> x) 0 decomposables