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 depth) -> `MainConclusion (Some (depth + 1))
67 | `MainHypothesis (Some depth) -> `MainHypothesis (Some (depth + 1))
70 let compute_term pos term =
71 let rec aux (pos: position) set = function
73 if is_main_pos pos then
74 S.add (`Rel (main_pos pos)) set
78 | Cic.Meta (_, local_context) ->
83 | Some term -> aux pos set term)
87 if is_main_pos pos then
88 S.add (`Sort (sort, main_pos pos)) set
91 | Cic.Implicit _ -> assert false
92 | Cic.Cast (term, ty) ->
93 (* TODO consider also ty? *)
95 | Cic.Prod (_, source, target) ->
97 | `MainConclusion _ ->
98 let set = aux (`MainHypothesis (Some 0)) set source in
99 aux (incr_depth pos) set target
100 | `MainHypothesis _ ->
101 let set = aux `InHypothesis set source in
102 aux (incr_depth pos) set target
106 let set = aux pos set source in
108 | Cic.Lambda (_, source, target) ->
109 assert (not (is_main_pos pos));
110 let set = aux pos set source in
112 | Cic.LetIn (_, term, target) ->
113 if is_main_pos pos then
114 aux pos set (CicSubstitution.subst term target)
116 let set = aux pos set term in
118 | Cic.Appl [] -> assert false
119 | Cic.Appl (hd :: tl) ->
120 let set = aux pos set hd in
122 (fun set term -> aux (next_pos pos) set term)
124 | Cic.Const (uri, subst) ->
125 let set = S.add (`Obj (string_of_uri uri, pos)) set in
127 (fun set (_, term) -> aux (next_pos pos) set term)
129 | Cic.MutInd (uri, typeno, subst) ->
130 let uri = UriManager.string_of_uriref (uri, [typeno]) in
131 let set = S.add (`Obj (uri, pos)) set in
132 List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
134 | Cic.MutConstruct (uri, typeno, consno, subst) ->
135 let uri = UriManager.string_of_uriref (uri, [typeno; consno]) in
136 let set = S.add (`Obj (uri, pos)) set in
137 List.fold_left (fun set (_, term) -> aux (next_pos pos) set term)
139 | Cic.MutCase (uri, _, outtype, term, pats) ->
140 let pos = next_pos pos in
141 let set = aux pos set term in
142 let set = aux pos set outtype in
143 List.fold_left (fun set term -> aux pos set term) set pats
144 | Cic.Fix (_, funs) ->
145 let pos = next_pos pos in
147 (fun set (_, _, ty, body) ->
148 let set = aux pos set ty in
151 | Cic.CoFix (_, funs) ->
152 let pos = next_pos pos in
154 (fun set (_, ty, body) ->
155 let set = aux pos set ty in
161 let compute_type uri typeno (name, _, ty, constructors) =
162 let consno = ref 0 in
164 (UriManager.string_of_uriref (uri, [typeno]), name,
165 S.elements (compute_term (`MainConclusion (Some 0)) ty))
167 let constructors_metadata =
171 let uri = UriManager.string_of_uriref (uri, [typeno; !consno]) in
172 (uri, name, S.elements (compute_term (`MainConclusion (Some 0)) term)))
175 type_metadata :: constructors_metadata
177 let compute_ind ~uri ~types =
179 List.concat (List.map (fun ty -> incr idx; compute_type uri !idx ty) types)
181 let compute ~body ~ty =
182 let type_metadata = compute_term (`MainConclusion (Some 0)) ty in
186 | Some body -> compute_term `InBody body
190 (fun metadata uris ->
192 | `Obj (uri, _) -> StringSet.add uri uris
194 type_metadata StringSet.empty
200 | `Obj (uri, _) when StringSet.mem uri uris -> false
205 let compute_term start_pos term = S.elements (compute_term start_pos term)