]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/metadata/metadataExtractor.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / metadata / metadataExtractor.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 open MetadataTypes
29
30 let is_main_pos = function
31   | `MainConclusion _
32   | `MainHypothesis _ -> true
33   | _ -> false
34
35 let main_pos (pos: position): main_position =
36   match pos with
37   | `MainConclusion depth -> `MainConclusion depth
38   | `MainHypothesis depth -> `MainHypothesis depth
39   | _ -> assert false
40
41 let next_pos = function
42   | `MainConclusion _ -> `InConclusion
43   | `MainHypothesis _ -> `InHypothesis
44   | pos -> pos
45
46 let string_of_uri = UriManager.string_of_uri
47
48 module OrderedMetadata =
49   struct
50     type t = MetadataTypes.metadata
51     let compare m1 m2 = (* ignore universes in Cic.Type sort *)
52       match (m1, m2) with
53       | `Sort (Cic.Type _, pos1), `Sort (Cic.Type _, pos2) ->
54           Pervasives.compare pos1 pos2
55       | _ -> Pervasives.compare m1 m2
56   end
57
58 module MetadataSet = Set.Make (OrderedMetadata)
59 module UriManagerSet = UriManager.UriSet
60
61 module S = MetadataSet
62
63 let unopt = function Some x -> x | None -> assert false
64
65 let incr_depth = function
66   | `MainConclusion (Some (Eq depth)) -> `MainConclusion (Some (Eq (depth + 1)))
67   | `MainHypothesis (Some (Eq depth)) -> `MainHypothesis (Some (Eq (depth + 1)))
68   | _ -> assert false
69
70 let var_has_body uri =
71   match CicEnvironment.get_obj CicUniv.empty_ugraph uri with
72   | Cic.Variable (_, Some body, _, _, _), _ -> true
73   | _ -> false
74
75 let compute_term pos term =
76   let rec aux (pos: position) set = function
77     | Cic.Var (uri, subst) when var_has_body uri ->
78         (* handles variables with body as constants *)
79         aux pos set (Cic.Const (uri, subst))
80     | Cic.Rel _
81     | Cic.Var _ ->
82         if is_main_pos pos then
83           S.add (`Rel (main_pos pos)) set
84         else
85           set
86     | Cic.Meta (_, local_context) ->
87         List.fold_left
88           (fun set context ->
89             match context with
90             | None -> set
91             | Some term -> aux (next_pos pos) set term)
92           set
93           local_context
94     | Cic.Sort sort ->
95         if is_main_pos pos then
96           S.add (`Sort (sort, main_pos pos)) set
97         else
98           set
99     | Cic.Implicit _ -> assert false
100     | Cic.Cast (term, ty) ->
101         (* TODO consider also ty? *)
102         aux pos set term
103     | Cic.Prod (_, source, target) ->
104         (match pos with
105         | `MainConclusion _ ->
106             let set = aux (`MainHypothesis (Some (Eq 0))) set source in
107             aux (incr_depth pos) set target
108         | `MainHypothesis _ ->
109             let set = aux `InHypothesis set source in
110             aux (incr_depth pos) set target
111         | `InConclusion
112         | `InHypothesis
113         | `InBody ->
114             let set = aux pos set source in
115             aux pos set target)
116     | Cic.Lambda (_, source, target) ->
117         (*assert (not (is_main_pos pos));*)
118         let set = aux (next_pos pos) set source in
119         aux (next_pos pos) set target
120     | Cic.LetIn (_, term, target) ->
121         if is_main_pos pos then
122           aux pos set (CicSubstitution.subst term target)
123         else
124           let set = aux pos set term in
125           aux pos set target
126     | Cic.Appl [] -> assert false
127     | Cic.Appl (hd :: tl) ->
128         let set = aux pos set hd in
129         List.fold_left
130           (fun set term -> aux (next_pos pos) set term)
131           set tl
132     | Cic.Const (uri, subst) ->
133         let set = S.add (`Obj (uri, pos)) set in
134         List.fold_left
135           (fun set (_, term) -> aux (next_pos pos) set term)
136           set subst
137     | Cic.MutInd (uri, typeno, subst) ->
138         let uri = UriManager.uri_of_uriref uri typeno None in
139         let set = S.add (`Obj (uri, pos)) set in
140         List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
141           set subst
142     | Cic.MutConstruct (uri, typeno, consno, subst) ->
143         let uri = UriManager.uri_of_uriref uri typeno (Some consno) in
144         let set = S.add (`Obj (uri, pos)) set in
145         List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
146           set subst
147     | Cic.MutCase (uri, _, outtype, term, pats) ->
148         let pos = next_pos pos in
149         let set = aux pos set term in
150         let set = aux pos set outtype in
151         List.fold_left (fun set term -> aux pos set term) set pats
152     | Cic.Fix (_, funs) ->
153         let pos = next_pos pos in
154         List.fold_left
155           (fun set (_, _, ty, body) ->
156             let set = aux pos set ty in
157             aux pos set body)
158           set funs
159     | Cic.CoFix (_, funs) ->
160         let pos = next_pos pos in
161         List.fold_left
162           (fun set (_, ty, body) ->
163             let set = aux pos set ty in
164             aux pos set body)
165           set funs
166   in
167   aux pos S.empty term
168
169 module OrderedInt =
170 struct
171   type t = int
172   let compare = Pervasives.compare
173 end
174
175 module IntSet = Set.Make (OrderedInt)
176
177 let compute_metas term =
178   let rec aux in_hyp ((concl_metas, hyp_metas) as acc) cic =
179     match cic with
180     | Cic.Rel _
181     | Cic.Sort _
182     | Cic.Var _ -> acc
183     | Cic.Meta (no, local_context) ->
184         let acc =
185           if in_hyp then
186             (concl_metas, IntSet.add no hyp_metas)
187           else
188             (IntSet.add no concl_metas, hyp_metas)
189         in
190         List.fold_left
191           (fun set context ->
192             match context with
193             | None -> set
194             | Some term -> aux in_hyp set term)
195           acc
196           local_context
197     | Cic.Implicit _ -> assert false
198     | Cic.Cast (term, ty) ->
199         (* TODO consider also ty? *)
200         aux in_hyp acc term
201     | Cic.Prod (_, source, target) ->
202         if in_hyp then
203           let acc = aux in_hyp acc source in
204           aux in_hyp acc target
205         else
206           let acc = aux true acc source in
207           aux in_hyp acc target
208     | Cic.Lambda (_, source, target) ->
209         let acc = aux in_hyp acc source in
210         aux in_hyp acc target
211     | Cic.LetIn (_, term, target) ->
212         aux in_hyp acc (CicSubstitution.subst term target)
213     | Cic.Appl [] -> assert false
214     | Cic.Appl (hd :: tl) ->
215         let acc = aux in_hyp acc hd in
216         List.fold_left (fun acc term -> aux in_hyp acc term) acc tl
217     | Cic.Const (_, subst)
218     | Cic.MutInd (_, _, subst)
219     | Cic.MutConstruct (_, _, _, subst) ->
220         List.fold_left (fun acc (_, term) -> aux in_hyp acc term) acc subst
221     | Cic.MutCase (uri, _, outtype, term, pats) ->
222         let acc = aux in_hyp acc term in
223         let acc = aux in_hyp acc outtype in
224         List.fold_left (fun acc term -> aux in_hyp acc term) acc pats
225     | Cic.Fix (_, funs) ->
226         List.fold_left
227           (fun acc (_, _, ty, body) ->
228             let acc = aux in_hyp acc ty in
229             aux in_hyp acc body)
230           acc funs
231     | Cic.CoFix (_, funs) ->
232         List.fold_left
233           (fun acc (_, ty, body) ->
234             let acc = aux in_hyp acc ty in
235             aux in_hyp acc body)
236           acc funs
237   in
238   aux false (IntSet.empty, IntSet.empty) term
239
240   (** type of inductiveType *)
241 let compute_type pos uri typeno (name, _, ty, constructors) =
242   let consno = ref 0 in
243   let type_metadata =
244     (UriManager.uri_of_uriref uri typeno None, name, (compute_term pos ty))
245   in
246   let constructors_metadata =
247     List.map
248       (fun (name, term) ->
249         incr consno;
250         let uri = UriManager.uri_of_uriref uri typeno (Some !consno) in
251         (uri, name, (compute_term pos term)))
252       constructors
253   in
254   type_metadata :: constructors_metadata
255
256 let compute_ind pos ~uri ~types =
257   let idx = ref ~-1 in
258   List.map (fun ty -> incr idx; compute_type pos uri !idx ty) types
259
260 let compute (pos:position) ~body ~ty = 
261   let type_metadata = compute_term pos ty in
262   let body_metadata =
263     match body with
264     | None -> S.empty
265     | Some body -> compute_term `InBody body
266   in
267   let uris =
268     S.fold
269       (fun metadata uris ->
270         match metadata with
271         | `Obj (uri, _) -> UriManagerSet.add uri uris
272         | _ -> uris)
273       type_metadata UriManagerSet.empty
274   in
275   S.union
276     (S.filter
277       (function
278         | `Obj (uri, _) when UriManagerSet.mem uri uris -> false
279         | _ -> true)
280       body_metadata)
281     type_metadata
282
283 let depth_offset params =
284   let non p x = not (p x) in
285   List.length (List.filter (non var_has_body) params)
286
287 let rec compute_var pos uri =
288   let o, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
289   match o with
290     | Cic.Variable (_, Some _, _, _, _) -> S.empty
291     | Cic.Variable (_, None, ty, params, _) ->
292         let var_metadata = 
293           List.fold_left
294             (fun metadata uri ->
295               S.union metadata (compute_var (next_pos pos) uri))
296             S.empty
297             params
298         in
299         (match pos with
300            | `MainHypothesis (Some (Eq 0)) -> 
301                let pos = `MainHypothesis (Some (Eq (depth_offset params))) in
302                let ty_metadata = compute_term pos ty in
303                S.union ty_metadata var_metadata
304            | `InHypothesis ->
305                let ty_metadata = compute_term pos ty in
306                S.union ty_metadata var_metadata
307            | _ -> assert false)
308     | _ -> assert false 
309
310 let compute_obj uri =
311   let o, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
312   match o with
313   | Cic.Variable (_, body, ty, params, _)
314   | Cic.Constant (_, body, ty, params, _) -> 
315       let pos = `MainConclusion (Some (Eq (depth_offset params))) in
316       let metadata = compute pos ~body ~ty in
317       let var_metadata = 
318         List.fold_left
319           (fun metadata uri ->
320             S.union metadata (compute_var (`MainHypothesis (Some (Eq 0))) uri))
321           S.empty
322           params
323       in
324       [ uri, 
325         UriManager.name_of_uri uri,
326         S.union metadata var_metadata ]
327   | Cic.InductiveDefinition (types, params, _, _) ->
328       let pos = `MainConclusion(Some (Eq (depth_offset params))) in
329       let metadata = compute_ind pos ~uri ~types in
330       let var_metadata = 
331         List.fold_left
332           (fun metadata uri ->
333             S.union metadata (compute_var (`MainHypothesis (Some (Eq 0))) uri))
334           S.empty params
335       in
336       List.fold_left
337         (fun acc m -> 
338           (List.map (fun (uri,name,md) -> (uri,name,S.union md var_metadata)) m)
339           @ acc)
340         [] metadata
341   | Cic.CurrentProof _ -> assert false    
342
343 let compute_obj uri = 
344   List.map (fun (u, n, md) -> (u, n, S.elements md)) (compute_obj uri)
345   
346 let compute ~body ~ty =
347   S.elements (compute (`MainConclusion (Some (Eq 0))) ~body ~ty)
348