1 (* Copyright (C) 2004, 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/
32 let is_main_pos = function
34 | `MainHypothesis _ -> true
37 let main_pos (pos: position): main_position =
39 | `MainConclusion depth -> `MainConclusion depth
40 | `MainHypothesis depth -> `MainHypothesis depth
43 let next_pos = function
44 | `MainConclusion _ -> `InConclusion
45 | `MainHypothesis _ -> `InHypothesis
48 let string_of_uri = UriManager.string_of_uri
50 module OrderedMetadata =
52 type t = MetadataTypes.metadata
53 let compare m1 m2 = (* ignore universes in Cic.Type sort *)
55 | `Sort (Cic.Type _, pos1), `Sort (Cic.Type _, pos2) ->
56 Pervasives.compare pos1 pos2
57 | _ -> Pervasives.compare m1 m2
60 module MetadataSet = Set.Make (OrderedMetadata)
61 module UriManagerSet = UriManager.UriSet
63 module S = MetadataSet
65 let unopt = function Some x -> x | None -> assert false
67 let incr_depth = function
68 | `MainConclusion (Some (Eq depth)) -> `MainConclusion (Some (Eq (depth + 1)))
69 | `MainHypothesis (Some (Eq depth)) -> `MainHypothesis (Some (Eq (depth + 1)))
72 let var_has_body uri =
73 match CicEnvironment.get_obj CicUniv.oblivion_ugraph uri with
74 | Cic.Variable (_, Some body, _, _, _), _ -> true
77 let compute_term pos term =
78 let rec aux (pos: position) set = function
79 | Cic.Var (uri, subst) when var_has_body uri ->
80 (* handles variables with body as constants *)
81 aux pos set (Cic.Const (uri, subst))
84 if is_main_pos pos then
85 S.add (`Rel (main_pos pos)) set
88 | Cic.Meta (_, local_context) ->
93 | Some term -> aux (next_pos pos) set term)
97 if is_main_pos pos then
98 S.add (`Sort (sort, main_pos pos)) set
101 | Cic.Implicit _ -> assert false
102 | Cic.Cast (term, ty) ->
103 (* TODO consider also ty? *)
105 | Cic.Prod (_, source, target) ->
107 | `MainConclusion _ ->
108 let set = aux (`MainHypothesis (Some (Eq 0))) set source in
109 aux (incr_depth pos) set target
110 | `MainHypothesis _ ->
111 let set = aux `InHypothesis set source in
112 aux (incr_depth pos) set target
116 let set = aux pos set source in
118 | Cic.Lambda (_, source, target) ->
119 (*assert (not (is_main_pos pos));*)
120 let set = aux (next_pos pos) set source in
121 aux (next_pos pos) set target
122 | Cic.LetIn (_, term, _, target) ->
123 if is_main_pos pos then
124 aux pos set (CicSubstitution.subst term target)
126 let set = aux pos set term in
128 | Cic.Appl [] -> assert false
129 | Cic.Appl (hd :: tl) ->
130 let set = aux pos set hd in
132 (fun set term -> aux (next_pos pos) set term)
134 | Cic.Const (uri, subst) ->
135 let set = S.add (`Obj (uri, pos)) set in
137 (fun set (_, term) -> aux (next_pos pos) set term)
139 | Cic.MutInd (uri, typeno, subst) ->
140 let uri = UriManager.uri_of_uriref uri typeno None in
141 let set = S.add (`Obj (uri, pos)) set in
142 List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
144 | Cic.MutConstruct (uri, typeno, consno, subst) ->
145 let uri = UriManager.uri_of_uriref uri typeno (Some consno) in
146 let set = S.add (`Obj (uri, pos)) set in
147 List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
149 | Cic.MutCase (uri, _, outtype, term, pats) ->
150 let pos = next_pos pos in
151 let set = aux pos set term in
152 let set = aux pos set outtype in
153 List.fold_left (fun set term -> aux pos set term) set pats
154 | Cic.Fix (_, funs) ->
155 let pos = next_pos pos in
157 (fun set (_, _, ty, body) ->
158 let set = aux pos set ty in
161 | Cic.CoFix (_, funs) ->
162 let pos = next_pos pos in
164 (fun set (_, ty, body) ->
165 let set = aux pos set ty in
174 let compare = Pervasives.compare
177 module IntSet = Set.Make (OrderedInt)
179 let compute_metas term =
180 let rec aux in_hyp ((concl_metas, hyp_metas) as acc) cic =
185 | Cic.Meta (no, local_context) ->
188 (concl_metas, IntSet.add no hyp_metas)
190 (IntSet.add no concl_metas, hyp_metas)
196 | Some term -> aux in_hyp set term)
199 | Cic.Implicit _ -> assert false
200 | Cic.Cast (term, ty) ->
201 (* TODO consider also ty? *)
203 | Cic.Prod (_, source, target) ->
205 let acc = aux in_hyp acc source in
206 aux in_hyp acc target
208 let acc = aux true acc source in
209 aux in_hyp acc target
210 | Cic.Lambda (_, source, target) ->
211 let acc = aux in_hyp acc source in
212 aux in_hyp acc target
213 | Cic.LetIn (_, term, _, target) ->
214 aux in_hyp acc (CicSubstitution.subst term target)
215 | Cic.Appl [] -> assert false
216 | Cic.Appl (hd :: tl) ->
217 let acc = aux in_hyp acc hd in
218 List.fold_left (fun acc term -> aux in_hyp acc term) acc tl
219 | Cic.Const (_, subst)
220 | Cic.MutInd (_, _, subst)
221 | Cic.MutConstruct (_, _, _, subst) ->
222 List.fold_left (fun acc (_, term) -> aux in_hyp acc term) acc subst
223 | Cic.MutCase (uri, _, outtype, term, pats) ->
224 let acc = aux in_hyp acc term in
225 let acc = aux in_hyp acc outtype in
226 List.fold_left (fun acc term -> aux in_hyp acc term) acc pats
227 | Cic.Fix (_, funs) ->
229 (fun acc (_, _, ty, body) ->
230 let acc = aux in_hyp acc ty in
233 | Cic.CoFix (_, funs) ->
235 (fun acc (_, ty, body) ->
236 let acc = aux in_hyp acc ty in
240 aux false (IntSet.empty, IntSet.empty) term
242 (** type of inductiveType *)
243 let compute_type pos uri typeno (name, _, ty, constructors) =
244 let consno = ref 0 in
246 (UriManager.uri_of_uriref uri typeno None, name, (compute_term pos ty))
248 let constructors_metadata =
252 let uri = UriManager.uri_of_uriref uri typeno (Some !consno) in
253 (uri, name, (compute_term pos term)))
256 type_metadata :: constructors_metadata
258 let compute_ind pos ~uri ~types =
260 List.map (fun ty -> incr idx; compute_type pos uri !idx ty) types
262 let compute (pos:position) ~body ~ty =
263 let type_metadata = compute_term pos ty in
267 | Some body -> compute_term `InBody body
271 (fun metadata uris ->
273 | `Obj (uri, _) -> UriManagerSet.add uri uris
275 type_metadata UriManagerSet.empty
280 | `Obj (uri, _) when UriManagerSet.mem uri uris -> false
285 let depth_offset params =
286 let non p x = not (p x) in
287 List.length (List.filter (non var_has_body) params)
289 let rec compute_var pos uri =
290 let o, _ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
292 | Cic.Variable (_, Some _, _, _, _) -> S.empty
293 | Cic.Variable (_, None, ty, params, _) ->
297 S.union metadata (compute_var (next_pos pos) uri))
302 | `MainHypothesis (Some (Eq 0)) ->
303 let pos = `MainHypothesis (Some (Eq (depth_offset params))) in
304 let ty_metadata = compute_term pos ty in
305 S.union ty_metadata var_metadata
307 let ty_metadata = compute_term pos ty in
308 S.union ty_metadata var_metadata
312 let compute_obj uri =
313 let o, _ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
315 | Cic.Variable (_, body, ty, params, _)
316 | Cic.Constant (_, body, ty, params, _) ->
317 let pos = `MainConclusion (Some (Eq (depth_offset params))) in
318 let metadata = compute pos ~body ~ty in
322 S.union metadata (compute_var (`MainHypothesis (Some (Eq 0))) uri))
327 UriManager.name_of_uri uri,
328 S.union metadata var_metadata ]
329 | Cic.InductiveDefinition (types, params, _, _) ->
330 let pos = `MainConclusion(Some (Eq (depth_offset params))) in
331 let metadata = compute_ind pos ~uri ~types in
335 S.union metadata (compute_var (`MainHypothesis (Some (Eq 0))) uri))
340 (List.map (fun (uri,name,md) -> (uri,name,S.union md var_metadata)) m)
343 | Cic.CurrentProof _ -> assert false
345 let compute_obj uri =
346 List.map (fun (u, n, md) -> (u, n, S.elements md)) (compute_obj uri)
348 let compute ~body ~ty =
349 S.elements (compute (`MainConclusion (Some (Eq 0))) ~body ~ty)