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 exception Meta_not_found of int
31 exception Subst_not_found of int
33 let lookup_meta index metasenv =
35 List.find (fun (index', _, _) -> index = index') metasenv
36 with Not_found -> raise (Meta_not_found index)
38 let lookup_subst n subst =
41 with Not_found -> raise (Subst_not_found n)
43 let exists_meta index = List.exists (fun (index', _, _) -> (index = index'))
45 (* clean_up_meta take a substitution, a metasenv a meta_inex and a local
46 context l and clean up l with respect to the hidden hipothesis in the
49 let clean_up_local_context subst metasenv n l =
52 let (cc,_,_) = lookup_subst n subst in cc
53 with Subst_not_found _ ->
55 let (_,cc,_) = lookup_meta n metasenv in cc
56 with Meta_not_found _ -> assert false) in
64 Invalid_argument _ -> assert false)
70 C.Rel m when m > k -> false
74 (fun i t -> i && (match t with None -> true | Some t -> is_closed k t)
77 | C.Implicit _ -> assert false
78 | C.Cast (te,ty) -> is_closed k te && is_closed k ty
79 | C.Prod (name,so,dest) -> is_closed k so && is_closed (k+1) dest
80 | C.Lambda (_,so,dest) -> is_closed k so && is_closed (k+1) dest
81 | C.LetIn (_,so,dest) -> is_closed k so && is_closed (k+1) dest
83 List.fold_right (fun x i -> i && is_closed k x) l true
84 | C.Var (_,exp_named_subst)
85 | C.Const (_,exp_named_subst)
86 | C.MutInd (_,_,exp_named_subst)
87 | C.MutConstruct (_,_,_,exp_named_subst) ->
88 List.fold_right (fun (_,x) i -> i && is_closed k x)
90 | C.MutCase (_,_,out,te,pl) ->
91 is_closed k out && is_closed k te &&
92 List.fold_right (fun x i -> i && is_closed k x) pl true
94 let len = List.length fl in
95 let k_plus_len = k + len in
97 (fun (_,_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
100 let len = List.length fl in
101 let k_plus_len = k + len in
103 (fun (_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
109 let rec is_meta_closed =
112 | Cic.Meta _ -> false
114 | Cic.Implicit _ -> assert false
115 | Cic.Cast (te,ty) -> is_meta_closed te && is_meta_closed ty
116 | Cic.Prod (name,so,dest) -> is_meta_closed so && is_meta_closed dest
117 | Cic.Lambda (_,so,dest) -> is_meta_closed so && is_meta_closed dest
118 | Cic.LetIn (_,so,dest) -> is_meta_closed so && is_meta_closed dest
120 not (List.exists (fun x -> not (is_meta_closed x)) l)
121 | Cic.Var (_,exp_named_subst)
122 | Cic.Const (_,exp_named_subst)
123 | Cic.MutInd (_,_,exp_named_subst)
124 | Cic.MutConstruct (_,_,_,exp_named_subst) ->
125 not (List.exists (fun (_,x) -> not (is_meta_closed x)) exp_named_subst)
126 | Cic.MutCase (_,_,out,te,pl) ->
127 is_meta_closed out && is_meta_closed te &&
128 not (List.exists (fun x -> not (is_meta_closed x)) pl)
132 not (is_meta_closed ty) || not (is_meta_closed bo))
134 | Cic.CoFix (_,fl) ->
137 not (is_meta_closed ty) || not (is_meta_closed bo))
141 let xpointer_RE = Str.regexp "\\([^#]+\\)#xpointer(\\(.*\\))"
142 let slash_RE = Str.regexp "/"
144 let term_of_uri uri =
145 let s = UriManager.string_of_uri uri in
147 (if UriManager.uri_is_con uri then
149 else if UriManager.uri_is_var uri then
151 else if not (Str.string_match xpointer_RE s 0) then
152 raise (UriManager.IllFormedUri s)
154 let (baseuri,xpointer) = (Str.matched_group 1 s, Str.matched_group 2 s) in
155 let baseuri = UriManager.uri_of_string baseuri in
156 (match Str.split slash_RE xpointer with
157 | [_; tyno] -> Cic.MutInd (baseuri, int_of_string tyno - 1, [])
158 | [_; tyno; consno] ->
160 (baseuri, int_of_string tyno - 1, int_of_string consno, [])
165 | Not_found -> raise (UriManager.IllFormedUri s)
167 let uri_of_term = function
168 | Cic.Const (uri, [])
169 | Cic.Var (uri, []) -> uri
170 | Cic.MutInd (baseuri, tyno, []) ->
171 UriManager.uri_of_string
172 (sprintf "%s#xpointer(1/%d)" (UriManager.string_of_uri baseuri) (tyno+1))
173 | Cic.MutConstruct (baseuri, tyno, consno, []) ->
174 UriManager.uri_of_string
175 (sprintf "%s#xpointer(1/%d/%d)" (UriManager.string_of_uri baseuri)
177 | _ -> raise (Invalid_argument "uri_of_term")
183 (fun term acc -> Cic.Prod (Cic.Anonymous, term, acc))
184 terms (Cic.Sort (Cic.Type (CicUniv.fresh ())))
186 let rec unpack = function
187 | Cic.Prod (Cic.Anonymous, term, Cic.Sort (Cic.Type _)) -> [term]
188 | Cic.Prod (Cic.Anonymous, term, tgt) -> term :: unpack tgt
192 let rec strip_prods n = function
194 | Cic.Prod (_, _, tgt) when n > 0 -> strip_prods (n-1) tgt
195 | _ -> failwith "not enough prods"
197 let params_of_obj = function
198 | Cic.Constant (_, _, _, params, _)
199 | Cic.Variable (_, _, _, params, _)
200 | Cic.CurrentProof (_, _, _, _, params, _)
201 | Cic.InductiveDefinition (_, params, _, _) ->
204 let attributes_of_obj = function
205 | Cic.Constant (_, _, _, _, attributes)
206 | Cic.Variable (_, _, _, _, attributes)
207 | Cic.CurrentProof (_, _, _, _, _, attributes)
208 | Cic.InductiveDefinition (_, _, _, attributes) ->
210 let rec mk_rels howmany from =
213 | _ -> (Cic.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
217 | Cic.ARel (id,_,_,_)
221 | Cic.AImplicit (id,_)
223 | Cic.AProd (id,_,_,_)
224 | Cic.ALambda (id,_,_,_)
225 | Cic.ALetIn (id,_,_,_)
227 | Cic.AConst (id,_,_)
228 | Cic.AMutInd (id,_,_,_)
229 | Cic.AMutConstruct (id,_,_,_,_)
230 | Cic.AMutCase (id,_,_,_,_,_)
232 | Cic.ACoFix (id,_,_) -> id
235 let rec rehash_term =
236 let module C = Cic in
237 let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
239 | (C.Rel _) as t -> t
240 | C.Var (uri,exp_named_subst) ->
241 let uri' = recons uri in
242 let exp_named_subst' =
244 (function (uri,t) ->(recons uri,rehash_term t))
247 C.Var (uri',exp_named_subst')
253 | Some t -> Some (rehash_term t)
257 | C.Sort (C.Type u) ->
258 CicUniv.assert_univ u;
259 C.Sort (C.Type (CicUniv.recons_univ u))
261 | C.Implicit _ as t -> t
262 | C.Cast (te,ty) -> C.Cast (rehash_term te, rehash_term ty)
263 | C.Prod (n,s,t) -> C.Prod (n, rehash_term s, rehash_term t)
264 | C.Lambda (n,s,t) -> C.Lambda (n, rehash_term s, rehash_term t)
265 | C.LetIn (n,s,t) -> C.LetIn (n, rehash_term s, rehash_term t)
266 | C.Appl l -> C.Appl (List.map rehash_term l)
267 | C.Const (uri,exp_named_subst) ->
268 let uri' = recons uri in
269 let exp_named_subst' =
271 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
273 C.Const (uri',exp_named_subst')
274 | C.MutInd (uri,tyno,exp_named_subst) ->
275 let uri' = recons uri in
276 let exp_named_subst' =
278 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
280 C.MutInd (uri',tyno,exp_named_subst')
281 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
282 let uri' = recons uri in
283 let exp_named_subst' =
285 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
287 C.MutConstruct (uri',tyno,consno,exp_named_subst')
288 | C.MutCase (uri,i,outty,t,pl) ->
289 C.MutCase (recons uri, i, rehash_term outty, rehash_term t,
290 List.map rehash_term pl)
294 (fun (name, i, ty, bo) ->
295 (name, i, rehash_term ty, rehash_term bo))
302 (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
305 C.CoFix (i, liftedfl)
308 let module C = Cic in
309 let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
311 C.Constant (name,bo,ty,params,attrs) ->
315 | Some bo -> Some (rehash_term bo)
317 let ty' = rehash_term ty in
318 let params' = List.map recons params in
319 C.Constant (name, bo', ty', params',attrs)
320 | C.CurrentProof (name,conjs,bo,ty,params,attrs) ->
323 (function (i,hyps,ty) ->
327 | Some (name,C.Decl t) ->
328 Some (name,C.Decl (rehash_term t))
329 | Some (name,C.Def (bo,ty)) ->
333 | Some ty'' -> Some (rehash_term ty'')
335 Some (name,C.Def (rehash_term bo, ty'))) hyps,
339 let bo' = rehash_term bo in
340 let ty' = rehash_term ty in
341 let params' = List.map recons params in
342 C.CurrentProof (name, conjs', bo', ty', params',attrs)
343 | C.Variable (name,bo,ty,params,attrs) ->
347 | Some bo -> Some (rehash_term bo)
349 let ty' = rehash_term ty in
350 let params' = List.map recons params in
351 C.Variable (name, bo', ty', params',attrs)
352 | C.InductiveDefinition (tl,params,paramsno,attrs) ->
353 let params' = List.map recons params in
355 List.map (function (name, inductive, ty, constructors) ->
360 (function (name, ty) -> name, rehash_term ty)
364 C.InductiveDefinition (tl', params', paramsno, attrs)
366 let rec metas_of_term = function
367 | Cic.Meta (i, c) -> [i,c]
370 | Cic.MutInd (_, _, ens)
371 | Cic.MutConstruct (_, _, _, ens) ->
372 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
375 | Cic.Lambda (_, s, t)
376 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
377 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
378 | Cic.MutCase (uri, i, s, t, l) ->
379 (metas_of_term s) @ (metas_of_term t) @
380 (List.flatten (List.map metas_of_term l))
383 (List.map (fun (s, i, t1, t2) ->
384 (metas_of_term t1) @ (metas_of_term t2)) il)
385 | Cic.CoFix (i, il) ->
387 (List.map (fun (s, t1, t2) ->
388 (metas_of_term t1) @ (metas_of_term t2)) il)