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/
30 let is_main_pos = function
32 | `MainHypothesis _ -> true
35 let main_pos (pos: position): main_position =
37 | `MainConclusion depth -> `MainConclusion depth
38 | `MainHypothesis depth -> `MainHypothesis depth
41 let next_pos = function
42 | `MainConclusion _ -> `InConclusion
43 | `MainHypothesis _ -> `InHypothesis
46 let string_of_uri = UriManager.string_of_uri
48 module OrderedMetadata =
50 type t = MetadataTypes.metadata
51 let compare m1 m2 = (* ignore universes in Cic.Type sort *)
53 | `Sort (Cic.Type _, pos1), `Sort (Cic.Type _, pos2) ->
54 Pervasives.compare pos1 pos2
55 | _ -> Pervasives.compare m1 m2
58 module MetadataSet = Set.Make (OrderedMetadata)
59 module StringSet = Set.Make (String)
61 module S = MetadataSet
63 let unopt = function Some x -> x | None -> assert false
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)))
70 let var_has_body uri =
71 match CicEnvironment.get_obj CicUniv.empty_ugraph uri with
72 | Cic.Variable (_, Some body, _, _, _), _ -> true
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))
82 if is_main_pos pos then
83 S.add (`Rel (main_pos pos)) set
86 | Cic.Meta (_, local_context) ->
91 | Some term -> aux (next_pos pos) set term)
95 if is_main_pos pos then
96 S.add (`Sort (sort, main_pos pos)) set
99 | Cic.Implicit _ -> assert false
100 | Cic.Cast (term, ty) ->
101 (* TODO consider also ty? *)
103 | Cic.Prod (_, source, target) ->
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
114 let set = aux pos set source in
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)
124 let set = aux pos set term in
126 | Cic.Appl [] -> assert false
127 | Cic.Appl (hd :: tl) ->
128 let set = aux pos set hd in
130 (fun set term -> aux (next_pos pos) set term)
132 | Cic.Const (uri, subst) ->
133 let set = S.add (`Obj (string_of_uri uri, pos)) set in
135 (fun set (_, term) -> aux (next_pos pos) set term)
137 | Cic.MutInd (uri, typeno, subst) ->
138 let uri = UriManager.string_of_uriref (uri, [typeno]) in
139 let set = S.add (`Obj (uri, pos)) set in
140 List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
142 | Cic.MutConstruct (uri, typeno, consno, subst) ->
143 let uri = UriManager.string_of_uriref (uri, [typeno; 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)
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
155 (fun set (_, _, ty, body) ->
156 let set = aux pos set ty in
159 | Cic.CoFix (_, funs) ->
160 let pos = next_pos pos in
162 (fun set (_, ty, body) ->
163 let set = aux pos set ty in
172 let compare = Pervasives.compare
175 module IntSet = Set.Make (OrderedInt)
177 let compute_metas term =
178 let rec aux in_hyp ((concl_metas, hyp_metas) as acc) cic =
183 | Cic.Meta (no, local_context) ->
186 (concl_metas, IntSet.add no hyp_metas)
188 (IntSet.add no concl_metas, hyp_metas)
194 | Some term -> aux in_hyp set term)
197 | Cic.Implicit _ -> assert false
198 | Cic.Cast (term, ty) ->
199 (* TODO consider also ty? *)
201 | Cic.Prod (_, source, target) ->
203 let acc = aux in_hyp acc source in
204 aux in_hyp acc target
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) ->
227 (fun acc (_, _, ty, body) ->
228 let acc = aux in_hyp acc ty in
231 | Cic.CoFix (_, funs) ->
233 (fun acc (_, ty, body) ->
234 let acc = aux in_hyp acc ty in
238 aux false (IntSet.empty, IntSet.empty) term
240 (** type of inductiveType *)
241 let compute_type pos uri typeno (name, _, ty, constructors) =
242 let consno = ref 0 in
244 (UriManager.string_of_uriref (uri, [typeno]), name, (compute_term pos ty))
246 let constructors_metadata =
250 let uri = UriManager.string_of_uriref (uri, [typeno; !consno]) in
251 (uri, name, (compute_term pos term)))
254 type_metadata :: constructors_metadata
256 let compute_ind pos ~uri ~types =
258 List.map (fun ty -> incr idx; compute_type pos uri !idx ty) types
260 let compute (pos:position) ~body ~ty =
261 let type_metadata = compute_term pos ty in
265 | Some body -> compute_term `InBody body
269 (fun metadata uris ->
271 | `Obj (uri, _) -> StringSet.add uri uris
273 type_metadata StringSet.empty
278 | `Obj (uri, _) when StringSet.mem uri uris -> false
283 let depth_offset params =
284 let non p x = not (p x) in
285 List.length (List.filter (non var_has_body) params)
287 let rec compute_var pos uri =
288 let o, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
290 | Cic.Variable (_, Some _, _, _, _) -> S.empty
291 | Cic.Variable (_, None, ty, params, _) ->
295 S.union metadata (compute_var (next_pos pos) uri))
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
305 let ty_metadata = compute_term pos ty in
306 S.union ty_metadata var_metadata
310 let compute_obj uri =
311 let o, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
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
320 S.union metadata (compute_var (`MainHypothesis (Some (Eq 0))) uri))
324 [ UriManager.string_of_uri 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
333 S.union metadata (compute_var (`MainHypothesis (Some (Eq 0))) uri))
338 (List.map (fun (uri,name,md) -> (uri,name,S.union md var_metadata)) m)
341 | Cic.CurrentProof _ -> assert false
343 let compute_obj uri =
344 List.map (fun (u, n, md) -> (u, n, S.elements md)) (compute_obj uri)
346 let compute ~body ~ty =
347 S.elements (compute (`MainConclusion (Some (Eq 0))) ~body ~ty)