]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/cic/cicUtil.ml
some tests patched
[helm.git] / helm / software / components / cic / cicUtil.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 (* $Id$ *)
27
28 open Printf
29
30 exception Meta_not_found of int
31 exception Subst_not_found of int
32
33 let lookup_meta index metasenv =
34   try
35     List.find (fun (index', _, _) -> index = index') metasenv
36   with Not_found -> raise (Meta_not_found index)
37
38 let lookup_subst n subst =
39   try
40     List.assoc n subst
41   with Not_found -> raise (Subst_not_found n)
42
43 let exists_meta index = List.exists (fun (index', _, _) -> (index = index'))
44
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 
47 canonical context *)
48
49 let clean_up_local_context subst metasenv n l =
50   let cc =
51     (try
52        let (cc,_,_) = lookup_subst n subst in cc
53      with Subst_not_found _ ->
54        try
55          let (_,cc,_) = lookup_meta n metasenv in cc
56        with Meta_not_found _ -> assert false) in
57   (try 
58      List.map2
59        (fun t1 t2 ->
60           match t1,t2 with 
61               None , _ -> None
62             | _ , t -> t) cc l
63    with 
64        Invalid_argument _ -> 
65          assert false)
66
67 let is_closed =
68  let module C = Cic in
69  let rec is_closed k =
70   function
71       C.Rel m when m > k -> false
72     | C.Rel m -> true
73     | C.Meta (_,l) ->
74        List.fold_left
75         (fun i t -> i && (match t with None -> true | Some t -> is_closed k t)
76         ) true l
77     | C.Sort _ -> true
78     | C.Implicit _ -> assert false
79     | C.Cast (te,ty) -> is_closed k te && is_closed k ty
80     | C.Prod (name,so,dest) -> is_closed k so && is_closed (k+1) dest
81     | C.Lambda (_,so,dest) -> is_closed k so && is_closed (k+1) dest
82     | C.LetIn (_,so,dest) -> is_closed k so && is_closed (k+1) dest
83     | C.Appl l ->
84        List.fold_right (fun x i -> i && is_closed k x) l true
85     | C.Var (_,exp_named_subst)
86     | C.Const (_,exp_named_subst)
87     | C.MutInd (_,_,exp_named_subst)
88     | C.MutConstruct (_,_,_,exp_named_subst) ->
89        List.fold_right (fun (_,x) i -> i && is_closed k x)
90         exp_named_subst true
91     | C.MutCase (_,_,out,te,pl) ->
92        is_closed k out && is_closed k te &&
93         List.fold_right (fun x i -> i && is_closed k x) pl true
94     | C.Fix (_,fl) ->
95        let len = List.length fl in
96         let k_plus_len = k + len in
97          List.fold_right
98           (fun (_,_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
99           ) fl true
100     | C.CoFix (_,fl) ->
101        let len = List.length fl in
102         let k_plus_len = k + len in
103          List.fold_right
104           (fun (_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
105           ) fl true
106 in 
107  is_closed 0
108 ;;
109
110 let rec is_meta_closed =
111   function
112       Cic.Rel _ -> true
113     | Cic.Meta _ -> false
114     | Cic.Sort _ -> true
115     | Cic.Implicit _ -> assert false
116     | Cic.Cast (te,ty) -> is_meta_closed te && is_meta_closed ty
117     | Cic.Prod (name,so,dest) -> is_meta_closed so && is_meta_closed dest
118     | Cic.Lambda (_,so,dest) -> is_meta_closed so && is_meta_closed dest
119     | Cic.LetIn (_,so,dest) -> is_meta_closed so && is_meta_closed dest
120     | Cic.Appl l ->
121        not (List.exists (fun x -> not (is_meta_closed x)) l)
122     | Cic.Var (_,exp_named_subst)
123     | Cic.Const (_,exp_named_subst)
124     | Cic.MutInd (_,_,exp_named_subst)
125     | Cic.MutConstruct (_,_,_,exp_named_subst) ->
126        not (List.exists (fun (_,x) -> not (is_meta_closed x)) exp_named_subst)
127     | Cic.MutCase (_,_,out,te,pl) ->
128        is_meta_closed out && is_meta_closed te &&
129         not (List.exists (fun x -> not (is_meta_closed x)) pl)
130     | Cic.Fix (_,fl) ->
131         not (List.exists 
132               (fun (_,_,ty,bo) -> 
133                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
134               fl)
135     | Cic.CoFix (_,fl) ->
136         not (List.exists 
137               (fun (_,ty,bo) -> 
138                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
139               fl)
140 ;;
141
142 let xpointer_RE = Str.regexp "\\([^#]+\\)#xpointer(\\(.*\\))"
143 let slash_RE = Str.regexp "/"
144
145 let term_of_uri uri =
146   let s = UriManager.string_of_uri uri in
147   try
148     (if UriManager.uri_is_con uri then
149       Cic.Const (uri, [])
150     else if UriManager.uri_is_var uri then
151       Cic.Var (uri, [])
152     else if not (Str.string_match xpointer_RE s 0) then
153       raise (UriManager.IllFormedUri s)
154     else
155       let (baseuri,xpointer) = (Str.matched_group 1 s, Str.matched_group 2 s) in
156       let baseuri = UriManager.uri_of_string baseuri in
157       (match Str.split slash_RE xpointer with
158       | [_; tyno] -> Cic.MutInd (baseuri, int_of_string tyno - 1, [])
159       | [_; tyno; consno] ->
160           Cic.MutConstruct
161             (baseuri, int_of_string tyno - 1, int_of_string consno, [])
162       | _ -> raise Exit))
163   with
164   | Exit
165   | Failure _
166   | Not_found -> raise (UriManager.IllFormedUri s)
167
168 let uri_of_term = function
169   | Cic.Const (uri, _)
170   | Cic.Var (uri, _) -> uri
171   | Cic.MutInd (baseuri, tyno, _) ->
172      UriManager.uri_of_string
173       (sprintf "%s#xpointer(1/%d)" (UriManager.string_of_uri baseuri) (tyno+1))
174   | Cic.MutConstruct (baseuri, tyno, consno, _) ->
175      UriManager.uri_of_string
176       (sprintf "%s#xpointer(1/%d/%d)" (UriManager.string_of_uri baseuri)
177         (tyno + 1) consno)
178   | _ -> raise (Invalid_argument "uri_of_term")
179
180
181 (*
182 let pack terms =
183   List.fold_right
184     (fun term acc -> Cic.Prod (Cic.Anonymous, term, acc))
185     terms (Cic.Sort (Cic.Type (CicUniv.fresh ())))
186
187 let rec unpack = function
188   | Cic.Prod (Cic.Anonymous, term, Cic.Sort (Cic.Type _)) -> [term]
189   | Cic.Prod (Cic.Anonymous, term, tgt) -> term :: unpack tgt
190   | _ -> assert false
191 *)
192
193 let rec strip_prods n = function
194   | t when n = 0 -> t
195   | Cic.Prod (_, _, tgt) when n > 0 -> strip_prods (n-1) tgt
196   | _ -> failwith "not enough prods"
197
198 let params_of_obj = function
199   | Cic.Constant (_, _, _, params, _)
200   | Cic.Variable (_, _, _, params, _)
201   | Cic.CurrentProof (_, _, _, _, params, _)
202   | Cic.InductiveDefinition (_, params, _, _) ->
203       params
204
205 let attributes_of_obj = function
206   | Cic.Constant (_, _, _, _, attributes)
207   | Cic.Variable (_, _, _, _, attributes)
208   | Cic.CurrentProof (_, _, _, _, _, attributes)
209   | Cic.InductiveDefinition (_, _, _, attributes) ->
210       attributes
211
212 let arity_of_composed_coercion obj =
213   let attrs = attributes_of_obj obj in
214   try
215     let tag=List.find (function `Class (`Coercion _) -> true|_->false) attrs in
216     match tag with
217     |  `Class (`Coercion n) -> n
218     | _-> assert false 
219   with Not_found -> 0
220 ;;
221       
222 let rec mk_rels howmany from =
223   match howmany with 
224   | 0 -> []
225   | _ -> (Cic.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
226
227 let id_of_annterm =
228   function
229   | Cic.ARel (id,_,_,_)
230   | Cic.AVar (id,_,_)
231   | Cic.AMeta (id,_,_)
232   | Cic.ASort (id,_)
233   | Cic.AImplicit (id,_)
234   | Cic.ACast (id,_,_)
235   | Cic.AProd (id,_,_,_)
236   | Cic.ALambda (id,_,_,_)
237   | Cic.ALetIn (id,_,_,_)
238   | Cic.AAppl (id,_)
239   | Cic.AConst (id,_,_)
240   | Cic.AMutInd (id,_,_,_)
241   | Cic.AMutConstruct (id,_,_,_,_)
242   | Cic.AMutCase (id,_,_,_,_,_)
243   | Cic.AFix (id,_,_)
244   | Cic.ACoFix (id,_,_) -> id
245
246
247 let rec rehash_term =
248   let module C = Cic in
249   let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
250   function
251    | (C.Rel _) as t -> t
252    | C.Var (uri,exp_named_subst) ->
253       let uri' = recons uri in
254       let exp_named_subst' =
255        List.map
256         (function (uri,t) ->(recons uri,rehash_term t)) 
257          exp_named_subst
258       in
259        C.Var (uri',exp_named_subst')
260    | C.Meta (i,l) ->
261       let l' =
262        List.map
263         (function
264             None -> None
265           | Some t -> Some (rehash_term t)
266         ) l
267       in
268        C.Meta(i,l')
269    | C.Sort (C.Type u) -> 
270        CicUniv.assert_univ u;
271        C.Sort (C.Type (CicUniv.recons_univ u))
272    | C.Sort _ as t -> t
273    | C.Implicit _ as t -> t
274    | C.Cast (te,ty) -> C.Cast (rehash_term te, rehash_term ty)
275    | C.Prod (n,s,t) -> C.Prod (n, rehash_term s, rehash_term t)
276    | C.Lambda (n,s,t) -> C.Lambda (n, rehash_term s, rehash_term t)
277    | C.LetIn (n,s,t) -> C.LetIn (n, rehash_term s, rehash_term t)
278    | C.Appl l -> C.Appl (List.map rehash_term l)
279    | C.Const (uri,exp_named_subst) ->
280       let uri' = recons uri in
281       let exp_named_subst' = 
282        List.map
283         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
284       in
285        C.Const (uri',exp_named_subst')
286    | C.MutInd (uri,tyno,exp_named_subst) ->
287       let uri' = recons uri in
288       let exp_named_subst' = 
289        List.map
290         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
291       in
292        C.MutInd (uri',tyno,exp_named_subst')
293    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
294       let uri' = recons uri in
295       let exp_named_subst' = 
296        List.map
297         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
298       in
299        C.MutConstruct (uri',tyno,consno,exp_named_subst')
300    | C.MutCase (uri,i,outty,t,pl) ->
301       C.MutCase (recons uri, i, rehash_term outty, rehash_term t,
302        List.map rehash_term pl)
303    | C.Fix (i, fl) ->
304       let liftedfl =
305        List.map
306         (fun (name, i, ty, bo) ->
307           (name, i, rehash_term ty, rehash_term bo))
308          fl
309       in
310        C.Fix (i, liftedfl)
311    | C.CoFix (i, fl) ->
312       let liftedfl =
313        List.map
314         (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
315          fl
316       in
317        C.CoFix (i, liftedfl)
318
319 let rehash_obj =
320  let module C = Cic in
321  let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
322  function 
323    C.Constant (name,bo,ty,params,attrs) ->
324      let bo' =
325        match bo with
326          None -> None
327        | Some bo -> Some (rehash_term bo)
328      in
329      let ty' = rehash_term ty in
330      let params' = List.map recons params in
331      C.Constant (name, bo', ty', params',attrs)
332  | C.CurrentProof (name,conjs,bo,ty,params,attrs) ->
333      let conjs' =
334        List.map
335          (function (i,hyps,ty) ->
336            (i,
337            List.map (function
338                None -> None
339              | Some (name,C.Decl t) ->
340                  Some (name,C.Decl (rehash_term t))
341              | Some (name,C.Def (bo,ty)) ->
342                  let ty' =
343                    match ty with
344                      None -> None
345                    | Some ty'' -> Some (rehash_term ty'')
346                  in
347                  Some (name,C.Def (rehash_term bo, ty'))) hyps,
348            rehash_term ty))
349          conjs
350      in
351      let bo' = rehash_term bo in
352      let ty' = rehash_term ty in
353      let params' = List.map recons params in
354      C.CurrentProof (name, conjs', bo', ty', params',attrs)
355  | C.Variable (name,bo,ty,params,attrs) ->
356      let bo' =
357        match bo with
358          None -> None
359        | Some bo -> Some (rehash_term bo)
360      in
361      let ty' = rehash_term ty in
362      let params' = List.map recons params in
363      C.Variable (name, bo', ty', params',attrs)
364  | C.InductiveDefinition (tl,params,paramsno,attrs) ->
365      let params' = List.map recons params in
366      let tl' =
367        List.map (function (name, inductive, ty, constructors) ->
368          name,
369          inductive,
370          rehash_term ty,
371          (List.map
372            (function (name, ty) -> name, rehash_term ty)
373            constructors))
374          tl
375      in
376      C.InductiveDefinition (tl', params', paramsno, attrs)
377
378 let rec metas_of_term = function
379   | Cic.Meta (i, c) -> [i,c]
380   | Cic.Var (_, ens) 
381   | Cic.Const (_, ens) 
382   | Cic.MutInd (_, _, ens) 
383   | Cic.MutConstruct (_, _, _, ens) ->
384       List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
385   | Cic.Cast (s, t)
386   | Cic.Prod (_, s, t)
387   | Cic.Lambda (_, s, t)
388   | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
389   | Cic.Appl l -> List.flatten (List.map metas_of_term l)
390   | Cic.MutCase (uri, i, s, t, l) ->
391       (metas_of_term s) @ (metas_of_term t) @
392         (List.flatten (List.map metas_of_term l))
393   | Cic.Fix (i, il) ->
394       List.flatten
395         (List.map (fun (s, i, t1, t2) ->
396                      (metas_of_term t1) @ (metas_of_term t2)) il)
397   | Cic.CoFix (i, il) ->
398       List.flatten
399         (List.map (fun (s, t1, t2) ->
400                      (metas_of_term t1) @ (metas_of_term t2)) il)
401   | _ -> []
402 ;;      
403
404 module MetaOT = struct
405   type t = int * Cic.term option list
406   let compare = Pervasives.compare
407 end
408
409 module S = Set.Make(MetaOT)
410
411 let rec metas_of_term_set = function
412   | Cic.Meta (i, c) -> S.singleton (i,c)
413   | Cic.Var (_, ens) 
414   | Cic.Const (_, ens) 
415   | Cic.MutInd (_, _, ens) 
416   | Cic.MutConstruct (_, _, _, ens) ->
417       List.fold_left 
418         (fun s (_,t) -> S.union s (metas_of_term_set t)) 
419         S.empty ens
420   | Cic.Cast (s, t)
421   | Cic.Prod (_, s, t)
422   | Cic.Lambda (_, s, t)
423   | Cic.LetIn (_, s, t) -> S.union (metas_of_term_set s) (metas_of_term_set t)
424   | Cic.Appl l -> 
425       List.fold_left 
426         (fun s t -> S.union s (metas_of_term_set t)) 
427         S.empty l
428   | Cic.MutCase (uri, i, s, t, l) ->
429       S.union 
430         (S.union (metas_of_term_set s)  (metas_of_term_set t))
431         (List.fold_left 
432           (fun s t -> S.union s (metas_of_term_set t)) 
433           S.empty l)
434   | Cic.Fix (_, il) ->
435       (List.fold_left 
436         (fun s (_,_,t1,t2) -> 
437           S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
438         S.empty il
439   | Cic.CoFix (i, il) ->
440       (List.fold_left 
441         (fun s (_,t1,t2) -> 
442           S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
443         S.empty il
444   | _ -> S.empty
445 ;;      
446
447 let metas_of_term_set t = 
448   let s = metas_of_term_set t in
449   S.elements s
450 ;;
451