]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/whelp/whelp.ml
4a863eba8ea50ac9c6880045067b48b7b7c34096
[helm.git] / helm / software / components / whelp / whelp.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 open Printf
29
30 let nonvar uri = not (UriManager.uri_is_var uri)
31
32   (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
33   * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
34 let sqlpat_of_shellglob =
35   let star_RE, qmark_RE, percent_RE, uscore_RE =
36     Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
37   in
38   fun shellglob ->
39     Pcre.replace ~rex:star_RE ~templ:"%"
40       (Pcre.replace ~rex:qmark_RE ~templ:"_"
41         (Pcre.replace ~rex:percent_RE ~templ:"\\%"
42           (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
43             shellglob)))
44
45 let locate ~(dbd:HSql.dbd) ?(vars = false) pat =
46   let escape s = HSql.escape s in
47   let sql_pat = sqlpat_of_shellglob pat in
48   let query =
49         sprintf 
50           ("SELECT source FROM %s WHERE value LIKE \"%s\" "
51            ^^ HSql.escape_string_for_like
52            ^^ " UNION " ^^
53            "SELECT source FROM %s WHERE value LIKE \"%s\" "
54            ^^ HSql.escape_string_for_like)
55           (MetadataTypes.name_tbl ()) (escape sql_pat)
56            MetadataTypes.library_name_tbl (escape sql_pat)
57   in
58   let result = HSql.exec dbd query in
59   List.filter nonvar
60     (HSql.map result
61       (fun cols -> match cols.(0) with Some s -> UriManager.uri_of_string s | _ -> assert false))
62
63 let match_term ~(dbd:HSql.dbd) ty =
64 (*   debug_print (lazy (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     MetadataConstraints.at_least ~dbd ?full_card ?diff constraints
105
106 let fill_with_dummy_constants t =
107   let rec aux i types =
108     function
109         Cic.Lambda (n,s,t) -> 
110           let dummy_uri = 
111             UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)^".con") in
112             (aux (i+1) (s::types)
113                (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
114       | t -> t,types
115   in 
116   let t,types = aux 0 [] t in
117   t, List.rev types
118       
119 let instance ~dbd t =
120   let t',types = fill_with_dummy_constants t in 
121   let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
122 (*   List.iter 
123     (fun x -> 
124        debug_print 
125          (lazy (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x)))) 
126     metadata; *)
127   let no_concl = MetadataDb.count_distinct `Conclusion metadata in
128   let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
129   let no_full = MetadataDb.count_distinct `Statement metadata in
130   let is_dummy = function
131     | `Obj(s, _) -> (String.sub (UriManager.string_of_uri s) 0 10) <> "cic:/dummy" 
132           | _ -> true 
133   in
134   let rec look_for_dummy_main = function
135           | [] -> None
136     | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_ 
137       when (String.sub (UriManager.string_of_uri s) 0 10 = "cic:/dummy") -> 
138       let s = UriManager.string_of_uri s in
139       let len = String.length s in
140             let dummy_index = int_of_string (String.sub s 11 (len-15)) in
141       let dummy_type = List.nth types dummy_index in
142       Some (d,dummy_type)
143     | _::l -> look_for_dummy_main l 
144   in
145   match (look_for_dummy_main metadata) with
146     | None->
147 (*         debug_print (lazy "Caso None"); *)
148         (* no dummy in main position *)
149         let metadata = List.filter is_dummy metadata in
150         let constraints = List.map MetadataTypes.constr_of_metadata metadata in
151         let concl_card = Some (MetadataConstraints.Eq no_concl) in
152         let full_card = Some (MetadataConstraints.Eq no_full) in
153         let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
154           MetadataConstraints.at_least ~dbd ?concl_card ?full_card ?diff
155             constraints
156     | Some (depth, dummy_type) ->
157 (*         debug_print 
158           (lazy (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type))); *)
159         (* a dummy in main position *)
160         let metadata_for_dummy_type = 
161           MetadataExtractor.compute ~body:None ~ty:dummy_type in
162         (* Let us skip this for the moment 
163            let main_of_dummy_type = 
164            look_for_dummy_main metadata_for_dummy_type in *)
165         let metadata = List.filter is_dummy metadata in
166         let constraints = List.map MetadataTypes.constr_of_metadata metadata in
167         let metadata_for_dummy_type = 
168           List.filter is_dummy metadata_for_dummy_type in
169         let metadata_for_dummy_type, depth' = 
170           (* depth' = the depth of the A -> A -> Prop *)
171           List.fold_left (fun (acc,dep) c ->
172             match c with
173             | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) -> 
174                 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
175             | `Obj  (s,`MainConclusion (Some (MetadataTypes.Eq i))) -> 
176                 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
177             | `Rel  (`MainConclusion (Some (MetadataTypes.Eq i))) -> 
178                 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
179             | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
180         in
181         let constraints_for_dummy_type =
182            List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
183         (* start with the dummy constant in main conlusion *)
184         let from = ["refObj as table0"] in
185         let where = 
186           [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
187                  sprintf "table0.h_depth >= %d" depth] in
188         let (n,from,where) =
189           List.fold_left 
190             (MetadataConstraints.add_constraint ~start:2)
191             (2,from,where) constraints in
192         let concl_card = Some (MetadataConstraints.Eq no_concl) in
193         let full_card = Some (MetadataConstraints.Eq no_full) in
194         let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
195         let (n,from,where) = 
196           MetadataConstraints.add_all_constr 
197             (n,from,where) concl_card full_card diff in
198               (* join with the constraints over the type of the constant *)
199         let where = 
200           (sprintf "table0.h_occurrence = table%d.source" n)::where in
201         let where = 
202           sprintf "table0.h_depth - table%d.h_depth = %d" 
203             n (depth - depth')::where
204         in
205         let (m,from,where) =
206           List.fold_left 
207             (MetadataConstraints.add_constraint ~start:n)
208             (n,from,where) constraints_for_dummy_type in
209         MetadataConstraints.exec ~dbd (m,from,where)
210
211 let elim ~dbd uri =
212   let constraints =
213     [`Rel [`MainConclusion None];
214      `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
215      `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
216      `Obj (uri,[`InHypothesis]);
217     ]
218   in
219   MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
220